blib/lib/SQL/QueryBuilder/OO.pm

filter: Statementstmt 31.8% 277 / 870 Branchbran 0.0% 0 / 256 Conditioncond 0.0% 0 / 103 MC/DCmcdc 0.0% 0 / 61 Subroutinesub 43.7% 91 / 208 Podpod n/a totaltotal 24.5% 368 / 1498 CC 177 SCAR 33.1
CC
177
Cov
23%
_convertArgument
156.0
translateQueryFields
156.0
_populateParameters
110.0
CRAP 27.4
↑ Summary
1 package SQL::QueryBuilder::OO;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 $VERSION = '0.2.6';
9 ##------------------------------------------------------------------------------
10 package sqlQuery;
11  
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use overload '""' => '_getInterpolatedQuery';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Data::Dumper; # vital
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Carp qw(croak cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 $sqlQuery::DBI = undef;
22 1 %sqlQuery::PARAMS = ();
23 1 $sqlQuery::PARAMETER_PLACEHOLDER = '?';
24  
25 sub setup
26 0 ▶ {
Subroutine setup: not called CC=3 SCAR=24.8
27 0 my %params = validate @_, {
28 -dbh => {isa => 'DBI::db', default => undef},
29 -connect => {type => CODEREF, default => undef}
30 };
31  
32 0 ▶ if (defined $params{'-dbh'} && defined $params{'-connect'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"-dbh"} and defined $params{"-connect"}) 0 0 0
Condition0%0/3
defined $params{"-dbh"} and defined $params{"-connect"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params{"-dbh"} and defined $params{"-connect"}
defined $params{"-dbh"} defined $params{"-connect"}
Truth table0%0/3
A and B
A= defined $params{"-dbh"}B= defined $params{"-connect"}
ABresult
0X0
100
111
33 {
34 0 croak('Make up your mind: either use "-dbh" to pass a handle or "-connect" for ad-hoc connecting');
35 }
36  
37 0 %sqlQuery::PARAMS = %params;
38  
39 0 return 1
40 }
41  
42 sub dbh
43 0 ▶ {
Subroutine dbh: not called CC=5 SCAR=34.0
44 0 ▶ unless (defined $sqlQuery::DBI) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sqlQuery::DBI) 0 0 0
45 0 ▶ if (defined $sqlQuery::PARAMS{'-dbh'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $sqlQuery::PARAMS{'-dbh'}) { } 0 0 0
elsif (defined $sqlQuery::PARAMS{'-connect'}) { } 0 0 0
46 0 $sqlQuery::DBI = $sqlQuery::PARAMS{'-dbh'};
47 } elsif (defined $sqlQuery::PARAMS{'-connect'}) {
48 0 $sqlQuery::DBI = eval {$sqlQuery::PARAMS{'-connect'}->()};
49 0 ▶ croak 'Setup failed; ad-hoc connector died: '.$@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
50 } else {
51 0 croak 'sqlQuery is not setup, yet.';
52 }
53 }
54  
55 0 return $sqlQuery::DBI
56 }
57  
58 sub q
59 0 ▶ {
Subroutine q: not called CC=1 SCAR=6.9
60 0 local $Carp::CarpLevel = $Carp::CarpLevel + 2; ## no critic
61 0 return __PACKAGE__->new(@_);
62 }
63  
64 sub exec
65 0 ▶ {
Subroutine exec: not called CC=3 SCAR=24.8
66 0 my $sql = shift;
67 0 my $q = __PACKAGE__->new($sql);
68 0 my $rows = $q->execute(@_);
69 0 ▶ if (blessed($rows) && $rows->isa('sqlQueryResult')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($rows) and $rows->isa("sqlQueryResult")) 0 0 0
Condition0%0/3
builtin::blessed($rows) and $rows->isa("sqlQueryResult")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($rows) and $rows->isa("sqlQueryResult")
builtin::blessed($rows) $rows->isa("sqlQueryResult")
Truth table0%0/3
A and B
A= builtin::blessed($rows)B= $rows->isa("sqlQueryResult")
ABresult
0X0
100
111
70 0 cluck('Discarded query with results');
71 0 $rows = undef;
72 }
73  
74 0 undef $q;
75 0 return $rows
76 }
77  
78 sub foundRows
79 0 ▶ {
Subroutine foundRows: not called CC=1 SCAR=6.9
80 0 my $res = __PACKAGE__->new(q(SELECT FOUND_ROWS()))->execute;
81 0 my $rows = $res->fetchColumn(0);
82 0 $res->freeResource();
83  
84 0 return $rows
85 }
86  
87 sub getLastInsertId
88 0 ▶ {
Subroutine getLastInsertId: not called CC=1 SCAR=6.9
89 0 return $sqlQuery::DBI->last_insert_id(undef, undef, undef, undef);
90 }
91  
92 sub new
93 0 ▶ {
Subroutine new: not called CC=7 SCAR=40.3
94 0 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
95 0 my $sql = shift;
96 0 my $self = {-sql => undef, -params => undef, -named => 0};
97  
98 0 ▶ unless (blessed($sql)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($sql)) { } 0 0 0
99 0 ▶ croak 'Not a scalar argument; query must either be a string or an instance of "sqlSelectAssemble"'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $sql or ref $sql or &looks_like_number($sql) 0 0 0
Condition0%0/6
not defined $sql or ref $sql or &looks_like_number($sql)
l!l&&r!l&&!r
000
not defined $sql or ref $sql
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $sql or ref $sql or &looks_like_number($sql)
not defined $sql ref $sql &looks_like_number($sql)
Truth table0%0/4
not(A) or B or C
A= not defined $sqlB= ref $sqlC= &looks_like_number($sql)
ABCresult
1XX1
01X1
0011
0000
100 if !defined $sql || ref $sql || looks_like_number $sql;
101 } else {
102 0 ▶ croak sprintf 'Parameter is not an instance of "sqlSelectAssemble" (got "%s")', ref $sql
Branch: 1 branch
BranchTrueFalseTotal
unless $sql->isa("sqlSelectAssemble") 0 0 0
103 unless $sql->isa('sqlSelectAssemble');
104 0 $self->{'-params'} = undef
105 }
106  
107 0 $self->{'-sql'} = $sql;
108 0 return bless $self, $class
109 }
110  
111 sub debugQuery
112 0 ▶ {
Subroutine debugQuery: not called CC=4 SCAR=30.0
113 0 my $self = shift;
114 0 my $sql = "$self->{-sql}";
115  
116 0 $sql =~ s/(?:\r?\n)+$//;
117 0 print "$sql\n";
118  
119 0 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 0 0
elsif (builtin::blessed($self->{'-sql'})) { } 0 0 0
120 0 $self->_populateParameters(@_);
121 } elsif (blessed $self->{'-sql'}) {
122 0 $self->_populateParameters($self->{'-sql'}->gatherBoundArgs());
123 }
124  
125 0 ▶ if (defined $self->{'-params'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-params"}) 0 0 0
126 0 printf "%s\n%s\n%s\n", ('-'x80), Dumper($self->{'-params'}), ('-'x80);
127 0 $self->_interpolateQuery();
128 0 printf "%s\n", $self->{'-interpolated-query'};
129 }
130  
131 return
132 0 }
133  
134 sub execute
135 0 ▶ {
Subroutine execute: not called CC=4 SCAR=30.0
136 0 my $self = shift;
137  
138 0 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 0 0
elsif (builtin::blessed($self->{'-sql'})) { } 0 0 0
139 0 $self->_populateParameters(@_);
140 } elsif (blessed $self->{'-sql'}) {
141 0 $self->_populateParameters($self->{'-sql'}->gatherBoundArgs());
142 }
143  
144 0 $self->_interpolateQuery();
145  
146 0 my $res = eval {$self->_query($self->{'-interpolated-query'})};
147 0 $self->{'-params'} = undef;
148 0 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
149  
150 0 return $res
151 }
152  
153 sub setParameters
154 0 ▶ {
Subroutine setParameters: not called CC=1 SCAR=6.9
155 0 my $self = shift;
156 0 $self->_populateParameters(@_);
157 0 return $self
158 }
159  
160 sub _getInterpolatedQuery
161 0 ▶ {
Subroutine _getInterpolatedQuery: not called CC=1 SCAR=6.9
162 0 my $self = shift;
163 0 $self->_interpolateQuery();
164 0 return $self->{'-interpolated-query'}
165 }
166  
167 sub _populateParameters
168 0 ▶ {
Subroutine _populateParameters: not called CC=10 SCAR=47.0
169 0 my $self = shift;
170  
171 0 ▶ if (defined $self->{'-params'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-params"}) 0 0 0
172 0 local $Carp::CarpLevel = $Carp::CarpLevel + 2; ## no critic
173 0 croak 'Query parameters are already populated'
174 }
175  
176 0 ▶ if (1 == scalar @_ && 'HASH' eq ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == scalar @_ and 'HASH' eq ref $_[0]) { } 0 0 0
Condition0%0/3
1 == scalar @_ and "HASH" eq ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
1 == scalar @_ and "HASH" eq ref $_[0]
1 == scalar @_ "HASH" eq ref $_[0]
Truth table0%0/3
A and B
A= 1 == scalar @_B= "HASH" eq ref $_[0]
ABresult
0X0
100
111
177 0 $self->{'-named'} = 1;
178 0 $self->{'-params'} = shift;
179 0 foreach my $p (keys %{$self->{'-params'}}) {
180 0 $self->{'-params'}->{$p} = _convertArgument($self->{'-params'}->{$p});
181 croak "Argument '$p' could not be converted"
182 0 ▶ unless defined $self->{'-params'}->{$p};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"-params"}{$p} 0 0 0
183 }
184 } else {
185 0 foreach (@_) {
186 0 ▶ croak 'Mixed named and positional parameters are unsupported'
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq ref $_ 0 0 0
187 if 'HASH' eq ref $_;
188 }
189  
190 0 $self->{'-named'} = 0;
191 0 $self->{'-params'} = [@_];
192  
193 0 foreach my $index (0..$#_) {
194 0 $self->{'-params'}->[$index] = _convertArgument($self->{'-params'}->[$index]);
195 croak "Argument at index '$index' could not be converted"
196 0 ▶ unless defined $self->{'-params'}->[$index];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"-params"}[$index] 0 0 0
197 }
198 }
199  
200 return
201 0 }
202  
203 sub _interpolateQuery
204 0 ▶ {
Subroutine _interpolateQuery: not called CC=2 SCAR=17.9
205 0 my $self = shift;
206  
207 0 ▶ if ($self->{'-named'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'-named'}) { } 0 0 0
208 0 $self->_interpolateByName();
209 } else {
210 0 $self->_interpolateByIndex();
211 }
212  
213 0 return $self->_checkLeftoverParameters();
214 }
215  
216 sub _interpolateByIndex
217 0 ▶ {
Subroutine _interpolateByIndex: not called CC=5 SCAR=34.0
218 0 my $self = shift;
219 0 my $sql = "$self->{-sql}";
220 0 my $pos = 0;
221  
222 0 while (1) {
223 0 ▶ last if $pos >= length($sql) || 0 > ($pos = index $sql, $sqlQuery::PARAMETER_PLACEHOLDER, $pos);
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= length $sql or 0 > ($pos = index($sql, $sqlQuery::PARAMETER_PLACEHOLDER, $pos)) 0 0 0
Condition0%0/3
$pos >= length $sql or 0 > ($pos = index($sql, $sqlQuery::PARAMETER_PLACEHOLDER, $pos))
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos >= length $sql or 0 > ($pos = index($sql, $sqlQuery::PARAMETER_PLACEHOLDER, $pos))
$pos >= length $sql 0 > ($pos = index($sql, $sqlQuery::PARAMETER_PLACEHOLDER, $pos))
Truth table0%0/3
A or B
A= $pos >= length $sqlB= 0 > ($pos = index($sql, $sqlQuery::PARAMETER_PLACEHOLDER, $pos))
ABresult
1X1
011
000
224  
225 0 my $param = eval{$self->_fetchParameter()};
226 0 ▶ croak "$@: interpolated so far: $sql" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
227 0 my $value = "$param";
228  
229 0 ▶ $sql =
Branch: 1 branch
BranchTrueFalseTotal
0 < $pos ? : 0 0 0
230 (0 < $pos ? substr($sql, 0, $pos) : '') .
231 $value .
232 (substr $sql, $pos + 1);
233 0 $pos += length $value;
234 }
235  
236 0 $self->{'-interpolated-query'} = $sql;
237  
238 return
239 0 }
240  
241 sub _interpolateByName
242 0 ▶ {
Subroutine _interpolateByName: not called CC=5 SCAR=34.0
243 0 my $self = shift;
244 0 my $sql = "$self->{-sql}";
245 0 my $pos = 0;
246  
247 0 while(1) {
248 0 ▶ last if $pos >= length($sql) || 0 > ($pos = index $sql, q(:), $pos);
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= length $sql or 0 > ($pos = index($sql, ":", $pos)) 0 0 0
Condition0%0/3
$pos >= length $sql or 0 > ($pos = index($sql, ":", $pos))
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos >= length $sql or 0 > ($pos = index($sql, ":", $pos))
$pos >= length $sql 0 > ($pos = index($sql, ":", $pos))
Truth table0%0/3
A or B
A= $pos >= length $sqlB= 0 > ($pos = index($sql, ":", $pos))
ABresult
1X1
011
000
249  
250 0 my ($name) = (substr $sql, $pos) =~ m/^:([[:lower:][:upper:]_\d-]+)/;
251 0 my $param = eval{$self->_fetchParameter($name)};
252 0 ▶ croak "$@: interpolated so far: $sql" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
253 0 my $value = "$param";
254  
255 0 ▶ $sql = (0 < $pos ? substr($sql, 0, $pos) : '') .
Branch: 1 branch
BranchTrueFalseTotal
0 < $pos ? : 0 0 0
256 $value .
257 (substr $sql, $pos + 1 + length $name);
258 0 $pos += length $value;
259 }
260  
261 0 $self->{'-interpolated-query'} = $sql;
262  
263 return
264 0 }
265  
266 sub _fetchParameter
267 0 ▶ {
Subroutine _fetchParameter: not called CC=5 SCAR=34.0
268 0 my $self = shift;
269 0 my $name = shift;
270  
271 0 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 0 0 0
272 0 ▶ if (!exists($self->{'-params'}->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"-params"}{$name}) 0 0 0
273 0 croak sprintf 'No such query parameter "%s"', $name;
274 }
275 0 return $self->{'-params'}->{$name};
276 } else {
277 0 ▶ unless (ref $self->{'-params'} && @{$self->{'-params'}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"-params"} and @{$self->{"-params"};}) 0 0 0
Condition0%0/3
ref $self->{"-params"} and @{$self->{"-params"};}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"-params"} and @{$self->{"-params"};}
ref $self->{"-params"} @{$self->{"-params"};}
Truth table0%0/3
A and B
A= ref $self->{"-params"}B= @{$self->{"-params"};}
ABresult
0X0
100
111
278 0 croak 'Too few query parameters provided';
279 }
280 }
281  
282 0 return shift @{$self->{'-params'}};
283 }
284  
285 sub _checkLeftoverParameters
286 0 ▶ {
Subroutine _checkLeftoverParameters: not called CC=3 SCAR=24.8
287 0 my $self = shift;
288  
289 0 ▶ if ('ARRAY' eq ref $self->{'-params'} && @{$self->{'-params'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ("ARRAY" eq ref $self->{"-params"} and @{$self->{"-params"};}) 0 0 0
Condition0%0/3
"ARRAY" eq ref $self->{"-params"} and @{$self->{"-params"};}
!ll&&!rl&&r
000
MC/DC0%0/2
"ARRAY" eq ref $self->{"-params"} and @{$self->{"-params"};}
"ARRAY" eq ref $self->{"-params"} @{$self->{"-params"};}
Truth table0%0/3
A and B
A= "ARRAY" eq ref $self->{"-params"}B= @{$self->{"-params"};}
ABresult
0X0
100
111
290 0 croak 'Too many query parameters provided';
291 }
292  
293 return
294 0 }
295  
296 sub _query
297 0 ▶ {
Subroutine _query: not called CC=4 SCAR=30.0
298 0 my $self = shift;
299 0 my $sql = shift;
300 0 my $dbh = sqlQuery::dbh();
301 0 my $error;
302  
303 EXECUTE: {
304 0 ▶ if ($sql !~ m/^select/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sql =~ /^select/i) 0 0 0
305 0 local $dbh->{RaiseError} = 1;
306 0 local $dbh->{PrintError} = 0;
307 0 my $rows;
308 0 ▶ eval{$dbh->do($sql); 1} or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $dbh->do($sql); 1 } } or do { $error = $@; last EXECUTE } 0 0 0
309 0 $error = $@;
310 0 last EXECUTE;
311 };
312 0 return $rows;
313 }
314  
315 0 $self->{'-sth'} = $dbh->prepare($sql);
316  
317 0 local $self->{'-sth'}->{RaiseError} = 1;
318 0 local $self->{'-sth'}->{PrintError} = 0;
319 0 ▶ eval {$self->{'-sth'}->execute; 1} or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"-sth"}->execute; 1 } } or do { $error = $@; last EXECUTE } 0 0 0
320 0 $error = $@;
321 0 last EXECUTE;
322 };
323 0 return sqlQueryResult->new($self, $self->{'-sth'});
324 }
325  
326 0 my $file = __FILE__;
327  
328 0 $self->{'-sth'} = undef;
329  
330 0 $error =~ s/\s+at $file line \d+[.]\r?\n//;
331 0 $error =~ s/\s*at line \d$//;
332 0 $sql =~ s/(?:\r?\n)+$//;
333 0 croak "$error\n\n<<SQL\n$sql\nSQL\n\nCalled";
334 }
335  
336 sub quoteTable
337 0 ▶ {
Subroutine quoteTable: not called CC=3 SCAR=24.8
338 0 my $table = shift;
339  
340 0 ▶ if (ref $table)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $table) 0 0 0
341 {
342 0 my ($k,$v);
343 0 ($k) = keys %$table;
344 0 ($v) = values %$table;
345 0 return sprintf '%s AS %s', sqlQuery::quoteTable($k), sqlQuery::quoteTable($v);
346 }
347  
348 0 ▶ return '*'
Branch: 1 branch
BranchTrueFalseTotal
if "*" eq $table 0 0 0
349 if '*' eq $table;
350 0 $table = join '.', map {"`$_`"} split /[.]/, $table;
351 0 $table =~ s/`+/`/g;
352 0 return $table
353 }
354  
355 sub quoteWhenTable
356 0 ▶ {
Subroutine quoteWhenTable: not called CC=4 SCAR=30.0
357 0 my $table = shift;
358  
359 0 ▶ return sqlQuery::quoteTable($table)
Branch: 1 branch
BranchTrueFalseTotal
if ref $table or (".$table") =~ /^(?:[.][[:lower:]_][[:lower:]\d_]*){1,2}$/i 0 0 0
Condition0%0/3
ref $table or (".$table") =~ /^(?:[.][[:lower:]_][[:lower:]\d_]*){1,2}$/i
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $table or (".$table") =~ /^(?:[.][[:lower:]_][[:lower:]\d_]*){1,2}$/i
ref $table (".$table") =~ /^(?:[.][[:lower:]_][[:lower:]\d_]*){1,2}$/i
Truth table0%0/3
A or B
A= ref $tableB= (".$table") =~ /^(?:[.][[:lower:]_][[:lower:]\d_]*){1,2}$/i
ABresult
1X1
011
000
360 if ref $table || ".$table" =~ m/^(?:[.][[:lower:]_][[:lower:]\d_]*){1,2}$/i;
361  
362 0 ▶ if ($table =~ m/^([[:lower:]_][[:lower:]\d_]*)[.][*]$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table =~ /^([[:lower:]_][[:lower:]\d_]*)[.][*]$/i) { } 0 0 0
363 0 return sqlQuery::quoteTable($1).'.*';
364 } else {
365 0 return $table;
366 }
367 }
368  
369 sub convertArgument
370 0 ▶ {
Subroutine convertArgument: not called CC=2 SCAR=17.9
371 0 my $arg = shift;
372 0 my $value = _convertArgument($arg);
373  
374 0 ▶ unless (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
375 0 local $Carp::CarpLevel = $Carp::CarpLevel + 1; ## no critic
376 0 croak 'Argument to "sqlCondition::bind()" cannot be converted; consider using an implicit "sqlValue" instance instead'
377 }
378  
379 0 return $value
380 }
381  
382 sub _convertArgument
383 0 ▶ {
Subroutine _convertArgument: not called CC=12 SCAR=50.5
384 0 my $arg = shift;
385  
386 0 ▶ unless(ref $arg) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $arg) { } 0 0 0
if (do { 'ARRAY' eq ref $arg }) { } 0 0 0
elsif (builtin::blessed($arg)) { } 0 0 0
387 0 ▶ return sqlValueNull->new
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 0 0
388 unless defined $arg;
389 0 ▶ return sqlValueInt->new($arg)
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /^-?\d+$/ 0 0 0
390 if $arg =~ m/^-?\d+$/;
391 0 ▶ return sqlValueFloat->new($arg)
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /^-?\d+[.]\d+$/ 0 0 0
392 if $arg =~ m/^-?\d+[.]\d+$/;
393 0 return sqlValueString->new($arg);
394 } elsif ('ARRAY' eq ref $arg) {
395 0 return sqlValueList->new($arg);
396 } elsif (blessed $arg) {
397 0 ▶ return $arg if $arg->isa('sqlParameter');
Branch: 1 branch
BranchTrueFalseTotal
if $arg->isa("sqlParameter") 0 0 0
398 0 ▶ return unless $arg->can('to_string');
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->can("to_string") 0 0 0
399 0 my $value = $arg->to_string();
400 0 ▶ return sqlValueNull->new() unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
401 0 ▶ return $value if blessed $value && $value->isa('sqlParameter');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) and $value->isa("sqlParameter") 0 0 0
Condition0%0/3
builtin::blessed($value) and $value->isa("sqlParameter")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and $value->isa("sqlParameter")
builtin::blessed($value) $value->isa("sqlParameter")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->isa("sqlParameter")
ABresult
0X0
100
111
402 0 return sqlValueString->new($value);
403 }
404  
405 return
406 0 }
407 ##------------------------------------------------------------------------------
408 package sqlQueryResult;
409  
410 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
411 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
412 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
413 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
414  
415 sub new
416 0 ▶ {
Subroutine new: not called CC=2 SCAR=17.9
417 0 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
418 0 my $query = shift;
419 0 my $result = shift;
420  
421 0 return bless {-query => $query, -result => $result}, $class;
422 }
423  
424 0 ▶sub fetchAssoc {goto &fetchRow}
Subroutine fetchAssoc: not called CC=1 SCAR=6.9
425 sub fetchRow
426 0 ▶ {
Subroutine fetchRow: not called CC=1 SCAR=6.9
427 0 my $self = shift;
428 0 return $self->{'-result'}->fetchrow_hashref
429 }
430  
431 sub fetchArray
432 0 ▶ {
Subroutine fetchArray: not called CC=1 SCAR=6.9
433 0 my $self = shift;
434 0 return $self->{'-result'}->fetchrow_array;
435 }
436  
437 sub fetchColumn
438 0 ▶ {
Subroutine fetchColumn: not called CC=5 SCAR=34.0
439 0 my $self = shift;
440 0 ▶ my $column = shift || '0';
Condition0%0/2
shift() || '0'
l!l
00
MC/DC0%0/1
shift() || '0'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
441  
442 0 ▶ if (looks_like_number $column) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($column)) { } 0 0 0
443 0 my @row = $self->{'-result'}->fetchrow_array;
444 0 ▶ croak "No such query result offset $column"
Branch: 1 branch
BranchTrueFalseTotal
if $column > $#row 0 0 0
445 if $column > $#row;
446 0 return $row[$column];
447 } else {
448 0 my $row = $self->fetchRow();
449 croak "No such query result column $column"
450 0 ▶ unless exists($row->{$column});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $row->{$column} 0 0 0
451 0 return $row->{$column};
452 }
453 }
454  
455 sub fetchAll
456 0 ▶ {
Subroutine fetchAll: not called CC=2 SCAR=17.9
457 0 my $self = shift;
458 0 my ($row,@rows);
459  
460 0 ▶ push @rows, $row
Branch: 1 branch
BranchTrueFalseTotal
defined($row = $self->fetchAssoc) and do { push @rows, $row } 0 0 0
461 while defined($row = $self->fetchAssoc());
462 return @rows
463 0 }
464  
465 0 ▶sub numRows {goto &getNumRows}
Subroutine numRows: not called CC=1 SCAR=6.9
466 sub getNumRows
467 0 ▶ {
Subroutine getNumRows: not called CC=1 SCAR=6.9
468 0 return shift->{'-result'}->rows;
469 }
470  
471 sub freeResource
472 0 ▶ {
Subroutine freeResource: not called CC=2 SCAR=17.9
473 0 my $self = shift;
474  
475 croak 'Statement seems unexecuted'
476 0 ▶ unless defined $self->{'-result'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"-result"} 0 0 0
477 0 $self->{'-result'}->finish();
478 0 undef $self->{'-result'};
479  
480 0 return $self;
481 }
482 ##------------------------------------------------------------------------------
483 package sqlQueryBase;
484  
485 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
486 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
487 ##------------------------------------------------------------------------------
488 sub select
489 0 ▶ {
Subroutine select: not called CC=4 SCAR=30.0
490 0 my @fields;
491 my @params;
492  
493 0 ▶ if (@_ && 'ARRAY' eq ref $_[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and "ARRAY" eq ref $_[-1]) 0 0 0
Condition0%0/3
@_ and "ARRAY" eq ref $_[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and "ARRAY" eq ref $_[-1]
@_ "ARRAY" eq ref $_[-1]
Truth table0%0/3
A and B
A= @_B= "ARRAY" eq ref $_[-1]
ABresult
0X0
100
111
494 0 @params = @{pop()}; ## no critic
495 }
496  
497 0 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
498 0 @fields = '*';
499 } else {
500 @fields = (
501 0 (split /,/, (join ',', grep {!ref} @_)),
502 0 grep {ref} @_
503 );
504 }
505  
506 0 return sqlSelectFrom->new(
507 fields => [@fields],
508 params => [@params]
509 );
510 }
511 ##------------------------------------------------------------------------------
512 package sqlParameter;
513  
514 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
515 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
516 1 ▶use overload '""' => 'getSafeQuotedValue';
Subroutine BEGIN: called CC=1 SCAR=0.0
517 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
518  
519 sub new
520 0 ▶ {
Subroutine new: not called CC=2 SCAR=17.9
521 0 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
522 0 return bless {-value => shift}, $class;
523 }
524  
525 sub getSafeQuotedValue
526 0 ▶ {
Subroutine getSafeQuotedValue: not called CC=1 SCAR=6.9
527 0 croak 'sqlParameter::getSafeQuotedValue() is abstract; implement it in '.(ref $_[0])
528 }
529 ##------------------------------------------------------------------------------
530 package sqlValueNull;
531  
532 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
533 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
534 1 ▶use base 'sqlParameter';
Subroutine BEGIN: called CC=1 SCAR=0.0
535  
536 0 ▶sub getSafeQuotedValue {return 'NULL'}
Subroutine getSafeQuotedValue: not called CC=1 SCAR=6.9
537 ##------------------------------------------------------------------------------
538 package sqlValueLiteral;
539  
540 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
541 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
542 1 ▶use base 'sqlParameter';
Subroutine BEGIN: called CC=1 SCAR=0.0
543  
544 0 ▶sub getSafeQuotedValue {return shift->{-value}}
Subroutine getSafeQuotedValue: not called CC=1 SCAR=6.9
545 ##------------------------------------------------------------------------------
546 package sqlValueString;
547  
548 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
549 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
550 1 ▶use base 'sqlParameter';
Subroutine BEGIN: called CC=1 SCAR=0.0
551  
552 0 ▶sub getSafeQuotedValue {
Subroutine getSafeQuotedValue: not called CC=1 SCAR=6.9
553 0 my $self = shift;
554 0 return sqlQuery::dbh()->quote($self->{-value});
555 }
556 ##------------------------------------------------------------------------------
557 package sqlValueInt;
558  
559 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
560 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
561 1 ▶use base 'sqlParameter';
Subroutine BEGIN: called CC=1 SCAR=0.0
562  
563 0 ▶sub getSafeQuotedValue {
Subroutine getSafeQuotedValue: not called CC=1 SCAR=6.9
564 return int(shift->{-value})
565 0 }
566 ##------------------------------------------------------------------------------
567 package sqlValueFloat;
568  
569 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
570 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
571 1 ▶use base 'sqlParameter';
Subroutine BEGIN: called CC=1 SCAR=0.0
572  
573 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
574 0 my $self = shift->SUPER::new(@_);
575 0 ▶ $self->{-precision} = $_[1] || 8;
Condition0%0/2
$_[1] || 8
l!l
00
MC/DC0%0/1
$_[1] || 8
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
576 0 return $self
577 }
578  
579 0 ▶sub getSafeQuotedValue {
Subroutine getSafeQuotedValue: not called CC=1 SCAR=6.9
580 0 my $self = shift;
581 return sprintf '%.'.$self->{-precision}.'f', $self->{-value}
582 0 }
583 ##------------------------------------------------------------------------------
584 package sqlValueList;
585  
586 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
587 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
588 1 ▶use base 'sqlParameter';
Subroutine BEGIN: called CC=1 SCAR=0.0
589 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
590 1 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
591  
592 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
593 0 my $self = shift->SUPER::new(@_);
594  
595 0 ▶ unless (@{$self->{-value}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'-value'};}) { } 0 0 0
596 0 local $Carp::CarpLevel = $Carp::CarpLevel + 2; ## no critic
597 0 croak 'Empty lists can break SQL syntax.';
598 } else {
599 $self->{-value} = [map {
600 0 ▶ blessed($_) && $_->isa('sqlParameter')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) && $_->isa('sqlParameter') ? : 0 0 0
Condition0%0/3
builtin::blessed($_) and $_->isa("sqlParameter")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) and $_->isa("sqlParameter")
builtin::blessed($_) $_->isa("sqlParameter")
Truth table0%0/3
A and B
A= builtin::blessed($_)B= $_->isa("sqlParameter")
ABresult
0X0
100
111
601 ? $_
602 : sqlQuery::convertArgument($_)
603 0 } @{$self->{-value}}];
604 }
605  
606 0 return $self
607 }
608  
609 0 ▶sub getSafeQuotedValue {
Subroutine getSafeQuotedValue: not called CC=1 SCAR=6.9
610 0 return join ',', map {"$_"} @{shift->{-value}};
611 }
612 ##------------------------------------------------------------------------------
613 package sqlValueDateTimeBase;
614  
615 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
616 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
617 1 ▶use base 'sqlParameter';
Subroutine BEGIN: called CC=1 SCAR=0.0
618  
619 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
620 1 ▶use Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
621 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
622  
623 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
624 0 my $self = shift->SUPER::new(@_);
625  
626 0 ▶ unless (defined $self->{-value}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'-value'}) { } 0 0 0
if (do { &looks_like_number($self->{'-value'}) }) { } 0 0 0
627 0 $self->{-value} = time;
628 } elsif (looks_like_number $self->{-value}) {
629  
630 } else {
631 0 $self->{-value} = str2time($self->{-value});
632 }
633  
634 0 return $self
635 }
636  
637 0 ▶sub getSafeQuotedValue {
Subroutine getSafeQuotedValue: not called CC=1 SCAR=6.9
638 0 return sqlQuery::dbh()->quote(shift->format());
639 }
640  
641 0 ▶sub format {
Subroutine format: not called CC=1 SCAR=6.9
642 0 croak __PACKAGE__.'::format() is "abstract"';
643 }
644 ##------------------------------------------------------------------------------
645 package sqlValueDate;
646  
647 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
648 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
649 1 ▶use base 'sqlValueDateTimeBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
650 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
651  
652 0 ▶sub format {
Subroutine format: not called CC=1 SCAR=6.9
653 0 return strftime '%Y-%m-%d', localtime shift->{-value};
654 }
655 ##------------------------------------------------------------------------------
656 package sqlValueDateTime;
657  
658 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
659 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
660 1 ▶use base 'sqlValueDateTimeBase';
Subroutine BEGIN: called CC=1 SCAR=0.0
661 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
662  
663 0 ▶sub format {
Subroutine format: not called CC=1 SCAR=6.9
664 0 return strftime '%Y-%m-%d %H:%M:%S', localtime shift->{-value};
665 }
666 ##------------------------------------------------------------------------------
667 package sqlSelectAssemble;
668  
669 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
670 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
671 1 ▶use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
672 1 ▶use overload '""' => 'assemble';
Subroutine BEGIN: called CC=1 SCAR=0.0
673  
674 sub new
675 0 ▶ {
Subroutine new: not called CC=4 SCAR=30.0
676 0 my $class = shift;
677 0 my ($prev,$prevClass,%args) = @_;
678 0 my $self = bless {boundArgs => undef, prev => $prev, %args}, $class;
679  
680 0 ▶ if ($prevClass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prevClass) 0 0 0
681 confess sprintf 'Invalid predecessor. Got "%s". Wanted "%s"', ref $self->{prev}, $prevClass
682 0 ▶ unless ref $self->{prev} && $self->{prev}->isa($prevClass);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"prev"} and $self->{"prev"}->isa($prevClass) 0 0 0
Condition0%0/3
ref $self->{"prev"} and $self->{"prev"}->isa($prevClass)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"prev"} and $self->{"prev"}->isa($prevClass)
ref $self->{"prev"} $self->{"prev"}->isa($prevClass)
Truth table0%0/3
A and B
A= ref $self->{"prev"}B= $self->{"prev"}->isa($prevClass)
ABresult
0X0
100
111
683 }
684  
685 0 return $self
686 }
687  
688 sub addBoundArgs
689 0 ▶ {
Subroutine addBoundArgs: not called CC=1 SCAR=6.9
690 0 my $self = shift;
691 0 push @{$self->{boundArgs}}, @_;
692 0 return $self
693 }
694  
695 sub gatherBoundArgs
696 0 ▶ {
Subroutine gatherBoundArgs: not called CC=3 SCAR=24.8
697 0 my $self = shift;
698 0 my (@args);
699  
700 0 push @args, @{$self->{boundArgs}}
701 0 ▶ if defined $self->{boundArgs};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"boundArgs"} 0 0 0
702 0 push @args, $self->gatherConditionArgs();
703  
704 0 ▶ if (defined $self->{prev}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"prev"}) 0 0 0
705 0 push @args, $self->{prev}->gatherBoundArgs();
706 }
707  
708 return @args
709 0 }
710  
711 0 ▶sub gatherConditionArgs {}
Subroutine gatherConditionArgs: not called CC=1 SCAR=0.0
712  
713 sub assemble
714 0 ▶ {
Subroutine assemble: not called CC=2 SCAR=17.9
715 0 my $self = shift;
716 0 my $assembled = $self->_assemble();
717  
718 $assembled = $self->{prev}->assemble() . $assembled
719 0 ▶ if defined $self->{prev};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"prev"} 0 0 0
720  
721 0 return $assembled
722 }
723  
724 sub _assemble
725 0 ▶ {
Subroutine _assemble: not called CC=1 SCAR=6.9
726 0 return ''
727 }
728 ##------------------------------------------------------------------------------
729 package sqlSelectFrom;
730  
731 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
732 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
733 1 ▶use base 'sqlSelectAssemble';
Subroutine BEGIN: called CC=1 SCAR=0.0
734 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
735  
736 sub new
737 0 ▶ {
Subroutine new: not called CC=2 SCAR=17.9
738 0 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
739 0 my (%args) = @_;
740 0 my (@fields);
741  
742 0 @fields = @{$args{fields}};
743  
744 my $self = bless {
745 queryFields => undef,
746 tables => undef,
747 params => $args{params}
748 0 }, $class;
749  
750 0 $self->{queryFields} = [$self->translateQueryFields(@fields)];
751 0 return $self
752 }
753  
754 sub from
755 0 ▶ {
Subroutine from: not called CC=1 SCAR=6.9
756 0 my $self = shift;
757 0 $self->{tables} = [@_];
758 0 return sqlSelectJoin->new($self);
759 }
760  
761 sub translateQueryFields
762 0 ▶ {
Subroutine translateQueryFields: not called CC=12 SCAR=50.5
763 0 my $self = shift;
764 0 my (@fields) = @_;
765 0 my @columns;
766  
767 0 foreach my $fieldIn (@fields)
768 {
769 0 my (@parts);
770  
771 0 ▶ unless ('HASH' eq ref $fieldIn) {
Branch: 1 branch
BranchTrueFalseTotal
if ('HASH' eq ref $fieldIn) { } 0 0 0
772 0 @parts = ($fieldIn, undef);
773 } else {
774 0 @parts = %$fieldIn;
775 }
776  
777 0 ▶ while (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 0 0 0
778 0 my ($field,$alias) = (splice @parts, 0, 2);
779  
780 0 ▶ if (blessed $field && $field->isa('sqlParameter'))
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($field) and $field->isa("sqlParameter")) 0 0 0
Condition0%0/3
builtin::blessed($field) and $field->isa("sqlParameter")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($field) and $field->isa("sqlParameter")
builtin::blessed($field) $field->isa("sqlParameter")
Truth table0%0/3
A and B
A= builtin::blessed($field)B= $field->isa("sqlParameter")
ABresult
0X0
100
111
781 {
782 0 ▶ push @columns, $sqlQuery::PARAMETER_PLACEHOLDER
Branch: 1 branch
BranchTrueFalseTotal
unless $alias 0 0 0
783 unless $alias;
784 0 ▶ push @columns, sprintf '%s AS %s',
Branch: 1 branch
BranchTrueFalseTotal
if $alias 0 0 0
785 $sqlQuery::PARAMETER_PLACEHOLDER,
786 sqlQuery::quoteTable($alias)
787 if $alias;
788 0 $self->addBoundArgs($field);
789 0 next;
790 }
791  
792 0 ▶ $field = sqlQuery::quoteWhenTable($field)
Branch: 1 branch
BranchTrueFalseTotal
if "*" ne $field and 0 == ~index($field, " ") 0 0 0
Condition0%0/3
"*" ne $field and 0 == ~index($field, " ")
!ll&&!rl&&r
000
MC/DC0%0/2
"*" ne $field and 0 == ~index($field, " ")
"*" ne $field 0 == ~index($field, " ")
Truth table0%0/3
A and B
A= "*" ne $fieldB= 0 == ~index($field, " ")
ABresult
0X0
100
111
793 if '*' ne $field && 0 == ~index $field, ' ';
794  
795 0 ▶ unless ($alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias) { } 0 0 0
796 0 push @columns, $field
797 } else {
798 0 ▶ $alias = sqlQuery::quoteWhenTable($alias)
Branch: 1 branch
BranchTrueFalseTotal
unless ~index($alias, " ") 0 0 0
799 unless ~index $alias, ' ';
800 0 push @columns, "\n\t$field AS $alias";
801 }
802 }
803 }
804  
805 return @columns
806 0 }
807  
808 sub _assemble
809 0 ▶ {
Subroutine _assemble: not called CC=8 SCAR=42.8
810 0 my $self = shift;
811 0 my $s = 'SELECT';
812  
813 0 $s .= ' ' . join ',', @{$self->{params}}
814 0 ▶ if @{$self->{params}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"params"};} 0 0 0
815 0 $s .= ' ' . join ',', @{$self->{queryFields}};
816  
817 0 ▶ if (defined $self->{tables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"tables"}) 0 0 0
818 0 $s .= "\nFROM ";
819 0 my @t;
820  
821 0 foreach my $tableSpec (@{$self->{tables}}) {
822 0 my (@tables);
823  
824 0 ▶ if ('HASH' eq ref $tableSpec) {
Branch: 1 branch
BranchTrueFalseTotal
if ('HASH' eq ref $tableSpec) { } 0 0 0
825 0 @tables = %$tableSpec;
826 } else {
827 0 @tables = ($tableSpec,undef);
828 }
829  
830 0 ▶ while (@tables) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tables) 0 0 0
831 0 my ($table,$alias) = (splice @tables, 0, 2);
832  
833 0 ▶ push @t, sqlQuery::quoteTable($table)
Branch: 1 branch
BranchTrueFalseTotal
unless $alias 0 0 0
834 unless $alias;
835 0 ▶ push @t, sqlQuery::quoteTable($table)." AS `$alias`"
Branch: 1 branch
BranchTrueFalseTotal
if $alias 0 0 0
836 if $alias;
837 }
838 }
839  
840 0 $s .= join ',', @t;
841 }
842  
843 0 return "$s\n";
844 }
845 ##------------------------------------------------------------------------------
846 package sqlSelectLimit;
847  
848 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
849 1 ▶use base 'sqlSelectAssemble';
Subroutine BEGIN: called CC=1 SCAR=0.0
850  
851 sub new
852 0 ▶ {
Subroutine new: not called CC=1 SCAR=6.9
853 0 return sqlSelectAssemble::new(@_, 'sqlSelectOrderBy',
854 limit => undef,
855 offset => undef);
856 }
857  
858 sub limit
859 0 ▶ {
Subroutine limit: not called CC=8 SCAR=42.8
860 0 my $self = shift;
861  
862 0 ▶ if (!@_ || (1 == @_ && !defined $_[0]) || (2 == @_ && !defined $_[0] && !defined $_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ or 1 == @_ and not defined $_[0] or 2 == @_ and not defined $_[0] and not defined $_[1]) { } 0 0 0
Condition0%0/15
not @_ or 1 == @_ and not defined $_[0] or 2 == @_ and not defined $_[0] and not defined $_[1]
l!l&&r!l&&!r
000
not @_ or 1 == @_ and not defined $_[0]
l!l&&r!l&&!r
000
1 == @_ and not defined $_[0]
!ll&&!rl&&r
000
2 == @_ and not defined $_[0] and not defined $_[1]
!ll&&!rl&&r
000
2 == @_ and not defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/6
not @_ or 1 == @_ and not defined $_[0] or 2 == @_ and not defined $_[0] and not defined $_[1]
not @_ 1 == @_ not defined $_[0] 2 == @_ not defined $_[0] not defined $_[1]
Truth table0%0/10
not(A) or B and not(C) or D and not(E) and not(F)
A= not @_B= 1 == @_C= not defined $_[0]D= 2 == @_E= not defined $_[0]F= not defined $_[1]
ABCDEFresult
1XXXXX1
011XXX1
00X1111
0101111
00X0XX0
00X10X0
00X1100
0100XX0
01010X0
0101100
863 0 $self->{limit} = undef;
864 } else {
865 0 $self->{limit} = int shift;
866 0 ▶ $self->{offset} = int shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
867 }
868 0 return sqlSelectAssemble->new($self);
869 }
870  
871 sub _assemble
872 0 ▶ {
Subroutine _assemble: not called CC=3 SCAR=24.8
873 0 my $self = shift;
874 0 my $s;
875  
876 0 ▶ unless (defined $self->{limit}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'limit'}) { } 0 0 0
if (do { defined $self->{'offset'} }) { } 0 0 0
877 0 $s = '';
878 } elsif (defined $self->{offset}) {
879 0 $s = "LIMIT $self->{offset},$self->{limit}";
880 } else {
881 0 $s = "LIMIT $self->{limit}";
882 }
883  
884 0 return $s
885 }
886 ##------------------------------------------------------------------------------
887 package sqlSelectOrderBy;
888  
889 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
890 1 ▶use base 'sqlSelectLimit';
Subroutine BEGIN: called CC=1 SCAR=0.0
891  
892 sub new
893 0 ▶ {
Subroutine new: not called CC=1 SCAR=6.9
894 0 return sqlSelectAssemble::new(@_, 'sqlSelectHaving', ordering => undef);
895 }
896  
897 sub orderBy
898 0 ▶ {
Subroutine orderBy: not called CC=1 SCAR=6.9
899 0 my $self = shift;
900 0 $self->{ordering} = [@_];
901 0 return sqlSelectLimit->new($self);
902 }
903  
904 sub _assemble
905 0 ▶ {
Subroutine _assemble: not called CC=6 SCAR=37.4
906 0 my $self = shift;
907 0 my $s;
908  
909 0 ▶ unless(defined $self->{ordering}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'ordering'}) { } 0 0 0
910 0 $s = '';
911 } else {
912 0 $s = [];
913  
914 0 foreach my $order (@{$self->{ordering}}) {
915 0 my ($theOrder,$direction) = ($order);
916 0 ▶ if ('HASH' eq ref $theOrder) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq ref $theOrder) 0 0 0
917 0 ($direction) = values %$theOrder;
918 0 ($theOrder) = keys %$theOrder;
919 }
920  
921 0 ▶ push @$s, sqlQuery::quoteWhenTable($theOrder)
Branch: 1 branch
BranchTrueFalseTotal
unless $direction 0 0 0
922 unless $direction;
923 0 ▶ push @$s, sqlQuery::quoteWhenTable($theOrder)." $direction"
Branch: 1 branch
BranchTrueFalseTotal
if $direction 0 0 0
924 if $direction;
925 }
926  
927 0 $s = join ',', @$s;
928 0 $s = "ORDER BY $s\n";
929 }
930  
931 0 return $s . $self->SUPER::_assemble();
932 }
933 ##------------------------------------------------------------------------------
934 package sqlSelectHaving;
935  
936 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
937 1 ▶use base 'sqlSelectOrderBy';
Subroutine BEGIN: called CC=1 SCAR=0.0
938 1 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
939  
940 sub new
941 0 ▶ {
Subroutine new: not called CC=1 SCAR=6.9
942 0 return sqlSelectAssemble::new(@_, 'sqlSelectGroupBy', havingCond => undef);
943 }
944  
945 sub having
946 0 ▶ {
Subroutine having: not called CC=3 SCAR=24.8
947 0 my $self = shift;
948 0 my $condition = shift;
949  
950 0 ▶ confess 'Invalid condition'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $condition and $condition->isa("sqlCondition") 0 0 0
Condition0%0/3
ref $condition and $condition->isa("sqlCondition")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $condition and $condition->isa("sqlCondition")
ref $condition $condition->isa("sqlCondition")
Truth table0%0/3
A and B
A= ref $conditionB= $condition->isa("sqlCondition")
ABresult
0X0
100
111
951 unless ref $condition && $condition->isa('sqlCondition');
952  
953 0 $self->{havingCond} = $condition;
954 0 return sqlSelectOrderBy->new($self);
955 }
956  
957 sub gatherConditionArgs
958 0 ▶ {
Subroutine gatherConditionArgs: not called CC=2 SCAR=17.9
959 0 my $self = shift;
960 0 my @args;
961  
962 push @args, $self->{havingCond}->getBoundArgs()
963 0 ▶ if defined $self->{havingCond};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"havingCond"} 0 0 0
964 return @args
965 0 }
966  
967 sub _assemble
968 0 ▶ {
Subroutine _assemble: not called CC=3 SCAR=24.8
969 0 my $self = shift;
970 0 my $s;
971  
972 0 ▶ unless (defined $self->{havingCond} && defined($s = $self->{havingCond}->assemble())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'havingCond'} and defined($s = $self->{'havingCond'}->assemble)) { } 0 0 0
Condition0%0/3
defined $self->{"havingCond"} and defined($s = $self->{"havingCond"}->assemble)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"havingCond"} and defined($s = $self->{"havingCond"}->assemble)
defined $self->{"havingCond"} defined($s = $self->{"havingCond"}->assemble)
Truth table0%0/3
A and B
A= defined $self->{"havingCond"}B= defined($s = $self->{"havingCond"}->assemble)
ABresult
0X0
100
111
973 0 $s = '';
974 } else {
975 0 $s = "HAVING $s\n";
976 }
977  
978 0 return $s . $self->SUPER::_assemble();
979 }
980 ##------------------------------------------------------------------------------
981 package sqlSelectGroupBy;
982  
983 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
984 1 ▶use base 'sqlSelectHaving';
Subroutine BEGIN: called CC=1 SCAR=0.0
985 1 ▶use overload '+' => 'union';
Subroutine BEGIN: called CC=1 SCAR=0.0
986  
987 sub union
988 0 ▶ {
Subroutine union: not called CC=1 SCAR=6.9
989 0 my ($lhs,$rhs) = @_;
990  
991 0 return "($lhs) UNION ($rhs)";
992 }
993  
994 sub new
995 0 ▶ {
Subroutine new: not called CC=1 SCAR=6.9
996 0 return sqlSelectAssemble::new(@_, 'sqlSelectWhere', grouping => undef);
997 }
998  
999 sub groupBy
1000 0 ▶ {
Subroutine groupBy: not called CC=1 SCAR=6.9
1001 0 my $self = shift;
1002 0 $self->{grouping} = [@_];
1003 0 return sqlSelectHaving->new($self);
1004 }
1005  
1006 sub _assemble
1007 0 ▶ {
Subroutine _assemble: not called CC=2 SCAR=17.9
1008 0 my $self = shift;
1009 0 my $s = '';
1010  
1011 0 ▶ if (defined $self->{grouping})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"grouping"}) 0 0 0
1012 {
1013 0 $s = join ',', map {sqlQuery::quoteWhenTable($_)} @{$self->{grouping}};
1014 0 $s = "GROUP BY $s\n";
1015 }
1016  
1017 0 return $s . $self->SUPER::_assemble();
1018 }
1019 ##------------------------------------------------------------------------------
1020 package sqlSelectWhere;
1021  
1022 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1023 1 ▶use base 'sqlSelectGroupBy';
Subroutine BEGIN: called CC=1 SCAR=0.0
1024 1 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
1025  
1026 sub where
1027 0 ▶ {
Subroutine where: not called CC=4 SCAR=30.0
1028 0 my $self = shift;
1029 0 my $condition = shift;
1030  
1031 0 ▶ confess 'Invalid condition'
Branch: 1 branch
BranchTrueFalseTotal
if defined $condition and not ref $condition && $condition->isa("sqlCondition") 0 0 0
Condition0%0/6
defined $condition and not ref $condition && $condition->isa("sqlCondition")
!ll&&!rl&&r
000
ref $condition && $condition->isa('sqlCondition')
!ll&&!rl&&r
000
MC/DC0%0/3
defined $condition and not ref $condition && $condition->isa("sqlCondition")
defined $condition ref $condition $condition->isa('sqlCondition')
Truth table0%0/4
A and not(B && C)
A= defined $conditionB= ref $conditionC= $condition->isa('sqlCondition')
ABCresult
0XX0
1110
10X1
1101
1032 if defined $condition && !(ref $condition && $condition->isa('sqlCondition'));
1033  
1034 0 $self->{whereCond} = $condition;
1035 0 return sqlSelectGroupBy->new($self);
1036 }
1037  
1038 sub gatherConditionArgs
1039 0 ▶ {
Subroutine gatherConditionArgs: not called CC=2 SCAR=17.9
1040 0 my $self = shift;
1041 0 my @args;
1042  
1043 push @args, $self->{whereCond}->getBoundArgs()
1044 0 ▶ if defined $self->{whereCond};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"whereCond"} 0 0 0
1045 return @args
1046 0 }
1047  
1048 sub _assemble
1049 0 ▶ {
Subroutine _assemble: not called CC=3 SCAR=24.8
1050 0 my $self = shift;
1051 0 my ($s,$c) = ('');
1052  
1053 0 ▶ if ($self->{whereCond} && defined($c = $self->{whereCond}->assemble()))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"whereCond"} and defined($c = $self->{"whereCond"}->assemble)) 0 0 0
Condition0%0/3
$self->{"whereCond"} and defined($c = $self->{"whereCond"}->assemble)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"whereCond"} and defined($c = $self->{"whereCond"}->assemble)
$self->{"whereCond"} defined($c = $self->{"whereCond"}->assemble)
Truth table0%0/3
A and B
A= $self->{"whereCond"}B= defined($c = $self->{"whereCond"}->assemble)
ABresult
0X0
100
111
1054 {
1055 0 $s = "WHERE $c\n";
1056 }
1057 0 return $s . $self->SUPER::_assemble();
1058 }
1059 ##------------------------------------------------------------------------------
1060 package sqlSelectJoin;
1061  
1062 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1063 1 ▶use base 'sqlSelectWhere';
Subroutine BEGIN: called CC=1 SCAR=0.0
1064  
1065 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
1066  
1067 sub new
1068 0 ▶ {
Subroutine new: not called CC=1 SCAR=6.9
1069 0 return sqlSelectAssemble::new(@_, 'sqlSelectFrom', joins => []);
1070 }
1071  
1072 sub gatherConditionArgs
1073 0 ▶ {
Subroutine gatherConditionArgs: not called CC=4 SCAR=30.0
1074 0 my $self = shift;
1075 0 my (@args);
1076  
1077 0 ▶ if ($self->isa('sqlSelectJoin')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa("sqlSelectJoin")) 0 0 0
1078 0 foreach my $join (@{$self->{joins}}) {
1079 0 my ($type,$table,$condition) = @$join;
1080 0 ▶ push @args, $condition->getBoundArgs()
Branch: 1 branch
BranchTrueFalseTotal
if ref $condition 0 0 0
1081 if ref $condition;
1082 }
1083 }
1084  
1085 0 return (@args, $self->SUPER::gatherConditionArgs())
1086 }
1087  
1088 0 ▶sub innerJoin {return shift->_addJoin('INNER', @_)}
Subroutine innerJoin: not called CC=1 SCAR=6.9
1089 0 ▶sub rightJoin {return shift->_addJoin('RIGHT', @_)}
Subroutine rightJoin: not called CC=1 SCAR=6.9
1090 0 ▶sub leftJoin {return shift->_addJoin('LEFT', @_)}
Subroutine leftJoin: not called CC=1 SCAR=6.9
1091  
1092 sub _addJoin
1093 0 ▶ {
Subroutine _addJoin: not called CC=1 SCAR=6.9
1094 0 my $self = shift;
1095 0 my ($type,$table,$condition) = @_;
1096 0 push @{$self->{joins}}, [$type, $table, $condition];
1097  
1098 0 return $self
1099 }
1100  
1101 sub _assemble
1102 0 ▶ {
Subroutine _assemble: not called CC=5 SCAR=34.0
1103 0 my $self = shift;
1104 0 my $s;
1105  
1106 0 ▶ unless ($self->isa('sqlSelectJoin')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa('sqlSelectJoin')) { } 0 0 0
1107 0 $s = ref $self;
1108 } else {
1109 0 $s = [];
1110  
1111 0 foreach my $join (@{$self->{joins}}) {
1112 0 my ($type, $table, $condition) = @$join;
1113 0 $table = sqlQuery::quoteTable($table);
1114 0 my $j = "$type JOIN $table ";
1115  
1116 0 ▶ unless (ref $condition) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $condition) { } 0 0 0
if (do { $condition->isa('sqlCondition') }) { } 0 0 0
1117 0 $j .= "USING(`$condition`)";
1118 } elsif ($condition->isa('sqlCondition')) {
1119 0 my $cond = $condition->assemble();
1120 0 $j .= "ON($cond)";
1121 } else {
1122 0 confess sprintf 'Cannot use argument "%s" as join condition', $condition;
1123 }
1124  
1125 0 push @$s, "$j\n";
1126 }
1127  
1128 0 $s = join '', @$s;
1129 }
1130  
1131 0 return $s . $self->SUPER::_assemble();
1132 }
1133 ##------------------------------------------------------------------------------
1134 package sqlCondition;
1135  
1136 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1137 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1138 1 ▶no if $] >= 5.017011, warnings => 'experimental::smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
1139 1 ▶use feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
1140 use overload
1141 1 ▶ '""' => 'assemble',
Subroutine BEGIN: called CC=1 SCAR=0.0
1142 '+' => 'overloadAdd',
1143 '!' => 'overloadNot',
1144 '&' => 'overloadAnd',
1145 1 '|' => 'overloadOr';
1146  
1147 1 ▶use constant TYPE_DEFAULT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1148 1 ▶use constant TYPE_CONNECT_AND => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
1149 1 ▶use constant TYPE_CONNECT_OR => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
1150 1 ▶use constant TYPE_UNARY_NOT => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
1151 1 ▶use Carp qw(confess cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
1152 1 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
1153 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
1154  
1155 sub new
1156 0 ▶ {
Subroutine new: not called CC=5 SCAR=34.0
1157 0 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
1158 0 my $self = bless{
1159 parent => undef,
1160 type => shift,
1161 _parts => undef,
1162 _condition => undef,
1163 _alterForNull => undef,
1164 _argument => undef,
1165 _queryArguments => []
1166 }, $class;
1167  
1168 0 ▶ if (TYPE_UNARY_NOT == $self->{type})
Branch: 1 branch
BranchTrueFalseTotal
if (4 == $self->{"type"}) 0 0 0
1169 {
1170 0 $self->{_argument} = shift;
1171 confess 'Invalid argument' unless
1172 0 ▶ ref $self->{_argument} && $self->{_argument}->isa('sqlCondition');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"_argument"} and $self->{"_argument"}->isa("sqlCondition") 0 0 0
Condition0%0/3
ref $self->{"_argument"} and $self->{"_argument"}->isa("sqlCondition")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"_argument"} and $self->{"_argument"}->isa("sqlCondition")
ref $self->{"_argument"} $self->{"_argument"}->isa("sqlCondition")
Truth table0%0/3
A and B
A= ref $self->{"_argument"}B= $self->{"_argument"}->isa("sqlCondition")
ABresult
0X0
100
111
1173 0 $self->{_argument}->setParent($self);
1174 }
1175  
1176 0 return $self
1177 }
1178  
1179 sub assemble
1180 0 ▶ {
Subroutine assemble: not called CC=5 SCAR=34.0
1181 0 my $self = shift;
1182  
1183 0 given($self->{type}) {
1184  
1185 0 when([TYPE_CONNECT_AND, TYPE_CONNECT_OR]) {
1186 0 ▶ return unless defined $self->{_parts};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_parts"} 0 0 0
1187 0 ▶ my ($glue) = (TYPE_CONNECT_AND == $self->{type} ? ' AND ' : ' OR ');
Branch: 1 branch
BranchTrueFalseTotal
2 == $self->{'type'} ? : 0 0 0
1188 0 return '('.join($glue, map {$_->assemble()} @{$self->{_parts}}).')';
1189 }
1190  
1191 0 when([TYPE_DEFAULT]) {
1192 return $self->{_condition}
1193 0 ▶ unless ref $self->{_condition} && $self->{_condition}->isa('sqlCondition');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"_condition"} and $self->{"_condition"}->isa("sqlCondition") 0 0 0
Condition0%0/3
ref $self->{"_condition"} and $self->{"_condition"}->isa("sqlCondition")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"_condition"} and $self->{"_condition"}->isa("sqlCondition")
ref $self->{"_condition"} $self->{"_condition"}->isa("sqlCondition")
Truth table0%0/3
A and B
A= ref $self->{"_condition"}B= $self->{"_condition"}->isa("sqlCondition")
ABresult
0X0
100
111
1194 0 return $self->{_condition}->assemble();
1195 }
1196  
1197 0 when([TYPE_UNARY_NOT]) {
1198 0 my $stmt = $self->{_argument}->assemble();
1199 0 return "NOT($stmt)";
1200 }
1201 }
1202  
1203 return
1204 0 }
1205  
1206 sub overloadAdd
1207 0 ▶ {
Subroutine overloadAdd: not called CC=2 SCAR=17.9
1208 0 my ($lhs,$rhs,$leftConstant) = @_;
1209  
1210 0 ▶ cluck 'sqlCondition + sqlCondition will modify the left operand'
Branch: 1 branch
BranchTrueFalseTotal
if defined $leftConstant 0 0 0
1211 if defined $leftConstant;
1212 0 return $lhs->add($rhs);
1213 }
1214  
1215 sub getOverloadArgs
1216 0 ▶ {
Subroutine getOverloadArgs: not called CC=8 SCAR=42.8
1217 0 my ($lhs,$rhs,$swap) = @_;
1218  
1219 0 ▶ ($lhs,$rhs) = ($rhs,$lhs) if $swap;
Branch: 1 branch
BranchTrueFalseTotal
if $swap 0 0 0
1220  
1221 0 ▶ $lhs = sqlCondition::C($lhs) unless ref $lhs;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $lhs 0 0 0
1222 0 ▶ $rhs = sqlCondition::C($rhs) unless ref $rhs;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rhs 0 0 0
1223  
1224 0 ▶ confess 'Illegal LHS operand' unless blessed($lhs) && $lhs->isa('sqlCondition');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($lhs) and $lhs->isa("sqlCondition") 0 0 0
Condition0%0/3
builtin::blessed($lhs) and $lhs->isa("sqlCondition")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($lhs) and $lhs->isa("sqlCondition")
builtin::blessed($lhs) $lhs->isa("sqlCondition")
Truth table0%0/3
A and B
A= builtin::blessed($lhs)B= $lhs->isa("sqlCondition")
ABresult
0X0
100
111
1225 0 ▶ confess 'Illegal RHS operand' unless blessed($rhs) && $rhs->isa('sqlCondition');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($rhs) and $rhs->isa("sqlCondition") 0 0 0
Condition0%0/3
builtin::blessed($rhs) and $rhs->isa("sqlCondition")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($rhs) and $rhs->isa("sqlCondition")
builtin::blessed($rhs) $rhs->isa("sqlCondition")
Truth table0%0/3
A and B
A= builtin::blessed($rhs)B= $rhs->isa("sqlCondition")
ABresult
0X0
100
111
1226  
1227 0 return ($lhs,$rhs);
1228 }
1229  
1230 sub overloadAnd
1231 0 ▶ {
Subroutine overloadAnd: not called CC=1 SCAR=6.9
1232 0 my ($lhs,$rhs) = getOverloadArgs(@_);
1233 0 return sqlCondition::AND($lhs, $rhs);
1234 }
1235  
1236 sub overloadNot
1237 0 ▶ {
Subroutine overloadNot: not called CC=1 SCAR=6.9
1238 0 return sqlCondition::NOT($_[0]);
1239 }
1240  
1241 sub overloadOr
1242 0 ▶ {
Subroutine overloadOr: not called CC=1 SCAR=6.9
1243 0 my ($lhs,$rhs) = getOverloadArgs(@_);
1244 0 return sqlCondition::OR($lhs, $rhs);
1245 }
1246  
1247 sub add
1248 0 ▶ {
Subroutine add: not called CC=3 SCAR=24.8
1249 0 my $self = shift;
1250 0 ▶ $self->{_parts} = [] unless defined $self->{_parts};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_parts"} 0 0 0
1251  
1252 0 push @{$self->{_parts}}, @_;
1253 0 $_->setParent($self) foreach @_;
1254  
1255 0 return $self
1256 }
1257  
1258 sub addSql
1259 0 ▶ {
Subroutine addSql: not called CC=1 SCAR=6.9
1260 0 my $self = shift;
1261 0 my $format = shift;
1262  
1263 0 return $self->add(C(sprintf $format, @_));
1264 }
1265  
1266 sub bind
1267 0 ▶ {
Subroutine bind: not called CC=6 SCAR=37.4
1268 0 my $self = shift;
1269  
1270 0 ▶ if (1 == scalar @_ && !defined $_[0] && defined $self->{_alterForNull}) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == scalar @_ and not defined $_[0] and defined $self->{"_alterForNull"}) 0 0 0
Condition0%0/6
1 == scalar @_ and not defined $_[0] and defined $self->{"_alterForNull"}
!ll&&!rl&&r
000
1 == scalar @_ and not defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
1 == scalar @_ and not defined $_[0] and defined $self->{"_alterForNull"}
1 == scalar @_ not defined $_[0] defined $self->{"_alterForNull"}
Truth table0%0/4
A and not(B) and C
A= 1 == scalar @_B= not defined $_[0]C= defined $self->{"_alterForNull"}
ABCresult
0XX0
10X0
1100
1111
1271 0 ($self->{_condition}) = (split / /, $self->{_condition}, 2);
1272 0 ▶ $self->{_condition} .= ' IS '.($self->{_alterForNull} ? '' : 'NOT ').'NULL';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_alterForNull'} ? : 0 0 0
1273 0 return $self;
1274 }
1275  
1276 $self->_bind(sqlQuery::convertArgument($_))
1277 0 foreach (@_);
1278 0 return $self
1279 }
1280  
1281 sub getBoundArgs
1282 0 ▶ {
Subroutine getBoundArgs: not called CC=1 SCAR=6.9
1283 0 return @{shift->{_queryArguments}};
1284 }
1285  
1286 sub releaseBoundArgs
1287 0 ▶ {
Subroutine releaseBoundArgs: not called CC=1 SCAR=6.9
1288 0 my $self = shift;
1289 0 my @args = $self->getBoundArgs();
1290 0 $self->{_queryArguments} = [];
1291 0 return @args;
1292 }
1293  
1294 0 ▶sub _OR {goto &OR}
Subroutine _OR: not called CC=1 SCAR=6.9
1295 sub OR
1296 0 ▶ {
Subroutine OR: not called CC=2 SCAR=17.9
1297 0 ▶ confess 'OR() expects at least 1 parameter.' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
1298 0 return connectedList(TYPE_CONNECT_OR, @_);
1299 }
1300  
1301 0 ▶sub _AND {goto &AND}
Subroutine _AND: not called CC=1 SCAR=6.9
1302 sub AND
1303 0 ▶ {
Subroutine AND: not called CC=2 SCAR=17.9
1304 0 ▶ confess 'AND() expects at least 1 parameter.' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
1305 0 return connectedList(TYPE_CONNECT_AND, @_);
1306 }
1307  
1308 sub NOT
1309 0 ▶ {
Subroutine NOT: not called CC=1 SCAR=6.9
1310 0 return sqlCondition->new(TYPE_UNARY_NOT, @_);
1311 }
1312  
1313 sub C
1314 0 ▶ {
Subroutine C: not called CC=2 SCAR=17.9
1315 0 my $cond = sqlCondition->new(TYPE_DEFAULT);
1316  
1317 0 ▶ if (1 == scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == scalar @_) { } 0 0 0
1318 0 $cond->{_condition} = shift;
1319 } else {
1320 0 $cond->{_condition} = sprintf $_[0], @_[1..$#_];
1321 }
1322  
1323 0 return $cond
1324 }
1325  
1326 sub IN
1327 0 ▶ {
Subroutine IN: not called CC=1 SCAR=6.9
1328 0 my $column = shift;
1329 0 return C("%s IN($sqlQuery::PARAMETER_PLACEHOLDER)", sqlQuery::quoteWhenTable($column));
1330 }
1331  
1332 sub NOTIN
1333 0 ▶ {
Subroutine NOTIN: not called CC=1 SCAR=6.9
1334 0 my $column = shift;
1335 0 return C("%s NOT IN($sqlQuery::PARAMETER_PLACEHOLDER)", sqlQuery::quoteWhenTable($column));
1336 }
1337  
1338 sub LIKE
1339 0 ▶ {
Subroutine LIKE: not called CC=1 SCAR=6.9
1340 0 my ($column,$pattern) = validate_pos @_,
1341 {column => {type => SCALAR}},
1342 {pattern => {type => SCALAR}};
1343  
1344 0 $pattern =~ s/"/"\"/g;
1345 0 $column = sqlQuery::quoteWhenTable($column);
1346 0 return C("$column LIKE \"$pattern\"");
1347 }
1348  
1349 sub BETWEEN
1350 0 ▶ {
Subroutine BETWEEN: not called CC=1 SCAR=6.9
1351 0 my ($column,$start,$end) = validate_pos @_,
1352 {column => {type => SCALAR}},
1353 {start => {isa => 'sqlParameter'}},
1354 {end => {isa => 'sqlParameter'}};
1355 0 $column = sqlQuery::quoteWhenTable($column);
1356  
1357 0 return C("$column BETWEEN $sqlQuery::PARAMETER_PLACEHOLDER AND $sqlQuery::PARAMETER_PLACEHOLDER")
1358 ->bind($start)->bind($end);
1359 }
1360  
1361 sub ISNULL
1362 0 ▶ {
Subroutine ISNULL: not called CC=1 SCAR=6.9
1363 0 my ($column) = validate_pos @_,
1364 {column => {type => SCALAR}};
1365 0 $column = sqlQuery::quoteWhenTable($column);
1366  
1367 0 return C("$column IS NULL")
1368 }
1369  
1370 sub ISNOTNULL
1371 0 ▶ {
Subroutine ISNOTNULL: not called CC=1 SCAR=6.9
1372 0 my ($column) = validate_pos @_,
1373 {column => {type => SCALAR}};
1374 0 $column = sqlQuery::quoteWhenTable($column);
1375  
1376 0 return C("$column IS NOT NULL")
1377 }
1378  
1379 sub EQ
1380 0 ▶ {
Subroutine EQ: not called CC=1 SCAR=6.9
1381 0 my $cond = _OP('=', @_);
1382 0 $cond->{_alterForNull} = 1;
1383 0 return $cond
1384 }
1385  
1386 sub NE
1387 0 ▶ {
Subroutine NE: not called CC=1 SCAR=6.9
1388 0 my $cond = _OP('!=', @_);
1389 0 $cond->{_alterForNull} = 0;
1390 0 return $cond
1391 }
1392  
1393 0 ▶sub LT {return _OP('<', @_)}
Subroutine LT: not called CC=1 SCAR=6.9
1394 0 ▶sub GT {return _OP('>', @_)}
Subroutine GT: not called CC=1 SCAR=6.9
1395 0 ▶sub LTE {return _OP('<=', @_)}
Subroutine LTE: not called CC=1 SCAR=6.9
1396 0 ▶sub GTE {return _OP('>=', @_)}
Subroutine GTE: not called CC=1 SCAR=6.9
1397  
1398 sub _OP
1399 0 ▶ {
Subroutine _OP: not called CC=2 SCAR=17.9
1400 0 my ($operator, $lhs, $rhs) = @_;
1401 0 ▶ return C('%s %s %s',
Branch: 1 branch
BranchTrueFalseTotal
3 != scalar @_ ? : 0 0 0
1402 sqlQuery::quoteWhenTable($lhs),
1403 $operator,
1404 3 != scalar @_
1405 ? $sqlQuery::PARAMETER_PLACEHOLDER
1406 : sqlQuery::quoteWhenTable($rhs));
1407 }
1408  
1409 sub connectedList
1410 0 ▶ {
Subroutine connectedList: not called CC=6 SCAR=37.4
1411 0 my $type = shift;
1412 0 my $cond = sqlCondition->new($type);
1413  
1414 0 foreach my $a (@_) {
1415 0 ▶ unless (blessed($a) && $a->isa('sqlCondition')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($a) and $a->isa("sqlCondition")) 0 0 0
Condition0%0/3
builtin::blessed($a) and $a->isa("sqlCondition")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($a) and $a->isa("sqlCondition")
builtin::blessed($a) $a->isa("sqlCondition")
Truth table0%0/3
A and B
A= builtin::blessed($a)B= $a->isa("sqlCondition")
ABresult
0X0
100
111
1416 0 $cond->insert($a);
1417 0 next;
1418 }
1419  
1420 0 ▶ if ($a->{type} != $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->{"type"} != $type) 0 0 0
1421 0 $cond->insert($a);
1422 0 next;
1423 }
1424  
1425 0 $cond->_bind($_) foreach $a->releaseBoundArgs();
1426 0 $cond->insert(@{$a->{_parts}});
1427 }
1428  
1429 0 return $cond
1430 }
1431  
1432 sub insert
1433 0 ▶ {
Subroutine insert: not called CC=2 SCAR=17.9
1434 0 my $self = shift;
1435  
1436 0 ▶ $self->{_parts} = [] unless defined $self->{_parts};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_parts"} 0 0 0
1437 0 return $self->add(@_);
1438 }
1439  
1440 sub _bind
1441 0 ▶ {
Subroutine _bind: not called CC=3 SCAR=24.8
1442 0 my $self = shift;
1443 0 my ($parameter) = validate_pos @_,
1444 {parameter => {isa => 'sqlParameter'}};
1445  
1446 0 push @{$self->{_queryArguments}}, $parameter
1447 0 ▶ unless defined $self->{parent};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"parent"} 0 0 0
1448 $self->{parent}->up()->_bind($parameter)
1449 0 ▶ if defined $self->{parent};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"parent"} 0 0 0
1450 0 return $self
1451 }
1452  
1453 sub setParent
1454 0 ▶ {
Subroutine setParent: not called CC=2 SCAR=17.9
1455 0 my $self = shift;
1456 0 my ($parent) = validate_pos @_,
1457 {parameter => {isa => 'sqlCondition'}};
1458  
1459 0 $self->{parent} = $parent;
1460 $self->{parent}->up()->_bind($_)
1461 0 foreach @{$self->{_queryArguments}};
1462 0 $self->{_queryArguments} = [];
1463 0 return $self
1464 }
1465  
1466 sub up
1467 0 ▶ {
Subroutine up: not called CC=2 SCAR=17.9
1468 0 my $self = shift;
1469  
1470 return $self
1471 0 ▶ unless defined $self->{parent};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"parent"} 0 0 0
1472 0 return $self->{parent}->up();
1473 }
1474  
1475 1 1;
1476 __END__
↑ Summary