| 1 | package DBIx::Oro; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 24 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 24 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 24 | our $VERSION = '0.32_1'; | |||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # See the bottom of this file for the POD documentation. | ||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # Todo: Fix treatments in joined tables! | ||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # Todo: Are combined indices necessarily in the same order? | ||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # In that case update and select etc. need ordered | ||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # keys! | ||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # Todo: Improve documentation | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # - =head1 ADVANCED CONCEPTS | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # - Joined tables | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # - Treatments | ||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # - Caching | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # - Injected SQL | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # - explain | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # - on_connect | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # Todo: -prefix is not documented! | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # Todo: Put 'created' in SQLite driver | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # implement ->errstr | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # implement "-with" (?) | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # Debug: $lemma_oro->insert({ | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # wcl => $_, | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # lemma => $inter | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # },{ | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # token => $search_for | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # (Should raise error) | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # Todo: Support left outer join (Maybe with id => 1, maybe => 1 in join hash) | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # Todo: Create all Queries in DBIx::Oro::Query | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # Todo: To change queries from different drivers, | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # use events. | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # Todo: Return key -column_order => [] with fetchall_arrayref. | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # Todo: my $value = $oro->value(Table => 'Field') # Ähnlich wie count | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # Todo: Oder my ($value) = $oro->value(Table => Field => { -limit => 1 }) # und es gibt ein Array zurück | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # Todo: Check for BDB-support: | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # http://charlesleifer.com/blog/sqlite-small-fast-reliable-choose-any-three-/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 24 | ▶ | use v5.10.1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 24 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 24 | ▶ | use Carp qw/carp croak/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 24 | our @CARP_NOT; | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # Database connection | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 24 | ▶ | use DBI; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 24 | our $AS_REGEX = qr/(?::~?`?[-_a-zA-Z0-9]+`?)/; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 24 | our $OP_REGEX = qr/^(?i: | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | (?:[\<\>\!=]?\=?)|<>| | ||||||||||||||||||||||||||||||||||||||||||||||
| 58 | (?:!|not[_ ])? | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | (?:match|like|glob|regex|between)| | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | (?:eq|ne|[gl][te]|not) | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | )$/x; | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 24 | our $KEY_REGEX = qr/`?[_\.0-9a-zA-Z]+`?/; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 24 | our $KEY_REGEX_NOPREF = qr/`?[_0-9a-zA-Z]+`?/; | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 24 | our $NUM_RE = qr/^\s*(\d+)\s*$/; | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 24 | our $SFIELD_REGEX = | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | qr/(?:$KEY_REGEX|(?:$KEY_REGEX\.)?\*|"[^"]*"|'[^']*')/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 24 | our $FIELD_OP_REGEX = qr/[-\+\/\%\*,]/; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 24 | our $FUNCTION_REGEX = | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | qr/([_a-zA-Z0-9]* | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | \(\s*(?:$SFIELD_REGEX|(?-1)) | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | (?:\s*$FIELD_OP_REGEX\s*(?:$SFIELD_REGEX|(?-1)))*\s*\))/x; | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 24 | our $VALID_FIELD_REGEX = | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | qr/^(?:$SFIELD_REGEX|$FUNCTION_REGEX)$AS_REGEX?$/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 24 | our $VALID_GROUPORDER_REGEX = | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | qr/^[-\+]?(?:$KEY_REGEX|$FUNCTION_REGEX)$/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 24 | our $FIELD_REST_RE = qr/^(.+?)(:~?)([^:"~][^:"]*?)$/; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 24 | our $CACHE_COMMENT = 'From Cache'; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 24 | our $ITEMS_PER_PAGE = 25; | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 24 | our @EXTENSIONS = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # Import extension | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 48 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=4 SCAR=25.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 48 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # Load extensions | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 48 | foreach (@_) { | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # Load extensions | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my $module = qq{DBIx::Oro::Extension::$_}; | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | unless (eval "require $module; 1;") { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | croak qq{Unable to load extension "$_"} and return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | # Push to extension array | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | push(@EXTENSIONS, $_); | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # Start import for extensions | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | $module->import; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # Constructor | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 28 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=14 SCAR=29.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 28 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | 28 | my ($self, %param); | |||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # SQLite - one parameter | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 28 | ▶ | if (@_ == 1) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | @param{qw/driver file/} = ('SQLite', shift); | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # SQLite - two parameter | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | ▶ | elsif (@_ == 2 && ref $_[1] && ref $_[1] eq 'CODE') { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
@_ == 2 and ref $_[1] and ref $_[1] eq "CODE"
@_ == 2 and ref $_[1]
MC/DC33%1/3
@_ == 2 and ref $_[1] and ref $_[1] eq "CODE"
@_ == 2
ref $_[1]
ref $_[1] eq "CODE"
Truth table50%2/4
A and B and C
A= @_ == 2B= ref $_[1]C= ref $_[1] eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 6 | @param{qw/driver file init/} = ('SQLite', @_); | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # Hash | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 21 | %param = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | # Init by default | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 28 | ${$param{in_txn}} = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | 28 | $param{last_sql} = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 28 | my $pwd = delete $param{password}; | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | # Set default to SQLite | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 28 | ▶ | $param{driver} //= 'SQLite'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$param{"driver"} //= "SQLite"
MC/DC100%1/1
$param{"driver"} //= "SQLite"
$param{"driver"}
Truth table100%2/2
A
A= $param{"driver"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | # Load driver | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 28 | my $package = 'DBIx::Oro::Driver::' . $param{driver}; | |||||||||||||||||||||||||||||||||||||||||||||
| 146 | 28 | ▶ | unless (eval 'require ' . $package . '; 1;') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | croak 'Unable to load ' . $package; | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | # On_connect event | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 28 | my $on_connect = delete $param{on_connect}; | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||
| 154 | # Get driver specific handle | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 28 | $self = $package->new( %param ); | |||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # No database created | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 28 | ▶ | return unless $self; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | # Connection identifier (for _password) | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 28 | $self->{_id} = "$self"; | |||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # Set password securely | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 28 | ▶ | $self->_password($pwd) if $pwd; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # On connect events | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 28 | $self->{on_connect} = {}; | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | 28 | $self->{_connect_cb} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 28 | ▶ | if ($on_connect) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | $self->on_connect( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | ref $on_connect eq 'HASH' ? | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | %$on_connect : $on_connect | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | ) or return; | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # Connect to database | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 28 | ▶ | $self->_connect or croak 'Unable to connect to database'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # Savepoint array | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # First element is a counter | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 28 | $self->{savepoint} = [1]; | |||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | # Initialize database and return Oro instance | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 28 | ▶ | return $self if $self->_init; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # Fail | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||
| 192 | # New table object | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 26 | ▶ | sub table { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
table: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 26 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # Joined table | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | my %param = ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 26 | table => do { | |||||||||||||||||||||||||||||||||||||||||||||
| 199 | 26 | ▶ | if (ref($_[0])) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 3 | [ _join_tables( shift(@_) ) ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # Table name | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 23 | shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 206 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 208 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | # Clone parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 26 | foreach (qw/dbh created in_txn savepoint pid tid | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | dsn _connect_cb on_connect/) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 234 | $param{$_} = $self->{$_}; | |||||||||||||||||||||||||||||||||||||||||||||
| 214 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | # Connection identifier (for _password) | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 26 | $param{_id} = "$self"; | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # Bless object with hash | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 26 | bless \%param, ref $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 221 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||
| 224 | # Database handle | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | # Based on DBIx::Connector | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 1449 | ▶ | sub dbh { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dbh: called CC=10 SCAR=28.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 1449 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # Store new database handle | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 1449 | ▶ | return ($self->{dbh} = shift) if $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 1449 | ▶ | return $self->{dbh} if ${$self->{in_txn}}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 522 | state $c = 'Unable to connect to database'; | |||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||
| 236 | # Check for thread id | ||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 522 | ▶ | if (defined $self->{tid} && $self->{tid} != threads->tid) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
defined $self->{"tid"} and $self->{"tid"} != "threads"->tid
MC/DC0%0/2
defined $self->{"tid"} and $self->{"tid"} != "threads"->tid
defined $self->{"tid"}
$self->{"tid"} != "threads"->tid
Truth table33%1/3
A and B
A= defined $self->{"tid"}B= $self->{"tid"} != "threads"->tid
| |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | return $self->_connect || croak $c; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->_connect || croak($c)
MC/DC0%0/1
$self->_connect || croak($c)
$self->_connect
Truth table0%0/2
A
A= $self->_connect
| |||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||
| 241 | # Check for process id | ||||||||||||||||||||||||||||||||||||||||||||||
| 242 | elsif ($self->{pid} != $$) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | return $self->_connect || croak $c; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->_connect || croak($c)
MC/DC0%0/1
$self->_connect || croak($c)
$self->_connect
Truth table0%0/2
A
A= $self->_connect
| |||||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||
| 246 | elsif ($self->{dbh}->{Active}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 519 | return $self->{dbh}; | |||||||||||||||||||||||||||||||||||||||||||||
| 248 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||
| 250 | # Return handle if active | ||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 3 | ▶ | return $self->_connect || croak $c; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_connect || croak($c)
MC/DC0%0/1
$self->_connect || croak($c)
$self->_connect
Truth table50%1/2
A
A= $self->_connect
| |||||||||||||||||||||||||||||||||||||||||||||||
| 252 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | # Last executed SQL | ||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 45 | ▶ | sub last_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_sql: called CC=6 SCAR=21.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 45 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 258 | 45 | my $last_sql = $self->{last_sql}; | |||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||
| 260 | # Check for recurrent placeholders | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 45 | ▶ | if ($last_sql =~ m/(?:UNION|\?(?:, \?){3,}|(?:\(\?(?:, \?)*\), ){3,})/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | our $c; | |||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||
| 265 | # Count Union selects | ||||||||||||||||||||||||||||||||||||||||||||||
| 266 | state $UNION_RE = | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | qr/(?{$c=1})(SELECT \?(?:, \?)*)(?: UNION \1(?{$c++})){3,}/; | |||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||
| 269 | # Count Union selects | ||||||||||||||||||||||||||||||||||||||||||||||
| 270 | state $BRACKET_RE = | ||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | qr/(?{$c=1})(\(\?(?:, \?)*\))(?:, \1(?{$c++})){3,}/; | |||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||
| 273 | # Count recurring placeholders | ||||||||||||||||||||||||||||||||||||||||||||||
| 274 | state $PLACEHOLDER_RE = | ||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | qr/(?{$c=1})\?(?:, \?(?{$c++})){3,}/; | |||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | # Rewrite placeholders with count | ||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | for ($last_sql) { | |||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | s/$UNION_RE/WITH $c x UNION $1/og; | |||||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | s/$BRACKET_RE/$c x $1/og; | |||||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | s/$PLACEHOLDER_RE/$c x ?/og; | |||||||||||||||||||||||||||||||||||||||||||||
| 282 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 45 | ▶ | return $last_sql || '' unless wantarray; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$last_sql || ''
MC/DC0%0/1
$last_sql || ''
$last_sql
Truth table50%1/2
A
A= $last_sql
| |||||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||||
| 287 | # Return as array | ||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 33 | ▶ | return ('', 0) unless $last_sql; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||
| 290 | # Check if database request | ||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 32 | state $offset = -1 * length $CACHE_COMMENT; | |||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||
| 293 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 32 | $last_sql, | |||||||||||||||||||||||||||||||||||||||||||||
| 295 | substr($last_sql, $offset) eq $CACHE_COMMENT | ||||||||||||||||||||||||||||||||||||||||||||||
| 296 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 297 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||
| 300 | # Database driver | ||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | sub driver { '' }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
driver: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # Extensions | ||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | sub extension { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
extension: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | return @EXTENSIONS; | |||||||||||||||||||||||||||||||||||||||||||||
| 307 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||
| 310 | # Insert values to database | ||||||||||||||||||||||||||||||||||||||||||||||
| 311 | # This is the MySQL way | ||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | ▶ | sub insert { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert: not called CC=18 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||||||
| 315 | # Get table name | ||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | ▶ | my $table = _table_name($self, \@_) or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||
| 318 | # No parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | return unless $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||
| 321 | # Properties | ||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | my $prop = shift if ref $_[0] eq 'HASH' && ref $_[1]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $_[0] eq "HASH" and ref $_[1]
MC/DC0%0/2
ref $_[0] eq "HASH" and ref $_[1]
ref $_[0] eq "HASH"
ref $_[1]
Truth table0%0/3
A and B
A= ref $_[0] eq "HASH"B= ref $_[1]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||
| 324 | # Single insert | ||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | ▶ | if (ref $_[0] eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||
| 327 | # Param | ||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | my %param = %{ shift(@_) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||
| 330 | # Create insert arrays | ||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | my (@keys, @values); | |||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | while (my ($key, $value) = each %param) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | # Insert pairs | ||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | ▶ | next if !ref $key && $key !~ $DBIx::Oro::KEY_REGEX; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $key or $key =~ /$DBIx::Oro::KEY_REGEX/
MC/DC0%0/2
ref $key or $key =~ /$DBIx::Oro::KEY_REGEX/
ref $key
$key =~ /$DBIx::Oro::KEY_REGEX/
Truth table0%0/3
A or B
A= ref $keyB= $key =~ /$DBIx::Oro::KEY_REGEX/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | push @keys, $key; | |||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | push @values, _stringify($value); | |||||||||||||||||||||||||||||||||||||||||||||
| 338 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||
| 340 | # Nothing to insert | ||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | return unless @keys; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||
| 343 | # Create insert string | ||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | my $sql = 'INSERT '; | |||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | ▶ | if ($prop && (my $oc = $prop->{-on_conflict})) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$prop and my $oc = $prop->{"-on_conflict"}
MC/DC0%0/2
$prop and my $oc = $prop->{"-on_conflict"}
$prop
my $oc = $prop->{"-on_conflict"}
Truth table0%0/3
A and B
A= $propB= my $oc = $prop->{"-on_conflict"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 0 | ▶ | if ($oc eq 'replace') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | $sql = 'REPLACE ' | |||||||||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 350 | elsif ($oc eq 'ignore') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | $sql .= 'IGNORE ' | |||||||||||||||||||||||||||||||||||||||||||||
| 352 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 353 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||
| 355 | $sql .= 'INTO ' . $table . | ||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | ' (' . join(', ', map { "`$_`" } @keys) . ') VALUES (' . _q(\@values) . ')'; | |||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||
| 358 | # Prepare and execute | ||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | return scalar $self->prep_and_exec( $sql, \@values ); | |||||||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||
| 362 | # Multiple inserts | ||||||||||||||||||||||||||||||||||||||||||||||
| 363 | elsif (ref($_[0]) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | ▶ | return unless $_[1]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | my @keys = @{ shift(@_) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||
| 369 | # Default values | ||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | my @default = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||
| 372 | # Check if keys are defaults | ||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | my @default_keys; | |||||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | ▶ | while ($keys[$i]) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||
| 377 | # No default - next | ||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | ▶ | $i++, next unless ref $keys[$i]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||||||
| 380 | # Has default value | ||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | my ($key, $value) = @{ splice( @keys, $i, 1) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | push(@default_keys, $key); | |||||||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | push(@default, _stringify($value)); | |||||||||||||||||||||||||||||||||||||||||||||
| 384 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||||
| 386 | # Unshift default keys to front | ||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | unshift(@keys, @default_keys); | |||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||
| 389 | my $sql .= 'INSERT INTO ' . $table . | ||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | ' (' . join(', ', map { "`$_`" } @keys) . ') ' . | |||||||||||||||||||||||||||||||||||||||||||||
| 391 | 'VALUES '; | ||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||
| 393 | # Add data in brackets | ||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | $sql .= join(', ', ('(' ._q(\@keys) . ')') x scalar @_ ); | |||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||
| 396 | # Prepare and execute with prepended defaults | ||||||||||||||||||||||||||||||||||||||||||||||
| 397 | return $self->prep_and_exec( | ||||||||||||||||||||||||||||||||||||||||||||||
| 398 | $sql, | ||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 0 | [ map { (@default, @$_); } @_ ] | |||||||||||||||||||||||||||||||||||||||||||||
| 400 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 401 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||
| 403 | # Unknown query | ||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 405 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||
| 408 | # Update existing values in the database | ||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 25 | ▶ | sub update { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=10 SCAR=23.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 25 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||
| 412 | # Get table name | ||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 25 | ▶ | my $table = _table_name($self, \@_) or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||
| 415 | # No parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 25 | ▶ | return unless $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||||
| 418 | # Get pairs | ||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 25 | my ($pairs, $values) = _get_pairs( shift(@_) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||||||
| 421 | # Nothing to update | ||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 25 | ▶ | return unless @$pairs; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||
| 424 | # No arrays or operators allowed | ||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 25 | foreach (@$pairs) { | |||||||||||||||||||||||||||||||||||||||||||||
| 426 | 26 | ▶ | return unless $_ =~ /^$KEY_REGEX\s+(?:=|IS\s)/o; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 427 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||
| 429 | # Set undef to pairs | ||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 24 | my @pairs = map { $_ =~ s{ IS NULL$}{ = NULL}io; $_ } @$pairs; | |||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||
| 432 | # Generate sql | ||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 24 | my $sql = 'UPDATE ' . $table . ' SET ' . join(', ', @pairs); | |||||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||||||
| 435 | # Condition | ||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 24 | ▶ | if ($_[0]) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 24 | my ($cond_pairs, $cond_values) = _get_pairs( shift(@_) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||
| 439 | # No conditions given | ||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 24 | ▶ | if (@$cond_pairs) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||
| 442 | # Append condition | ||||||||||||||||||||||||||||||||||||||||||||||
| 443 | 24 | $sql .= ' WHERE ' . join(' AND ', @$cond_pairs); | |||||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||||||
| 445 | # Append values | ||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 24 | push(@$values, @$cond_values); | |||||||||||||||||||||||||||||||||||||||||||||
| 447 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 448 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||
| 450 | # Prepare and execute | ||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 24 | my $rv = $self->prep_and_exec($sql, $values); | |||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||
| 453 | # Return value | ||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 24 | ▶ | return (!$rv || $rv eq '0E0') ? 0 : $rv; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $rv or $rv eq "0E0"
MC/DC100%2/2
not $rv or $rv eq "0E0"
not $rv
$rv eq "0E0"
Truth table100%3/3
not(A) or B
A= not $rvB= $rv eq "0E0"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 455 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||
| 458 | # Select from table | ||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 138 | ▶ | sub select { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
select: called CC=45 SCAR=50.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 138 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||||
| 462 | # Get table object | ||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 138 | my ($tables, | |||||||||||||||||||||||||||||||||||||||||||||
| 464 | $fields, | ||||||||||||||||||||||||||||||||||||||||||||||
| 465 | $join_pairs, | ||||||||||||||||||||||||||||||||||||||||||||||
| 466 | $treatment, | ||||||||||||||||||||||||||||||||||||||||||||||
| 467 | $field_alias) = _table_obj($self, \@_); | ||||||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 138 | my @pairs = @$join_pairs; | |||||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||||||
| 471 | # Fields to select | ||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 138 | ▶ | if ($_[0] && ref($_[0]) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_[0] and ref $_[0] eq "ARRAY"
MC/DC100%2/2
$_[0] and ref $_[0] eq "ARRAY"
$_[0]
ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= $_[0]B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||
| 474 | # Not allowed for join selects | ||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 38 | ▶ | return if $fields->[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 38 | ($fields, $treatment) = _fields($tables->[0], shift(@_) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 38 | $fields = [ $fields ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 480 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||||
| 482 | # Default | ||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 138 | ▶ | $fields->[0] ||= '*'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$fields->[0] ||= "*"
MC/DC100%1/1
$fields->[0] ||= "*"
$fields->[0]
Truth table100%2/2
A
A= $fields->[0]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||||
| 485 | # Create sql query | ||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 138 | my $sql = join(', ', @$fields) . ' FROM ' . join(', ', @$tables); | |||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||
| 488 | # Append condition | ||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 138 | my @values; | |||||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 138 | my ($cond, $prep); | |||||||||||||||||||||||||||||||||||||||||||||
| 492 | 138 | ▶ | if (($_[0] && ref($_[0]) eq 'HASH') || @$join_pairs) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$_[0] and ref $_[0] eq "HASH" or @$join_pairs
$_[0] and ref $_[0] eq "HASH"
MC/DC33%1/3
$_[0] and ref $_[0] eq "HASH" or @$join_pairs
$_[0]
ref $_[0] eq "HASH"
@$join_pairs
Truth table40%2/5
A and B or C
A= $_[0]B= ref $_[0] eq "HASH"C= @$join_pairs
| |||||||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||||||
| 494 | # Condition | ||||||||||||||||||||||||||||||||||||||||||||||
| 495 | 117 | my ($pairs, $values); | |||||||||||||||||||||||||||||||||||||||||||||
| 496 | 117 | ▶ | if ($_[0] && ref($_[0]) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_[0] and ref $_[0] eq "HASH"
MC/DC0%0/2
$_[0] and ref $_[0] eq "HASH"
$_[0]
ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= $_[0]B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 117 | ($pairs, $values, $prep) = _get_pairs( shift(@_), $field_alias); | |||||||||||||||||||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 117 | push(@values, @$values); | |||||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||||||
| 501 | # Add to pairs | ||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 117 | ▶ | push(@pairs, @$pairs) if $pairs->[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 503 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||||||
| 505 | # Add where clause | ||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 117 | ▶ | $sql .= ' WHERE ' . join(' AND ', @pairs) if @pairs; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||||||
| 508 | # Add distinct information | ||||||||||||||||||||||||||||||||||||||||||||||
| 509 | 117 | ▶ | if ($prep) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 75 | ▶ | $sql = 'DISTINCT ' . $sql if delete $prep->{'distinct'}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||||||
| 512 | # Apply restrictions | ||||||||||||||||||||||||||||||||||||||||||||||
| 513 | 75 | $sql .= _restrictions($prep, \@values); | |||||||||||||||||||||||||||||||||||||||||||||
| 514 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 515 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||||
| 517 | 138 | my $result; | |||||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||||||
| 519 | # Check cache | ||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 138 | my ($chi, $key, $chi_param); | |||||||||||||||||||||||||||||||||||||||||||||
| 521 | 138 | ▶ | if ($prep && $prep->{cache}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$prep and $prep->{"cache"}
MC/DC100%2/2
$prep and $prep->{"cache"}
$prep
$prep->{"cache"}
Truth table100%3/3
A and B
A= $prepB= $prep->{"cache"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||||||
| 523 | # Cache parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 11 | ($chi, $key, $chi_param) = @{delete $prep->{cache}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||||||
| 526 | # Generate key | ||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 11 | ▶ | $key = 'SELECT ' . $sql . '-' . join('-', @values) unless $key; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||||||
| 529 | # Get cache result | ||||||||||||||||||||||||||||||||||||||||||||||
| 530 | 11 | $result = $chi->get($key); | |||||||||||||||||||||||||||||||||||||||||||||
| 531 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||||||
| 533 | # Unknown restrictions | ||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 138 | ▶ | if (scalar keys %$prep) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 1 | carp 'Unknown restriction option: ' . join(', ', keys %$prep); | |||||||||||||||||||||||||||||||||||||||||||||
| 536 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 138 | my ($rv, $sth); | |||||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||||||
| 540 | # Result was not cached | ||||||||||||||||||||||||||||||||||||||||||||||
| 541 | 138 | ▶ | unless ($result) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||||||
| 543 | # Prepare and execute | ||||||||||||||||||||||||||||||||||||||||||||||
| 544 | 131 | ($rv, $sth) = $self->prep_and_exec('SELECT ' . $sql, \@values); | |||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||||
| 546 | # No statement created | ||||||||||||||||||||||||||||||||||||||||||||||
| 547 | 131 | ▶ | return unless $sth; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||
| 550 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 551 | # Last sql command | ||||||||||||||||||||||||||||||||||||||||||||||
| 552 | 7 | $self->{last_sql} = 'SELECT ' . $sql . ' -- ' . $CACHE_COMMENT; | |||||||||||||||||||||||||||||||||||||||||||||
| 553 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||||||
| 555 | # Prepare treatments | ||||||||||||||||||||||||||||||||||||||||||||||
| 556 | 136 | my (@treatment, %treatsub); | |||||||||||||||||||||||||||||||||||||||||||||
| 557 | 136 | ▶ | if ($treatment) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 558 | 8 | @treatment = keys %$treatment; | |||||||||||||||||||||||||||||||||||||||||||||
| 559 | 8 | foreach (@treatment) { | |||||||||||||||||||||||||||||||||||||||||||||
| 560 | 8 | $treatsub{$_} = shift(@{$treatment->{$_}}); | |||||||||||||||||||||||||||||||||||||||||||||
| 561 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 562 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||||||
| 564 | # Release callback | ||||||||||||||||||||||||||||||||||||||||||||||
| 565 | 136 | ▶ | if ($_[0] && ref $_[0] && ref $_[0] eq 'CODE' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$_[0] and ref $_[0] and ref $_[0] eq "CODE"
$_[0] and ref $_[0]
MC/DC0%0/3
$_[0] and ref $_[0] and ref $_[0] eq "CODE"
$_[0]
ref $_[0]
ref $_[0] eq "CODE"
Truth table25%1/4
A and B and C
A= $_[0]B= ref $_[0]C= ref $_[0] eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 566 | 0 | my $cb = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||||||
| 568 | 0 | carp 'Calling select with an iteration callback is deprecated since v0.31'; | |||||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||||||
| 570 | # Iterate through dbi result | ||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 0 | my ($i, $row) = (0); | |||||||||||||||||||||||||||||||||||||||||||||
| 572 | 0 | ▶ | while ($row = $sth ? $sth->fetchrow_hashref : $result->[$i]) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||||||||||
| 574 | # Iterate for cache result | ||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 0 | ▶ | push(@$result, $row) if $chi && $sth; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$chi and $sth
MC/DC0%0/2
$chi and $sth
$chi
$sth
Truth table0%0/3
A and B
A= $chiB= $sth
| |||||||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||||||
| 577 | # Increment for cached results | ||||||||||||||||||||||||||||||||||||||||||||||
| 578 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||||||
| 580 | # Treat result | ||||||||||||||||||||||||||||||||||||||||||||||
| 581 | 0 | ▶ | if ($treatment) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||||||
| 583 | # Treat each treatable row value | ||||||||||||||||||||||||||||||||||||||||||||||
| 584 | 0 | foreach ( grep { exists $row->{$_} } @treatment) { | |||||||||||||||||||||||||||||||||||||||||||||
| 585 | $row->{$_} = $treatsub{$_}->( | ||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | $row->{$_}, @{ $treatment->{$_} } | |||||||||||||||||||||||||||||||||||||||||||||
| 587 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 588 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 589 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||||||
| 591 | # Finish if callback returns -1 | ||||||||||||||||||||||||||||||||||||||||||||||
| 592 | 0 | local $_ = $row; | |||||||||||||||||||||||||||||||||||||||||||||
| 593 | 0 | my $rv = $cb->($row); | |||||||||||||||||||||||||||||||||||||||||||||
| 594 | 0 | ▶ | if ($rv && $rv eq '-1') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$rv and $rv eq -1
MC/DC0%0/2
$rv and $rv eq -1
$rv
$rv eq -1
Truth table0%0/3
A and B
A= $rvB= $rv eq -1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 0 | $result = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 596 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 597 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 598 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||||||||||||||||
| 600 | # Save to cache | ||||||||||||||||||||||||||||||||||||||||||||||
| 601 | 0 | ▶ | if ($sth && $chi && $result) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$sth and $chi and $result
$sth and $chi
MC/DC0%0/3
$sth and $chi and $result
$sth
$chi
$result
Truth table0%0/4
A and B and C
A= $sthB= $chiC= $result
| |||||||||||||||||||||||||||||||||||||||||||||||
| 602 | 0 | $chi->set($key => $result, $chi_param); | |||||||||||||||||||||||||||||||||||||||||||||
| 603 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||||||
| 605 | # Came from cache | ||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 0 | ▶ | return if !$sth && $chi; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $sth and $chi
MC/DC0%0/2
not $sth and $chi
not $sth
$chi
Truth table0%0/3
not(A) and B
A= not $sthB= $chi
| |||||||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||||||
| 608 | # Finish statement | ||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 0 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||||||
| 610 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 611 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||||||||||
| 613 | # Create array ref | ||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 136 | ▶ | unless ($result) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 129 | $result = $sth->fetchall_arrayref({}); | |||||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||||||
| 617 | # Save to stash | ||||||||||||||||||||||||||||||||||||||||||||||
| 618 | 129 | ▶ | if ($chi && $result) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$chi and $result
MC/DC50%1/2
$chi and $result
$chi
$result
Truth table66%2/3
A and B
A= $chiB= $result
| |||||||||||||||||||||||||||||||||||||||||||||||
| 619 | 4 | $chi->set($key => $result, $chi_param); | |||||||||||||||||||||||||||||||||||||||||||||
| 620 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 621 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||||||
| 623 | # Return array ref | ||||||||||||||||||||||||||||||||||||||||||||||
| 624 | 136 | ▶ | return $result unless $treatment; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||||||||||||
| 626 | # Treat each row | ||||||||||||||||||||||||||||||||||||||||||||||
| 627 | 8 | foreach my $row (@$result) { | |||||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||||||
| 629 | # Treat each treatable row value | ||||||||||||||||||||||||||||||||||||||||||||||
| 630 | 12 | foreach (@treatment) { | |||||||||||||||||||||||||||||||||||||||||||||
| 631 | $row->{$_} = $treatsub{$_}->( | ||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 12 | $row->{$_}, @{$treatment->{$_}} | |||||||||||||||||||||||||||||||||||||||||||||
| 633 | 12 | ▶ | ) if $row->{$_}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 634 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 635 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||||||
| 637 | # Return result | ||||||||||||||||||||||||||||||||||||||||||||||
| 638 | 8 | $result; | |||||||||||||||||||||||||||||||||||||||||||||
| 639 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||||||||||||||||||||
| 642 | # List elements | ||||||||||||||||||||||||||||||||||||||||||||||
| 643 | 21 | ▶ | sub list { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list: called CC=41 SCAR=38.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 21 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||||||
| 646 | # Get callback | ||||||||||||||||||||||||||||||||||||||||||||||
| 647 | 21 | ▶ | my $cb = pop if ref $_[-1] && ref $_[-1] eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_[-1] and ref $_[-1] eq "CODE"
MC/DC50%1/2
ref $_[-1] and ref $_[-1] eq "CODE"
ref $_[-1]
ref $_[-1] eq "CODE"
Truth table66%2/3
A and B
A= ref $_[-1]B= ref $_[-1] eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||||||||||||||
| 649 | # Get param hash reference | ||||||||||||||||||||||||||||||||||||||||||||||
| 650 | 21 | ▶ | my $param = pop if ref $_[-1] && ref $_[-1] eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_[-1] and ref $_[-1] eq "HASH"
MC/DC0%0/2
ref $_[-1] and ref $_[-1] eq "HASH"
ref $_[-1]
ref $_[-1] eq "HASH"
Truth table33%1/3
A and B
A= ref $_[-1]B= ref $_[-1] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||||||
| 652 | # Get table object | ||||||||||||||||||||||||||||||||||||||||||||||
| 653 | 21 | ▶ | $self = $self->table( @_ ) if $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||||||||||||||
| 655 | 21 | my (%condition, %pagination); | |||||||||||||||||||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||||||||||||||||||||
| 657 | # Check numerical values | ||||||||||||||||||||||||||||||||||||||||||||||
| 658 | 21 | ▶ | my $start_index = _check_param($param, 'startIndex', 'num') // 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_check_param($param, "startIndex", "num") // 0
MC/DC100%1/1
_check_param($param, "startIndex", "num") // 0
_check_param($param, "startIndex", "num")
Truth table100%2/2
A
A= _check_param($param, "startIndex", "num")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 659 | 21 | my $count = _check_param($param, 'count', 'num'); | |||||||||||||||||||||||||||||||||||||||||||||
| 660 | 21 | ▶ | my $start_page = _check_param($param, 'startPage', 'num') // 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
_check_param($param, "startPage", "num") // 1
MC/DC100%1/1
_check_param($param, "startPage", "num") // 1
_check_param($param, "startPage", "num")
Truth table100%2/2
A
A= _check_param($param, "startPage", "num")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||||||||||
| 662 | # Set caching condition | ||||||||||||||||||||||||||||||||||||||||||||||
| 663 | 21 | ▶ | $condition{-cache} = delete $param->{-cache} if $param->{-cache}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||||||||||||||||||||
| 665 | ### Sorting parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| 666 | 21 | my %sort; | |||||||||||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||||||||||||
| 668 | # Check, if parameter is a field | ||||||||||||||||||||||||||||||||||||||||||||||
| 669 | 21 | my $sort_by = _check_param($param, 'sortBy'); | |||||||||||||||||||||||||||||||||||||||||||||
| 670 | 21 | ▶ | if ($sort_by && $sort_by =~ s/^\s*($KEY_REGEX)\s*$/$1/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sort_by and $sort_by =~ s/^\s*($DBIx::Oro::KEY_REGEX)\s*$/$1/
MC/DC0%0/2
$sort_by and $sort_by =~ s/^\s*($DBIx::Oro::KEY_REGEX)\s*$/$1/
$sort_by
$sort_by =~ s/^\s*($DBIx::Oro::KEY_REGEX)\s*$/$1/
Truth table33%1/3
A and B
A= $sort_byB= $sort_by =~ s/^\s*($DBIx::Oro::KEY_REGEX)\s*$/$1/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||||||||||||||||||||
| 672 | 21 | ▶ | $param->{sortOrder} //= 'ascending'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$param->{"sortOrder"} //= "ascending"
MC/DC100%1/1
$param->{"sortOrder"} //= "ascending"
$param->{"sortOrder"}
Truth table100%2/2
A
A= $param->{"sortOrder"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 673 | 21 | ▶ | my $sort_order = index(lc($param->{sortOrder}), 'desc') == 0 ? 'descending' : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||||||||||||||
| 675 | # Set SQL limitation | ||||||||||||||||||||||||||||||||||||||||||||||
| 676 | 21 | ▶ | $pagination{-order} = $sort_order ? "-$sort_by" : $sort_by; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||||||||||||
| 678 | # Set sort information | ||||||||||||||||||||||||||||||||||||||||||||||
| 679 | 21 | $sort{sortBy} = $sort_by; | |||||||||||||||||||||||||||||||||||||||||||||
| 680 | 21 | ▶ | $sort{sortOrder} = $sort_order if $sort_order; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 681 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||||||||||||||||||||
| 683 | # Set SQL limitations | ||||||||||||||||||||||||||||||||||||||||||||||
| 684 | 21 | $pagination{-offset} = $start_index; | |||||||||||||||||||||||||||||||||||||||||||||
| 685 | 21 | ▶ | $pagination{-limit} = $count || $ITEMS_PER_PAGE; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$count || $DBIx::Oro::ITEMS_PER_PAGE
MC/DC0%0/2
$count || $DBIx::Oro::ITEMS_PER_PAGE
$count
$DBIx::Oro::ITEMS_PER_PAGE
Truth table66%2/3
A || B
A= $countB= $DBIx::Oro::ITEMS_PER_PAGE
| |||||||||||||||||||||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||||||||||||||||||||
| 687 | # Not first page | ||||||||||||||||||||||||||||||||||||||||||||||
| 688 | 21 | ▶ | if ($start_page > 1) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||||||||||||||||||||
| 690 | # Set SQL limitations | ||||||||||||||||||||||||||||||||||||||||||||||
| 691 | 2 | ▶ | $pagination{-offset} //= 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$pagination{"-offset"} //= 0
MC/DC0%0/1
$pagination{"-offset"} //= 0
$pagination{"-offset"}
Truth table50%1/2
A
A= $pagination{"-offset"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 692 | 2 | $pagination{-offset} += (($start_page - 1) * $pagination{-limit}); | |||||||||||||||||||||||||||||||||||||||||||||
| 693 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||||||
| 696 | ### Filter parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| 697 | 21 | my %filter; | |||||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||||||
| 699 | # Filter parameter is set | ||||||||||||||||||||||||||||||||||||||||||||||
| 700 | 21 | my $filter_by = _check_param($param, 'filterBy'); | |||||||||||||||||||||||||||||||||||||||||||||
| 701 | 21 | ▶ | if ($filter_by) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||||||||||||||||
| 703 | # Filter operation is set | ||||||||||||||||||||||||||||||||||||||||||||||
| 704 | 21 | my $filter_op = _check_param($param, 'filterOp'); | |||||||||||||||||||||||||||||||||||||||||||||
| 705 | 21 | ▶ | if ($filter_op) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 706 | 21 | $filter_op = lc $filter_op; | |||||||||||||||||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||||||||||||||||
| 708 | # Set parameters for response | ||||||||||||||||||||||||||||||||||||||||||||||
| 709 | 21 | $filter{filterBy} = $filter_by; | |||||||||||||||||||||||||||||||||||||||||||||
| 710 | 21 | $filter{filterOp} = $filter_op; | |||||||||||||||||||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||||||||||||||||||||
| 712 | # Check for presence | ||||||||||||||||||||||||||||||||||||||||||||||
| 713 | 21 | ▶ | if ($filter_op eq 'present') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||||||||||||||
| 715 | # Create SQL condition | ||||||||||||||||||||||||||||||||||||||||||||||
| 716 | 4 | $condition{$filter_by} = { not => undef }; | |||||||||||||||||||||||||||||||||||||||||||||
| 717 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||||||
| 719 | # Check for absence | ||||||||||||||||||||||||||||||||||||||||||||||
| 720 | elsif ($filter_op eq 'absent') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||||||||||||
| 722 | # Create SQL condition | ||||||||||||||||||||||||||||||||||||||||||||||
| 723 | 1 | $condition{$filter_by} = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 724 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||||||||||||||||
| 726 | # Check with filterValue | ||||||||||||||||||||||||||||||||||||||||||||||
| 727 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||||||||||||||||||||
| 729 | # Get filterValue | ||||||||||||||||||||||||||||||||||||||||||||||
| 730 | 16 | ▶ | if (my $fv = _check_param($param, 'filterValue')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||||||||||||||
| 732 | # Set filter value for response | ||||||||||||||||||||||||||||||||||||||||||||||
| 733 | 16 | $filter{filterValue} = $fv; | |||||||||||||||||||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||||||||||||||||||||
| 735 | # Check for equality | ||||||||||||||||||||||||||||||||||||||||||||||
| 736 | 16 | ▶ | if ($filter_op eq 'equals') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||||||||||||||||||||
| 738 | # Equals the value | ||||||||||||||||||||||||||||||||||||||||||||||
| 739 | 1 | $condition{$filter_by} = $fv; | |||||||||||||||||||||||||||||||||||||||||||||
| 740 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||||||||||||||||||||
| 742 | # Check for disparaty | ||||||||||||||||||||||||||||||||||||||||||||||
| 743 | elsif ($filter_op eq 'disparate') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||||||||||||||
| 745 | # Equals the value | ||||||||||||||||||||||||||||||||||||||||||||||
| 746 | 1 | $condition{$filter_by} = { ne => $fv }; | |||||||||||||||||||||||||||||||||||||||||||||
| 747 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||||||||||||||||
| 749 | # Check with SQL like | ||||||||||||||||||||||||||||||||||||||||||||||
| 750 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 751 | 14 | $fv =~ s/([\%_])/\\$1/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||||||||||||||
| 753 | # Check for containing | ||||||||||||||||||||||||||||||||||||||||||||||
| 754 | 14 | ▶ | if ($filter_op eq 'contains') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 755 | 1 | $condition{$filter_by} = { like => "%${fv}%" }; | |||||||||||||||||||||||||||||||||||||||||||||
| 756 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||||||||||||||
| 758 | # Check for beginning | ||||||||||||||||||||||||||||||||||||||||||||||
| 759 | elsif ($filter_op eq 'startswith') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||||||||||||||||
| 761 | # Set response operation | ||||||||||||||||||||||||||||||||||||||||||||||
| 762 | 13 | $filter{filterOp} = 'startsWith'; | |||||||||||||||||||||||||||||||||||||||||||||
| 763 | 13 | $condition{$filter_by} = { like => "${fv}%" }; | |||||||||||||||||||||||||||||||||||||||||||||
| 764 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 765 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 766 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||||||||||||||||||||
| 768 | # No filterValue - reset | ||||||||||||||||||||||||||||||||||||||||||||||
| 769 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 770 | 0 | %filter = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 771 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 772 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 773 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 774 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||||||||||||||||||||
| 776 | # Get count | ||||||||||||||||||||||||||||||||||||||||||||||
| 777 | 21 | my $total_results = $self->count(\%condition); | |||||||||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||||||||||
| 779 | # Something went wrong | ||||||||||||||||||||||||||||||||||||||||||||||
| 780 | 21 | ▶ | return unless defined $total_results; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||||||||||||||||||||
| 782 | # Check fields | ||||||||||||||||||||||||||||||||||||||||||||||
| 783 | 20 | my @fields; | |||||||||||||||||||||||||||||||||||||||||||||
| 784 | 20 | ▶ | if ($param->{fields}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||||||||||||||||||||
| 786 | # Fields is a reference | ||||||||||||||||||||||||||||||||||||||||||||||
| 787 | 5 | ▶ | if (ref $param->{fields}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 788 | 1 | @fields = @{ $param->{fields} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 789 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||||||||||||||||||||
| 791 | # Fields is a string | ||||||||||||||||||||||||||||||||||||||||||||||
| 792 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 793 | @fields = | ||||||||||||||||||||||||||||||||||||||||||||||
| 794 | 9 | grep { /^$KEY_REGEX$/ } | |||||||||||||||||||||||||||||||||||||||||||||
| 795 | 9 | map { s/\s//g; $_ } | |||||||||||||||||||||||||||||||||||||||||||||
| 796 | 4 | split /\s*,\s*/, $param->{fields}; | |||||||||||||||||||||||||||||||||||||||||||||
| 797 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 798 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||||||||||||||||||||
| 800 | 20 | my $entry; | |||||||||||||||||||||||||||||||||||||||||||||
| 801 | |||||||||||||||||||||||||||||||||||||||||||||||
| 802 | # More than one result | ||||||||||||||||||||||||||||||||||||||||||||||
| 803 | 20 | ▶ | if ($total_results) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||||||||||||||||
| 805 | # Table is joined and there are fields existing | ||||||||||||||||||||||||||||||||||||||||||||||
| 806 | 20 | ▶ | if (ref $self->{table} && @fields) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
ref $self->{"table"} and @fields
MC/DC50%1/2
ref $self->{"table"} and @fields
ref $self->{"table"}
@fields
Truth table66%2/3
A and B
A= ref $self->{"table"}B= @fields
| |||||||||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||||||
| 808 | # Is a joined table, filter fields afterwards | ||||||||||||||||||||||||||||||||||||||||||||||
| 809 | 2 | my $select = $self->select({ %condition, %pagination }); | |||||||||||||||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||||||||||||||||
| 811 | # Iterate for filtering | ||||||||||||||||||||||||||||||||||||||||||||||
| 812 | 2 | foreach my $row (@$select) { | |||||||||||||||||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||||||||||||||||
| 814 | # Filter | ||||||||||||||||||||||||||||||||||||||||||||||
| 815 | 12 | my %new; | |||||||||||||||||||||||||||||||||||||||||||||
| 816 | 12 | foreach (@fields) { | |||||||||||||||||||||||||||||||||||||||||||||
| 817 | 36 | ▶ | $new{$_} = $row->{$_} if exists $row->{$_}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 818 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 819 | 12 | push(@$entry, \%new); | |||||||||||||||||||||||||||||||||||||||||||||
| 820 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||||||||||||||||
| 822 | # Define fields | ||||||||||||||||||||||||||||||||||||||||||||||
| 823 | 2 | $filter{fields} = \@fields; | |||||||||||||||||||||||||||||||||||||||||||||
| 824 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||||||||||||||||||||
| 826 | # Fields in simple table | ||||||||||||||||||||||||||||||||||||||||||||||
| 827 | elsif (@fields) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 828 | # Just prepend field to select | ||||||||||||||||||||||||||||||||||||||||||||||
| 829 | 3 | $filter{fields} = \@fields; | |||||||||||||||||||||||||||||||||||||||||||||
| 830 | 3 | $entry = $self->select(\@fields, { %condition, %pagination }); | |||||||||||||||||||||||||||||||||||||||||||||
| 831 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||||||||||||||||
| 833 | # No fields | ||||||||||||||||||||||||||||||||||||||||||||||
| 834 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 835 | 15 | $entry = $self->select({ %condition, %pagination }); | |||||||||||||||||||||||||||||||||||||||||||||
| 836 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 837 | |||||||||||||||||||||||||||||||||||||||||||||||
| 838 | # Use callback for each entry | ||||||||||||||||||||||||||||||||||||||||||||||
| 839 | 20 | ▶ | if ($cb && @$entry) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$cb and @$entry
MC/DC50%1/2
$cb and @$entry
$cb
@$entry
Truth table66%2/3
A and B
A= $cbB= @$entry
| |||||||||||||||||||||||||||||||||||||||||||||||
| 840 | 1 | my @entry_cb; | |||||||||||||||||||||||||||||||||||||||||||||
| 841 | 1 | push( @entry_cb, $cb->($_) ) foreach @$entry; | |||||||||||||||||||||||||||||||||||||||||||||
| 842 | 1 | $entry = \@entry_cb; | |||||||||||||||||||||||||||||||||||||||||||||
| 843 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 844 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||||||||||||||||||||
| 846 | # Return response | ||||||||||||||||||||||||||||||||||||||||||||||
| 847 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 848 | totalResults => $total_results, | ||||||||||||||||||||||||||||||||||||||||||||||
| 849 | startIndex => $start_index, | ||||||||||||||||||||||||||||||||||||||||||||||
| 850 | itemsPerPage => $pagination{-limit}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 851 | 20 | ▶ | startPage => $start_page, | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$entry || []
MC/DC0%0/1
$entry || []
$entry
Truth table50%1/2
A
A= $entry
| |||||||||||||||||||||||||||||||||||||||||||||||
| 852 | entry => $entry || [], | ||||||||||||||||||||||||||||||||||||||||||||||
| 853 | %filter, | ||||||||||||||||||||||||||||||||||||||||||||||
| 854 | %sort | ||||||||||||||||||||||||||||||||||||||||||||||
| 855 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 856 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||||||||||||
| 859 | # Load one line | ||||||||||||||||||||||||||||||||||||||||||||||
| 860 | 32 | ▶ | sub load { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=6 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 861 | 32 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 862 | 32 | my @param = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||||||||||||||||||||
| 864 | # Has a condition | ||||||||||||||||||||||||||||||||||||||||||||||
| 865 | 32 | ▶ | if ($param[-1] && ref($param[-1])) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$param[-1] and ref $param[-1]
MC/DC0%0/2
$param[-1] and ref $param[-1]
$param[-1]
ref $param[-1]
Truth table33%1/3
A and B
A= $param[-1]B= ref $param[-1]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||||||||||||||||||||
| 867 | # Add limitation to the condition | ||||||||||||||||||||||||||||||||||||||||||||||
| 868 | 32 | ▶ | if (ref($param[-1]) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 869 | 27 | $param[-1]->{-limit} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 870 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 871 | |||||||||||||||||||||||||||||||||||||||||||||||
| 872 | # Load is malformed | ||||||||||||||||||||||||||||||||||||||||||||||
| 873 | elsif (ref($param[-1]) ne 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 874 | 0 | carp 'Load is malformed'; | |||||||||||||||||||||||||||||||||||||||||||||
| 875 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 876 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 877 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||||||||||||||||||||
| 879 | # Has no condition yet | ||||||||||||||||||||||||||||||||||||||||||||||
| 880 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 881 | 0 | push(@param, { -limit => 1 }); | |||||||||||||||||||||||||||||||||||||||||||||
| 882 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||||||||||||||||
| 884 | # Select with limit | ||||||||||||||||||||||||||||||||||||||||||||||
| 885 | 32 | my $row = $self->select(@param); | |||||||||||||||||||||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||||||||||||||||||||
| 887 | # Error or not found | ||||||||||||||||||||||||||||||||||||||||||||||
| 888 | 32 | ▶ | return unless $row; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||||||||||||||||||||
| 890 | # Return row | ||||||||||||||||||||||||||||||||||||||||||||||
| 891 | 31 | $row->[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 892 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||||||||||||||||
| 894 | |||||||||||||||||||||||||||||||||||||||||||||||
| 895 | # Delete entry | ||||||||||||||||||||||||||||||||||||||||||||||
| 896 | 11 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=8 SCAR=21.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 897 | 11 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 898 | |||||||||||||||||||||||||||||||||||||||||||||||
| 899 | # Get table name | ||||||||||||||||||||||||||||||||||||||||||||||
| 900 | 11 | ▶ | my $table = _table_name($self, \@_) or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||||||||||||||||||||
| 902 | # Build sql | ||||||||||||||||||||||||||||||||||||||||||||||
| 903 | 11 | my $sql = 'DELETE FROM ' . $table; | |||||||||||||||||||||||||||||||||||||||||||||
| 904 | |||||||||||||||||||||||||||||||||||||||||||||||
| 905 | # Condition | ||||||||||||||||||||||||||||||||||||||||||||||
| 906 | 11 | my ($pairs, $values, $prep, $secure); | |||||||||||||||||||||||||||||||||||||||||||||
| 907 | 11 | ▶ | if ($_[0]) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 908 | |||||||||||||||||||||||||||||||||||||||||||||||
| 909 | # Add condition | ||||||||||||||||||||||||||||||||||||||||||||||
| 910 | 5 | ($pairs, $values, $prep) = _get_pairs( shift(@_) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 911 | |||||||||||||||||||||||||||||||||||||||||||||||
| 912 | # Add where clause to sql | ||||||||||||||||||||||||||||||||||||||||||||||
| 913 | 5 | ▶ | $sql .= ' WHERE ' . join(' AND ', @$pairs) if @$pairs || $prep; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@$pairs or $prep
MC/DC0%0/2
@$pairs or $prep
@$pairs
$prep
Truth table33%1/3
A or B
A= @$pairsB= $prep
| |||||||||||||||||||||||||||||||||||||||||||||||
| 914 | |||||||||||||||||||||||||||||||||||||||||||||||
| 915 | # Apply restrictions | ||||||||||||||||||||||||||||||||||||||||||||||
| 916 | 5 | ▶ | $sql .= _restrictions($prep, $values) if $prep; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 917 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||||||||||||||||||||
| 919 | # Prepare and execute deletion | ||||||||||||||||||||||||||||||||||||||||||||||
| 920 | 11 | my $rv = $self->prep_and_exec($sql, $values); | |||||||||||||||||||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||||||||||||||||||||
| 922 | # Return value | ||||||||||||||||||||||||||||||||||||||||||||||
| 923 | 11 | ▶ | return (!$rv || $rv eq '0E0') ? 0 : $rv; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $rv or $rv eq "0E0"
MC/DC50%1/2
not $rv or $rv eq "0E0"
not $rv
$rv eq "0E0"
Truth table66%2/3
not(A) or B
A= not $rvB= $rv eq "0E0"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 924 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||||||||||||||||||||
| 926 | |||||||||||||||||||||||||||||||||||||||||||||||
| 927 | # Update or insert a value | ||||||||||||||||||||||||||||||||||||||||||||||
| 928 | 10 | ▶ | sub merge { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
merge: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 929 | 10 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||||||||||||||||||||
| 931 | # Get table name | ||||||||||||||||||||||||||||||||||||||||||||||
| 932 | 10 | ▶ | my $table = _table_name($self, \@_) or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 933 | |||||||||||||||||||||||||||||||||||||||||||||||
| 934 | 10 | my %param = %{ shift( @_ ) }; | |||||||||||||||||||||||||||||||||||||||||||||
| 935 | 10 | ▶ | my %cond = $_[0] ? %{ shift( @_ ) } : (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 936 | |||||||||||||||||||||||||||||||||||||||||||||||
| 937 | # Prefix with table if necessary | ||||||||||||||||||||||||||||||||||||||||||||||
| 938 | 10 | my @param = ( \%param, \%cond ); | |||||||||||||||||||||||||||||||||||||||||||||
| 939 | 10 | ▶ | unshift(@param, $table) unless $self->{table}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||||||||||||||||||||
| 941 | 10 | my $rv; | |||||||||||||||||||||||||||||||||||||||||||||
| 942 | 10 | my $job = 'update'; | |||||||||||||||||||||||||||||||||||||||||||||
| 943 | $self->txn( | ||||||||||||||||||||||||||||||||||||||||||||||
| 944 | 10 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 945 | |||||||||||||||||||||||||||||||||||||||||||||||
| 946 | # Update | ||||||||||||||||||||||||||||||||||||||||||||||
| 947 | 10 | $rv = $self->update( @param ); | |||||||||||||||||||||||||||||||||||||||||||||
| 948 | |||||||||||||||||||||||||||||||||||||||||||||||
| 949 | 10 | ▶ | return 1 if $rv; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 950 | |||||||||||||||||||||||||||||||||||||||||||||||
| 951 | # Delete all element conditions | ||||||||||||||||||||||||||||||||||||||||||||||
| 952 | 5 | delete $cond{$_} foreach grep { | |||||||||||||||||||||||||||||||||||||||||||||
| 953 | 6 | ▶ | ref($cond{$_}) && !blessed($cond{$_}) | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
ref $cond{$_} && !builtin::blessed($cond{$_})
MC/DC100%2/2
ref $cond{$_} && !builtin::blessed($cond{$_})
ref $cond{$_}
!builtin::blessed($cond{$_})
Truth table100%3/3
A && not(B)
A= ref $cond{$_}B= !builtin::blessed($cond{$_})
| |||||||||||||||||||||||||||||||||||||||||||||||
| 954 | } keys %cond; | ||||||||||||||||||||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||||||||||||||||||||
| 956 | # Insert | ||||||||||||||||||||||||||||||||||||||||||||||
| 957 | 5 | @param = ( { %param, %cond } ); | |||||||||||||||||||||||||||||||||||||||||||||
| 958 | 5 | ▶ | unshift(@param, $table) unless $self->{table}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 959 | |||||||||||||||||||||||||||||||||||||||||||||||
| 960 | 5 | ▶ | $rv = $self->insert(@param) or return -1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 961 | |||||||||||||||||||||||||||||||||||||||||||||||
| 962 | 4 | $job = 'insert'; | |||||||||||||||||||||||||||||||||||||||||||||
| 963 | |||||||||||||||||||||||||||||||||||||||||||||||
| 964 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 965 | 10 | ▶ | }) or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 966 | |||||||||||||||||||||||||||||||||||||||||||||||
| 967 | # Return value is bigger than 0 | ||||||||||||||||||||||||||||||||||||||||||||||
| 968 | 9 | ▶ | if ($rv && $rv > 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$rv and $rv > 0
MC/DC0%0/2
$rv and $rv > 0
$rv
$rv > 0
Truth table33%1/3
A and B
A= $rvB= $rv > 0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 969 | 9 | ▶ | return wantarray ? ($rv, $job) : $rv; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 970 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 971 | |||||||||||||||||||||||||||||||||||||||||||||||
| 972 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 973 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 974 | |||||||||||||||||||||||||||||||||||||||||||||||
| 975 | |||||||||||||||||||||||||||||||||||||||||||||||
| 976 | # Count results | ||||||||||||||||||||||||||||||||||||||||||||||
| 977 | 55 | ▶ | sub count { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count: called CC=16 SCAR=27.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 978 | 55 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 979 | |||||||||||||||||||||||||||||||||||||||||||||||
| 980 | # Init arrays | ||||||||||||||||||||||||||||||||||||||||||||||
| 981 | 55 | my ($tables, $fields, $join_pairs, $treatment, $field_alias) = | |||||||||||||||||||||||||||||||||||||||||||||
| 982 | _table_obj($self, \@_); | ||||||||||||||||||||||||||||||||||||||||||||||
| 983 | |||||||||||||||||||||||||||||||||||||||||||||||
| 984 | 55 | my @pairs = @$join_pairs; | |||||||||||||||||||||||||||||||||||||||||||||
| 985 | |||||||||||||||||||||||||||||||||||||||||||||||
| 986 | # Build sql | ||||||||||||||||||||||||||||||||||||||||||||||
| 987 | 55 | my $sql = | |||||||||||||||||||||||||||||||||||||||||||||
| 988 | 'SELECT ' . join(', ', 'count(1)', @$fields) . | ||||||||||||||||||||||||||||||||||||||||||||||
| 989 | ' FROM ' . join(', ', @$tables); | ||||||||||||||||||||||||||||||||||||||||||||||
| 990 | |||||||||||||||||||||||||||||||||||||||||||||||
| 991 | # Ignore fields | ||||||||||||||||||||||||||||||||||||||||||||||
| 992 | 55 | ▶ | shift if $_[0] && ref $_[0] eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$_[0] and ref $_[0] eq "ARRAY"
MC/DC100%2/2
$_[0] and ref $_[0] eq "ARRAY"
$_[0]
ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= $_[0]B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 993 | |||||||||||||||||||||||||||||||||||||||||||||||
| 994 | # Get conditions | ||||||||||||||||||||||||||||||||||||||||||||||
| 995 | 55 | my ($pairs, $values, $prep); | |||||||||||||||||||||||||||||||||||||||||||||
| 996 | 55 | ▶ | if ($_[0] && ref $_[0] eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[0] and ref $_[0] eq "HASH"
MC/DC50%1/2
$_[0] and ref $_[0] eq "HASH"
$_[0]
ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= $_[0]B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 997 | 32 | ($pairs, $values, $prep) = _get_pairs( shift(@_), $field_alias ); | |||||||||||||||||||||||||||||||||||||||||||||
| 998 | 32 | ▶ | push(@pairs, @$pairs) if $pairs->[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 999 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | # Add where clause | ||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | 55 | ▶ | $sql .= ' WHERE ' . join(' AND ', @pairs) if @pairs; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | 55 | $sql .= ' LIMIT 1'; | |||||||||||||||||||||||||||||||||||||||||||||
| 1004 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1005 | 55 | my $result; | |||||||||||||||||||||||||||||||||||||||||||||
| 1006 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1007 | # Check cache | ||||||||||||||||||||||||||||||||||||||||||||||
| 1008 | 55 | my ($chi, $key, $chi_param); | |||||||||||||||||||||||||||||||||||||||||||||
| 1009 | 55 | ▶ | if ($prep && $prep->{cache}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$prep and $prep->{"cache"}
MC/DC50%1/2
$prep and $prep->{"cache"}
$prep
$prep->{"cache"}
Truth table66%2/3
A and B
A= $prepB= $prep->{"cache"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1010 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1011 | # Cache parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| 1012 | 2 | ($chi, $key, $chi_param) = @{$prep->{cache}}; | |||||||||||||||||||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1014 | # Generate key | ||||||||||||||||||||||||||||||||||||||||||||||
| 1015 | 2 | ▶ | $key = $sql . '-' . join('-', @$values) unless $key; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1016 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1017 | # Get cache result | ||||||||||||||||||||||||||||||||||||||||||||||
| 1018 | 2 | ▶ | if ($result = $chi->get($key)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1019 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1020 | # Last sql command | ||||||||||||||||||||||||||||||||||||||||||||||
| 1021 | 1 | $self->{last_sql} = $sql . ' -- ' . $CACHE_COMMENT; | |||||||||||||||||||||||||||||||||||||||||||||
| 1022 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1023 | # Return cache result | ||||||||||||||||||||||||||||||||||||||||||||||
| 1024 | 1 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||
| 1025 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1026 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1027 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1028 | # Prepare and execute | ||||||||||||||||||||||||||||||||||||||||||||||
| 1029 | 54 | ▶ | my ($rv, $sth) = $self->prep_and_exec($sql, $values || []); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$values || []
MC/DC100%1/1
$values || []
$values
Truth table100%2/2
A
A= $values
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1030 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1031 | # Return value is empty | ||||||||||||||||||||||||||||||||||||||||||||||
| 1032 | 54 | ▶ | return undef if !$rv; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1033 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1034 | # Return count | ||||||||||||||||||||||||||||||||||||||||||||||
| 1035 | 51 | ▶ | $result = $sth->fetchrow_arrayref->[0] || 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$sth->fetchrow_arrayref->[0] || 0
MC/DC100%1/1
$sth->fetchrow_arrayref->[0] || 0
$sth->fetchrow_arrayref->[0]
Truth table100%2/2
A
A= $sth->fetchrow_arrayref->[0]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1036 | 51 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||||||
| 1037 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1038 | # Save to cache | ||||||||||||||||||||||||||||||||||||||||||||||
| 1039 | 51 | ▶ | $chi->set($key => $result, $chi_param) if $chi && $result; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$chi and $result
MC/DC50%1/2
$chi and $result
$chi
$result
Truth table66%2/3
A and B
A= $chiB= $result
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1040 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1041 | # Return result | ||||||||||||||||||||||||||||||||||||||||||||||
| 1042 | 51 | $result; | |||||||||||||||||||||||||||||||||||||||||||||
| 1043 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1044 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1045 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1046 | # Prepare and execute | ||||||||||||||||||||||||||||||||||||||||||||||
| 1047 | 1198 | ▶ | sub prep_and_exec { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prep_and_exec: called CC=9 SCAR=24.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1048 | 1198 | my ($self, $sql, $values, $cached) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 1049 | 1198 | my $dbh = $self->dbh; | |||||||||||||||||||||||||||||||||||||||||||||
| 1050 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1051 | # Last sql command | ||||||||||||||||||||||||||||||||||||||||||||||
| 1052 | 1198 | $self->{last_sql} = $sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 1053 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1054 | # Prepare | ||||||||||||||||||||||||||||||||||||||||||||||
| 1055 | 1198 | ▶ | my $sth = | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1056 | $cached ? $dbh->prepare_cached( $sql ) : | ||||||||||||||||||||||||||||||||||||||||||||||
| 1057 | $dbh->prepare( $sql ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 1058 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1059 | # Check for errors | ||||||||||||||||||||||||||||||||||||||||||||||
| 1060 | 1198 | ▶ | if ($dbh->err) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1061 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1062 | 11 | ▶ | if (index($dbh->errstr, 'database') <= 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1063 | 11 | carp $dbh->errstr . ' in "' . _trim_last_sql($self->last_sql) . '"'; | |||||||||||||||||||||||||||||||||||||||||||||
| 1064 | 11 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 1065 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1066 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1067 | # Retry with reconnect | ||||||||||||||||||||||||||||||||||||||||||||||
| 1068 | 0 | $dbh = $self->_connect; | |||||||||||||||||||||||||||||||||||||||||||||
| 1069 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1070 | 0 | ▶ | $sth = | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1071 | $cached ? $dbh->prepare_cached( $sql ) : | ||||||||||||||||||||||||||||||||||||||||||||||
| 1072 | $dbh->prepare( $sql ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 1073 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1074 | 0 | ▶ | if ($dbh->err) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1075 | 0 | carp $dbh->errstr . ' in "' . _trim_last_sql($self->last_sql) . '"'; | |||||||||||||||||||||||||||||||||||||||||||||
| 1076 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 1077 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1078 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1079 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1080 | # No statement handle established | ||||||||||||||||||||||||||||||||||||||||||||||
| 1081 | 1187 | ▶ | return unless $sth; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1082 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1083 | # Execute | ||||||||||||||||||||||||||||||||||||||||||||||
| 1084 | 1187 | my $rv = $sth->execute( @$values ); | |||||||||||||||||||||||||||||||||||||||||||||
| 1085 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1086 | # Check for errors | ||||||||||||||||||||||||||||||||||||||||||||||
| 1087 | 1187 | ▶ | if ($dbh->err) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1088 | 4 | carp $dbh->errstr . ' in "' . _trim_last_sql($self->last_sql) . '"'; | |||||||||||||||||||||||||||||||||||||||||||||
| 1089 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 1090 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1091 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1092 | # Return value and statement | ||||||||||||||||||||||||||||||||||||||||||||||
| 1093 | 1183 | ▶ | return ($rv, $sth) if wantarray; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1094 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1095 | # Finish statement | ||||||||||||||||||||||||||||||||||||||||||||||
| 1096 | 971 | $sth->finish; | |||||||||||||||||||||||||||||||||||||||||||||
| 1097 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1098 | # Return value | ||||||||||||||||||||||||||||||||||||||||||||||
| 1099 | 971 | $rv; | |||||||||||||||||||||||||||||||||||||||||||||
| 1100 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1101 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1102 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1103 | # Wrapper for DBI do | ||||||||||||||||||||||||||||||||||||||||||||||
| 1104 | 133 | ▶ | sub do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1105 | 133 | $_[0]->{last_sql} = $_[1]; | |||||||||||||||||||||||||||||||||||||||||||||
| 1106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1107 | # Database connection | ||||||||||||||||||||||||||||||||||||||||||||||
| 1108 | 133 | my $dbh = shift->dbh; | |||||||||||||||||||||||||||||||||||||||||||||
| 1109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1110 | 133 | my $rv = $dbh->do( @_ ); | |||||||||||||||||||||||||||||||||||||||||||||
| 1111 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1112 | # Error | ||||||||||||||||||||||||||||||||||||||||||||||
| 1113 | 133 | ▶ | carp $dbh->errstr . ' in "' . $_[0] . '"' if !$rv && $dbh->err; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $rv and $dbh->err
MC/DC0%0/2
not $rv and $dbh->err
not $rv
$dbh->err
Truth table33%1/3
not(A) and B
A= not $rvB= $dbh->err
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1114 | 133 | return $rv; | |||||||||||||||||||||||||||||||||||||||||||||
| 1115 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1116 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1117 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1118 | # Explain query plan | ||||||||||||||||||||||||||||||||||||||||||||||
| 1119 | 0 | ▶ | sub explain { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
explain: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1120 | 0 | 'Not implemented for ' . $_[0]->driver; | |||||||||||||||||||||||||||||||||||||||||||||
| 1121 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1122 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1123 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1124 | # Wrap a transaction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1125 | 71 | ▶ | sub txn { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
txn: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1126 | 71 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 1127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1128 | # No callback defined | ||||||||||||||||||||||||||||||||||||||||||||||
| 1129 | 71 | ▶ | return unless $_[0] && ref($_[0]) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$_[0] and ref $_[0] eq "CODE"
MC/DC0%0/2
$_[0] and ref $_[0] eq "CODE"
$_[0]
ref $_[0] eq "CODE"
Truth table33%1/3
A and B
A= $_[0]B= ref $_[0] eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1130 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1131 | 71 | my $dbh = $self->dbh; | |||||||||||||||||||||||||||||||||||||||||||||
| 1132 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1133 | # Outside transaction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1134 | 71 | ▶ | if ($dbh->{AutoCommit}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1136 | # Start new transaction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1137 | 69 | $dbh->begin_work; | |||||||||||||||||||||||||||||||||||||||||||||
| 1138 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1139 | 69 | ${$self->{in_txn}} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 1140 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1141 | # start | ||||||||||||||||||||||||||||||||||||||||||||||
| 1142 | 69 | local $_ = $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 1143 | 69 | my $rv = $_[0]->($self); | |||||||||||||||||||||||||||||||||||||||||||||
| 1144 | 69 | ▶ | if (!$rv || $rv ne '-1') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $rv or $rv ne -1
MC/DC100%2/2
not $rv or $rv ne -1
not $rv
$rv ne -1
Truth table100%3/3
not(A) or B
A= not $rvB= $rv ne -1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1145 | 67 | ${$self->{in_txn}} = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 1146 | 67 | $dbh->commit; | |||||||||||||||||||||||||||||||||||||||||||||
| 1147 | 67 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 1148 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1150 | # Rollback | ||||||||||||||||||||||||||||||||||||||||||||||
| 1151 | 2 | ${$self->{in_txn}} = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 1152 | 2 | $dbh->rollback; | |||||||||||||||||||||||||||||||||||||||||||||
| 1153 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 1154 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1155 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1156 | # Inside transaction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1157 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1158 | 2 | ${$self->{in_txn}} = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 1159 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1160 | # Push savepoint on stack | ||||||||||||||||||||||||||||||||||||||||||||||
| 1161 | 2 | my $sp_array = $self->{savepoint}; | |||||||||||||||||||||||||||||||||||||||||||||
| 1162 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1163 | # Use PID for concurrent accesses | ||||||||||||||||||||||||||||||||||||||||||||||
| 1164 | 2 | my $sp = "orosp_${$}_"; | |||||||||||||||||||||||||||||||||||||||||||||
| 1165 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1166 | # Use TID for concurrent accesses | ||||||||||||||||||||||||||||||||||||||||||||||
| 1167 | 2 | ▶ | $sp .= threads->tid . '_' if $self->{tid}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1168 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1169 | 2 | $sp .= $sp_array->[0]++; | |||||||||||||||||||||||||||||||||||||||||||||
| 1170 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1171 | # Push new savepoint to array | ||||||||||||||||||||||||||||||||||||||||||||||
| 1172 | 2 | push(@$sp_array, $sp); | |||||||||||||||||||||||||||||||||||||||||||||
| 1173 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1174 | # Start transaction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1175 | 2 | $self->do("SAVEPOINT $sp"); | |||||||||||||||||||||||||||||||||||||||||||||
| 1176 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1177 | # Run wrap actions | ||||||||||||||||||||||||||||||||||||||||||||||
| 1178 | 2 | my $rv = $_[0]->($self); | |||||||||||||||||||||||||||||||||||||||||||||
| 1179 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1180 | # Pop savepoint from stack | ||||||||||||||||||||||||||||||||||||||||||||||
| 1181 | 2 | my $last_sp = pop(@$sp_array); | |||||||||||||||||||||||||||||||||||||||||||||
| 1182 | 2 | ▶ | if ($last_sp eq $sp) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1183 | 2 | $sp_array->[0]--; | |||||||||||||||||||||||||||||||||||||||||||||
| 1184 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1185 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1186 | # Last savepoint does not match | ||||||||||||||||||||||||||||||||||||||||||||||
| 1187 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1188 | 0 | carp "Savepoint $sp is not the last savepoint on stack"; | |||||||||||||||||||||||||||||||||||||||||||||
| 1189 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1190 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1191 | # Commit savepoint | ||||||||||||||||||||||||||||||||||||||||||||||
| 1192 | 2 | ▶ | if (!$rv || $rv ne '-1') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $rv or $rv ne -1
MC/DC50%1/2
not $rv or $rv ne -1
not $rv
$rv ne -1
Truth table66%2/3
not(A) or B
A= not $rvB= $rv ne -1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1193 | 1 | $self->do("RELEASE SAVEPOINT $sp"); | |||||||||||||||||||||||||||||||||||||||||||||
| 1194 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 1195 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1196 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1197 | # Rollback | ||||||||||||||||||||||||||||||||||||||||||||||
| 1198 | 1 | $self->do("ROLLBACK TO SAVEPOINT $sp"); | |||||||||||||||||||||||||||||||||||||||||||||
| 1199 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 1200 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1201 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1202 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1203 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1204 | # Add connect event | ||||||||||||||||||||||||||||||||||||||||||||||
| 1205 | 3 | ▶ | sub on_connect { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
on_connect: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1206 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 1207 | 3 | my $cb = pop; | |||||||||||||||||||||||||||||||||||||||||||||
| 1208 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1209 | # Parameter is no subroutine | ||||||||||||||||||||||||||||||||||||||||||||||
| 1210 | 3 | ▶ | return unless ref $cb && ref $cb eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $cb and ref $cb eq "CODE"
MC/DC0%0/2
ref $cb and ref $cb eq "CODE"
ref $cb
ref $cb eq "CODE"
Truth table33%1/3
A and B
A= ref $cbB= ref $cb eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1211 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1212 | 3 | ▶ | my $name = shift || '_cb_' . $self->{_connect_cb}++; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || '_cb_' . $self->{'_connect_cb'}++
MC/DC100%1/1
shift() || '_cb_' . $self->{'_connect_cb'}++
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1213 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1214 | # Push subroutines on_connect | ||||||||||||||||||||||||||||||||||||||||||||||
| 1215 | 3 | ▶ | unless (exists $self->{on_connect}->{$name}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1216 | 2 | $self->{on_connect}->{$name} = $cb; | |||||||||||||||||||||||||||||||||||||||||||||
| 1217 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 1218 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1219 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1220 | # Event was not newly established | ||||||||||||||||||||||||||||||||||||||||||||||
| 1221 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 1222 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1223 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1224 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1225 | # Wrapper for DBI last_insert_id | ||||||||||||||||||||||||||||||||||||||||||||||
| 1226 | 0 | ▶ | sub last_insert_id { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_insert_id: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1227 | 0 | my $dbh = shift->dbh; | |||||||||||||||||||||||||||||||||||||||||||||
| 1228 | 0 | ▶ | @_ = (undef) x 4 unless @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1229 | 0 | $dbh->last_insert_id(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 1230 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1231 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1232 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1233 | # Import files | ||||||||||||||||||||||||||||||||||||||||||||||
| 1234 | 0 | ▶ | sub import_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import_sql: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1235 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 1236 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1237 | 0 | carp 'import_sql is deprecated and will be deleted in further versions'; | |||||||||||||||||||||||||||||||||||||||||||||
| 1238 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1239 | # Get callback | ||||||||||||||||||||||||||||||||||||||||||||||
| 1240 | 0 | ▶ | my $cb = pop @_ if ref $_[-1] && ref $_[-1] eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $_[-1] and ref $_[-1] eq "CODE"
MC/DC0%0/2
ref $_[-1] and ref $_[-1] eq "CODE"
ref $_[-1]
ref $_[-1] eq "CODE"
Truth table0%0/3
A and B
A= ref $_[-1]B= ref $_[-1] eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1241 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1242 | 0 | ▶ | my $files = @_ > 1 ? \@_ : shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1243 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1244 | 0 | ▶ | return unless $files; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1245 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1246 | # Import subroutine | ||||||||||||||||||||||||||||||||||||||||||||||
| 1247 | 0 | ▶ | my $import = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1248 | 0 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 1249 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1250 | # No file given | ||||||||||||||||||||||||||||||||||||||||||||||
| 1251 | 0 | ▶ | return unless $file; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1252 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1253 | 0 | ▶ | if (open(SQL, '<:utf8', $file)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1254 | 0 | my @sql = split(/^--\s-.*?$/m, join('', <SQL>)); | |||||||||||||||||||||||||||||||||||||||||||||
| 1255 | 0 | close(SQL); | |||||||||||||||||||||||||||||||||||||||||||||
| 1256 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1257 | # Start transaction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1258 | return $self->txn( | ||||||||||||||||||||||||||||||||||||||||||||||
| 1259 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1260 | 0 | my ($sql, @sql_seq);; | |||||||||||||||||||||||||||||||||||||||||||||
| 1261 | 0 | foreach $sql (@sql) { | |||||||||||||||||||||||||||||||||||||||||||||
| 1262 | 0 | $sql =~ s/^(?:--.*?|\s*)?$//mg; | |||||||||||||||||||||||||||||||||||||||||||||
| 1263 | 0 | $sql =~ s/\n\n+/\n/sg; | |||||||||||||||||||||||||||||||||||||||||||||
| 1264 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1265 | # Use callback | ||||||||||||||||||||||||||||||||||||||||||||||
| 1266 | 0 | ▶ | @sql_seq = $cb->($sql) if $cb && $sql; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$cb and $sql
MC/DC0%0/2
$cb and $sql
$cb
$sql
Truth table0%0/3
A and B
A= $cbB= $sql
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1267 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1268 | 0 | ▶ | next unless $sql; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1269 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1270 | # Start import | ||||||||||||||||||||||||||||||||||||||||||||||
| 1271 | 0 | foreach (@sql_seq) { | |||||||||||||||||||||||||||||||||||||||||||||
| 1272 | 0 | ▶ | $self->do($_) or return -1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1273 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1274 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1275 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1276 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 1277 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1278 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1279 | # Unable to read SQL file | ||||||||||||||||||||||||||||||||||||||||||||||
| 1280 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1281 | 0 | carp "Unable to import file '$file'"; | |||||||||||||||||||||||||||||||||||||||||||||
| 1282 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 1283 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1284 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 1285 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1286 | # Multiple file import | ||||||||||||||||||||||||||||||||||||||||||||||
| 1287 | 0 | ▶ | if (ref $files) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1288 | return $self->txn( | ||||||||||||||||||||||||||||||||||||||||||||||
| 1289 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1290 | 0 | foreach (@$files) { | |||||||||||||||||||||||||||||||||||||||||||||
| 1291 | 0 | ▶ | $import->($_) or return -1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1292 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1293 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||
| 1294 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1295 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1296 | # Single file import | ||||||||||||||||||||||||||||||||||||||||||||||
| 1297 | 0 | return $import->($files); | |||||||||||||||||||||||||||||||||||||||||||||
| 1298 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1299 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1300 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1301 | # Disconnect on destroy | ||||||||||||||||||||||||||||||||||||||||||||||
| 1302 | 54 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1303 | 54 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 1304 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1305 | # Check if table is parent | ||||||||||||||||||||||||||||||||||||||||||||||
| 1306 | 54 | ▶ | unless (exists $self->{table}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1307 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1308 | # No database connection | ||||||||||||||||||||||||||||||||||||||||||||||
| 1309 | 28 | ▶ | return $self unless $self->{dbh}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1310 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1311 | # Delete password | ||||||||||||||||||||||||||||||||||||||||||||||
| 1312 | 28 | $self->_password(0); | |||||||||||||||||||||||||||||||||||||||||||||
| 1313 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1314 | # Delete cached kids | ||||||||||||||||||||||||||||||||||||||||||||||
| 1315 | 28 | ▶ | if (blessed $self->{dbh}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1316 | 28 | local $SIG{__WARN__} = \&_no_warn; | |||||||||||||||||||||||||||||||||||||||||||||
| 1317 | 28 | my $kids = $self->{dbh}->{CachedKids}; | |||||||||||||||||||||||||||||||||||||||||||||
| 1318 | 28 | ▶ | %$kids = () if $kids; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1319 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1320 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1321 | # Disconnect | ||||||||||||||||||||||||||||||||||||||||||||||
| 1322 | # $self->{dbh}->disconnect unless $self->{dbh}->{Kids}; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1323 | 28 | $self->{dbh}->disconnect; | |||||||||||||||||||||||||||||||||||||||||||||
| 1324 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1325 | # Delete parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| 1326 | 28 | delete $self->{$_} foreach qw/dbh on_connect _connect_cb/; | |||||||||||||||||||||||||||||||||||||||||||||
| 1327 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1328 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1329 | # Return object | ||||||||||||||||||||||||||||||||||||||||||||||
| 1330 | 54 | $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 1331 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1332 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1333 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1334 | # Initialize database | ||||||||||||||||||||||||||||||||||||||||||||||
| 1335 | 0 | ▶ | sub _init { 1 }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1336 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1337 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1338 | # Connect with database | ||||||||||||||||||||||||||||||||||||||||||||||
| 1339 | 31 | ▶ | sub _connect { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_connect: called CC=7 SCAR=21.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1340 | 31 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 1341 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1342 | 31 | ▶ | croak 'No database given' unless $self->{dsn}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1343 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1344 | # DBI Connect | ||||||||||||||||||||||||||||||||||||||||||||||
| 1345 | my $dbh = DBI->connect( | ||||||||||||||||||||||||||||||||||||||||||||||
| 1346 | $self->{dsn}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 1347 | $self->{user} // undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 1348 | 31 | ▶ | $self->_password, | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"user"} // undef
MC/DC0%0/1
$self->{"user"} // undef
$self->{"user"}
Truth table50%1/2
A
A= $self->{"user"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1349 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1350 | PrintError => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 1351 | RaiseError => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 1352 | AutoCommit => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 1353 | @_ | ||||||||||||||||||||||||||||||||||||||||||||||
| 1354 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 1355 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1356 | # Unable to connect to database | ||||||||||||||||||||||||||||||||||||||||||||||
| 1357 | 31 | ▶ | carp $DBI::errstr and return unless $dbh; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%1/4
$dbh || carp($DBI::errstr) && (return)
carp($DBI::errstr) and return
MC/DC0%0/2
$dbh || carp($DBI::errstr) && (return)
$dbh
carp($DBI::errstr)
Truth table33%1/3
A || B
A= $dbhB= carp($DBI::errstr)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1358 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1359 | # Store database handle | ||||||||||||||||||||||||||||||||||||||||||||||
| 1360 | 31 | $self->{dbh} = $dbh; | |||||||||||||||||||||||||||||||||||||||||||||
| 1361 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1362 | # Save process id | ||||||||||||||||||||||||||||||||||||||||||||||
| 1363 | 31 | $self->{pid} = $$; | |||||||||||||||||||||||||||||||||||||||||||||
| 1364 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1365 | # Save thread id | ||||||||||||||||||||||||||||||||||||||||||||||
| 1366 | 31 | ▶ | $self->{tid} = threads->tid if $INC{'threads.pm'}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1367 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1368 | # Emit all on_connect events | ||||||||||||||||||||||||||||||||||||||||||||||
| 1369 | 31 | foreach (values %{ $self->{on_connect} }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 1370 | 2 | $_->( $self, $dbh ); | |||||||||||||||||||||||||||||||||||||||||||||
| 1371 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1372 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1373 | # Return handle | ||||||||||||||||||||||||||||||||||||||||||||||
| 1374 | 31 | $dbh; | |||||||||||||||||||||||||||||||||||||||||||||
| 1375 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1376 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1377 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1378 | # Password closure should prevent accidentally overt passwords | ||||||||||||||||||||||||||||||||||||||||||||||
| 1379 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1380 | # Password hash | ||||||||||||||||||||||||||||||||||||||||||||||
| 1381 | 24 | my %pwd; | |||||||||||||||||||||||||||||||||||||||||||||
| 1382 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1383 | # Password method | ||||||||||||||||||||||||||||||||||||||||||||||
| 1384 | 59 | ▶ | sub _password { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_password: called CC=8 SCAR=28.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1385 | 59 | my $id = shift->{_id}; | |||||||||||||||||||||||||||||||||||||||||||||
| 1386 | 59 | my $pwd_set = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 1387 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1388 | 59 | my ($this) = caller(0); | |||||||||||||||||||||||||||||||||||||||||||||
| 1389 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1390 | # Request only allowed in this namespace | ||||||||||||||||||||||||||||||||||||||||||||||
| 1391 | 59 | ▶ | return if index(__PACKAGE__, $this) != 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1392 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1393 | # Return password | ||||||||||||||||||||||||||||||||||||||||||||||
| 1394 | 59 | ▶ | unless (defined $pwd_set) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1395 | 31 | return $pwd{$id}; | |||||||||||||||||||||||||||||||||||||||||||||
| 1396 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1397 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1398 | # Delete password | ||||||||||||||||||||||||||||||||||||||||||||||
| 1399 | 28 | ▶ | unless ($pwd_set) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1400 | 28 | delete $pwd{$id}; | |||||||||||||||||||||||||||||||||||||||||||||
| 1401 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1402 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1403 | # Set password | ||||||||||||||||||||||||||||||||||||||||||||||
| 1404 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1405 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1406 | # Password can only be set on construction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1407 | 0 | for ((caller(1))[3]) { | |||||||||||||||||||||||||||||||||||||||||||||
| 1408 | 0 | ▶ | m/::new$/o or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1409 | 0 | ▶ | index($_, __PACKAGE__) == 0 or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1410 | 0 | ▶ | !$pwd{$id} or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1411 | 0 | $pwd{$id} = $pwd_set; | |||||||||||||||||||||||||||||||||||||||||||||
| 1412 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1413 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1414 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1415 | 24 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 1416 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1417 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1418 | # Get table name | ||||||||||||||||||||||||||||||||||||||||||||||
| 1419 | 985 | ▶ | sub _table_name { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_table_name: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1420 | 985 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 1421 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1422 | # Table name | ||||||||||||||||||||||||||||||||||||||||||||||
| 1423 | 985 | my $table; | |||||||||||||||||||||||||||||||||||||||||||||
| 1424 | 985 | ▶ | unless (exists $self->{table}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1425 | 967 | ▶ | return shift(@{ $_[0] }) unless ref $_[0]->[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1426 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1427 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1428 | # Table object | ||||||||||||||||||||||||||||||||||||||||||||||
| 1429 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1430 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1431 | # Join table object not allowed | ||||||||||||||||||||||||||||||||||||||||||||||
| 1432 | 18 | ▶ | return $self->{table} unless ref $self->{table}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1433 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1434 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1435 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 1436 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1437 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1438 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1439 | # Get table object | ||||||||||||||||||||||||||||||||||||||||||||||
| 1440 | 193 | ▶ | sub _table_obj { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_table_obj: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1441 | 193 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 1442 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1443 | 193 | my $tables; | |||||||||||||||||||||||||||||||||||||||||||||
| 1444 | 193 | my ($fields, $pairs) = ([], []); | |||||||||||||||||||||||||||||||||||||||||||||
| 1445 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1446 | # Not a table object | ||||||||||||||||||||||||||||||||||||||||||||||
| 1447 | 193 | ▶ | unless (exists $self->{table}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1448 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1449 | 140 | my $table = shift( @{ shift @_ } ); | |||||||||||||||||||||||||||||||||||||||||||||
| 1450 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1451 | # Table name as a string | ||||||||||||||||||||||||||||||||||||||||||||||
| 1452 | 140 | ▶ | unless (ref $table) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1453 | 130 | $tables = [ $table ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 1454 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1455 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1456 | # Joined tables | ||||||||||||||||||||||||||||||||||||||||||||||
| 1457 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1458 | 10 | return _join_tables( $table ); | |||||||||||||||||||||||||||||||||||||||||||||
| 1459 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1460 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1461 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1462 | # A table object | ||||||||||||||||||||||||||||||||||||||||||||||
| 1463 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1464 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1465 | # joined table | ||||||||||||||||||||||||||||||||||||||||||||||
| 1466 | 53 | ▶ | if (ref $self->{table}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1467 | 8 | return @{ $self->{table} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 1468 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1469 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1470 | # Table name | ||||||||||||||||||||||||||||||||||||||||||||||
| 1471 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1472 | 45 | $tables = [ $self->{table} ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 1473 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1474 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1475 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1476 | 175 | return ($tables, $fields, $pairs); | |||||||||||||||||||||||||||||||||||||||||||||
| 1477 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1478 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1479 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1480 | # Join tables | ||||||||||||||||||||||||||||||||||||||||||||||
| 1481 | 13 | ▶ | sub _join_tables { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_join_tables: called CC=27 SCAR=33.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1482 | 13 | my @join = @{ shift @_ }; | |||||||||||||||||||||||||||||||||||||||||||||
| 1483 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1484 | 13 | my (@tables, @fields, @pairs, $treatment); | |||||||||||||||||||||||||||||||||||||||||||||
| 1485 | 13 | my %field_alias; | |||||||||||||||||||||||||||||||||||||||||||||
| 1486 | 13 | my %marker; | |||||||||||||||||||||||||||||||||||||||||||||
| 1487 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1488 | # Parse table array | ||||||||||||||||||||||||||||||||||||||||||||||
| 1489 | 13 | ▶ | while (@join) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1490 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1491 | # Table name | ||||||||||||||||||||||||||||||||||||||||||||||
| 1492 | 26 | my $table = shift @join; | |||||||||||||||||||||||||||||||||||||||||||||
| 1493 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1494 | # Check table name | ||||||||||||||||||||||||||||||||||||||||||||||
| 1495 | 26 | ▶ | my $t_alias = $2 if $table =~ s/^([^:]+?):([^:]+?)$/$1 $2/o; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1496 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1497 | # Push table | ||||||||||||||||||||||||||||||||||||||||||||||
| 1498 | 26 | push(@tables, $table); | |||||||||||||||||||||||||||||||||||||||||||||
| 1499 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1500 | # Set prefix | ||||||||||||||||||||||||||||||||||||||||||||||
| 1501 | 26 | ▶ | my $prefix = $t_alias ? $t_alias : $table; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1502 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1503 | 26 | ▶ | if (my $ref = ref $join[0]) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1504 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1505 | # Remember aliases | ||||||||||||||||||||||||||||||||||||||||||||||
| 1506 | 26 | my %alias; | |||||||||||||||||||||||||||||||||||||||||||||
| 1507 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1508 | # Field array | ||||||||||||||||||||||||||||||||||||||||||||||
| 1509 | 26 | ▶ | if ($ref eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1510 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1511 | 22 | my $field_array = shift @join; | |||||||||||||||||||||||||||||||||||||||||||||
| 1512 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1513 | 22 | my $f_prefix = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 1514 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1515 | # Has a hash next to it | ||||||||||||||||||||||||||||||||||||||||||||||
| 1516 | 22 | ▶ | if (ref $join[0] && ref $join[0] eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $join[0] and ref $join[0] eq "HASH"
MC/DC0%0/2
ref $join[0] and ref $join[0] eq "HASH"
ref $join[0]
ref $join[0] eq "HASH"
Truth table33%1/3
A and B
A= ref $join[0]B= ref $join[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1517 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1518 | # Set Prefix if given | ||||||||||||||||||||||||||||||||||||||||||||||
| 1519 | # Todo: Is this documented? | ||||||||||||||||||||||||||||||||||||||||||||||
| 1520 | 22 | ▶ | if (exists $join[0]->{-prefix}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1521 | 1 | $f_prefix = delete $join[0]->{-prefix}; | |||||||||||||||||||||||||||||||||||||||||||||
| 1522 | 1 | ▶ | $f_prefix = _clean_alias($prefix) . '_' if $f_prefix eq '*'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1523 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1524 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1525 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1526 | # Reformat field values | ||||||||||||||||||||||||||||||||||||||||||||||
| 1527 | my $reformat = [ | ||||||||||||||||||||||||||||||||||||||||||||||
| 1528 | map { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1529 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1530 | # Is a reference | ||||||||||||||||||||||||||||||||||||||||||||||
| 1531 | 22 | ▶ | unless (ref $_) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1532 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1533 | # Set alias semi explicitely | ||||||||||||||||||||||||||||||||||||||||||||||
| 1534 | 33 | ▶ | if (index($_, ':') == -1) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1535 | 24 | $_ .= ':~' . $f_prefix . _clean_alias($_); | |||||||||||||||||||||||||||||||||||||||||||||
| 1536 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1537 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1538 | # Field is not a function | ||||||||||||||||||||||||||||||||||||||||||||||
| 1539 | 33 | ▶ | if (index($_, '(') == -1) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1540 | 32 | ▶ | $_ = "$prefix.$_" if index($_, '.') == -1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1541 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1542 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1543 | # Field is a function | ||||||||||||||||||||||||||||||||||||||||||||||
| 1544 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1545 | 1 | s/((?:\(|$FIELD_OP_REGEX)\s*)($KEY_REGEX_NOPREF) | |||||||||||||||||||||||||||||||||||||||||||||
| 1546 | (\s*(?:$FIELD_OP_REGEX|\)))/$1$prefix\.$2$3/ogx; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1547 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1548 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1549 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1550 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1551 | 33 | $_; | |||||||||||||||||||||||||||||||||||||||||||||
| 1552 | } @$field_array | ||||||||||||||||||||||||||||||||||||||||||||||
| 1553 | ]; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1554 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1555 | # Automatically prepend table and, if not given, alias | ||||||||||||||||||||||||||||||||||||||||||||||
| 1556 | 22 | (my $fields, $treatment, my $alias) = _fields($t_alias, $reformat); | |||||||||||||||||||||||||||||||||||||||||||||
| 1557 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1558 | # Set alias for markers | ||||||||||||||||||||||||||||||||||||||||||||||
| 1559 | # $alias{$_} = 1 foreach keys %$alias; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1560 | 22 | ▶ | while (my ($key, $val) = each %$alias) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1561 | 33 | $field_alias{$key} = $alias{$key} = $val ; | |||||||||||||||||||||||||||||||||||||||||||||
| 1562 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1563 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1564 | # TODO: only use alias if necessary, as they can't be used in WHERE! | ||||||||||||||||||||||||||||||||||||||||||||||
| 1565 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1566 | 22 | ▶ | push(@fields, $fields) if $fields; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1567 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1568 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1569 | # Add prepended * | ||||||||||||||||||||||||||||||||||||||||||||||
| 1570 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1571 | 4 | push(@fields, "$prefix.*"); | |||||||||||||||||||||||||||||||||||||||||||||
| 1572 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1573 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1574 | # Marker hash reference | ||||||||||||||||||||||||||||||||||||||||||||||
| 1575 | 26 | ▶ | if (ref $join[0] && ref $join[0] eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $join[0] and ref $join[0] eq "HASH"
MC/DC0%0/2
ref $join[0] and ref $join[0] eq "HASH"
ref $join[0]
ref $join[0] eq "HASH"
Truth table33%1/3
A and B
A= ref $join[0]B= ref $join[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1576 | 26 | my $hash = shift @join; | |||||||||||||||||||||||||||||||||||||||||||||
| 1577 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1578 | # Add database fields to marker hash | ||||||||||||||||||||||||||||||||||||||||||||||
| 1579 | 26 | ▶ | while (my ($key, $value) = each %$hash) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1580 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1581 | # TODO: Does this work? | ||||||||||||||||||||||||||||||||||||||||||||||
| 1582 | 26 | ▶ | unless ($alias{$key}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1583 | 26 | ▶ | $key = "$prefix.$key" if $key =~ $KEY_REGEX_NOPREF; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1584 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1585 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1586 | 0 | $key = $alias{$key}; | |||||||||||||||||||||||||||||||||||||||||||||
| 1587 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1588 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1589 | # Prefix, if not an explicite alias | ||||||||||||||||||||||||||||||||||||||||||||||
| 1590 | 26 | ▶ | foreach (ref $value ? @$value : $value) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1591 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1592 | 28 | ▶ | my $array = ($marker{$_} //= []); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$marker{$_} //= []
MC/DC100%1/1
$marker{$_} //= []
$marker{$_}
Truth table100%2/2
A
A= $marker{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1593 | 28 | push(@$array, $key); | |||||||||||||||||||||||||||||||||||||||||||||
| 1594 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1595 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1596 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1597 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1598 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1599 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1600 | # Create condition pairs based on markers | ||||||||||||||||||||||||||||||||||||||||||||||
| 1601 | 13 | my ($ind, $fields); | |||||||||||||||||||||||||||||||||||||||||||||
| 1602 | 13 | ▶ | while (($ind, $fields) = each %marker) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1603 | 14 | my $field = shift(@$fields); | |||||||||||||||||||||||||||||||||||||||||||||
| 1604 | 14 | foreach (@$fields) { | |||||||||||||||||||||||||||||||||||||||||||||
| 1605 | 14 | ▶ | push( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1606 | @pairs, | ||||||||||||||||||||||||||||||||||||||||||||||
| 1607 | "$field " . ($ind < 0 ? '!' : '') . "= $_" | ||||||||||||||||||||||||||||||||||||||||||||||
| 1608 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 1609 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1610 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1611 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1612 | # Return join initialised values | ||||||||||||||||||||||||||||||||||||||||||||||
| 1613 | 13 | return (\@tables, \@fields, \@pairs, $treatment, \%field_alias); | |||||||||||||||||||||||||||||||||||||||||||||
| 1614 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1615 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1616 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1617 | # Get pairs and values | ||||||||||||||||||||||||||||||||||||||||||||||
| 1618 | 212 | ▶ | sub _get_pairs { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_pairs: called CC=57 SCAR=44.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1619 | 212 | my (@pairs, @values, %prep); | |||||||||||||||||||||||||||||||||||||||||||||
| 1620 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1621 | # Get alias for fields | ||||||||||||||||||||||||||||||||||||||||||||||
| 1622 | 212 | ▶ | my $alias = @_ == 2 ? pop @_ : {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1623 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1624 | 212 | ▶ | while (my ($key, $value) = each %{ $_[0] }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1625 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1626 | # Not a valid key | ||||||||||||||||||||||||||||||||||||||||||||||
| 1627 | 333 | ▶ | unless ($key =~ m/^-?$KEY_REGEX$/o) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1628 | 0 | ▶ | carp "$key is not a valid Oro key" and next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1629 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1630 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1631 | # Normal key | ||||||||||||||||||||||||||||||||||||||||||||||
| 1632 | 333 | ▶ | if (substr($key, 0, 1) ne '-') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1633 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1634 | # Get alias | ||||||||||||||||||||||||||||||||||||||||||||||
| 1635 | 205 | ▶ | $key = exists $alias->{$key} ? $alias->{$key} : (index($key, '.') >= 0 ? $key : '`' . $key . '`'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1636 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1637 | # Equality | ||||||||||||||||||||||||||||||||||||||||||||||
| 1638 | 205 | ▶ | unless (ref $value) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1639 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1640 | # NULL value | ||||||||||||||||||||||||||||||||||||||||||||||
| 1641 | 103 | ▶ | unless (defined $value) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1642 | 3 | push(@pairs, "$key IS NULL"); | |||||||||||||||||||||||||||||||||||||||||||||
| 1643 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1644 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1645 | # Simple value | ||||||||||||||||||||||||||||||||||||||||||||||
| 1646 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1647 | 100 | push(@pairs, "$key = ?"), | |||||||||||||||||||||||||||||||||||||||||||||
| 1648 | push(@values, $value); | ||||||||||||||||||||||||||||||||||||||||||||||
| 1649 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1650 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1651 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1652 | # Element of or SQL | ||||||||||||||||||||||||||||||||||||||||||||||
| 1653 | elsif (ref $value eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1654 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1655 | # Escaped SQL | ||||||||||||||||||||||||||||||||||||||||||||||
| 1656 | 7 | ▶ | if (ref $value->[0] && ref $value->[0] eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $value->[0] and ref $value->[0] eq "SCALAR"
MC/DC50%1/2
ref $value->[0] and ref $value->[0] eq "SCALAR"
ref $value->[0]
ref $value->[0] eq "SCALAR"
Truth table66%2/3
A and B
A= ref $value->[0]B= ref $value->[0] eq "SCALAR"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1657 | 1 | push(@pairs, "$key = (" . ${$value->[0]} . ')'), | |||||||||||||||||||||||||||||||||||||||||||||
| 1658 | 1 | push(@values, map { _stringify($_) } @{$value}[ 1 .. $#$value ]); | |||||||||||||||||||||||||||||||||||||||||||||
| 1659 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 1660 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1661 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1662 | # Undefined values in the array are not specified | ||||||||||||||||||||||||||||||||||||||||||||||
| 1663 | # as ' IN (NULL, ...)' does not work | ||||||||||||||||||||||||||||||||||||||||||||||
| 1664 | push (@pairs, "$key IN (" . _q($value) . ')' ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 1665 | 6 | push(@values, map { _stringify($_) } @$value); | |||||||||||||||||||||||||||||||||||||||||||||
| 1666 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1667 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1668 | # Operators | ||||||||||||||||||||||||||||||||||||||||||||||
| 1669 | elsif (ref $value eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1670 | 91 | ▶ | while (my ($op, $val) = each %$value) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1671 | 93 | ▶ | if ($op =~ $OP_REGEX) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1672 | 93 | for ($op) { | |||||||||||||||||||||||||||||||||||||||||||||
| 1673 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1674 | # Uppercase | ||||||||||||||||||||||||||||||||||||||||||||||
| 1675 | 93 | $_ = uc; | |||||||||||||||||||||||||||||||||||||||||||||
| 1676 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1677 | # Translate negation | ||||||||||||||||||||||||||||||||||||||||||||||
| 1678 | 93 | s{^(?:NOT_|!(?=[MLGRB]))}{NOT }; | |||||||||||||||||||||||||||||||||||||||||||||
| 1679 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1680 | # Translate literal compare operators | ||||||||||||||||||||||||||||||||||||||||||||||
| 1681 | 93 | ▶ | tr/GLENTQ/><=!/d if $_ =~ m/^(?:[GL][TE]|NE|EQ)$/o; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1682 | 93 | s/==/=/o; | |||||||||||||||||||||||||||||||||||||||||||||
| 1683 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1684 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1685 | # Array operators | ||||||||||||||||||||||||||||||||||||||||||||||
| 1686 | 93 | ▶ | if (ref $val && ref $val eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $val and ref $val eq "ARRAY"
MC/DC50%1/2
ref $val and ref $val eq "ARRAY"
ref $val
ref $val eq "ARRAY"
Truth table66%2/3
A and B
A= ref $valB= ref $val eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1687 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1688 | # Between operator | ||||||||||||||||||||||||||||||||||||||||||||||
| 1689 | 3 | ▶ | if (index($op, 'BETWEEN') >= 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1690 | push(@pairs, "$key $op ? AND ?"), | ||||||||||||||||||||||||||||||||||||||||||||||
| 1691 | 2 | push(@values, map { _stringify($_) } @{$val}[0, 1]); | |||||||||||||||||||||||||||||||||||||||||||||
| 1692 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1693 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1694 | # Not element of | ||||||||||||||||||||||||||||||||||||||||||||||
| 1695 | elsif ($op =~ /^NOT( IN)?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1696 | # Undefined values in the array are not specified | ||||||||||||||||||||||||||||||||||||||||||||||
| 1697 | # as ' NOT IN (NULL, ...)' does not work | ||||||||||||||||||||||||||||||||||||||||||||||
| 1698 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1699 | push(@pairs, "$key NOT IN (" . _q($val) . ')' ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 1700 | 1 | push(@values, map { _stringify($_) } @$val); | |||||||||||||||||||||||||||||||||||||||||||||
| 1701 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1702 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1703 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1704 | # Simple operator | ||||||||||||||||||||||||||||||||||||||||||||||
| 1705 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1706 | 90 | my $p = "$key $op "; | |||||||||||||||||||||||||||||||||||||||||||||
| 1707 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1708 | # Value is an object | ||||||||||||||||||||||||||||||||||||||||||||||
| 1709 | 90 | ▶ | if (blessed $val) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1710 | 0 | ▶ | $val = _stringify($val) or | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
($val = _stringify($val)) || carp("Unknown Oro value $key $op $val") && (next)
carp("Unknown Oro value $key $op $val") and next
MC/DC0%0/2
($val = _stringify($val)) || carp("Unknown Oro value $key $op $val") && (next)
($val = _stringify($val))
carp("Unknown Oro value $key $op $val")
Truth table0%0/3
A || B
A= ($val = _stringify($val))B= carp("Unknown Oro value $key $op $val")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1711 | carp "Unknown Oro value $key $op $val" and next; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1712 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1713 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1714 | # Defined value | ||||||||||||||||||||||||||||||||||||||||||||||
| 1715 | 90 | ▶ | if (defined $val) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1716 | 82 | $p .= '?'; | |||||||||||||||||||||||||||||||||||||||||||||
| 1717 | 82 | push(@values, $val); | |||||||||||||||||||||||||||||||||||||||||||||
| 1718 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1719 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1720 | # Null value | ||||||||||||||||||||||||||||||||||||||||||||||
| 1721 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1722 | 8 | $p .= 'NULL'; | |||||||||||||||||||||||||||||||||||||||||||||
| 1723 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1724 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1725 | # Add LIKE escape sequence | ||||||||||||||||||||||||||||||||||||||||||||||
| 1726 | 90 | ▶ | if ($op eq 'LIKE') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1727 | 34 | $p .= q! ESCAPE '\'!; | |||||||||||||||||||||||||||||||||||||||||||||
| 1728 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1729 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1730 | 90 | push(@pairs, $p); | |||||||||||||||||||||||||||||||||||||||||||||
| 1731 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1732 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1733 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1734 | # Unknown operator | ||||||||||||||||||||||||||||||||||||||||||||||
| 1735 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1736 | 0 | ▶ | $val //= '?'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$val //= "?"
MC/DC0%0/1
$val //= "?"
$val
Truth table0%0/2
A
A= $val
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1737 | 0 | ▶ | carp "Unknown Oro operator $key $op $val" and next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1738 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1739 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1740 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1741 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1742 | # Escaped SQL | ||||||||||||||||||||||||||||||||||||||||||||||
| 1743 | elsif (ref $value eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1744 | 1 | push(@pairs, "$key = ($$value)"), | |||||||||||||||||||||||||||||||||||||||||||||
| 1745 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1746 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1747 | # Stringifiable object | ||||||||||||||||||||||||||||||||||||||||||||||
| 1748 | elsif ($value = _stringify($value)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1749 | # Simple object | ||||||||||||||||||||||||||||||||||||||||||||||
| 1750 | 3 | push(@pairs, "$key = ?"), | |||||||||||||||||||||||||||||||||||||||||||||
| 1751 | push(@values, $value); | ||||||||||||||||||||||||||||||||||||||||||||||
| 1752 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1753 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1754 | # Unknown pair | ||||||||||||||||||||||||||||||||||||||||||||||
| 1755 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1756 | 0 | ▶ | carp "Unknown Oro pair $key, " . ($value ? $value : '[undef]' ) and next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1757 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1758 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1759 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1760 | # Restriction of the result set | ||||||||||||||||||||||||||||||||||||||||||||||
| 1761 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1762 | 128 | $key = lc $key; | |||||||||||||||||||||||||||||||||||||||||||||
| 1763 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1764 | # No value existing | ||||||||||||||||||||||||||||||||||||||||||||||
| 1765 | 128 | ▶ | next unless defined $value; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1766 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1767 | # Limit and Offset restriction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1768 | 128 | ▶ | if ($key =~ m/^-(?:limit|offset|distinct)$/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1769 | 73 | ▶ | $prep{substr($key, 1)} = $value if $value =~ m/^\d+$/o; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1770 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1771 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1772 | # Order restriction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1773 | elsif ($key =~ s/^-(order|group)(?:[-_]by)?$/$1/) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1774 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1775 | # Already array and group | ||||||||||||||||||||||||||||||||||||||||||||||
| 1776 | 38 | ▶ | if ($key eq 'group' && ref $value) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key eq "group" and ref $value
MC/DC100%2/2
$key eq "group" and ref $value
$key eq "group"
ref $value
Truth table100%3/3
A and B
A= $key eq "group"B= ref $value
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1777 | 2 | ▶ | if (ref $value->[-1] && ref $value->[-1] eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $value->[-1] and ref $value->[-1] eq "HASH"
MC/DC50%1/2
ref $value->[-1] and ref $value->[-1] eq "HASH"
ref $value->[-1]
ref $value->[-1] eq "HASH"
Truth table66%2/3
A and B
A= ref $value->[-1]B= ref $value->[-1] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1778 | 1 | $prep{having} = pop @$value; | |||||||||||||||||||||||||||||||||||||||||||||
| 1779 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1780 | 1 | ▶ | unless (@$value) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1781 | 0 | ▶ | carp '"Having" without "Group" is not allowed' and next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1782 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1783 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1784 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1785 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1786 | 38 | my @field_array; | |||||||||||||||||||||||||||||||||||||||||||||
| 1787 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1788 | # Check group values | ||||||||||||||||||||||||||||||||||||||||||||||
| 1789 | 38 | ▶ | foreach (ref $value ? @$value : $value) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1790 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1791 | # Valid order/group_by value | ||||||||||||||||||||||||||||||||||||||||||||||
| 1792 | 39 | ▶ | if ($_ =~ $VALID_GROUPORDER_REGEX) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1793 | 38 | s/^([\-\+])//o; | |||||||||||||||||||||||||||||||||||||||||||||
| 1794 | 38 | ▶ | push(@field_array, $1 && $1 eq '-' ? "$_ DESC" : $_ ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$1 and $1 eq "-"
MC/DC50%1/2
$1 and $1 eq "-"
$1
$1 eq "-"
Truth table66%2/3
A and B
A= $1B= $1 eq "-"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1795 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1796 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1797 | # Invalid order/group_by value | ||||||||||||||||||||||||||||||||||||||||||||||
| 1798 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1799 | 1 | carp "$_ is not a valid Oro $key restriction"; | |||||||||||||||||||||||||||||||||||||||||||||
| 1800 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1801 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1802 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1803 | 38 | ▶ | $prep{$key} = join(', ', @field_array) if scalar @field_array; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1804 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1805 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1806 | # And or or | ||||||||||||||||||||||||||||||||||||||||||||||
| 1807 | elsif ($key =~ m/^-(or|and)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1808 | 4 | my $op = uc $1; | |||||||||||||||||||||||||||||||||||||||||||||
| 1809 | 4 | my @array = @$value; | |||||||||||||||||||||||||||||||||||||||||||||
| 1810 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1811 | 4 | my (@or_pairs, @or_values); | |||||||||||||||||||||||||||||||||||||||||||||
| 1812 | 4 | ▶ | while (@array) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1813 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1814 | # Not a hash | ||||||||||||||||||||||||||||||||||||||||||||||
| 1815 | 8 | ▶ | if (!ref $array[0]) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1816 | 4 | unshift(@array, { | |||||||||||||||||||||||||||||||||||||||||||||
| 1817 | shift @array => shift @array | ||||||||||||||||||||||||||||||||||||||||||||||
| 1818 | }); | ||||||||||||||||||||||||||||||||||||||||||||||
| 1819 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1820 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1821 | # Ignore prep | ||||||||||||||||||||||||||||||||||||||||||||||
| 1822 | 8 | my ($or_pairs, $or_values) = _get_pairs(shift(@array), $alias); | |||||||||||||||||||||||||||||||||||||||||||||
| 1823 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1824 | # Push values | ||||||||||||||||||||||||||||||||||||||||||||||
| 1825 | 8 | push(@values, @$or_values); | |||||||||||||||||||||||||||||||||||||||||||||
| 1826 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1827 | # Push local pairs | ||||||||||||||||||||||||||||||||||||||||||||||
| 1828 | 8 | ▶ | if (@$or_pairs > 1) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1829 | 2 | push(@or_pairs, '(' . join (' AND ', @$or_pairs) . ')'); | |||||||||||||||||||||||||||||||||||||||||||||
| 1830 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1831 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1832 | # Push single local pair | ||||||||||||||||||||||||||||||||||||||||||||||
| 1833 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1834 | 6 | push(@or_pairs, $or_pairs->[0]); | |||||||||||||||||||||||||||||||||||||||||||||
| 1835 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1836 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1837 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1838 | # Join with chosen operator | ||||||||||||||||||||||||||||||||||||||||||||||
| 1839 | 4 | push(@pairs, '(' . join(" $op ", @or_pairs) . ')'); | |||||||||||||||||||||||||||||||||||||||||||||
| 1840 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1841 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1842 | # Cache | ||||||||||||||||||||||||||||||||||||||||||||||
| 1843 | elsif ($key eq '-cache') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1844 | 13 | my $chi = $value->{chi}; | |||||||||||||||||||||||||||||||||||||||||||||
| 1845 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1846 | # Check chi existence | ||||||||||||||||||||||||||||||||||||||||||||||
| 1847 | 13 | ▶ | if ($chi) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1848 | 13 | ▶ | $prep{cache} = [ $chi, $value->{key} // '', $value ]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$value->{"key"} // ""
MC/DC0%0/1
$value->{"key"} // ""
$value->{"key"}
Truth table50%1/2
A
A= $value->{"key"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1849 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1850 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1851 | # No chi given | ||||||||||||||||||||||||||||||||||||||||||||||
| 1852 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1853 | 0 | carp 'No CHI driver given for cache'; | |||||||||||||||||||||||||||||||||||||||||||||
| 1854 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1855 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1856 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1857 | 0 | carp "$key is an unknown restriction"; | |||||||||||||||||||||||||||||||||||||||||||||
| 1858 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1859 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1860 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1861 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1862 | 212 | ▶ | return (\@pairs, \@values, (keys %prep ? \%prep : undef)); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1863 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1864 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1865 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1866 | # Get fields | ||||||||||||||||||||||||||||||||||||||||||||||
| 1867 | 60 | ▶ | sub _fields { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fields: called CC=11 SCAR=24.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1868 | 60 | my $table = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 1869 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1870 | 60 | my (%treatment, %alias, @fields); | |||||||||||||||||||||||||||||||||||||||||||||
| 1871 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1872 | 60 | foreach ( @{$_[0]} ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 1873 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1874 | # Ordinary String | ||||||||||||||||||||||||||||||||||||||||||||||
| 1875 | 84 | ▶ | unless (ref $_) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1876 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1877 | # Valid field | ||||||||||||||||||||||||||||||||||||||||||||||
| 1878 | 72 | ▶ | if ($_ =~ $VALID_FIELD_REGEX) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1879 | 71 | push(@fields, $_); | |||||||||||||||||||||||||||||||||||||||||||||
| 1880 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1881 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1882 | # Invalid field | ||||||||||||||||||||||||||||||||||||||||||||||
| 1883 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1884 | 1 | carp "$_ is not a valid Oro field value" | |||||||||||||||||||||||||||||||||||||||||||||
| 1885 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1886 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1887 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1888 | # Treatment | ||||||||||||||||||||||||||||||||||||||||||||||
| 1889 | 0 | elsif (ref $_ eq 'ARRAY') { | |||||||||||||||||||||||||||||||||||||||||||||
| 1890 | 12 | my ($sub, $alias) = @$_; | |||||||||||||||||||||||||||||||||||||||||||||
| 1891 | 12 | my ($sql, $inner_sub) = $sub->($table); | |||||||||||||||||||||||||||||||||||||||||||||
| 1892 | 12 | ▶ | ($sql, $inner_sub, my @param) = $sql->($table) if ref $sql; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1893 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1894 | 12 | ▶ | $treatment{ $alias } = [$inner_sub, @param ] if $inner_sub; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1895 | 12 | push(@fields, "$sql:$alias"); | |||||||||||||||||||||||||||||||||||||||||||||
| 1896 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1897 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1898 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1899 | 60 | my $fields = join(', ', @fields); | |||||||||||||||||||||||||||||||||||||||||||||
| 1900 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1901 | # Return if no alias fields exist | ||||||||||||||||||||||||||||||||||||||||||||||
| 1902 | 60 | ▶ | return $fields unless $fields =~ m/[\.:=]/o; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1903 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1904 | # Join with alias fields | ||||||||||||||||||||||||||||||||||||||||||||||
| 1905 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||
| 1906 | join( | ||||||||||||||||||||||||||||||||||||||||||||||
| 1907 | ', ', | ||||||||||||||||||||||||||||||||||||||||||||||
| 1908 | map { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1909 | # Explicite field alias | ||||||||||||||||||||||||||||||||||||||||||||||
| 1910 | 41 | ▶ | if ($_ =~ $FIELD_REST_RE) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1911 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1912 | # ~ indicates rather not explicite alias | ||||||||||||||||||||||||||||||||||||||||||||||
| 1913 | # Will only be set in case of agregate functions | ||||||||||||||||||||||||||||||||||||||||||||||
| 1914 | # TODO: if ($2 eq ':' && index($1,'(') >= 0); | ||||||||||||||||||||||||||||||||||||||||||||||
| 1915 | 53 | $alias{$3} = $1; | |||||||||||||||||||||||||||||||||||||||||||||
| 1916 | 53 | qq{$1 AS `$3`}; | |||||||||||||||||||||||||||||||||||||||||||||
| 1917 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1918 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1919 | # Implicite field alias | ||||||||||||||||||||||||||||||||||||||||||||||
| 1920 | elsif (m/^(?:.+?)\.(?:[^\.]+?)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1921 | 0 | my $cl = _clean_alias $_; | |||||||||||||||||||||||||||||||||||||||||||||
| 1922 | 0 | $alias{$cl} = qq{$_ AS `$cl`}; | |||||||||||||||||||||||||||||||||||||||||||||
| 1923 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1924 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1925 | # Field value | ||||||||||||||||||||||||||||||||||||||||||||||
| 1926 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 1927 | 8 | ▶ | $_ | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1928 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1929 | } @fields | ||||||||||||||||||||||||||||||||||||||||||||||
| 1930 | ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 1931 | (%treatment ? \%treatment : undef), | ||||||||||||||||||||||||||||||||||||||||||||||
| 1932 | \%alias | ||||||||||||||||||||||||||||||||||||||||||||||
| 1933 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 1934 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1935 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1936 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1937 | # Restrictions | ||||||||||||||||||||||||||||||||||||||||||||||
| 1938 | 75 | ▶ | sub _restrictions { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_restrictions: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1939 | 75 | my ($prep, $values) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 1940 | 75 | my $sql = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 1941 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1942 | # Group restriction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1943 | 75 | ▶ | if ($prep->{group}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1944 | 3 | $sql .= ' GROUP BY ' . delete $prep->{group}; | |||||||||||||||||||||||||||||||||||||||||||||
| 1945 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1946 | # Having restriction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1947 | 3 | ▶ | if ($prep->{having}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1948 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1949 | # Get conditions | ||||||||||||||||||||||||||||||||||||||||||||||
| 1950 | my ($cond_pairs, $cond_values) = _get_pairs( | ||||||||||||||||||||||||||||||||||||||||||||||
| 1951 | delete $prep->{having} | ||||||||||||||||||||||||||||||||||||||||||||||
| 1952 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 1953 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1954 | # Conditions given | ||||||||||||||||||||||||||||||||||||||||||||||
| 1955 | 1 | ▶ | if (@$cond_pairs) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1956 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1957 | # Append having condition | ||||||||||||||||||||||||||||||||||||||||||||||
| 1958 | 1 | $sql .= ' HAVING ' . join(' AND ', @$cond_pairs); | |||||||||||||||||||||||||||||||||||||||||||||
| 1959 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1960 | # Append values | ||||||||||||||||||||||||||||||||||||||||||||||
| 1961 | 1 | push(@$values, @$cond_values); | |||||||||||||||||||||||||||||||||||||||||||||
| 1962 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1963 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1964 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1965 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1966 | # Order restriction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1967 | 75 | ▶ | if (exists $prep->{order}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1968 | 34 | $sql .= ' ORDER BY ' . delete $prep->{order}; | |||||||||||||||||||||||||||||||||||||||||||||
| 1969 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1970 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1971 | # Limit restriction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1972 | 75 | ▶ | if ($prep->{limit}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1973 | 50 | $sql .= ' LIMIT ?'; | |||||||||||||||||||||||||||||||||||||||||||||
| 1974 | 50 | push(@$values, delete $prep->{limit}); | |||||||||||||||||||||||||||||||||||||||||||||
| 1975 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1976 | # Offset restriction | ||||||||||||||||||||||||||||||||||||||||||||||
| 1977 | 50 | ▶ | if (defined $prep->{offset}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1978 | 21 | $sql .= ' OFFSET ?'; | |||||||||||||||||||||||||||||||||||||||||||||
| 1979 | 21 | push(@$values, delete $prep->{offset}); | |||||||||||||||||||||||||||||||||||||||||||||
| 1980 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1981 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1982 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1983 | 75 | $sql; | |||||||||||||||||||||||||||||||||||||||||||||
| 1984 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1985 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1986 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1987 | # Check for stringification of blessed values | ||||||||||||||||||||||||||||||||||||||||||||||
| 1988 | 23 | ▶ | sub _stringify { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_stringify: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1989 | 23 | ▶ | my $ref = blessed $_[0] or return $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1990 | 3 | ▶ | if (index(($_ = "$_[0]"), $ref) != 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1991 | 3 | return $_; | |||||||||||||||||||||||||||||||||||||||||||||
| 1992 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 1993 | 0 | undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 1994 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 1995 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1996 | |||||||||||||||||||||||||||||||||||||||||||||||
| 1997 | # Clean alias string | ||||||||||||||||||||||||||||||||||||||||||||||
| 1998 | 25 | ▶ | sub _clean_alias { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_clean_alias: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 1999 | 25 | for (my $x = shift) { | |||||||||||||||||||||||||||||||||||||||||||||
| 2000 | 25 | tr/ ()[]"$@#./_/s; | |||||||||||||||||||||||||||||||||||||||||||||
| 2001 | 25 | s/[_\s]+$//; | |||||||||||||||||||||||||||||||||||||||||||||
| 2002 | 25 | return $x; | |||||||||||||||||||||||||||||||||||||||||||||
| 2003 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2004 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2005 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2006 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2007 | # Check list param | ||||||||||||||||||||||||||||||||||||||||||||||
| 2008 | 142 | ▶ | sub _check_param { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_param: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2009 | 142 | ▶ | if ($_[0]->{$_[1]} && !ref $_[0]->{ $_[1] }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[0]{$_[1]} and not ref $_[0]{$_[1]}
MC/DC50%1/2
$_[0]{$_[1]} and not ref $_[0]{$_[1]}
$_[0]{$_[1]}
not ref $_[0]{$_[1]}
Truth table66%2/3
A and not(B)
A= $_[0]{$_[1]}B= not ref $_[0]{$_[1]}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2010 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2011 | # Check for numerical value | ||||||||||||||||||||||||||||||||||||||||||||||
| 2012 | 98 | ▶ | if ($_[2]) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2013 | 19 | ▶ | return $_[0]->{$_[1]} =~ $NUM_RE ? $1 : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2014 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2015 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2016 | # Return value | ||||||||||||||||||||||||||||||||||||||||||||||
| 2017 | 79 | return $_[0]->{$_[1]}; | |||||||||||||||||||||||||||||||||||||||||||||
| 2018 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2019 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2020 | # Fail | ||||||||||||||||||||||||||||||||||||||||||||||
| 2021 | 44 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 2022 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2023 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2024 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2025 | # Questionmark string | ||||||||||||||||||||||||||||||||||||||||||||||
| 2026 | 945 | ▶ | sub _q { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_q: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2027 | 945 | my ($s, $i, $r); | |||||||||||||||||||||||||||||||||||||||||||||
| 2028 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2029 | # Loop over all values | ||||||||||||||||||||||||||||||||||||||||||||||
| 2030 | 945 | ▶ | for ($i = 0; $i < scalar(@{$_[0]});) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2031 | 1455 | $r = $_[0]->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||
| 2032 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2033 | # Append key | ||||||||||||||||||||||||||||||||||||||||||||||
| 2034 | 1455 | ▶ | unless (ref $r) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2035 | 1450 | $s .= '?,'; | |||||||||||||||||||||||||||||||||||||||||||||
| 2036 | 1450 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||
| 2037 | 1450 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 2038 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2039 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2040 | # Scalar for direct SQL input | ||||||||||||||||||||||||||||||||||||||||||||||
| 2041 | 5 | ▶ | if (ref $r eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2042 | 1 | $s .= "($$r),"; | |||||||||||||||||||||||||||||||||||||||||||||
| 2043 | 1 | splice(@{$_[0]}, $i, 1, ()); | |||||||||||||||||||||||||||||||||||||||||||||
| 2044 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 2045 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2046 | # Array for direct SQL input | ||||||||||||||||||||||||||||||||||||||||||||||
| 2047 | elsif (ref $r eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||
| 2048 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2049 | # Check for scalar reference | ||||||||||||||||||||||||||||||||||||||||||||||
| 2050 | 1 | ▶ | unless (ref $r->[0]) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2051 | 0 | carp 'First element of array insertion needs to be a scalar reference'; | |||||||||||||||||||||||||||||||||||||||||||||
| 2052 | 0 | splice(@{$_[0]}, $i++, 1); | |||||||||||||||||||||||||||||||||||||||||||||
| 2053 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 2054 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2055 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2056 | # Embed SQL statement directly | ||||||||||||||||||||||||||||||||||||||||||||||
| 2057 | 1 | $s .= '(' . ${ shift @$r } . '),'; | |||||||||||||||||||||||||||||||||||||||||||||
| 2058 | 1 | splice(@{$_[0]}, $i++, scalar @$r, @$r); | |||||||||||||||||||||||||||||||||||||||||||||
| 2059 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 2060 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2061 | # Stringifyable objects | ||||||||||||||||||||||||||||||||||||||||||||||
| 2062 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 2063 | 3 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||
| 2064 | 3 | ▶ | $s .= '?,' and next; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2065 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2066 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2067 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2068 | # Delete final ',' | ||||||||||||||||||||||||||||||||||||||||||||||
| 2069 | 945 | chop $s; | |||||||||||||||||||||||||||||||||||||||||||||
| 2070 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2071 | # Return value | ||||||||||||||||||||||||||||||||||||||||||||||
| 2072 | 945 | $s; | |||||||||||||||||||||||||||||||||||||||||||||
| 2073 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2074 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2075 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2076 | # Trim last_sql message for reporting | ||||||||||||||||||||||||||||||||||||||||||||||
| 2077 | 15 | ▶ | sub _trim_last_sql { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_trim_last_sql: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2078 | 15 | my $last_sql = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 2079 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2080 | 15 | ▶ | return $last_sql if length($last_sql) <= 500; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2081 | 0 | return substr($last_sql, 0, 497) . ' ...'; | |||||||||||||||||||||||||||||||||||||||||||||
| 2082 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2083 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2084 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2085 | # Empty code ref | ||||||||||||||||||||||||||||||||||||||||||||||
| 2086 | 0 | ▶ | sub _no_warn {}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_no_warn: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 2087 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2088 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2089 | 24 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 2090 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2091 | |||||||||||||||||||||||||||||||||||||||||||||||
| 2092 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||