SQL/Abstract.pm

filter: Statementstmt 77.9% 796 / 1021 Branchbran 73.8% 421 / 570 Conditioncond 71.6% 225 / 314 MC/DCmcdc 51.8% 85 / 164 Subroutinesub 74.7% 151 / 202 Podpod 96.6% 58 / 60 totaltotal 74.4% 1736 / 2331 CC 419 SCAR 33.2
CC
419
Cov
76%
new
305.8
generate
210.0
__ANON__
110.0
CRAP 27.7
« SQL/Abstract/Tree.pm ↑ Summary SQL/Abstract/Test.pm »
1 package SQL::Abstract; # see doc at end of file
2  
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 16 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 our @EXPORT_OK = qw(is_plain_value is_literal_value is_undef_value);
11  
12 16 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.5
13 16 ▶ if ($] < 5.009_005) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.009005) { } 0 16 16
14 0 require MRO::Compat;
15 }
16 else {
17 16 require mro;
18 }
19  
20 *SQL::Abstract::_ENV_::DETECT_AUTOGENERATED_STRINGIFICATION = $ENV{SQLA_ISVALUE_IGNORE_AUTOGENERATED_STRINGIFICATION}
21 ? sub () { 0 }
22 : sub () { 1 }
23 16 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'SQLA_ISVALUE_IGNORE_AUTOGENERATED_STRINGIFICATION'} ? : 0 16 16
24 }
25  
26 #======================================================================
27 # GLOBALS
28 #======================================================================
29  
30 16 our $VERSION = '2.000001';
31  
32 # This would confuse some packagers
33 16 ▶$VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
Branch: 1 branch
BranchTrueFalseTotal
if $SQL::Abstract::VERSION =~ /_/ 0 16 16
34  
35 16 our $AUTOLOAD;
36  
37 # special operators (-in, -between). May be extended/overridden by user.
38 # See section WHERE: BUILTIN SPECIAL OPERATORS below for implementation
39 my @BUILTIN_SPECIAL_OPS = (
40 0 ▶ {regex => qr/^ (?: not \s )? between $/ix, handler => sub { die "NOPE" }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 ▶ {regex => qr/^ is (?: \s+ not )? $/ix, handler => sub { die "NOPE" }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 ▶ {regex => qr/^ (?: not \s )? in $/ix, handler => sub { die "NOPE" }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 0 ▶ {regex => qr/^ ident $/ix, handler => sub { die "NOPE" }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 ▶ {regex => qr/^ value $/ix, handler => sub { die "NOPE" }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 16 );
46  
47 #======================================================================
48 # DEBUGGING AND ERROR REPORTING
49 #======================================================================
50  
51 46 ▶sub _debug {
Subroutine _debug: called CC=2 SCAR=11.6
52 0 ▶ return unless $_[0]->{debug}; shift; # a little faster
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"debug"} 46 0 46
53 0 my $func = (caller(1))[3];
54 0 warn "[$func] ", @_, "\n";
55 }
56  
57 152 ▶sub belch (@) {
Subroutine belch: called CC=1 SCAR=0.0
Pod undocumented
58 152 my($func) = (caller(1))[3];
59 152 Carp::carp "[$func] Warning: ", @_;
60 }
61  
62 116 ▶sub puke (@) {
Subroutine puke: called CC=1 SCAR=0.0
Pod undocumented
63 116 my($func) = (caller(1))[3];
64 116 Carp::croak "[$func] Fatal: ", @_;
65 }
66  
67 2165 ▶sub is_literal_value ($) {
Subroutine is_literal_value: called CC=4 SCAR=13.9
Pod documented
68 97 ▶ ref $_[0] eq 'SCALAR' ? [ ${$_[0]} ]
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'REF' && ref ${$_[0];} eq 'ARRAY' ? : 126 1942 2068
Condition66%2/3
ref $_[0] eq "REF" and ref ${$_[0];} eq "ARRAY"
!ll&&!rl&&r
19420126
MC/DC50%1/2
ref $_[0] eq "REF" and ref ${$_[0];} eq "ARRAY"
ref $_[0] eq "REF" ref ${$_[0];} eq "ARRAY"
Truth table66%2/3
A and B
A= ref $_[0] eq "REF"B= ref ${$_[0];} eq "ARRAY"
ABresult
0X0
100
111
69 2165 ▶ : ( ref $_[0] eq 'REF' and ref ${$_[0]} eq 'ARRAY' ) ? [ @${ $_[0] } ]
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'SCALAR' ? : 97 2068 2165
70 : undef;
71 }
72  
73 2119 ▶sub is_undef_value ($) {
Subroutine is_undef_value: called CC=4 SCAR=13.9
Pod documented
74 !defined($_[0])
75 or (
76 ref($_[0]) eq 'HASH'
77 and exists $_[0]->{-value}
78 and not defined $_[0]->{-value}
79 2119 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
not defined $_[0] or ref $_[0] eq "HASH" and exists $_[0]{"-value"} and not defined $_[0]{"-value"} 99 2020 2119
Condition100%9/9
!defined($_[0]) || ref $_[0] eq 'HASH' && exists $_[0]{'-value'} && !defined($_[0]{'-value'})
l!l&&r!l&&!r
99241996
ref $_[0] eq "HASH" and exists $_[0]{"-value"} and not defined $_[0]{"-value"}
!ll&&!rl&&r
19811524
ref $_[0] eq "HASH" and exists $_[0]{"-value"}
!ll&&!rl&&r
771121039
MC/DC100%4/4
!defined($_[0]) || ref $_[0] eq 'HASH' && exists $_[0]{'-value'} && !defined($_[0]{'-value'})
!defined($_[0]) ref $_[0] eq "HASH" exists $_[0]{"-value"} not defined $_[0]{"-value"}
Truth table100%5/5
not(A) || B and C and not(D)
A= !defined($_[0])B= ref $_[0] eq "HASH"C= exists $_[0]{"-value"}D= not defined $_[0]{"-value"}
ABCDresult
1XXX1
01111
00XX0
010X0
01100
80 }
81  
82 # FIXME XSify - this can be done so much more efficiently
83 47 ▶sub is_plain_value ($) {
Subroutine is_plain_value: called CC=12 SCAR=24.9
Pod documented
84 16 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 ! length ref $_[0] ? \($_[0])
86 : (
87 3 ▶ ref $_[0] eq 'HASH' and keys %{$_[0]} == 1
Branch: 1 branch
BranchTrueFalseTotal
defined($_[1] = builtin::blessed($_[0])) && (grep({*{qq[${_}::(""];}{'CODE'};} @{$_[2] = &mro::get_linear_isa($_[1]);}) || (grep({*{"${_}::(0+";}{'CODE'};} @{$_[2];}) || grep({*{"${_}::(bool";}{'CODE'};} @{$_[2];})) && !((($_[3]) = grep({*{"${_}::()";}{'CODE'};} @{$_[2];})) && defined ${"$_[3]::()";} && !${"$_[3]::()";})) ? : 32 11 43
Condition100%6/6
defined($_[1] = builtin::blessed($_[0])) and grep({*{qq[${_}::(""];}{"CODE"};} @{$_[2] = &mro::get_linear_isa($_[1]);}) || (grep({*{"${_}::(0+";}{"CODE"};} @{$_[2];}) || grep({*{"${_}::(bool";}{"CODE"};} @{$_[2];})) && !((($_[3]) = grep({*{"${_}::()";}{"CODE"};} @{$_[2];})) && defined ${"$_[3]::()";} && !${"$_[3]::()";})
!ll&&!rl&&r
3832
grep {*{qq[${_}::(""];}{"CODE"};} @{$_[2] = &mro::get_linear_isa($_[1]);} or grep({*{"${_}::(0+";}{"CODE"};} @{$_[2];}) || grep({*{"${_}::(bool";}{"CODE"};} @{$_[2];}) and not +(($_[3]) = grep({*{"${_}::()";}{"CODE"};} @{$_[2];})) && defined ${"$_[3]::()";} && !${"$_[3]::()";}
l!l&&r!l&&!r
8248
MC/DC100%3/3
defined($_[1] = builtin::blessed($_[0])) and grep({*{qq[${_}::(""];}{"CODE"};} @{$_[2] = &mro::get_linear_isa($_[1]);}) || (grep({*{"${_}::(0+";}{"CODE"};} @{$_[2];}) || grep({*{"${_}::(bool";}{"CODE"};} @{$_[2];})) && !((($_[3]) = grep({*{"${_}::()";}{"CODE"};} @{$_[2];})) && defined ${"$_[3]::()";} && !${"$_[3]::()";})
defined($_[1] = builtin::blessed($_[0])) grep {*{qq[${_}::(""];}{"CODE"};} @{$_[2] = &mro::get_linear_isa($_[1]);} grep({*{"${_}::(0+";}{"CODE"};} @{$_[2];}) || grep({*{"${_}::(bool";}{"CODE"};} @{$_[2];}) and not +(($_[3]) = grep({*{"${_}::()";}{"CODE"};} @{$_[2];})) && defined ${"$_[3]::()";} && !${"$_[3]::()";}
Truth table100%4/4
A and B or C
A= defined($_[1] = builtin::blessed($_[0]))B= grep {*{qq[${_}::(""];}{"CODE"};} @{$_[2] = &mro::get_linear_isa($_[1]);}C= grep({*{"${_}::(0+";}{"CODE"};} @{$_[2];}) || grep({*{"${_}::(bool";}{"CODE"};} @{$_[2];}) and not +(($_[3]) = grep({*{"${_}::()";}{"CODE"};} @{$_[2];})) && defined ${"$_[3]::()";} && !${"$_[3]::()";}
ABCresult
0XX0
1000
11X1
1011
88 and
89 exists $_[0]->{-value}
90 ) ? \($_[0]->{-value})
91 : (
92 # reuse @_ for even moar speedz
93 defined ( $_[1] = Scalar::Util::blessed $_[0] )
94 and
95 # deliberately not using Devel::OverloadInfo - the checks we are
96 # intersted in are much more limited than the fullblown thing, and
97 # this is a very hot piece of code
98 (
99 # simply using ->can('(""') can leave behind stub methods that
100 # break actually using the overload later (see L<perldiag/Stub
101 # found while resolving method "%s" overloading "%s" in package
102 # "%s"> and the source of overload::mycan())
103 #
104 # either has stringification which DBI SHOULD prefer out of the box
105 60 ▶ grep { *{ (qq[${_}::(""]) }{CODE} } @{ $_[2] = mro::get_linear_isa( $_[1] ) }
Condition100%6/6
grep({*{"${_}::(0+";}{"CODE"};} @{$_[2];}) || grep({*{"${_}::(bool";}{"CODE"};} @{$_[2];}) and not +(($_[3]) = grep({*{"${_}::()";}{"CODE"};} @{$_[2];})) && defined ${"$_[3]::()";} && !${"$_[3]::()";}
!ll&&!rl&&r
2624
grep {*{"${_}::(0+";}{"CODE"};} @{$_[2];} or grep {*{"${_}::(bool";}{"CODE"};} @{$_[2];}
l!l&&r!l&&!r
2282
MC/DC0%0/3
grep({*{"${_}::(0+";}{"CODE"};} @{$_[2];}) || grep({*{"${_}::(bool";}{"CODE"};} @{$_[2];}) and not +(($_[3]) = grep({*{"${_}::()";}{"CODE"};} @{$_[2];})) && defined ${"$_[3]::()";} && !${"$_[3]::()";}
grep {*{"${_}::(0+";}{"CODE"};} @{$_[2];} grep {*{"${_}::(bool";}{"CODE"};} @{$_[2];} not +(($_[3]) = grep({*{"${_}::()";}{"CODE"};} @{$_[2];})) && defined ${"$_[3]::()";} && !${"$_[3]::()";}
Truth table0%0/5
A or B and not(C)
A= grep {*{"${_}::(0+";}{"CODE"};} @{$_[2];}B= grep {*{"${_}::(bool";}{"CODE"};} @{$_[2];}C= not +(($_[3]) = grep({*{"${_}::()";}{"CODE"};} @{$_[2];})) && defined ${"$_[3]::()";} && !${"$_[3]::()";}
ABCresult
00X0
1X00
0100
1X11
0111
106 or
107 # has nummification or boolification, AND fallback is *not* disabled
108 (
109 SQL::Abstract::_ENV_::DETECT_AUTOGENERATED_STRINGIFICATION
110 and
111 (
112 48 grep { *{"${_}::(0+"}{CODE} } @{$_[2]}
113 or
114 14 ▶ grep { *{"${_}::(bool"}{CODE} } @{$_[2]}
Condition83%5/6
(($_[3]) = grep({*{"${_}::()";}{'CODE'};} @{$_[2];})) && defined ${"$_[3]::()";} && !${"$_[3]::()";}
!ll&&!rl&&r
1686
($_[3]) = grep({*{"${_}::()";}{"CODE"};} @{$_[2];}) and defined ${"$_[3]::()";}
!ll&&!rl&&r
16014
MC/DC0%0/3
(($_[3]) = grep({*{"${_}::()";}{'CODE'};} @{$_[2];})) && defined ${"$_[3]::()";} && !${"$_[3]::()";}
($_[3]) = grep({*{"${_}::()";}{"CODE"};} @{$_[2];}) defined ${"$_[3]::()";} !${"$_[3]::()";}
Truth table0%0/4
A and B && not(C)
A= ($_[3]) = grep({*{"${_}::()";}{"CODE"};} @{$_[2];})B= defined ${"$_[3]::()";}C= !${"$_[3]::()";}
ABCresult
0XX0
10X0
1100
1111
115 )
116 and
117 (
118 # no fallback specified at all
119 46 ! ( ($_[3]) = grep { *{"${_}::()"}{CODE} } @{$_[2]} )
120 or
121 # fallback explicitly undef
122 14 ! defined ${"$_[3]::()"}
123 or
124 # explicitly true
125 47 ▶ !! ${"$_[3]::()"}
Branch: 2 branches
BranchTrueFalseTotal
!length(ref $_[0]) ? : 2 45 47
ref $_[0] eq 'HASH' && keys %{$_[0];} == 1 && exists $_[0]{'-value'} ? : 2 43 45
Condition83%5/6
ref $_[0] eq "HASH" and keys %{$_[0];} == 1 and exists $_[0]{"-value"}
!ll&&!rl&&r
4302
ref $_[0] eq "HASH" and keys %{$_[0];} == 1
!ll&&!rl&&r
4212
MC/DC66%2/3
ref $_[0] eq "HASH" and keys %{$_[0];} == 1 and exists $_[0]{"-value"}
ref $_[0] eq "HASH" keys %{$_[0];} == 1 exists $_[0]{"-value"}
Truth table75%3/4
A and B and C
A= ref $_[0] eq "HASH"B= keys %{$_[0];} == 1C= exists $_[0]{"-value"}
ABCresult
0XX0
10X0
1100
1111
126 )
127 )
128 )
129 ) ? \($_[0])
130 : undef;
131 }
132  
133  
134  
135 #======================================================================
136 # NEW
137 #======================================================================
138  
139 our %Defaults = (
140 expand => {
141 bool => '_expand_bool',
142 row => '_expand_row',
143 op => '_expand_op',
144 func => '_expand_func',
145 values => '_expand_values',
146 list => '_expand_list',
147 },
148 expand_op => {
149 (map +($_ => __PACKAGE__->make_binop_expander('_expand_between')),
150 qw(between not_between)),
151 (map +($_ => __PACKAGE__->make_binop_expander('_expand_in')),
152 qw(in not_in)),
153 (map +($_ => '_expand_op_andor'), ('and', 'or')),
154 (map +($_ => '_expand_op_is'), ('is', 'is_not')),
155 (map +($_ => __PACKAGE__->make_unop_expander("_expand_${_}")),
156 qw(ident value nest)),
157 569 ▶ bind => __PACKAGE__->make_unop_expander(sub { +{ -bind => $_[2] } }),
Subroutine __ANON__: called CC=1 SCAR=0.0
158 },
159 render => {
160 (map +($_, "_render_$_"),
161 qw(op func bind ident literal row values keyword)),
162 },
163 render_op => {
164 (map +($_ => '_render_op_between'), 'between', 'not_between'),
165 (map +($_ => '_render_op_in'), 'in', 'not_in'),
166 (map +($_ => '_render_unop_postfix'),
167 'is_null', 'is_not_null', 'asc', 'desc',
168 ),
169 (not => '_render_unop_paren'),
170 (map +($_ => '_render_op_andor'), qw(and or)),
171 ',' => '_render_op_multop',
172 },
173 clauses_of => {
174 delete => [ qw(target where returning) ],
175 update => [ qw(target set where returning) ],
176 insert => [ qw(target fields from returning) ],
177 select => [ qw(select from where order_by) ],
178 },
179 expand_clause => {
180 'delete.from' => '_expand_delete_clause_target',
181 'update.update' => '_expand_update_clause_target',
182 'insert.into' => '_expand_insert_clause_target',
183 'insert.values' => '_expand_insert_clause_from',
184 },
185 render_clause => {
186 'delete.target' => '_render_delete_clause_target',
187 'update.target' => '_render_update_clause_target',
188 'insert.target' => '_render_insert_clause_target',
189 'insert.fields' => '_render_insert_clause_fields',
190 'insert.from' => '_render_insert_clause_from',
191 },
192 );
193  
194 16 foreach my $stmt (keys %{$Defaults{clauses_of}}) {
195 64 $Defaults{expand}{$stmt} = '_expand_statement';
196 64 $Defaults{render}{$stmt} = '_render_statement';
197 64 foreach my $clause (@{$Defaults{clauses_of}{$stmt}}) {
198 240 $Defaults{expand_clause}{"${stmt}.${clause}"}
199 = "_expand_${stmt}_clause_${clause}";
200 }
201 }
202  
203 775 ▶sub new {
Subroutine new: called CC=29 SCAR=57.2
Pod documented
204 775 my $self = shift;
205 775 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
07750
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
206 775 ▶ my %opt = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 249 526 775
207  
208 # choose our case by keeping an option around
209 775 ▶ delete $opt{case} if $opt{case} && $opt{case} ne 'lower';
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"case"} and $opt{"case"} ne "lower" 2 773 775
Condition100%3/3
$opt{"case"} and $opt{"case"} ne "lower"
!ll&&!rl&&r
76762
MC/DC100%2/2
$opt{"case"} and $opt{"case"} ne "lower"
$opt{"case"} $opt{"case"} ne "lower"
Truth table100%3/3
A and B
A= $opt{"case"}B= $opt{"case"} ne "lower"
ABresult
0X0
100
111
210  
211 # default logic for interpreting arrayrefs
212 775 ▶ $opt{logic} = $opt{logic} ? uc $opt{logic} : 'OR';
Branch: 1 branch
BranchTrueFalseTotal
$opt{'logic'} ? : 8 767 775
213  
214 # how to return bind vars
215 775 ▶ $opt{bindtype} ||= 'normal';
Condition100%2/2
$opt{"bindtype"} ||= "normal"
l!l
54721
MC/DC100%1/1
$opt{"bindtype"} ||= "normal"
$opt{"bindtype"}
Truth table100%2/2
A
A= $opt{"bindtype"}
Aresult
11
00
216  
217 # default comparison is "=", but can be overridden
218 775 ▶ $opt{cmp} ||= '=';
Condition100%2/2
$opt{"cmp"} ||= "="
l!l
9766
MC/DC100%1/1
$opt{"cmp"} ||= "="
$opt{"cmp"}
Truth table100%2/2
A
A= $opt{"cmp"}
Aresult
11
00
219  
220 # try to recognize which are the 'equality' and 'inequality' ops
221 # (temporary quickfix (in 2007), should go through a more seasoned API)
222 775 $opt{equality_op} = qr/^( \Q$opt{cmp}\E | \= )$/ix;
223 775 $opt{inequality_op} = qr/^( != | <> )$/ix;
224  
225 775 $opt{like_op} = qr/^ (is_)?r?like $/xi;
226 775 $opt{not_like_op} = qr/^ (is_)?not_r?like $/xi;
227  
228 # SQL booleans
229 775 ▶ $opt{sqltrue} ||= '1=1';
Condition50%1/2
$opt{"sqltrue"} ||= "1=1"
l!l
0775
MC/DC0%0/1
$opt{"sqltrue"} ||= "1=1"
$opt{"sqltrue"}
Truth table50%1/2
A
A= $opt{"sqltrue"}
Aresult
11
00
230 775 ▶ $opt{sqlfalse} ||= '0=1';
Condition50%1/2
$opt{"sqlfalse"} ||= "0=1"
l!l
0775
MC/DC0%0/1
$opt{"sqlfalse"} ||= "0=1"
$opt{"sqlfalse"}
Truth table50%1/2
A
A= $opt{"sqlfalse"}
Aresult
11
00
231  
232 # special operators
233 775 ▶ $opt{special_ops} ||= [];
Condition100%2/2
$opt{"special_ops"} ||= []
l!l
1774
MC/DC100%1/1
$opt{"special_ops"} ||= []
$opt{"special_ops"}
Truth table100%2/2
A
A= $opt{"special_ops"}
Aresult
11
00
234  
235 # unary operators
236 775 ▶ $opt{unary_ops} ||= [];
Condition100%2/2
$opt{"unary_ops"} ||= []
l!l
1774
MC/DC100%1/1
$opt{"unary_ops"} ||= []
$opt{"unary_ops"}
Truth table100%2/2
A
A= $opt{"unary_ops"}
Aresult
11
00
237  
238 # rudimentary sanity-check for user supplied bits treated as functions/operators
239 # If a purported function matches this regular expression, an exception is thrown.
240 # Literal SQL is *NOT* subject to this check, only functions (and column names
241 # when quoting is not in effect)
242  
243 # FIXME
244 # need to guard against ()'s in column names too, but this will break tons of
245 # hacks... ideas anyone?
246 775 ▶ $opt{injection_guard} ||= qr/
Condition50%1/2
$opt{"injection_guard"} ||= qr/ \; | ^ \s* go \s /mix
l!l
0775
MC/DC0%0/1
$opt{"injection_guard"} ||= qr/ \; | ^ \s* go \s /mix
$opt{"injection_guard"}
Truth table50%1/2
A
A= $opt{"injection_guard"}
Aresult
11
00
247 \;
248 |
249 ^ \s* go \s
250 /xmi;
251  
252 775 $opt{expand_unary} = {};
253  
254 775 foreach my $name (sort keys %Defaults) {
255 5425 ▶ $opt{$name} = { %{$Defaults{$name}}, %{$opt{$name}||{}} };
Condition50%1/2
$opt{$name} || {}
l!l
05425
MC/DC0%0/1
$opt{$name} || {}
$opt{$name}
Truth table50%1/2
A
A= $opt{$name}
Aresult
11
00
256 }
257  
258 775 ▶ if ($class ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class ne __PACKAGE__) 0 775 775
259  
260 # check for overriden methods
261  
262 0 foreach my $type (qw(insert update delete)) {
263 0 my $method = "_${type}_returning";
264 0 ▶ if (__PACKAGE__->can($method) ne $class->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if (__PACKAGE__->can($method) ne $class->can($method)) 0 0 0
265 0 my $clause = "${type}.returning";
266 0 ▶ $opt{expand_clause}{$clause} = sub { $_[2] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
267 $opt{render_clause}{$clause}
268 0 ▶ = sub { [ $_[0]->$method($_[3]) ] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
269 }
270 }
271 0 ▶ if (__PACKAGE__->can('_table') ne $class->can('_table')) {
Branch: 1 branch
BranchTrueFalseTotal
if (__PACKAGE__->can("_table") ne $class->can("_table")) 0 0 0
272 0 ▶ $opt{expand_clause}{'select.from'} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
273 0 return +{ -literal => [ $_[0]->_table($_[2]) ] };
274 0 };
275 }
276 0 ▶ if (__PACKAGE__->can('_order_by') ne $class->can('_order_by')) {
Branch: 1 branch
BranchTrueFalseTotal
if (__PACKAGE__->can("_order_by") ne $class->can("_order_by")) 0 0 0
277 0 ▶ $opt{expand_clause}{'select.order_by'} = sub { $_[2] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
278 0 ▶ $opt{render_clause}{'select.order_by'} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
279 0 [ $_[0]->_order_by($_[2]) ];
280 0 };
281 }
282 0 ▶ if (__PACKAGE__->can('_select_fields') ne $class->can('_select_fields')) {
Branch: 1 branch
BranchTrueFalseTotal
if (__PACKAGE__->can("_select_fields") ne $class->can("_select_fields")) 0 0 0
283 0 ▶ $opt{expand_clause}{'select.select'} = sub { $_[2] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
284 0 ▶ $opt{render_clause}{'select.select'} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
285 0 my @super = $_[0]->_select_fields($_[2]);
286 0 ▶ my $effort = [
Branch: 1 branch
BranchTrueFalseTotal
ref $super[0] eq 'HASH' ? : 0 0 0
287 ref($super[0]) eq 'HASH'
288 ? $_[0]->render_expr($super[0])
289 : @super
290 ];
291 0 return $_[0]->join_query_parts(
292 ' ', { -keyword => 'select' }, $effort
293 );
294 0 };
295 }
296 0 foreach my $type (qw(in between)) {
297 0 my $meth = "_where_field_".uc($type);
298 0 ▶ if (__PACKAGE__->can($meth) ne $class->can($meth)) {
Branch: 1 branch
BranchTrueFalseTotal
if (__PACKAGE__->can($meth) ne $class->can($meth)) 0 0 0
299 0 ▶ my $exp = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
300 0 my ($self, $op, $v, $k) = @_;
301 0 $op = join ' ', split '_', $op;
302 0 return +{ -literal => [
303 $self->$meth($k, $op, $v)
304 ] };
305 0 };
306 0 $opt{expand_op}{$_} = $exp for $type, "not_${type}";
307 }
308 }
309 0 ▶ if ($class->isa('DBIx::Class::SQLMaker')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->isa("DBIx::Class::SQLMaker")) 0 0 0
310 0 $opt{warn_once_on_nest} = 1;
311 0 $opt{disable_old_special_ops} = 1;
312 0 ▶ $opt{render_clause}{'select.where'} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
313 0 my ($sql, @bind) = $_[0]->where($_[2]);
314 0 s/\A\s+//, s/\s+\Z// for $sql;
315 0 return [ $sql, @bind ];
316 0 };
317 0 ▶ $opt{expand_op}{ident} = $class->make_unop_expander(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
318 0 my ($self, undef, $body) = @_;
319 0 ▶ $body = $body->from if Scalar::Util::blessed($body);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($body) 0 0 0
320 0 $self->_expand_ident(ident => $body);
321 0 });
322 }
323 0 ▶ if ($class->isa('SQL::Abstract::More')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->isa("SQL::Abstract::More")) 0 0 0
324 0 my $orig = $opt{expand_op}{or};
325 0 ▶ $opt{expand_op}{or} = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
326 0 my ($self, $logop, $v, $k) = @_;
327 0 ▶ if ($k and ref($v) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k and ref $v eq "ARRAY") 0 0 0
Condition0%0/3
$k and ref $v eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$k and ref $v eq "ARRAY"
$k ref $v eq "ARRAY"
Truth table0%0/3
A and B
A= $kB= ref $v eq "ARRAY"
ABresult
0X0
100
111
328 0 my ($type, $val) = @$v;
329 0 my $op;
330 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq "HASH" and ref $val eq "HASH" and keys %$type == 1 and keys %$val == 1 and (keys %$type)[0] eq (keys %$val)[0]) 0 0 0
Condition0%0/12
ref $type eq "HASH" and ref $val eq "HASH" and keys %$type == 1 and keys %$val == 1 and (keys %$type)[0] eq (keys %$val)[0]
!ll&&!rl&&r
000
ref $type eq "HASH" and ref $val eq "HASH" and keys %$type == 1 and keys %$val == 1
!ll&&!rl&&r
000
ref $type eq "HASH" and ref $val eq "HASH" and keys %$type == 1
!ll&&!rl&&r
000
ref $type eq "HASH" and ref $val eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/5
ref $type eq "HASH" and ref $val eq "HASH" and keys %$type == 1 and keys %$val == 1 and (keys %$type)[0] eq (keys %$val)[0]
ref $type eq "HASH" ref $val eq "HASH" keys %$type == 1 keys %$val == 1 (keys %$type)[0] eq (keys %$val)[0]
Truth table0%0/6
A and B and C and D and E
A= ref $type eq "HASH"B= ref $val eq "HASH"C= keys %$type == 1D= keys %$val == 1E= (keys %$type)[0] eq (keys %$val)[0]
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
331 ref($type) eq 'HASH' and ref($val) eq 'HASH'
332 and keys %$type == 1 and keys %$val == 1
333 and (keys %$type)[0] eq (keys %$val)[0]
334 ) {
335 0 ($op) = keys %$type;
336 0 ($type) = values %$type;
337 0 ($val) = values %$val;
338 }
339 0 ▶ if ($self->is_bind_value_with_type(my $v = [ $type, $val ])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_bind_value_with_type(my $v = [$type, $val])) 0 0 0
340 0 ▶ return $self->expand_expr(
Branch: 1 branch
BranchTrueFalseTotal
$op ? : 0 0 0
341 { $k, map +($op ? { $op => $_ } : $_), { -bind => $v } }
342 );
343 }
344 }
345 0 return $self->$orig($logop, $v, $k);
346 0 };
347 0 ▶ $opt{render}{bind} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
348 0 ▶ return [ '?', map +(ref($_->[0]) ? $_ : $_->[1]), $_[2] ]
Branch: 1 branch
BranchTrueFalseTotal
ref $_->[0] ? : 0 0 0
349 0 };
350 }
351 }
352  
353 775 ▶ if ($opt{lazy_join_sql_parts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"lazy_join_sql_parts"}) 0 775 775
354 0 require SQL::Abstract::Parts;
355 0 ▶ $opt{join_sql_parts} ||= sub { SQL::Abstract::Parts->new(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
$opt{"join_sql_parts"} ||= sub { "SQL::Abstract::Parts"->new(@_); }
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"join_sql_parts"} ||= sub { "SQL::Abstract::Parts"->new(@_); }
$opt{"join_sql_parts"} sub { "SQL::Abstract::Parts"->new(@_); }
Truth table0%0/3
A ||= B
A= $opt{"join_sql_parts"}B= sub { "SQL::Abstract::Parts"->new(@_); }
ABresult
1X1
011
000
356 }
357  
358 775 ▶ $opt{join_sql_parts} ||= sub { join $_[0], @_[1..$#_] };
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$opt{"join_sql_parts"} ||= sub { join $_[0], @_[1 .. $#_]; }
l!l&&r!l&&!r
07750
MC/DC0%0/2
$opt{"join_sql_parts"} ||= sub { join $_[0], @_[1 .. $#_]; }
$opt{"join_sql_parts"} sub { join $_[0], @_[1 .. $#_]; }
Truth table33%1/3
A ||= B
A= $opt{"join_sql_parts"}B= sub { join $_[0], @_[1 .. $#_]; }
ABresult
1X1
011
000
359  
360 775 return bless \%opt, $class;
361 }
362  
363 86 ▶sub _ext_rw {
Subroutine _ext_rw: called CC=2 SCAR=6.9
364 86 my ($self, $name, $key, $value) = @_;
365 86 ▶ return $self->{$name}{$key} unless @_ > 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 3 19 67 86
366 67 $self->{$name}{$key} = $value;
367 67 return $self;
368 }
369  
370 64 ▶sub make_unop_expander {
Subroutine make_unop_expander: called CC=1 SCAR=0.0
Pod documented
371 64 my (undef, $exp) = @_;
372 4143 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
373 4143 my ($self, $name, $body, $k) = @_;
374 4143 ▶ return $self->_expand_hashpair_cmp($k, { "-${name}" => $body })
Branch: 1 branch
BranchTrueFalseTotal
if defined $k 15 4128 4143
375 if defined($k);
376 4128 return $self->$exp($name, $body);
377 }
378 64 }
379  
380 65 ▶sub make_binop_expander {
Subroutine make_binop_expander: called CC=1 SCAR=0.0
Pod documented
381 65 my (undef, $exp) = @_;
382 137 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
383 137 my ($self, $name, $body, $k) = @_;
384 137 ▶ $k = shift @{$body = [ @$body ]} unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 40 97 137
385 137 ▶ $k = ref($k) ? $k : { -ident => $k };
Branch: 1 branch
BranchTrueFalseTotal
ref $k ? : 36 101 137
386 137 return $self->$exp($name, $body, $k);
387 }
388 65 }
389  
390 1 ▶sub plugin {
Subroutine plugin: called CC=2 SCAR=7.0
Pod documented
391 1 my ($self, $plugin, @args) = @_;
392 1 ▶ unless (ref $plugin) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $plugin) 1 0 1
393 1 $plugin =~ s/\A\+/${\__PACKAGE__}::Plugin::/;
394 1 require(join('/', split '::', $plugin).'.pm');
395 }
396 1 $plugin->apply_to($self, @args);
397 1 return $self;
398 }
399  
400 16 ▶BEGIN {
Subroutine BEGIN: called CC=10 SCAR=30.2
401 16 foreach my $type (qw(
402 expand op_expand render op_render clause_expand clause_render
403 )) {
404 96 my $name = join '_', reverse split '_', $type;
405 96 my $singular = "${type}er";
406  
407 0 ▶ eval qq{sub ${singular} {
Subroutine clause_expander: called CC=2 SCAR=9.0
Subroutine clause_renderer: called CC=2 SCAR=9.0
Subroutine expander: called CC=2 SCAR=9.0
Subroutine op_expander: not called CC=2 SCAR=9.0
Subroutine op_renderer: not called CC=2 SCAR=9.0
Subroutine renderer: called CC=2 SCAR=9.0
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 7 branches
BranchTrueFalseTotal
eval "sub $singular {\n my \$self = shift;\n return \$self->_ext_rw('${name}', \@_) if \@_ == 1;\n return \$self->${singular}s(\@_)\n }; 1 " or die "Method builder failed for ${singular}: $@" 96 0 96
if @_ == 1 1 0 1
if @_ == 1 0 5 5
if @_ == 1 0 11 11
if @_ == 1 0 0 0
if @_ == 1 0 0 0
if @_ == 1 0 7 7
408 my \$self = shift;
409 return \$self->_ext_rw('${name}', \@_) if \@_ == 1;
410 return \$self->${singular}s(\@_)
411 }; 1 } or die "Method builder failed for ${singular}: $@";
412 0 ▶ eval qq{sub wrap_${singular} {
Subroutine wrap_clause_expander: called CC=1 SCAR=1.6
Subroutine wrap_clause_renderer: not called CC=1 SCAR=1.6
Subroutine wrap_expander: called CC=1 SCAR=1.6
Subroutine wrap_op_expander: not called CC=1 SCAR=1.6
Subroutine wrap_op_renderer: not called CC=1 SCAR=1.6
Subroutine wrap_renderer: not called CC=1 SCAR=1.6
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
eval "sub wrap_$singular {\n shift->wrap_${singular}s(\@_)\n }; 1 " or die "Method builder failed for wrap_${singular}: $@" 96 0 96
413 shift->wrap_${singular}s(\@_)
414 }; 1 } or die "Method builder failed for wrap_${singular}: $@";
415  
416 0 ▶ eval qq{sub ${singular}s {
Subroutine clause_expanders: called CC=2 SCAR=7.6
Subroutine clause_renderers: called CC=2 SCAR=7.6
Subroutine expanders: called CC=2 SCAR=7.6
Subroutine op_expanders: not called CC=2 SCAR=7.6
Subroutine op_renderers: not called CC=2 SCAR=7.6
Subroutine renderers: called CC=2 SCAR=7.6
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 7 branches
BranchTrueFalseTotal
eval "sub ${singular}s {\n my (\$self, \@args) = \@_;\n while (my (\$this_key, \$this_value) = splice(\@args, 0, 2)) {\n \$self->_ext_rw('${name}', \$this_key, \$this_value);\n }\n return \$self;\n }; 1 " or die "Method builder failed for ${singular}s: $@" 96 0 96
if (() = splice(@args, 0, 2)) 25 17 42
if (() = splice(@args, 0, 2)) 5 5 10
if (() = splice(@args, 0, 2)) 11 11 22
if (() = splice(@args, 0, 2)) 0 0 0
if (() = splice(@args, 0, 2)) 0 0 0
if (() = splice(@args, 0, 2)) 7 7 14
417 my (\$self, \@args) = \@_;
418 while (my (\$this_key, \$this_value) = splice(\@args, 0, 2)) {
419 \$self->_ext_rw('${name}', \$this_key, \$this_value);
420 }
421 return \$self;
422 }; 1 } or die "Method builder failed for ${singular}s: $@";
423 0 ▶ eval qq{sub wrap_${singular}s {
Subroutine wrap_clause_expanders: called CC=2 SCAR=9.0
Subroutine wrap_clause_renderers: not called CC=2 SCAR=9.0
Subroutine wrap_expanders: called CC=2 SCAR=9.0
Subroutine wrap_op_expanders: called CC=2 SCAR=9.0
Subroutine wrap_op_renderers: not called CC=2 SCAR=9.0
Subroutine wrap_renderers: not called CC=2 SCAR=9.0
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 7 branches
BranchTrueFalseTotal
eval "sub wrap_${singular}s {\n my (\$self, \@args) = \@_;\n while (my (\$this_key, \$this_builder) = splice(\@args, 0, 2)) {\n my \$orig = \$self->_ext_rw('${name}', \$this_key);\n \$self->_ext_rw(\n '${name}', \$this_key,\n \$this_builder->(\$orig, '${name}', \$this_key),\n );\n }\n return \$self;\n }; 1 " or die "Method builder failed for wrap_${singular}s: $@" 96 0 96
if (() = splice(@args, 0, 2)) 1 1 2
if (() = splice(@args, 0, 2)) 0 0 0
if (() = splice(@args, 0, 2)) 9 9 18
if (() = splice(@args, 0, 2)) 8 4 12
if (() = splice(@args, 0, 2)) 0 0 0
if (() = splice(@args, 0, 2)) 0 0 0
424 my (\$self, \@args) = \@_;
425 while (my (\$this_key, \$this_builder) = splice(\@args, 0, 2)) {
426 my \$orig = \$self->_ext_rw('${name}', \$this_key);
427 \$self->_ext_rw(
428 '${name}', \$this_key,
429 \$this_builder->(\$orig, '${name}', \$this_key),
430 );
431 }
432 return \$self;
433 }; 1 } or die "Method builder failed for wrap_${singular}s: $@";
434 0 ▶ eval qq{sub ${singular}_list { sort keys %{\$_[0]->{\$name}} }; 1; }
Subroutine clause_expander_list: not called CC=1 SCAR=5.0
Subroutine clause_renderer_list: not called CC=1 SCAR=5.0
Subroutine expander_list: not called CC=1 SCAR=5.0
Subroutine op_expander_list: not called CC=1 SCAR=5.0
Subroutine op_renderer_list: not called CC=1 SCAR=5.0
Subroutine renderer_list: not called CC=1 SCAR=5.0
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
eval "sub ${singular}_list { sort keys %{\$_[0]->{\$name}} }; 1; " or die "Method builder failed for ${singular}_list: $@" 96 0 96
435 or die "Method builder failed for ${singular}_list: $@";
436 }
437 16 foreach my $singular (qw(unop_expander binop_expander)) {
438 0 ▶ eval qq{sub ${singular} { shift->${singular}s(\@_) }; 1 }
Subroutine binop_expander: called CC=1 SCAR=0.6
Subroutine unop_expander: not called CC=1 SCAR=0.6
Pod documented
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
eval "sub $singular { shift->${singular}s(\@_) }; 1 " or die "Method builder failed for ${singular}: $@" 32 0 32
439 or die "Method builder failed for ${singular}: $@";
440 0 ▶ eval qq{sub ${singular}s {
Subroutine binop_expanders: called CC=2 SCAR=8.8
Subroutine unop_expanders: not called CC=2 SCAR=8.8
Pod documented
Pod documented
Branch: 3 branches
BranchTrueFalseTotal
eval "sub ${singular}s {\n my (\$self, \@args) = \@_;\n while (my (\$this_key, \$this_value) = splice(\@args, 0, 2)) {\n \$self->_ext_rw(\n expand_op => \$this_key,\n \$self->make_$singular(\$this_value),\n );\n }\n return \$self;\n }; 1 " or die "Method builder failed for ${singular}s: $@" 32 0 32
if (() = splice(@args, 0, 2)) 1 1 2
if (() = splice(@args, 0, 2)) 0 0 0
441 my (\$self, \@args) = \@_;
442 while (my (\$this_key, \$this_value) = splice(\@args, 0, 2)) {
443 \$self->_ext_rw(
444 expand_op => \$this_key,
445 \$self->make_${singular}(\$this_value),
446 );
447 }
448 return \$self;
449 }; 1 } or die "Method builder failed for ${singular}s: $@";
450 }
451 }
452  
453 #sub register_op { $_[0]->{is_op}{$_[1]} = 1; $_[0] }
454  
455 1 ▶sub statement_list { sort keys %{$_[0]->{clauses_of}} }
Subroutine statement_list: called CC=1 SCAR=0.0
Pod documented
456  
457 14 ▶sub clauses_of {
Subroutine clauses_of: called CC=5 SCAR=16.2
Pod documented
458 14 my ($self, $of, @clauses) = @_;
459 14 ▶ unless (@clauses) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@clauses) 6 8 14
460 6 ▶ return @{$self->{clauses_of}{$of}||[]};
Condition50%1/2
$self->{'clauses_of'}{$of} || []
l!l
60
MC/DC0%0/1
$self->{'clauses_of'}{$of} || []
$self->{'clauses_of'}{$of}
Truth table50%1/2
A
A= $self->{'clauses_of'}{$of}
Aresult
11
00
461 }
462 8 ▶ if (ref($clauses[0]) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $clauses[0] eq "CODE") 2 6 8
463 2 ▶ @clauses = $self->${\($clauses[0])}(@{$self->{clauses_of}{$of}||[]});
Condition50%1/2
$self->{'clauses_of'}{$of} || []
l!l
20
MC/DC0%0/1
$self->{'clauses_of'}{$of} || []
$self->{'clauses_of'}{$of}
Truth table50%1/2
A
A= $self->{'clauses_of'}{$of}
Aresult
11
00
464 }
465 8 $self->{clauses_of}{$of} = \@clauses;
466 8 return $self;
467 }
468  
469 5 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
470 5 my ($self) = @_;
471 bless(
472 {
473 (map +($_ => (
474 ref($self->{$_}) eq 'HASH'
475 40 ? { %{$self->{$_}} }
476 5 ▶ : $self->{$_}
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{$_} eq 'HASH' ? : 40 75 115
477 )), keys %$self),
478 },
479 ref($self)
480 );
481 }
482  
483 26 ▶sub sqltrue { +{ -literal => [ $_[0]->{sqltrue} ] } }
Subroutine sqltrue: called CC=1 SCAR=0.0
Pod documented
484 19 ▶sub sqlfalse { +{ -literal => [ $_[0]->{sqlfalse} ] } }
Subroutine sqlfalse: called CC=1 SCAR=0.0
Pod documented
485  
486 8573 ▶sub _assert_pass_injection_guard {
Subroutine _assert_pass_injection_guard: called CC=2 SCAR=6.9
487 8573 ▶ if ($_[1] =~ $_[0]->{injection_guard}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] =~ /$_[0]{"injection_guard"}/mix) 5 8568 8573
488 5 my $class = ref $_[0];
489 5 puke "Possible SQL injection attempt '$_[1]'. If this is indeed a part of the "
490 . "desired SQL use literal SQL ( \'...' or \[ '...' ] ) or supply your own "
491 . "{injection_guard} attribute to ${class}->new()"
492 }
493 }
494  
495  
496 #======================================================================
497 # INSERT methods
498 #======================================================================
499  
500 52 ▶sub insert {
Subroutine insert: called CC=4 SCAR=13.9
Pod documented
501 52 my ($self, $table, $data, $options) = @_;
502  
503 52 my $stmt = do {
504 52 ▶ if (ref($table) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $table eq 'HASH') { } 3 49 52
505 3 $table;
506 } else {
507 49 ▶ my %clauses = (target => $table, values => $data, %{$options||{}});
Condition100%2/2
$options || {}
l!l
1039
MC/DC100%1/1
$options || {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
508 49 \%clauses;
509 }
510 };
511 52 my @rendered = $self->render_statement({ -insert => $stmt });
512 50 ▶ return wantarray ? @rendered : $rendered[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 50 0 50
513 }
514  
515 53 ▶sub _expand_insert_clause_target {
Subroutine _expand_insert_clause_target: called CC=1 SCAR=0.0
516 53 +(target => $_[0]->expand_expr($_[2], -ident));
517 }
518  
519 2 ▶sub _expand_insert_clause_fields {
Subroutine _expand_insert_clause_fields: called CC=2 SCAR=9.2
520 2 ▶ return +{ -row => [
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[2] eq "ARRAY" 2 0 2
521 $_[0]->expand_expr({ -list => $_[2] }, -ident)
522 ] } if ref($_[2]) eq 'ARRAY';
523 0 return $_[2]; # should maybe still expand somewhat?
524 }
525  
526 49 ▶sub _expand_insert_clause_from {
Subroutine _expand_insert_clause_from: called CC=4 SCAR=14.5
527 49 my ($self, undef, $data) = @_;
528 49 ▶ if (ref($data) eq 'HASH' and (keys(%$data))[0] =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH" and (keys %$data)[0] =~ /^-/) 0 49 49
Condition66%2/3
ref $data eq "HASH" and (keys %$data)[0] =~ /^-/
!ll&&!rl&&r
21280
MC/DC0%0/2
ref $data eq "HASH" and (keys %$data)[0] =~ /^-/
ref $data eq "HASH" (keys %$data)[0] =~ /^-/
Truth table66%2/3
A and B
A= ref $data eq "HASH"B= (keys %$data)[0] =~ /^-/
ABresult
0X0
100
111
529 0 return $self->expand_expr($data);
530 }
531 49 my ($f_aqt, $v_aqt) = $self->_expand_insert_values($data);
532 return (
533 49 ▶ from => { -values => [ $v_aqt ] },
Branch: 1 branch
BranchTrueFalseTotal
$f_aqt ? : 28 21 49
534 ($f_aqt ? (fields => $f_aqt) : ()),
535 );
536 }
537  
538 11 ▶sub _expand_insert_clause_returning {
Subroutine _expand_insert_clause_returning: called CC=1 SCAR=0.0
539 11 +(returning => $_[0]->expand_expr({ -list => $_[2] }, -ident));
540 }
541  
542 49 ▶sub _expand_insert_values {
Subroutine _expand_insert_values: called CC=6 SCAR=18.8
543 49 my ($self, $data) = @_;
544 49 ▶ if (is_literal_value($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_literal_value($data)) { } 0 49 49
545 0 (undef, $self->expand_expr($data));
546 } else {
547 my ($fields, $values) = (
548 ref($data) eq 'HASH' ?
549 49 ▶ ([ sort keys %$data ], [ @{$data}{sort keys %$data} ])
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 28 21 49
550 : ([], $data)
551 );
552  
553 # no names (arrayref) means can't generate bindtype
554 49 ▶ !($fields) && $self->{bindtype} eq 'columns'
Branch: 1 branch
BranchTrueFalseTotal
not $fields and $self->{"bindtype"} eq "columns" and &belch("can't do 'columns' bindtype when called with arrayref") 0 49 49
Condition33%1/3
not $fields and $self->{"bindtype"} eq "columns"
!ll&&!rl&&r
4900
MC/DC0%0/2
not $fields and $self->{"bindtype"} eq "columns"
not $fields $self->{"bindtype"} eq "columns"
Truth table33%1/3
not(A) and B
A= not $fieldsB= $self->{"bindtype"} eq "columns"
ABresult
0X0
100
111
555 && belch "can't do 'columns' bindtype when called with arrayref";
556  
557 +(
558 (@$fields
559 ? $self->expand_expr({ -row => $fields }, -ident)
560 : undef
561 ),
562 +{ -row => [
563 map {
564 49 ▶ local our $Cur_Col_Meta = $fields->[$_];
Branch: 1 branch
BranchTrueFalseTotal
@$fields ? : 28 21 49
565 251 $self->_expand_insert_value($values->[$_])
566 } 0..$#$values
567 ] },
568 );
569 }
570 }
571  
572 30 ▶sub _render_insert_clause_fields {
Subroutine _render_insert_clause_fields: called CC=1 SCAR=0.0
573 30 return $_[0]->render_aqt($_[2]);
574 }
575  
576 53 ▶sub _render_insert_clause_target {
Subroutine _render_insert_clause_target: called CC=1 SCAR=0.0
577 53 my ($self, undef, $from) = @_;
578 53 $self->join_query_parts(' ', { -keyword => 'insert into' }, $from);
579 }
580  
581 53 ▶sub _render_insert_clause_from {
Subroutine _render_insert_clause_from: called CC=1 SCAR=0.0
582 53 return $_[0]->render_aqt($_[2], 1);
583 }
584  
585 # So that subclasses can override INSERT ... RETURNING separately from
586 # UPDATE and DELETE (e.g. DBIx::Class::SQLMaker::Oracle does this)
587 0 ▶sub _insert_returning { shift->_returning(@_) }
Subroutine _insert_returning: not called CC=1 SCAR=6.9
588  
589 0 ▶sub _redispatch_returning {
Subroutine _redispatch_returning: not called CC=1 SCAR=6.9
590 0 my ($self, $type, undef, $returning) = @_;
591 0 [ $self->${\"_${type}_returning"}({ returning => $returning }) ];
592 }
593  
594 0 ▶sub _returning {
Subroutine _returning: not called CC=2 SCAR=17.9
595 0 my ($self, $options) = @_;
596  
597 0 my $f = $options->{returning};
598  
599 0 my ($sql, @bind) = @{ $self->render_aqt(
600 $self->expand_expr({ -list => $f }, -ident)
601 ) };
602 0 my $rsql = $self->_sqlcase(' returning ').$sql;
603 0 ▶ return wantarray ? ($rsql, @bind) : $rsql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
604 }
605  
606 251 ▶sub _expand_insert_value {
Subroutine _expand_insert_value: called CC=6 SCAR=17.9
607 251 my ($self, $v) = @_;
608  
609 251 my $k = our $Cur_Col_Meta;
610  
611 251 ▶ if (ref($v) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "ARRAY") 5 246 251
612 5 ▶ if ($self->{array_datatypes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"array_datatypes"}) 4 1 5
613 4 return +{ -bind => [ $k, $v ] };
614 }
615 1 my ($sql, @bind) = @$v;
616 1 $self->_assert_bindval_matches_bindtype(@bind);
617 1 return +{ -literal => $v };
618 }
619 246 ▶ if (ref($v) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "HASH") 2 244 246
620 2 ▶ if (grep !/^-/, keys %$v) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep((!/^-/), keys %$v)) 2 0 2
621 2 belch "HASH ref as bind value in insert is not supported";
622 2 return +{ -bind => [ $k, $v ] };
623 }
624 }
625 244 ▶ if (!defined($v)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 8 236 244
626 8 return +{ -bind => [ $k, undef ] };
627 }
628 236 return $self->expand_expr($v);
629 }
630  
631  
632  
633 #======================================================================
634 # UPDATE methods
635 #======================================================================
636  
637 46 ▶sub update {
Subroutine update: called CC=4 SCAR=13.9
Pod documented
638 46 my ($self, $table, $set, $where, $options) = @_;
639  
640 46 my $stmt = do {
641 46 ▶ if (ref($table) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $table eq 'HASH') { } 4 42 46
642 4 $table
643 } else {
644 42 my %clauses;
645 42 @clauses{qw(target set where)} = ($table, $set, $where);
646 puke "Unsupported data type specified to \$sql->update"
647 42 ▶ unless ref($clauses{set}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $clauses{"set"} eq "HASH" 0 42 42
648 42 @clauses{keys %$options} = values %$options;
649 42 \%clauses;
650 }
651 };
652 46 my @rendered = $self->render_statement({ -update => $stmt });
653 44 ▶ return wantarray ? @rendered : $rendered[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 43 1 44
654 }
655  
656 46 ▶sub _render_update_clause_target {
Subroutine _render_update_clause_target: called CC=1 SCAR=0.0
657 46 my ($self, undef, $target) = @_;
658 46 $self->join_query_parts(' ', { -keyword => 'update' }, $target);
659 }
660  
661 0 ▶sub _update_set_values {
Subroutine _update_set_values: not called CC=1 SCAR=6.9
662 0 my ($self, $data) = @_;
663  
664 0 return @{ $self->render_aqt(
665 $self->_expand_update_set_values(undef, $data),
666 ) };
667 }
668  
669 46 ▶sub _expand_update_set_values {
Subroutine _expand_update_set_values: called CC=4 SCAR=13.9
670 46 my ($self, undef, $data) = @_;
671 $self->expand_expr({ -list => [
672 map {
673 91 my ($k, $set) = @$_;
674 91 ▶ $set = { -bind => $_ } unless defined $set;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $set 2 89 91
675 91 +{ -op => [ '=', { -ident => $k }, $set ] };
676 }
677 map {
678 46 my $k = $_;
679 91 my $v = $data->{$k};
680 (ref($v) eq 'ARRAY'
681 ? ($self->{array_datatypes}
682 ? [ $k, +{ -bind => [ $k, $v ] } ]
683 : [ $k, +{ -literal => $v } ])
684 91 ▶ : do {
Branch: 2 branches
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 4 87 91
$self->{'array_datatypes'} ? : 4 0 4
685 87 local our $Cur_Col_Meta = $k;
686 87 [ $k, $self->_expand_expr($v) ]
687 }
688 );
689 } sort keys %$data
690 ] });
691 }
692  
693 42 ▶sub _expand_update_clause_target {
Subroutine _expand_update_clause_target: called CC=1 SCAR=0.0
694 42 my ($self, undef, $target) = @_;
695 42 +(target => $self->expand_expr({ -list => $target }, -ident));
696 }
697  
698 46 ▶sub _expand_update_clause_set {
Subroutine _expand_update_clause_set: called CC=4 SCAR=16.9
699 46 ▶ return $_[2] if ref($_[2]) eq 'HASH' and ($_[2]->{-op}||[''])->[0] eq ',';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[2] eq "HASH" and +($_[2]{"-op"} || [""])->[0] eq "," 0 46 46
Condition40%2/5
ref $_[2] eq "HASH" and +($_[2]{"-op"} || [""])->[0] eq ","
!ll&&!rl&&r
0460
$_[2]{'-op'} || ['']
l!l
046
MC/DC0%0/2
ref $_[2] eq "HASH" and +($_[2]{"-op"} || [""])->[0] eq ","
ref $_[2] eq "HASH" $_[2]{'-op'}
Truth table33%1/3
A and B
A= ref $_[2] eq "HASH"B= $_[2]{'-op'}
ABresult
0X0
100
111
700 46 +(set => $_[0]->_expand_update_set_values($_[1], $_[2]));
701 }
702  
703 39 ▶sub _expand_update_clause_where {
Subroutine _expand_update_clause_where: called CC=1 SCAR=0.0
704 39 +(where => $_[0]->expand_expr($_[2]));
705 }
706  
707 6 ▶sub _expand_update_clause_returning {
Subroutine _expand_update_clause_returning: called CC=1 SCAR=0.0
708 6 +(returning => $_[0]->expand_expr({ -list => $_[2] }, -ident));
709 }
710  
711 # So that subclasses can override UPDATE ... RETURNING separately from
712 # INSERT and DELETE
713 0 ▶sub _update_returning { shift->_returning(@_) }
Subroutine _update_returning: not called CC=1 SCAR=6.9
714  
715  
716  
717 #======================================================================
718 # SELECT
719 #======================================================================
720  
721 118 ▶sub select {
Subroutine select: called CC=5 SCAR=16.1
Pod documented
722 118 my ($self, @args) = @_;
723 118 my $stmt = do {
724 118 ▶ if (ref(my $sel = $args[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref(my $sel = $args[0]) eq 'HASH') { } 10 108 118
725 10 $sel
726 } else {
727 108 my %clauses;
728 108 @clauses{qw(from select where order_by)} = @args;
729  
730 # This oddity is to literalify since historically SQLA doesn't quote
731 # a single identifier argument, so we convert it into a literal
732  
733 $clauses{select} = { -literal => [ $clauses{select}||'*' ] }
734 108 ▶ unless ref($clauses{select});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $clauses{"select"} 92 16 108
Condition50%1/2
$clauses{'select'} || '*'
l!l
920
MC/DC0%0/1
$clauses{'select'} || '*'
$clauses{'select'}
Truth table50%1/2
A
A= $clauses{'select'}
Aresult
11
00
735 108 \%clauses;
736 }
737 };
738  
739 118 my @rendered = $self->render_statement({ -select => $stmt });
740 105 ▶ return wantarray ? @rendered : $rendered[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 88 17 105
741 }
742  
743 140 ▶sub _expand_select_clause_select {
Subroutine _expand_select_clause_select: called CC=1 SCAR=0.0
744 140 my ($self, undef, $select) = @_;
745 140 +(select => $self->expand_expr({ -list => $select }, -ident));
746 }
747  
748 123 ▶sub _expand_select_clause_from {
Subroutine _expand_select_clause_from: called CC=1 SCAR=0.0
749 123 my ($self, undef, $from) = @_;
750 123 +(from => $self->expand_expr({ -list => $from }, -ident));
751 }
752  
753 568 ▶sub _expand_select_clause_where {
Subroutine _expand_select_clause_where: called CC=2 SCAR=7.8
754 568 my ($self, undef, $where) = @_;
755  
756 568 my $sqla = do {
757 568 ▶ if (my $conv = $self->{convert}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conv = $self->{'convert'}) { } 4 564 568
758 12 ▶ my $_wrap = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
759 12 my $orig = shift;
760 52 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
761 52 my $self = shift;
762 52 +{ -func => [
763 $conv,
764 $self->$orig(@_)
765 ] };
766 12 };
767 4 };
768 $self->clone
769 ->wrap_expander(bind => $_wrap)
770 ->wrap_op_expanders(map +($_ => $_wrap), qw(ident value))
771 4 ▶ ->wrap_expander(func => sub {
Subroutine __ANON__: called CC=1 SCAR=1.3
772 4 my $orig = shift;
773 16 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=30.3
774 16 my ($self, $type, $thing) = @_;
775 16 ▶ if (ref($thing) eq 'ARRAY' and $thing->[0] eq $conv
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing eq "ARRAY" and $thing->[0] eq $conv and @$thing == 2 and ref $thing->[1] eq "HASH" and $thing->[1]{"-ident"} || $thing->[1]{"-value"} || $thing->[1]{"-bind"}) 16 0 16
Condition33%6/18
ref $thing eq "ARRAY" and $thing->[0] eq $conv and @$thing == 2 and ref $thing->[1] eq "HASH" and $thing->[1]{"-ident"} || $thing->[1]{"-value"} || $thing->[1]{"-bind"}
!ll&&!rl&&r
0016
ref $thing eq "ARRAY" and $thing->[0] eq $conv and @$thing == 2 and ref $thing->[1] eq "HASH"
!ll&&!rl&&r
0016
ref $thing eq "ARRAY" and $thing->[0] eq $conv and @$thing == 2
!ll&&!rl&&r
0016
ref $thing eq "ARRAY" and $thing->[0] eq $conv
!ll&&!rl&&r
0016
$thing->[1]{"-ident"} or $thing->[1]{"-value"} or $thing->[1]{"-bind"}
l!l&&r!l&&!r
0160
$thing->[1]{"-ident"} or $thing->[1]{"-value"}
l!l&&r!l&&!r
0016
MC/DC0%0/7
ref $thing eq "ARRAY" and $thing->[0] eq $conv and @$thing == 2 and ref $thing->[1] eq "HASH" and $thing->[1]{"-ident"} || $thing->[1]{"-value"} || $thing->[1]{"-bind"}
ref $thing eq "ARRAY" $thing->[0] eq $conv @$thing == 2 ref $thing->[1] eq "HASH" $thing->[1]{"-ident"} $thing->[1]{"-value"} $thing->[1]{"-bind"}
Truth table12%1/8
A and B and C and D and E or F or G
A= ref $thing eq "ARRAY"B= $thing->[0] eq $convC= @$thing == 2D= ref $thing->[1] eq "HASH"E= $thing->[1]{"-ident"}F= $thing->[1]{"-value"}G= $thing->[1]{"-bind"}
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
11110000
11111XX1
111101X1
11110011
776 and @$thing == 2 and ref($thing->[1]) eq 'HASH'
777 and (
778 $thing->[1]{-ident}
779 or $thing->[1]{-value}
780 or $thing->[1]{-bind})
781 ) {
782 16 return { -func => $thing }; # already went through our expander
783 }
784 0 return $self->$orig($type, $thing);
785 }
786 4 });
787 } else {
788 564 $self;
789 }
790 };
791  
792 568 return +(where => $sqla->expand_expr($where));
793 }
794  
795 8 ▶sub _expand_select_clause_order_by {
Subroutine _expand_select_clause_order_by: called CC=1 SCAR=0.0
796 8 my ($self, undef, $order_by) = @_;
797 8 +(order_by => $self->_expand_order_by($order_by));
798 }
799  
800 0 ▶sub _select_fields {
Subroutine _select_fields: not called CC=3 SCAR=24.8
801 0 my ($self, $fields) = @_;
802 0 ▶ return $fields unless ref($fields);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fields 0 0 0
803 0 my ($sql, @bind) = @{ $self->render_aqt(
804 $self->expand_expr({ -list => $fields }, '-ident')
805 ) };
806 0 ▶ return wantarray ? ($sql, @bind) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
807 }
808  
809 #======================================================================
810 # DELETE
811 #======================================================================
812  
813 14 ▶sub delete {
Subroutine delete: called CC=4 SCAR=13.9
Pod documented
814 14 my ($self, $table, $where, $options) = @_;
815  
816 14 my $stmt = do {
817 14 ▶ if (ref($table) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $table eq 'HASH') { } 2 12 14
818 2 $table;
819 } else {
820 12 ▶ my %clauses = (target => $table, where => $where, %{$options||{}});
Condition100%2/2
$options || {}
l!l
66
MC/DC100%1/1
$options || {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
821 12 \%clauses;
822 }
823 };
824 14 my @rendered = $self->render_statement({ -delete => $stmt });
825 14 ▶ return wantarray ? @rendered : $rendered[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 14 0 14
826 }
827  
828 # So that subclasses can override DELETE ... RETURNING separately from
829 # INSERT and UPDATE
830 0 ▶sub _delete_returning { shift->_returning(@_) }
Subroutine _delete_returning: not called CC=1 SCAR=6.9
831  
832 14 ▶sub _expand_delete_clause_target {
Subroutine _expand_delete_clause_target: called CC=1 SCAR=0.0
833 14 +(target => $_[0]->expand_expr({ -list => $_[2] }, -ident));
834 }
835  
836 14 ▶sub _expand_delete_clause_where { +(where => $_[0]->expand_expr($_[2])); }
Subroutine _expand_delete_clause_where: called CC=1 SCAR=0.0
837  
838 6 ▶sub _expand_delete_clause_returning {
Subroutine _expand_delete_clause_returning: called CC=1 SCAR=0.0
839 6 +(returning => $_[0]->expand_expr({ -list => $_[2] }, -ident));
840 }
841  
842 14 ▶sub _render_delete_clause_target {
Subroutine _render_delete_clause_target: called CC=1 SCAR=0.0
843 14 my ($self, undef, $from) = @_;
844 14 $self->join_query_parts(' ', { -keyword => 'delete from' }, $from);
845 }
846  
847 #======================================================================
848 # WHERE: entry point
849 #======================================================================
850  
851  
852  
853 # Finally, a separate routine just to handle WHERE clauses
854 498 ▶sub where {
Subroutine where: called CC=6 SCAR=18.0
Pod documented
855 498 my ($self, $where, $order) = @_;
856  
857 498 local $self->{convert_where} = $self->{convert};
858  
859 # where ?
860 498 ▶ my ($sql, @bind) = defined($where)
Branch: 1 branch
BranchTrueFalseTotal
defined $where ? : 460 38 498
861 ? $self->_recurse_where($where)
862 : (undef);
863 402 ▶ $sql = (defined $sql and length $sql) ? $self->_sqlcase(' where ') . "( $sql )" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $sql && length $sql ? : 362 40 402
Condition66%2/3
defined $sql and length $sql
!ll&&!rl&&r
400362
MC/DC50%1/2
defined $sql and length $sql
defined $sql length $sql
Truth table66%2/3
A and B
A= defined $sqlB= length $sql
ABresult
0X0
100
111
864  
865 # order by?
866 402 ▶ if ($order) {
Branch: 1 branch
BranchTrueFalseTotal
if ($order) 47 355 402
867 47 my ($order_sql, @order_bind) = $self->_order_by($order);
868 47 $sql .= $order_sql;
869 47 push @bind, @order_bind;
870 }
871  
872 402 ▶ return wantarray ? ($sql, @bind) : $sql;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 402 0 402
873 }
874  
875 16 { our $Default_Scalar_To = -value }
876  
877 3586 ▶sub expand_expr {
Subroutine expand_expr: called CC=2 SCAR=6.9
Pod documented
878 3586 my ($self, $expr, $default_scalar_to) = @_;
879 3586 ▶ local our $Default_Scalar_To = $default_scalar_to if $default_scalar_to;
Branch: 1 branch
BranchTrueFalseTotal
if $default_scalar_to 1343 2243 3586
880 3586 $self->_expand_expr($expr);
881 }
882  
883 8141 ▶sub render_aqt {
Subroutine render_aqt: called CC=5 SCAR=16.8
Pod documented
884 8141 my ($self, $aqt, $top_level) = @_;
885 8141 my ($k, $v, @rest) = %$aqt;
886 8141 ▶ die "No" if @rest;
Branch: 1 branch
BranchTrueFalseTotal
if @rest 0 8141 8141
887 8141 ▶ die "Not a node type: $k" unless $k =~ s/^-//;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ s/^-// 0 8141 8141
888 8141 ▶ if (my $meth = $self->{render}{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meth = $self->{"render"}{$k}) 8141 0 8141
889 8141 local our $Render_Top_Level = $top_level;
890 8141 ▶ return $self->$meth($k, $v)||[];
Condition100%2/2
$self->$meth($k, $v) || []
l!l
81054
MC/DC100%1/1
$self->$meth($k, $v) || []
$self->$meth($k, $v)
Truth table100%2/2
A
A= $self->$meth($k, $v)
Aresult
11
00
891 }
892 0 die "notreached: $k";
893 }
894  
895 0 ▶sub render_expr {
Subroutine render_expr: not called CC=1 SCAR=6.9
Pod documented
896 0 my ($self, $expr, $default_scalar_to) = @_;
897 0 return @{ $self->render_aqt(
898 $self->expand_expr($expr, $default_scalar_to)
899 ) };
900 }
901  
902 230 ▶sub render_statement {
Subroutine render_statement: called CC=1 SCAR=0.0
Pod documented
903 230 my ($self, $expr, $default_scalar_to) = @_;
904 230 @{$self->render_aqt(
905 $self->expand_expr($expr, $default_scalar_to), 1
906 )};
907 }
908  
909 254 ▶sub _expand_statement {
Subroutine _expand_statement: called CC=6 SCAR=17.9
910 254 my ($self, $type, $args) = @_;
911 254 my $ec = $self->{expand_clause};
912 254 ▶ if ($args->{_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"_"}) 9 245 254
913 9 $args = { %$args };
914 $args->{$type} = delete $args->{_}
915 9 }
916 254 my %has_clause = map +($_ => 1), @{$self->{clauses_of}{$type}};
917 return +{ "-${type}" => +{
918 map {
919 254 my $val = $args->{$_};
920 821 ▶ if (defined($val) and my $exp = $ec->{"${type}.$_"}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $val and my $exp = $$ec{"$type.$_"}) { } 698 123 821
elsif ($has_clause{$_}) { } 123 0 123
Condition100%3/3
defined $val and my $exp = $$ec{"$type.$_"}
!ll&&!rl&&r
1203698
MC/DC100%2/2
defined $val and my $exp = $$ec{"$type.$_"}
defined $val my $exp = $$ec{"$type.$_"}
Truth table100%3/3
A and B
A= defined $valB= my $exp = $$ec{"$type.$_"}
ABresult
0X0
100
111
921 698 ▶ if ((my (@exp) = $self->$exp($_ => $val)) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my(@exp) = $self->$exp($_, $val)) == 1) { } 24 665 689
922 24 ($_ => $exp[0])
923 } else {
924 @exp
925 665 }
926 } elsif ($has_clause{$_}) {
927 123 ($_ => $self->expand_expr($val))
928 } else {
929 0 ($_ => $val)
930 }
931 } sort keys %$args
932 } };
933 }
934  
935 242 ▶sub _render_statement {
Subroutine _render_statement: called CC=7 SCAR=19.5
936 242 my ($self, $type, $args) = @_;
937 242 my @parts;
938 242 foreach my $clause (@{$self->{clauses_of}{$type}}) {
939 1082 ▶ next unless my $clause_expr = $args->{$clause};
Branch: 1 branch
BranchTrueFalseTotal
unless my $clause_expr = $args->{$clause} 396 686 1082
940 686 my $part = do {
941 686 ▶ if (my $rdr = $self->{render_clause}{"${type}.${clause}"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rdr = $self->{'render_clause'}{"$type.$clause"}) { } 203 483 686
942 203 $self->$rdr($clause, $clause_expr, $args);
943 } else {
944 483 my $r = $self->render_aqt($clause_expr, 1);
945 477 ▶ next unless defined $r->[0] and length $r->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->[0] and length $r->[0] 2 475 477
Condition66%2/3
defined $r->[0] and length $r->[0]
!ll&&!rl&&r
20475
MC/DC50%1/2
defined $r->[0] and length $r->[0]
defined $r->[0] length $r->[0]
Truth table66%2/3
A and B
A= defined $r->[0]B= length $r->[0]
ABresult
0X0
100
111
946 475 $self->join_query_parts(' ',
947 { -keyword => $clause },
948 $r
949 );
950 }
951 };
952 676 push @parts, $part;
953 }
954 234 my $q = $self->join_query_parts(' ', @parts);
955 234 ▶ return $self->join_query_parts('',
Branch: 1 branch
BranchTrueFalseTotal
our $Render_Top_Level ? : 216 18 234
956 (our $Render_Top_Level ? $q : ('(', $q, ')'))
957 );
958 }
959  
960 6922 ▶sub _normalize_op {
Subroutine _normalize_op: called CC=3 SCAR=11.0
961 6922 my ($self, $raw) = @_;
962 6922 my $op = lc $raw;
963 6922 ▶ return $op if grep $_->{$op}, @{$self}{qw(expand_op render_op)};
Branch: 1 branch
BranchTrueFalseTotal
if grep $_->{$op}, @{$self;}{"expand_op", "render_op"} 91 6831 6922
964 6831 s/^-(?=.)//, s/\s+/_/g for $op;
965 6831 $op;
966 }
967  
968 8901 ▶sub _expand_expr {
Subroutine _expand_expr: called CC=12 SCAR=24.9
969 8901 my ($self, $expr) = @_;
970 8901 ▶ our $Expand_Depth ||= 0; local $Expand_Depth = $Expand_Depth + 1;
Condition100%2/2
our $Expand_Depth ||= 0
l!l
73821519
MC/DC100%1/1
our $Expand_Depth ||= 0
our $Expand_Depth
Truth table100%2/2
A
A= our $Expand_Depth
Aresult
11
00
971 8901 ▶ return undef unless defined($expr);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 131 8770 8901
972 8770 ▶ if (ref($expr) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expr eq "HASH") 6982 1788 8770
973 6982 ▶ return undef unless my $kc = keys %$expr;
Branch: 1 branch
BranchTrueFalseTotal
unless my $kc = keys %$expr 3 6979 6982
974 6979 ▶ if ($kc > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kc > 1) 164 6815 6979
975 164 return $self->_expand_logop(and => $expr);
976 }
977 6815 my ($key, $value) = %$expr;
978 6815 ▶ if ($key =~ /^-/ and $key =~ s/ [_\s]? \d+ $//x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^-/ and $key =~ s/ [_\s]? \d+ $//x) 9 6806 6815
Condition100%3/3
$key =~ /^-/ and $key =~ s/ [_\s]? \d+ $//x
!ll&&!rl&&r
126255449
MC/DC100%2/2
$key =~ /^-/ and $key =~ s/ [_\s]? \d+ $//x
$key =~ /^-/ $key =~ s/ [_\s]? \d+ $//x
Truth table100%3/3
A and B
A= $key =~ /^-/B= $key =~ s/ [_\s]? \d+ $//x
ABresult
0X0
100
111
979 9 belch 'Use of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0. '
980 . "You probably wanted ...-and => [ $key => COND1, $key => COND2 ... ]";
981 }
982 6815 return $self->_expand_hashpair($key, $value);
983 }
984 1788 ▶ if (ref($expr) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $expr eq "ARRAY") 130 1658 1788
985 130 return $self->_expand_logop(lc($self->{logic}), $expr);
986 }
987 1658 ▶ if (my $literal = is_literal_value($expr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $literal = &is_literal_value($expr)) 146 1512 1658
988 146 return +{ -literal => $literal };
989 }
990 1512 ▶ if (!ref($expr) or Scalar::Util::blessed($expr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $expr or builtin::blessed($expr)) 1512 0 1512
Condition33%1/3
not ref $expr or builtin::blessed($expr)
l!l&&r!l&&!r
151200
MC/DC0%0/2
not ref $expr or builtin::blessed($expr)
not ref $expr builtin::blessed($expr)
Truth table33%1/3
not(A) or B
A= not ref $exprB= builtin::blessed($expr)
ABresult
1X1
011
000
991 1512 return $self->_expand_scalar($expr);
992 }
993 0 die "notreached";
994 }
995  
996 6915 ▶sub _expand_hashpair {
Subroutine _expand_hashpair: called CC=8 SCAR=20.8
997 6915 my ($self, $k, $v) = @_;
998 6915 ▶ unless (defined($k) and length($k)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $k and length $k) 52 6863 6915
Condition66%2/3
defined $k and length $k
!ll&&!rl&&r
0526863
MC/DC50%1/2
defined $k and length $k
defined $k length $k
Truth table66%2/3
A and B
A= defined $kB= length $k
ABresult
0X0
100
111
999 52 ▶ if (defined($k) and my $literal = is_literal_value($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $k and my $literal = &is_literal_value($v)) 24 28 52
Condition66%2/3
defined $k and my $literal = &is_literal_value($v)
!ll&&!rl&&r
02824
MC/DC50%1/2
defined $k and my $literal = &is_literal_value($v)
defined $k my $literal = &is_literal_value($v)
Truth table66%2/3
A and B
A= defined $kB= my $literal = &is_literal_value($v)
ABresult
0X0
100
111
1000 24 belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead';
1001 24 return { -literal => $literal };
1002 }
1003 28 puke "Supplying an empty left hand side argument is not supported";
1004 }
1005 6863 ▶ if ($k =~ /^-./) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k =~ /^-./) { } 5553 1310 6863
elsif ($k =~ /^\W+$/) { } 3 1307 1310
1006 5553 return $self->_expand_hashpair_op($k, $v);
1007 } elsif ($k =~ /^\W+$/) {
1008 3 ▶ my ($lhs, @rhs) = ref($v) eq 'ARRAY' ? @$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 2 1 3
1009 3 return $self->_expand_op(
1010 -op, [ $k, $self->expand_expr($lhs, -ident), @rhs ]
1011 );
1012 }
1013 1307 return $self->_expand_hashpair_ident($k, $v);
1014 }
1015  
1016 1307 ▶sub _expand_hashpair_ident {
Subroutine _expand_hashpair_ident: called CC=16 SCAR=27.9
1017 1307 my ($self, $k, $v) = @_;
1018  
1019 1307 local our $Cur_Col_Meta = $k;
1020  
1021 # hash with multiple or no elements is andor
1022  
1023 1307 ▶ if (ref($v) eq 'HASH' and keys %$v != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "HASH" and keys %$v != 1) 7 1300 1307
Condition100%3/3
ref $v eq "HASH" and keys %$v != 1
!ll&&!rl&&r
5737277
MC/DC100%2/2
ref $v eq "HASH" and keys %$v != 1
ref $v eq "HASH" keys %$v != 1
Truth table100%3/3
A and B
A= ref $v eq "HASH"B= keys %$v != 1
ABresult
0X0
100
111
1024 7 return $self->_expand_logop(and => $v, $k);
1025 }
1026  
1027 # undef needs to be re-sent with cmp to achieve IS/IS NOT NULL
1028  
1029 1300 ▶ if (is_undef_value($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_undef_value($v)) 23 1277 1300
1030 23 return $self->_expand_hashpair_cmp($k => undef);
1031 }
1032  
1033 # scalars and objects get expanded as whatever requested or values
1034  
1035 1277 ▶ if (!ref($v) or Scalar::Util::blessed($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $v or builtin::blessed($v)) 468 809 1277
Condition100%3/3
not ref $v or builtin::blessed($v)
l!l&&r!l&&!r
4671809
MC/DC100%2/2
not ref $v or builtin::blessed($v)
not ref $v builtin::blessed($v)
Truth table100%3/3
not(A) or B
A= not ref $vB= builtin::blessed($v)
ABresult
1X1
011
000
1036 468 return $self->_expand_hashpair_scalar($k, $v);
1037 }
1038  
1039 # single key hashref is a hashtriple
1040  
1041 809 ▶ if (ref($v) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "HASH") 723 86 809
1042 723 return $self->_expand_hashtriple($k, %$v);
1043 }
1044  
1045 # arrayref needs re-engineering over the elements
1046  
1047 86 ▶ if (ref($v) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "ARRAY") 47 39 86
1048 47 ▶ return $self->sqlfalse unless @$v;
Branch: 1 branch
BranchTrueFalseTotal
unless @$v 1 46 47
1049 46 $self->_debug("ARRAY($k) means distribute over elements");
1050 my $logic = lc(
1051 ($v->[0]||'') =~ /^-(and|or)$/i
1052 22 ▶ ? (shift(@{$v = [ @$v ]}), $1)
Condition50%1/2
$self->{'logic'} || 'OR'
l!l
240
MC/DC0%0/1
$self->{'logic'} || 'OR'
$self->{'logic'}
Truth table50%1/2
A
A= $self->{'logic'}
Aresult
11
00
1053 46 ▶ : lc($self->{logic} || 'OR')
Branch: 1 branch
BranchTrueFalseTotal
($v->[0] || '') =~ /^-(and|or)$/i ? : 22 24 46
Condition100%2/2
$v->[0] || ''
l!l
451
MC/DC100%1/1
$v->[0] || ''
$v->[0]
Truth table100%2/2
A
A= $v->[0]
Aresult
11
00
1054 );
1055 46 return $self->_expand_logop(
1056 $logic => $v, $k
1057 );
1058 }
1059  
1060 39 ▶ if (my $literal = is_literal_value($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $literal = &is_literal_value($v)) 39 0 39
1061 39 ▶ unless (length $k) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $k) 0 39 39
1062 0 belch 'Hash-pairs consisting of an empty string with a literal are deprecated, and will be removed in 2.0: use -and => [ $literal ] instead';
1063 0 return \$literal;
1064 }
1065 39 my ($sql, @bind) = @$literal;
1066 39 ▶ if ($self->{bindtype} eq 'columns') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bindtype"} eq "columns") 6 33 39
1067 6 for (@bind) {
1068 6 $self->_assert_bindval_matches_bindtype($_);
1069 }
1070 }
1071 37 return +{ -literal => [ $self->_quote($k).' '.$sql, @bind ] };
1072 }
1073 0 die "notreached";
1074 }
1075  
1076 1980 ▶sub _expand_scalar {
Subroutine _expand_scalar: called CC=1 SCAR=0.0
1077 1980 my ($self, $expr) = @_;
1078  
1079 1980 return $self->_expand_expr({ (our $Default_Scalar_To) => $expr });
1080 }
1081  
1082 468 ▶sub _expand_hashpair_scalar {
Subroutine _expand_hashpair_scalar: called CC=1 SCAR=0.0
1083 468 my ($self, $k, $v) = @_;
1084  
1085 468 return $self->_expand_hashpair_cmp(
1086 $k, $self->_expand_scalar($v),
1087 );
1088 }
1089  
1090 5553 ▶sub _expand_hashpair_op {
Subroutine _expand_hashpair_op: called CC=20 SCAR=30.0
1091 5553 my ($self, $k, $v) = @_;
1092  
1093 5553 $self->_assert_pass_injection_guard($k =~ /\A-(.*)\Z/s);
1094  
1095 5551 my $op = $self->_normalize_op($k);
1096  
1097 5551 my $wsop = join(' ', split '_', $op);
1098  
1099 36 ▶ my $is_special = List::Util::first { $wsop =~ $_->{regex} }
Subroutine __ANON__: called CC=1 SCAR=0.0
1100 5551 @{$self->{special_ops}};
1101  
1102 { # Old SQLA compat
1103  
1104 # the old special op system requires illegality for top-level use
1105  
1106 5551 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (our $Expand_Depth == 1 and $is_special || $self->{"disable_old_special_ops"} && &List::Util::first(sub { $wsop =~ /$_->{"regex"}/; } , @BUILTIN_SPECIAL_OPS)) 4 5547 5551
Condition88%8/9
our $Expand_Depth == 1 and $is_special || $self->{"disable_old_special_ops"} && &List::Util::first(sub { $wsop =~ /$_->{"regex"}/; } , @BUILTIN_SPECIAL_OPS)
!ll&&!rl&&r
50904574
$is_special or $self->{"disable_old_special_ops"} and &List::Util::first(sub { $wsop =~ /$_->{"regex"}/; } , @BUILTIN_SPECIAL_OPS)
l!l&&r!l&&!r
04457
$self->{"disable_old_special_ops"} and &List::Util::first(sub { $wsop =~ /$_->{"regex"}/; } , @BUILTIN_SPECIAL_OPS)
!ll&&!rl&&r
45344
MC/DC75%3/4
our $Expand_Depth == 1 and $is_special || $self->{"disable_old_special_ops"} && &List::Util::first(sub { $wsop =~ /$_->{"regex"}/; } , @BUILTIN_SPECIAL_OPS)
our $Expand_Depth == 1 $is_special $self->{"disable_old_special_ops"} &List::Util::first(sub { $wsop =~ /$_->{"regex"}/; } , @BUILTIN_SPECIAL_OPS)
Truth table80%4/5
A and B or C and D
A= our $Expand_Depth == 1B= $is_specialC= $self->{"disable_old_special_ops"}D= &List::Util::first(sub { $wsop =~ /$_->{"regex"}/; } , @BUILTIN_SPECIAL_OPS)
ABCDresult
0XXX0
100X0
10100
11XX1
10111
1107 (our $Expand_Depth) == 1
1108 and (
1109 $is_special
1110 or (
1111 $self->{disable_old_special_ops}
1112 36 ▶ and List::Util::first { $wsop =~ $_->{regex} } @BUILTIN_SPECIAL_OPS
Subroutine __ANON__: called CC=1 SCAR=0.0
1113 )
1114 )
1115 ) {
1116 4 puke "Illegal use of top-level '-$wsop'"
1117 }
1118 }
1119  
1120 5547 ▶ if (my $exp = $self->{expand}{$op}||$self->{expand_op}{$op}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exp = $self->{"expand"}{$op} || $self->{"expand_op"}{$op}) 5356 191 5547
Condition100%3/3
$self->{'expand'}{$op} || $self->{'expand_op'}{$op}
l!l&&r!l&&!r
10584298191
MC/DC100%2/2
$self->{'expand'}{$op} || $self->{'expand_op'}{$op}
$self->{'expand'}{$op} $self->{'expand_op'}{$op}
Truth table100%3/3
A || B
A= $self->{'expand'}{$op}B= $self->{'expand_op'}{$op}
ABresult
1X1
011
000
1121 5356 return $self->$exp($op, $v);
1122 }
1123  
1124 191 ▶ if ($self->{render}{$op}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"render"}{$op}) 133 58 191
1125 133 return { "-${op}" => $v };
1126 }
1127  
1128 # Ops prefixed with -not_ get converted
1129  
1130 58 ▶ if (my ($rest) = $op =~/^not_(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($rest) = $op =~ /^not_(.*)$/) 15 43 58
1131 15 return +{ -op => [
1132 'not',
1133 $self->_expand_expr({ "-${rest}", $v })
1134 ] };
1135 }
1136  
1137 { # Old SQLA compat
1138  
1139 # the old unary op system means we should touch nothing and let it work
1140  
1141 43 my $op = join(' ', split '_', $op);
1142  
1143 43 ▶ if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $us = &List::Util::first(sub { $op =~ /$_->{"regex"}/; } , @{$$self{"unary_ops"};})) 1 42 43
1144 1 return { -op => [ $op, $v ] };
1145 }
1146 }
1147  
1148 42 ▶ my $type = $is_special || $self->{render_op}{$op} ? -op : -func;
Branch: 1 branch
BranchTrueFalseTotal
$is_special || $self->{'render_op'}{$op} ? : 11 31 42
Condition100%3/3
$is_special or $self->{"render_op"}{$op}
l!l&&r!l&&!r
11031
MC/DC100%2/2
$is_special or $self->{"render_op"}{$op}
$is_special $self->{"render_op"}{$op}
Truth table100%3/3
A or B
A= $is_specialB= $self->{"render_op"}{$op}
ABresult
1X1
011
000
1149  
1150 42 ▶ if ($self->{restore_old_unop_handling}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"restore_old_unop_handling"}) 11 31 42
1151  
1152 # Old SQLA compat
1153  
1154 11 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'HASH' and keys %$v == 1 and (keys %$v)[0] =~ /^-/ and not $self->{'render_op'}{$op} and not $is_special) { } 4 7 11
Condition66%8/12
ref $v eq "HASH" and keys %$v == 1 and (keys %$v)[0] =~ /^-/ and not $self->{"render_op"}{$op} and not $is_special
!ll&&!rl&&r
704
ref $v eq "HASH" and keys %$v == 1 and (keys %$v)[0] =~ /^-/ and not $self->{"render_op"}{$op}
!ll&&!rl&&r
704
ref $v eq "HASH" and keys %$v == 1 and (keys %$v)[0] =~ /^-/
!ll&&!rl&&r
704
ref $v eq "HASH" and keys %$v == 1
!ll&&!rl&&r
704
MC/DC20%1/5
ref $v eq "HASH" and keys %$v == 1 and (keys %$v)[0] =~ /^-/ and not $self->{"render_op"}{$op} and not $is_special
ref $v eq "HASH" keys %$v == 1 (keys %$v)[0] =~ /^-/ not $self->{"render_op"}{$op} not $is_special
Truth table33%2/6
A and B and C and not(D) and not(E)
A= ref $v eq "HASH"B= keys %$v == 1C= (keys %$v)[0] =~ /^-/D= not $self->{"render_op"}{$op}E= not $is_special
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
1155 ref($v) eq 'HASH'
1156 and keys %$v == 1
1157 and (keys %$v)[0] =~ /^-/
1158 and not $self->{render_op}{$op}
1159 and not $is_special
1160 ) {
1161 4 $type = -func;
1162 } else {
1163 7 $type = -op;
1164 }
1165 }
1166  
1167 42 ▶ if ($type eq -func and ref($v) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "-func" and ref $v eq "ARRAY") 1 41 42
Condition100%3/3
$type eq "-func" and ref $v eq "ARRAY"
!ll&&!rl&&r
18231
MC/DC100%2/2
$type eq "-func" and ref $v eq "ARRAY"
$type eq "-func" ref $v eq "ARRAY"
Truth table100%3/3
A and B
A= $type eq "-func"B= ref $v eq "ARRAY"
ABresult
0X0
100
111
1168 1 return $self->_expand_expr({ -func => [ $op, @$v ] });
1169 }
1170  
1171 41 return $self->_expand_expr({ $type => [ $op, $v ] });
1172 }
1173  
1174 506 ▶sub _expand_hashpair_cmp {
Subroutine _expand_hashpair_cmp: called CC=1 SCAR=0.0
1175 506 my ($self, $k, $v) = @_;
1176 506 $self->_expand_hashtriple($k, $self->{cmp}, $v);
1177 }
1178  
1179 1229 ▶sub _expand_hashtriple {
Subroutine _expand_hashtriple: called CC=16 SCAR=27.7
1180 1229 my ($self, $k, $vk, $vv) = @_;
1181  
1182 1229 my $ik = $self->_expand_expr({ -ident => $k });
1183  
1184 1228 my $op = $self->_normalize_op($vk);
1185 1228 $self->_assert_pass_injection_guard($op);
1186  
1187 1226 ▶ if ($op =~ s/ _? \d+ $//x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ s/ _? \d+ $//x) 0 1226 1226
1188 0 return $self->_expand_expr($k, { $vk, $vv });
1189 }
1190 1226 ▶ if (my $x = $self->{expand_op}{$op}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $x = $self->{"expand_op"}{$op}) 253 973 1226
1191 253 local our $Cur_Col_Meta = $k;
1192 253 return $self->$x($op, $vv, $k);
1193 }
1194 { # Old SQLA compat
1195  
1196 973 my $op = join(' ', split '_', $op);
1197  
1198 973 ▶ if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $us = &List::Util::first(sub { $op =~ /$_->{"regex"}/; } , @{$$self{"special_ops"};})) 4 969 973
1199 4 return { -op => [ $op, $ik, $vv ] };
1200 }
1201 969 ▶ if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $us = &List::Util::first(sub { $op =~ /$_->{"regex"}/; } , @{$$self{"unary_ops"};})) 1 968 969
1202 return { -op => [
1203 $self->{cmp},
1204 1 $ik,
1205 { -op => [ $op, $vv ] }
1206 ] };
1207 }
1208 }
1209 968 ▶ if (ref($vv) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $vv eq "ARRAY") 149 819 968
1210 149 my @raw = @$vv;
1211 149 ▶ my $logic = (defined($raw[0]) and $raw[0] =~ /^-(and|or)$/i)
Branch: 1 branch
BranchTrueFalseTotal
defined $raw[0] && $raw[0] =~ /^-(and|or)$/i ? : 2 147 149
Condition100%3/3
defined $raw[0] and $raw[0] =~ /^-(and|or)$/i
!ll&&!rl&&r
75722
MC/DC100%2/2
defined $raw[0] and $raw[0] =~ /^-(and|or)$/i
defined $raw[0] $raw[0] =~ /^-(and|or)$/i
Truth table100%3/3
A and B
A= defined $raw[0]B= $raw[0] =~ /^-(and|or)$/i
ABresult
0X0
100
111
1212 ? (shift(@raw), lc $1) : 'or';
1213 149 my @values = map +{ $vk => $_ }, @raw;
1214 149 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /$self->{"inequality_op"}/xi or $op =~ /$self->{"not_like_op"}/) 127 22 149
Condition100%3/3
$op =~ /$self->{"inequality_op"}/xi or $op =~ /$self->{"not_like_op"}/
l!l&&r!l&&!r
309722
MC/DC100%2/2
$op =~ /$self->{"inequality_op"}/xi or $op =~ /$self->{"not_like_op"}/
$op =~ /$self->{"inequality_op"}/xi $op =~ /$self->{"not_like_op"}/
Truth table100%3/3
A or B
A= $op =~ /$self->{"inequality_op"}/xiB= $op =~ /$self->{"not_like_op"}/
ABresult
1X1
011
000
1215 $op =~ $self->{inequality_op}
1216 or $op =~ $self->{not_like_op}
1217 ) {
1218 127 ▶ if (lc($logic) eq 'or' and @values > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $logic eq "or" and @values > 1) 36 91 127
Condition100%3/3
lc $logic eq "or" and @values > 1
!ll&&!rl&&r
28936
MC/DC100%2/2
lc $logic eq "or" and @values > 1
lc $logic eq "or" @values > 1
Truth table100%3/3
A and B
A= lc $logic eq "or"B= @values > 1
ABresult
0X0
100
111
1219 36 belch "A multi-element arrayref as an argument to the inequality op '${\uc(join ' ', split '_', $op)}' "
1220 . 'is technically equivalent to an always-true 1=1 (you probably wanted '
1221 . "to say ...{ \$inequality_op => [ -and => \@values ] }... instead)"
1222 ;
1223 }
1224 }
1225 149 ▶ unless (@values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@values) 43 106 149
1226 # try to DWIM on equality operators
1227 43 ▶ return ($self->_dwim_op_to_is($op,
Branch: 1 branch
BranchTrueFalseTotal
$self->_dwim_op_to_is($op, q[Supplying an empty arrayref to '%s' is deprecated], "operator '%s' applied on an empty array (field '${k}')") ? : 16 25 41
1228 "Supplying an empty arrayref to '%s' is deprecated",
1229 "operator '%s' applied on an empty array (field '$k')"
1230 ) ? $self->sqlfalse : $self->sqltrue);
1231 }
1232 106 return $self->_expand_logop($logic => \@values, $k);
1233 }
1234 819 ▶ if (is_undef_value($vv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_undef_value($vv)) 100 719 819
1235 100 ▶ my $is = ($self->_dwim_op_to_is($op,
Branch: 1 branch
BranchTrueFalseTotal
$self->_dwim_op_to_is($op, q[Supplying an undefined argument to '%s' is deprecated], q[unexpected operator '%s' with undef operand]) ? : 40 60 100
1236 "Supplying an undefined argument to '%s' is deprecated",
1237 "unexpected operator '%s' with undef operand",
1238 ) ? 'is' : 'is not');
1239  
1240 100 return $self->_expand_hashpair($k => { $is, undef });
1241 }
1242 719 local our $Cur_Col_Meta = $k;
1243 719 return +{ -op => [
1244 $op,
1245 $ik,
1246 $self->_expand_expr($vv)
1247 ] };
1248 }
1249  
1250 143 ▶sub _dwim_op_to_is {
Subroutine _dwim_op_to_is: called CC=6 SCAR=17.9
1251 143 my ($self, $raw, $empty, $fail) = @_;
1252  
1253 143 my $op = $self->_normalize_op($raw);
1254  
1255 143 ▶ if ($op =~ /^not$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /^not$/i) 8 135 143
1256 8 return 0;
1257 }
1258 135 ▶ if ($op =~ $self->{equality_op}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /$self->{"equality_op"}/) 31 104 135
1259 31 return 1;
1260 }
1261 104 ▶ if ($op =~ $self->{like_op}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /$self->{"like_op"}/) 25 79 104
1262 25 belch(sprintf $empty, uc(join ' ', split '_', $op));
1263 25 return 1;
1264 }
1265 79 ▶ if ($op =~ $self->{inequality_op}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /$self->{"inequality_op"}/) 21 58 79
1266 21 return 0;
1267 }
1268 58 ▶ if ($op =~ $self->{not_like_op}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /$self->{"not_like_op"}/) 56 2 58
1269 56 belch(sprintf $empty, uc(join ' ', split '_', $op));
1270 56 return 0;
1271 }
1272 2 puke(sprintf $fail, $op);
1273 }
1274  
1275 32 ▶sub _expand_func {
Subroutine _expand_func: called CC=1 SCAR=0.0
1276 32 my ($self, undef, $args) = @_;
1277 32 my ($func, @args) = @$args;
1278 32 return +{ -func => [ $func, map $self->expand_expr($_), @args ] };
1279 }
1280  
1281 2427 ▶sub _expand_ident {
Subroutine _expand_ident: called CC=10 SCAR=23.3
1282 2427 my ($self, undef, $body) = @_;
1283 2427 ▶ unless (defined($body) or (ref($body) and ref($body) eq 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body or ref $body and ref $body eq "ARRAY") 2 2425 2427
Condition50%3/6
defined $body or ref $body and ref $body eq "ARRAY"
l!l&&r!l&&!r
242502
ref $body and ref $body eq "ARRAY"
!ll&&!rl&&r
200
MC/DC33%1/3
defined $body or ref $body and ref $body eq "ARRAY"
defined $body ref $body ref $body eq "ARRAY"
Truth table50%2/4
A or B and C
A= defined $bodyB= ref $bodyC= ref $body eq "ARRAY"
ABCresult
1XX1
0111
00X0
0100
1284 2 puke "-ident requires a single plain scalar argument (a quotable identifier) or an arrayref of identifier parts";
1285 }
1286 2425 ▶ my ($sep) = map +(defined() ? $_ : '.') , $self->{name_sep};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 727 1698 2425
1287 2425 ▶ my @parts = map +($sep
Branch: 2 branches
BranchTrueFalseTotal
$sep ? : 2349 76 2425
ref $body ? : 108 2317 2425
1288 ? map split(/\Q${sep}\E/, $_), @$_
1289 : @$_
1290 ), ref($body) ? $body : [ $body ];
1291 2425 ▶ return { -ident => $parts[-1] } if $self->{_dequalify_idents};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_dequalify_idents"} 0 2425 2425
1292 2425 ▶ unless ($self->{quote_char}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"quote_char"}) 1651 774 2425
1293 1651 $self->_assert_pass_injection_guard($_) for @parts;
1294 }
1295 2424 return +{ -ident => \@parts };
1296 }
1297  
1298 1106 ▶sub _expand_value {
Subroutine _expand_value: called CC=1 SCAR=0.0
1299 1106 +{ -bind => [ our $Cur_Col_Meta, $_[2] ] };
1300 }
1301  
1302 33 ▶sub _expand_row {
Subroutine _expand_row: called CC=1 SCAR=0.0
1303 33 my ($self, undef, $args) = @_;
1304 33 +{ -row => [ map $self->expand_expr($_), @$args ] };
1305 }
1306  
1307 127 ▶sub _expand_op {
Subroutine _expand_op: called CC=3 SCAR=11.1
1308 127 my ($self, undef, $args) = @_;
1309 127 my ($op, @opargs) = @$args;
1310 127 ▶ if (my $exp = $self->{expand_op}{$op}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exp = $self->{"expand_op"}{$op}) 1 126 127
1311 1 return $self->$exp($op, \@opargs);
1312 }
1313 126 ▶ if (List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
if (&List::Util::first(sub { $op =~ /$_->{"regex"}/; } , @{$$self{"unary_ops"};})) 0 126 126
1314 0 return { -op => [ $op, @opargs ] };
1315 }
1316 126 +{ -op => [ $op, map $self->expand_expr($_), @opargs ] };
1317 }
1318  
1319 24 ▶sub _expand_bool {
Subroutine _expand_bool: called CC=3 SCAR=11.0
1320 24 my ($self, undef, $v) = @_;
1321 24 ▶ if (ref($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) 10 14 24
1322 10 return $self->_expand_expr($v);
1323 }
1324 14 ▶ puke "-bool => undef not supported" unless defined($v);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 14 14
1325 14 return $self->_expand_expr({ -ident => $v });
1326 }
1327  
1328 392 ▶sub _expand_list {
Subroutine _expand_list: called CC=5 SCAR=16.1
1329 392 my ($self, undef, $expr) = @_;
1330 return { -op => [
1331 ',', map $self->expand_expr($_),
1332 5 @{$expr->{-op}}[1..$#{$expr->{-op}}]
1333 392 ▶ ] } if ref($expr) eq 'HASH' and ($expr->{-op}||[''])->[0] eq ',';
Branch: 1 branch
BranchTrueFalseTotal
if ref $expr eq "HASH" and +($expr->{"-op"} || [""])->[0] eq "," 5 387 392
Condition100%5/5
ref $expr eq "HASH" and +($expr->{"-op"} || [""])->[0] eq ","
!ll&&!rl&&r
292955
$expr->{'-op'} || ['']
l!l
595
MC/DC100%2/2
ref $expr eq "HASH" and +($expr->{"-op"} || [""])->[0] eq ","
ref $expr eq "HASH" $expr->{'-op'}
Truth table100%3/3
A and B
A= ref $expr eq "HASH"B= $expr->{'-op'}
ABresult
0X0
100
111
1334 387 ▶ return +{ -op => [ ',',
Branch: 1 branch
BranchTrueFalseTotal
ref $expr eq 'ARRAY' ? : 88 299 387
1335 map $self->expand_expr($_),
1336 ref($expr) eq 'ARRAY' ? @$expr : $expr
1337 ] };
1338 }
1339  
1340 453 ▶sub _expand_logop {
Subroutine _expand_logop: called CC=1 SCAR=0.0
1341 453 my ($self, $logop, $v, $k) = @_;
1342 453 $self->${\$self->{expand_op}{$logop}}($logop, $v, $k);
1343 }
1344  
1345 584 ▶sub _expand_op_andor {
Subroutine _expand_op_andor: called CC=21 SCAR=30.7
1346 584 my ($self, $logop, $v, $k) = @_;
1347 584 ▶ if (defined $k) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $k) 160 424 584
1348 $v = [ map +{ $k, $_ },
1349 (ref($v) eq 'HASH')
1350 160 ▶ ? (map +{ $_ => $v->{$_} }, sort keys %$v)
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'HASH' ? : 8 152 160
1351 : @$v,
1352 ];
1353 }
1354 584 ▶ if (ref($v) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "HASH") 212 372 584
1355 212 ▶ return undef unless keys %$v;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$v 0 212 212
1356 return +{ -op => [
1357 $logop,
1358 212 map $self->_expand_expr({ $_ => $v->{$_} }),
1359 sort keys %$v
1360 ] };
1361 }
1362 372 ▶ if (ref($v) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "ARRAY") 372 0 372
1363 372 ▶ $logop eq 'and' or $logop eq 'or' or puke "unknown logic: $logop";
Branch: 1 branch
BranchTrueFalseTotal
$logop eq "and" or $logop eq "or" or &puke("unknown logic: $logop") 372 0 372
Condition66%2/3
$logop eq "and" or $logop eq "or"
l!l&&r!l&&!r
802920
MC/DC0%0/2
$logop eq "and" or $logop eq "or"
$logop eq "and" $logop eq "or"
Truth table66%2/3
A or B
A= $logop eq "and"B= $logop eq "or"
ABresult
1X1
011
000
1364  
1365 my @expr = grep {
1366 372 ▶ (ref($_) eq 'ARRAY' and @$_)
Condition100%11/11
ref $_ eq 'ARRAY' && @$_ || ref $_ eq 'HASH' && %$_ || 1
l!l
434575
ref $_ eq "ARRAY" and @$_ or ref $_ eq "HASH" and %$_
l!l&&r!l&&!r
74360575
ref $_ eq "ARRAY" and @$_
!ll&&!rl&&r
933274
ref $_ eq "HASH" and %$_
!ll&&!rl&&r
5723360
MC/DC100%4/4
ref $_ eq 'ARRAY' && @$_ || ref $_ eq 'HASH' && %$_ || 1
ref $_ eq "ARRAY" @$_ ref $_ eq "HASH" %$_
Truth table71%5/7
A and B or C and D
A= ref $_ eq "ARRAY"B= @$_C= ref $_ eq "HASH"D= %$_
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1367 or (ref($_) eq 'HASH' and %$_)
1368 or 1
1369 } @$v;
1370  
1371 372 my @res;
1372  
1373 372 ▶ while (my ($el) = splice @expr, 0, 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($el) = splice(@expr, 0, 1)) 651 312 963
1374 651 ▶ puke "Supplying an empty left hand side argument is not supported in array-pairs"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $el and length $el 44 607 651
Condition100%3/3
defined $el and length $el
!ll&&!rl&&r
2222607
MC/DC100%2/2
defined $el and length $el
defined $el length $el
Truth table100%3/3
A and B
A= defined $elB= length $el
ABresult
0X0
100
111
1375 unless defined($el) and length($el);
1376 607 my $elref = ref($el);
1377 607 ▶ if (!$elref) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $elref) { } 270 337 607
elsif ($elref eq 'ARRAY') { } 18 319 337
elsif (my $l = &is_literal_value($el)) { } 1 318 319
elsif ($elref eq 'HASH') { } 318 0 318
1378 270 local our $Expand_Depth = 0;
1379 270 push(@res, grep defined, $self->_expand_expr({ $el, shift(@expr) }));
1380 } elsif ($elref eq 'ARRAY') {
1381 18 ▶ push(@res, grep defined, $self->_expand_expr($el)) if @$el;
Branch: 1 branch
BranchTrueFalseTotal
if @$el 16 2 18
1382 } elsif (my $l = is_literal_value($el)) {
1383 1 push @res, { -literal => $l };
1384 } elsif ($elref eq 'HASH') {
1385 318 local our $Expand_Depth = 0;
1386 318 ▶ push @res, grep defined, $self->_expand_expr($el) if %$el;
Branch: 1 branch
BranchTrueFalseTotal
if %$el 315 3 318
1387 } else {
1388 0 die "notreached";
1389 }
1390 }
1391 # ???
1392 # return $res[0] if @res == 1;
1393 312 return { -op => [ $logop, @res ] };
1394 }
1395 0 die "notreached";
1396 }
1397  
1398 141 ▶sub _expand_op_is {
Subroutine _expand_op_is: called CC=6 SCAR=20.3
1399 141 my ($self, $op, $vv, $k) = @_;
1400 141 ▶ ($k, $vv) = @$vv unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 1 140 141
1401 puke "$op can only take undef as argument"
1402 if defined($vv)
1403 and not (
1404 ref($vv) eq 'HASH'
1405 and exists($vv->{-value})
1406 and !defined($vv->{-value})
1407 141 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if defined $vv and not ref $vv eq "HASH" && exists $vv->{"-value"} && !defined($vv->{"-value"}) 0 141 141
Condition44%4/9
defined $vv and not ref $vv eq "HASH" && exists $vv->{"-value"} && !defined($vv->{"-value"})
!ll&&!rl&&r
13380
ref $vv eq 'HASH' && exists $vv->{'-value'} && !defined($vv->{'-value'})
!ll&&!rl&&r
008
ref $vv eq "HASH" and exists $vv->{"-value"}
!ll&&!rl&&r
008
MC/DC0%0/4
defined $vv and not ref $vv eq "HASH" && exists $vv->{"-value"} && !defined($vv->{"-value"})
defined $vv ref $vv eq "HASH" exists $vv->{"-value"} !defined($vv->{'-value'})
Truth table40%2/5
A and not(B and C && not(D))
A= defined $vvB= ref $vv eq "HASH"C= exists $vv->{"-value"}D= !defined($vv->{'-value'})
ABCDresult
0XXX0
11110
10XX1
110X1
11101
1408 141 return +{ -op => [ $op.'_null', $self->expand_expr($k, -ident) ] };
1409 }
1410  
1411 59 ▶sub _expand_between {
Subroutine _expand_between: called CC=8 SCAR=20.8
1412 59 my ($self, $op, $vv, $k) = @_;
1413 59 ▶ my @rhs = map $self->_expand_expr($_),
Branch: 1 branch
BranchTrueFalseTotal
ref $vv eq 'ARRAY' ? : 45 14 59
1414 ref($vv) eq 'ARRAY' ? @$vv : $vv;
1415 59 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (@rhs == 1 and ref $rhs[0] eq "HASH" and $rhs[0]{"-literal"} or @rhs == 2 and defined $rhs[0] and defined $rhs[1]) 13 46 59
Condition100%15/15
@rhs == 1 and ref $rhs[0] eq "HASH" and $rhs[0]{"-literal"} or @rhs == 2 and defined $rhs[0] and defined $rhs[1]
l!l&&r!l&&!r
123413
@rhs == 1 and ref $rhs[0] eq "HASH" and $rhs[0]{"-literal"}
!ll&&!rl&&r
44312
@rhs == 1 and ref $rhs[0] eq "HASH"
!ll&&!rl&&r
42215
@rhs == 2 and defined $rhs[0] and defined $rhs[1]
!ll&&!rl&&r
12134
@rhs == 2 and defined $rhs[0]
!ll&&!rl&&r
10235
MC/DC100%6/6
@rhs == 1 and ref $rhs[0] eq "HASH" and $rhs[0]{"-literal"} or @rhs == 2 and defined $rhs[0] and defined $rhs[1]
@rhs == 1 ref $rhs[0] eq "HASH" $rhs[0]{"-literal"} @rhs == 2 defined $rhs[0] defined $rhs[1]
Truth table53%7/13
A and B and C or D and E and F
A= @rhs == 1B= ref $rhs[0] eq "HASH"C= $rhs[0]{"-literal"}D= @rhs == 2E= defined $rhs[0]F= defined $rhs[1]
ABCDEFresult
111XXX1
0XX1111
10X1111
1101111
0XX0XX0
0XX10X0
0XX1100
10X0XX0
10X10X0
10X1100
1100XX0
11010X0
1101100
1416 (@rhs == 1 and ref($rhs[0]) eq 'HASH' and $rhs[0]->{-literal})
1417 or
1418 (@rhs == 2 and defined($rhs[0]) and defined($rhs[1]))
1419 ) {
1420 13 puke "Operator '${\uc($op)}' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref";
1421 }
1422 46 return +{ -op => [
1423 $op,
1424 $self->expand_expr($k),
1425 map $self->expand_expr($_, -value), @rhs
1426 ] }
1427 }
1428  
1429 42 ▶sub _expand_in {
Subroutine _expand_in: called CC=7 SCAR=19.5
1430 42 my ($self, $op, $vv, $k) = @_;
1431 42 ▶ if (my $literal = is_literal_value($vv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $literal = &is_literal_value($vv)) 11 31 42
1432 11 my ($sql, @bind) = @$literal;
1433 11 my $opened_sql = $self->_open_outer_paren($sql);
1434 11 return +{ -op => [
1435 $op, $self->expand_expr($k, -ident),
1436 { -literal => [ $opened_sql, @bind ] }
1437 ] };
1438 }
1439 31 my $undef_err =
1440 'SQL::Abstract before v1.75 used to generate incorrect SQL when the '
1441 31 . "-${\uc($op)} operator was given an undef-containing list: !!!AUDIT YOUR CODE "
1442 . 'AND DATA!!! (the upcoming Data::Query-based version of SQL::Abstract '
1443 . 'will emit the logically correct SQL instead of raising this exception)'
1444 ;
1445 31 ▶ puke("Argument passed to the '${\uc($op)}' operator can not be undefined")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vv 1 30 31
1446 if !defined($vv);
1447 my @rhs = map $self->expand_expr($_, -value),
1448 30 ▶ map { defined($_) ? $_: puke($undef_err) }
Branch: 2 branches
BranchTrueFalseTotal
defined $_ ? : 71 4 75
ref $vv eq 'ARRAY' ? : 27 3 30
1449 (ref($vv) eq 'ARRAY' ? @$vv : $vv);
1450 26 ▶ return $self->${\($op =~ /^not/ ? 'sqltrue' : 'sqlfalse')} unless @rhs;
Branch: 2 branches
BranchTrueFalseTotal
unless @rhs 3 23 26
$op =~ /^not/ ? : 1 2 3
1451  
1452 23 return +{ -op => [
1453 $op,
1454 $self->expand_expr($k, -ident),
1455 @rhs
1456 ] };
1457 }
1458  
1459 26 ▶sub _expand_nest {
Subroutine _expand_nest: called CC=3 SCAR=16.0
1460 26 my ($self, undef, $v) = @_;
1461 # DBIx::Class requires a nest warning to be emitted once but the private
1462 # method it overrode to do so no longer exists
1463 26 ▶ if ($self->{warn_once_on_nest}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"warn_once_on_nest"}) 0 26 26
1464 0 ▶ unless (our $Nest_Warned) {
Branch: 1 branch
BranchTrueFalseTotal
unless (our $Nest_Warned) 0 0 0
1465 0 belch(
1466 "-nest in search conditions is deprecated, you most probably wanted:\n"
1467 .q|{..., -and => [ \%cond0, \@cond1, \'cond2', \[ 'cond3', [ col => bind ] ], etc. ], ... }|
1468 );
1469 0 $Nest_Warned = 1;
1470 }
1471 }
1472 26 return $self->_expand_expr($v);
1473 }
1474  
1475 3 ▶sub _expand_values {
Subroutine _expand_values: called CC=3 SCAR=11.1
1476 3 my ($self, undef, $values) = @_;
1477 3 ▶ return { -values => [
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 2 5 7
ref $values eq 'ARRAY' ? : 3 0 3
1478 map +(
1479 ref($_) eq 'HASH'
1480 ? $self->expand_expr($_)
1481 : +{ -row => [ map $self->expand_expr($_), @$_ ] }
1482 ), ref($values) eq 'ARRAY' ? @$values : $values
1483 ] };
1484 }
1485  
1486 461 ▶sub _recurse_where {
Subroutine _recurse_where: called CC=5 SCAR=17.5
1487 461 my ($self, $where) = @_;
1488  
1489 # Special case: top level simple string treated as literal
1490  
1491 461 ▶ my $where_exp = (ref($where)
Branch: 1 branch
BranchTrueFalseTotal
ref $where ? : 460 1 461
1492 ? $self->_expand_select_clause_where(undef, $where)
1493 : { -literal => [ $where ] });
1494  
1495 # dispatch expanded expression
1496  
1497 365 ▶ my ($sql, @bind) = defined($where_exp) ? @{ $self->render_aqt($where_exp) || [] } : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $where_exp ? : 365 0 365
Condition50%1/2
$self->render_aqt($where_exp) || []
l!l
3650
MC/DC0%0/1
$self->render_aqt($where_exp) || []
$self->render_aqt($where_exp)
Truth table50%1/2
A
A= $self->render_aqt($where_exp)
Aresult
11
00
1498 # DBIx::Class used to call _recurse_where in scalar context
1499 # something else might too...
1500 365 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 365 0 365
1501 365 return ($sql, @bind);
1502 }
1503 else {
1504 0 belch "Calling _recurse_where in scalar context is deprecated and will go away before 2.0";
1505 0 return $sql;
1506 }
1507 }
1508  
1509 1771 ▶sub _render_ident {
Subroutine _render_ident: called CC=1 SCAR=0.0
1510 1771 my ($self, undef, $ident) = @_;
1511  
1512 1771 return [ $self->_quote($ident) ];
1513 }
1514  
1515 87 ▶sub _render_row {
Subroutine _render_row: called CC=1 SCAR=0.0
1516 87 my ($self, undef, $values) = @_;
1517 87 return $self->join_query_parts('',
1518 '(',
1519 $self->_render_op(undef, [ ',', @$values ]),
1520 ')'
1521 );
1522 }
1523  
1524 73 ▶sub _render_func {
Subroutine _render_func: called CC=1 SCAR=0.0
1525 73 my ($self, undef, $rest) = @_;
1526 73 my ($func, @args) = @$rest;
1527 73 return $self->join_query_parts('',
1528 $self->_sqlcase($func),
1529 $self->join_query_parts('',
1530 '(',
1531 $self->join_query_parts(', ', @args),
1532 ')'
1533 ),
1534 );
1535 }
1536  
1537 1087 ▶sub _render_bind {
Subroutine _render_bind: called CC=1 SCAR=0.0
1538 1087 my ($self, undef, $bind) = @_;
1539 1087 return [ '?', $self->_bindtype(@$bind) ];
1540 }
1541  
1542 354 ▶sub _render_literal {
Subroutine _render_literal: called CC=1 SCAR=0.0
1543 354 my ($self, undef, $literal) = @_;
1544 354 $self->_assert_bindval_matches_bindtype(@{$literal}[1..$#$literal]);
1545 346 return $literal;
1546 }
1547  
1548 2279 ▶sub _render_keyword {
Subroutine _render_keyword: called CC=2 SCAR=6.9
1549 2279 my ($self, undef, $keyword) = @_;
1550 2279 ▶ return [ $self->_sqlcase(
Branch: 1 branch
BranchTrueFalseTotal
ref $keyword ? : 27 2252 2279
1551 ref($keyword) ? $$keyword : join ' ', split '_', $keyword
1552 ) ];
1553 }
1554  
1555 2239 ▶sub _render_op {
Subroutine _render_op: called CC=9 SCAR=22.0
1556 2239 my ($self, undef, $v) = @_;
1557 2239 my ($op, @args) = @$v;
1558 2239 ▶ if (my $r = $self->{render_op}{$op}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $self->{"render_op"}{$op}) 1434 805 2239
1559 1434 return $self->$r($op, \@args);
1560 }
1561  
1562 { # Old SQLA compat
1563  
1564 805 my $op = join(' ', split '_', $op);
1565  
1566 805 ▶ my $ss = List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}};
Subroutine __ANON__: called CC=1 SCAR=0.0
1567 805 ▶ if ($ss and @args > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ss and @args > 1) 4 801 805
Condition100%3/3
$ss and @args > 1
!ll&&!rl&&r
80014
MC/DC100%2/2
$ss and @args > 1
$ss @args > 1
Truth table100%3/3
A and B
A= $ssB= @args > 1
ABresult
0X0
100
111
1568 puke "Special op '${op}' requires first value to be identifier"
1569 4 ▶ unless my ($ident) = map $_->{-ident}, grep ref($_) eq 'HASH', $args[0];
Branch: 1 branch
BranchTrueFalseTotal
unless my($ident) = map($_->{"-ident"}, grep((ref $_ eq "HASH"), $args[0])) 0 4 4
1570 4 ▶ my $k = join(($self->{name_sep}||'.'), @$ident);
Condition50%1/2
$self->{'name_sep'} || '.'
l!l
04
MC/DC0%0/1
$self->{'name_sep'} || '.'
$self->{'name_sep'}
Truth table50%1/2
A
A= $self->{'name_sep'}
Aresult
11
00
1571 4 local our $Expand_Depth = 1;
1572 4 return [ $self->${\($ss->{handler})}($k, $op, $args[1]) ];
1573 }
1574 801 ▶ if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (my $us = &List::Util::first(sub { $op =~ /$_->{"regex"}/; } , @{$$self{"unary_ops"};})) 2 799 801
1575 2 return [ $self->${\($us->{handler})}($op, $args[0]) ];
1576 }
1577 799 ▶ if ($ss) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ss) 1 798 799
1578 1 return $self->_render_unop_paren($op, \@args);
1579 }
1580 }
1581 798 ▶ if (@args == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1) { } 8 790 798
1582 8 return $self->_render_unop_prefix($op, \@args);
1583 } else {
1584 790 return $self->_render_op_multop($op, \@args);
1585 }
1586 0 die "notreached";
1587 }
1588  
1589  
1590 44 ▶sub _render_op_between {
Subroutine _render_op_between: called CC=3 SCAR=11.0
1591 44 my ($self, $op, $args) = @_;
1592 44 my ($left, $low, $high) = @$args;
1593 44 my @rh = do {
1594 44 ▶ if (@$args == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args == 2) { } 12 32 44
1595 puke "Single arg to between must be a literal"
1596 12 ▶ unless $low->{-literal};
Branch: 1 branch
BranchTrueFalseTotal
unless $low->{"-literal"} 0 12 12
1597 12 $low;
1598 } else {
1599 32 +($low, { -keyword => 'and' }, $high);
1600 }
1601 };
1602 44 return $self->join_query_parts(' ',
1603 '(', $left, { -keyword => $op }, @rh, ')',
1604 );
1605 }
1606  
1607 34 ▶sub _render_op_in {
Subroutine _render_op_in: called CC=1 SCAR=0.0
1608 34 my ($self, $op, $args) = @_;
1609 34 my ($lhs, @rhs) = @$args;
1610  
1611 34 return $self->join_query_parts(' ',
1612 $lhs,
1613 { -keyword => $op },
1614 $self->join_query_parts(' ',
1615 '(',
1616 $self->join_query_parts(', ', @rhs),
1617 ')'
1618 ),
1619 );
1620 }
1621  
1622 502 ▶sub _render_op_andor {
Subroutine _render_op_andor: called CC=5 SCAR=16.1
1623 502 my ($self, $op, $args) = @_;
1624 502 ▶ return undef unless @$args;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args 3 499 502
1625 499 ▶ return $self->join_query_parts('', $args->[0]) if @$args == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$args == 1 124 375 499
1626 375 my $inner = $self->_render_op_multop($op, $args);
1627 371 ▶ return undef unless defined($inner->[0]) and length($inner->[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inner->[0] and length $inner->[0] 1 370 371
Condition66%2/3
defined $inner->[0] and length $inner->[0]
!ll&&!rl&&r
01370
MC/DC50%1/2
defined $inner->[0] and length $inner->[0]
defined $inner->[0] length $inner->[0]
Truth table66%2/3
A and B
A= defined $inner->[0]B= length $inner->[0]
ABresult
0X0
100
111
1628 370 return $self->join_query_parts(' ',
1629 '(', $inner, ')'
1630 );
1631 }
1632  
1633 1708 ▶sub _render_op_multop {
Subroutine _render_op_multop: called CC=4 SCAR=13.9
1634 1708 my ($self, $op, $args) = @_;
1635 1708 my @parts = @$args;
1636 1708 ▶ return undef unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 0 1708 1708
1637 1708 ▶ return $self->render_aqt($parts[0]) if @parts == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parts == 1 370 1338 1708
1638 1338 ▶ my $join = ($op eq ','
Branch: 1 branch
BranchTrueFalseTotal
$op eq ',' ? : 173 1165 1338
1639 ? ', '
1640 : { -keyword => " ${op} " }
1641 );
1642 1338 return $self->join_query_parts($join, @parts);
1643 }
1644  
1645 51 ▶sub _render_values {
Subroutine _render_values: called CC=3 SCAR=11.1
1646 51 my ($self, undef, $values) = @_;
1647 51 ▶ my $inner = $self->join_query_parts(' ',
Branch: 1 branch
BranchTrueFalseTotal
ref $values eq 'ARRAY' ? : 51 0 51
1648 { -keyword => 'values' },
1649 $self->join_query_parts(', ',
1650 ref($values) eq 'ARRAY' ? @$values : $values
1651 ),
1652 );
1653 49 ▶ return $self->join_query_parts('',
Branch: 1 branch
BranchTrueFalseTotal
our $Render_Top_Level ? : 48 1 49
1654 (our $Render_Top_Level ? $inner : ('(', $inner, ')'))
1655 );
1656 }
1657  
1658 3891 ▶sub join_query_parts {
Subroutine join_query_parts: called CC=5 SCAR=16.1
Pod documented
1659 3891 my ($self, $join, @parts) = @_;
1660 3891 ▶ if (ref($join) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $join eq "HASH") 1168 2723 3891
1661 1168 $join = $self->render_aqt($join)->[0];
1662 }
1663 3891 ▶ my @final = map +(
Branch: 2 branches
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 5210 3559 8769
ref $_ eq 'ARRAY' ? : 2173 1386 3559
1664 ref($_) eq 'HASH'
1665 ? $self->render_aqt($_)
1666 : ((ref($_) eq 'ARRAY') ? $_ : [ $_ ])
1667 ), @parts;
1668 return [
1669 $self->{join_sql_parts}->(
1670 $join, grep defined && length, map $_->[0], @final
1671 ),
1672 3875 ▶ (map @{$_}[1..$#$_], @final),
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
208743
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
1673 ];
1674 }
1675  
1676 26 ▶sub _render_unop_paren {
Subroutine _render_unop_paren: called CC=1 SCAR=0.0
1677 26 my ($self, $op, $v) = @_;
1678 26 return $self->join_query_parts('',
1679 '(', $self->_render_unop_prefix($op, $v), ')'
1680 );
1681 }
1682  
1683 34 ▶sub _render_unop_prefix {
Subroutine _render_unop_prefix: called CC=3 SCAR=11.0
1684 34 my ($self, $op, $v) = @_;
1685 my $op_sql = $self->{restore_old_unop_handling}
1686 34 ▶ ? $self->_sqlcase($op)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'restore_old_unop_handling'} ? : 7 27 34
1687 : { -keyword => $op };
1688 return $self->join_query_parts(' ',
1689 ($self->{restore_old_unop_handling}
1690 34 ▶ ? $self->_sqlcase($op)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'restore_old_unop_handling'} ? : 7 27 34
1691 : { -keyword => \$op }),
1692 $v->[0]
1693 );
1694 }
1695  
1696 286 ▶sub _render_unop_postfix {
Subroutine _render_unop_postfix: called CC=1 SCAR=0.0
1697 286 my ($self, $op, $v) = @_;
1698 286 return $self->join_query_parts(' ',
1699 $v->[0], { -keyword => $op },
1700 );
1701 }
1702  
1703 # Some databases (SQLite) treat col IN (1, 2) different from
1704 # col IN ( (1, 2) ). Use this to strip all outer parens while
1705 # adding them back in the corresponding method
1706 11 ▶sub _open_outer_paren {
Subroutine _open_outer_paren: called CC=5 SCAR=16.1
1707 11 my ($self, $sql) = @_;
1708  
1709 11 ▶ while (my ($inner) = $sql =~ /^ \s* \( (.*) \) \s* $/xs) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($inner) = $sql =~ /^ \s* \( (.*) \) \s* $/sx) 9 10 19
1710  
1711 # there are closing parens inside, need the heavy duty machinery
1712 # to reevaluate the extraction starting from $sql (full reevaluation)
1713 9 ▶ if ($inner =~ /\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner =~ /\)/) 8 1 9
1714 8 require Text::Balanced;
1715  
1716 8 my (undef, $remainder) = do {
1717 # idiotic design - writes to $@ but *DOES NOT* throw exceptions
1718 8 local $@;
1719 8 Text::Balanced::extract_bracketed($sql, '()', qr/\s*/);
1720 };
1721  
1722 # the entire expression needs to be a balanced bracketed thing
1723 # (after an extract no remainder sans trailing space)
1724 8 ▶ last if defined $remainder and $remainder =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $remainder and $remainder =~ /\S/ 1 7 8
Condition66%2/3
defined $remainder and $remainder =~ /\S/
!ll&&!rl&&r
071
MC/DC50%1/2
defined $remainder and $remainder =~ /\S/
defined $remainder $remainder =~ /\S/
Truth table66%2/3
A and B
A= defined $remainderB= $remainder =~ /\S/
ABresult
0X0
100
111
1725 }
1726  
1727 8 $sql = $inner;
1728 }
1729  
1730 11 $sql;
1731 }
1732  
1733 0 ▶sub _where_field_IN {
Subroutine _where_field_IN: not called CC=2 SCAR=17.9
1734 0 my ($self, $k, $op, $vals) = @_;
1735 0 ▶ @{$self->_render_op_in(
Branch: 1 branch
BranchTrueFalseTotal
ref $vals eq 'ARRAY' ? : 0 0 0
1736 $op,
1737 [
1738 $self->expand_expr($k, -ident),
1739 map $self->expand_expr($_, -value),
1740 ref($vals) eq 'ARRAY' ? @$vals : $vals
1741 ]
1742 )};
1743 }
1744  
1745 0 ▶sub _where_field_BETWEEN {
Subroutine _where_field_BETWEEN: not called CC=2 SCAR=17.9
1746 0 my ($self, $k, $op, $vals) = @_;
1747 0 ▶ @{$self->_render_op_between(
Branch: 1 branch
BranchTrueFalseTotal
ref $vals eq 'ARRAY' ? : 0 0 0
1748 $op,
1749 [ $self->expand_expr($k, -ident), ref($vals) eq 'ARRAY' ? @$vals : $vals ]
1750 )};
1751 }
1752  
1753 #======================================================================
1754 # ORDER BY
1755 #======================================================================
1756  
1757 199 ▶sub _expand_order_by {
Subroutine _expand_order_by: called CC=7 SCAR=19.6
1758 199 my ($self, $arg) = @_;
1759  
1760 199 ▶ return unless defined($arg) and not (ref($arg) eq 'ARRAY' and !@$arg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg and not ref $arg eq "ARRAY" && !@$arg 5 194 199
Condition83%5/6
defined $arg and not ref $arg eq "ARRAY" && !@$arg
!ll&&!rl&&r
05194
ref $arg eq 'ARRAY' && !@$arg
!ll&&!rl&&r
211735
MC/DC66%2/3
defined $arg and not ref $arg eq "ARRAY" && !@$arg
defined $arg ref $arg eq 'ARRAY' !@$arg
Truth table75%3/4
A and not(B && not(C))
A= defined $argB= ref $arg eq 'ARRAY'C= !@$arg
ABCresult
0XX0
1110
10X1
1101
1761  
1762 return $self->expand_expr({ -list => $arg })
1763 194 ▶ if ref($arg) eq 'HASH' and ($arg->{-op}||[''])->[0] eq ',';
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg eq "HASH" and +($arg->{"-op"} || [""])->[0] eq "," 0 194 194
Condition60%3/5
ref $arg eq "HASH" and +($arg->{"-op"} || [""])->[0] eq ","
!ll&&!rl&&r
18590
$arg->{'-op'} || ['']
l!l
09
MC/DC0%0/2
ref $arg eq "HASH" and +($arg->{"-op"} || [""])->[0] eq ","
ref $arg eq "HASH" $arg->{'-op'}
Truth table66%2/3
A and B
A= ref $arg eq "HASH"B= $arg->{'-op'}
ABresult
0X0
100
111
1764  
1765 303 ▶ my $expander = sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
1766 303 my ($self, $dir, $expr) = @_;
1767 303 ▶ my @to_expand = ref($expr) eq 'ARRAY' ? @$expr : $expr;
Branch: 1 branch
BranchTrueFalseTotal
ref $expr eq 'ARRAY' ? : 215 88 303
1768 303 foreach my $arg (@to_expand) {
1769 371 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq "HASH" and keys %$arg > 1 and grep /^-(asc|desc)$/, keys %$arg) 3 368 371
Condition83%5/6
ref $arg eq "HASH" and keys %$arg > 1 and grep /^-(asc|desc)$/, keys %$arg
!ll&&!rl&&r
36803
ref $arg eq "HASH" and keys %$arg > 1
!ll&&!rl&&r
288803
MC/DC66%2/3
ref $arg eq "HASH" and keys %$arg > 1 and grep /^-(asc|desc)$/, keys %$arg
ref $arg eq "HASH" keys %$arg > 1 grep /^-(asc|desc)$/, keys %$arg
Truth table75%3/4
A and B and C
A= ref $arg eq "HASH"B= keys %$arg > 1C= grep /^-(asc|desc)$/, keys %$arg
ABCresult
0XX0
10X0
1100
1111
1770 ref($arg) eq 'HASH'
1771 and keys %$arg > 1
1772 and grep /^-(asc|desc)$/, keys %$arg
1773 ) {
1774 3 puke "ordering direction hash passed to order by must have exactly one key (-asc or -desc)";
1775 }
1776 }
1777 300 ▶ my @exp = map +(
Branch: 2 branches
BranchTrueFalseTotal
defined $dir ? : 145 257 402
ref $_ eq 'ARRAY' ? : 34 334 368
1778 defined($dir) ? { -op => [ $dir =~ /^-?(.*)$/ ,=> $_ ] } : $_
1779 ),
1780 map $self->expand_expr($_, -ident),
1781 map ref($_) eq 'ARRAY' ? @$_ : $_, @to_expand;
1782 300 ▶ return undef unless @exp;
Branch: 1 branch
BranchTrueFalseTotal
unless @exp 0 300 300
1783 300 ▶ return undef if @exp == 1 and not defined($exp[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @exp == 1 and not defined $exp[0] 3 297 300
Condition100%3/3
@exp == 1 and not defined $exp[0]
!ll&&!rl&&r
872103
MC/DC100%2/2
@exp == 1 and not defined $exp[0]
@exp == 1 not defined $exp[0]
Truth table100%3/3
A and not(B)
A= @exp == 1B= not defined $exp[0]
ABresult
0X0
100
111
1784 297 return +{ -op => [ ',', @exp ] };
1785 194 };
1786  
1787 194 local @{$self->{expand}}{qw(asc desc)} = (($expander) x 2);
1788  
1789 194 return $self->$expander(undef, $arg);
1790 }
1791  
1792 50 ▶sub _order_by {
Subroutine _order_by: called CC=4 SCAR=14.0
1793 50 my ($self, $arg) = @_;
1794  
1795 50 ▶ return '' unless defined(my $expanded = $self->_expand_order_by($arg));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $expanded = $self->_expand_order_by($arg)) 7 40 47
1796  
1797 40 my ($sql, @bind) = @{ $self->render_aqt($expanded) };
1798  
1799 40 ▶ return '' unless length($sql);
Branch: 1 branch
BranchTrueFalseTotal
unless length $sql 0 40 40
1800  
1801 40 my $final_sql = $self->_sqlcase(' order by ').$sql;
1802  
1803 40 ▶ return $final_sql unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 40 40
1804  
1805 40 return ($final_sql, @bind);
1806 }
1807  
1808 # _order_by no longer needs to call this so doesn't but DBIC uses it.
1809  
1810 141 ▶sub _order_by_chunks {
Subroutine _order_by_chunks: called CC=4 SCAR=13.9
1811 141 my ($self, $arg) = @_;
1812  
1813 141 ▶ return () unless defined(my $expanded = $self->_expand_order_by($arg));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $expanded = $self->_expand_order_by($arg)) 1 140 141
1814  
1815 140 my @res = $self->_chunkify_order_by($expanded);
1816 140 ▶ (ref() ? $_->[0] : $_) .= '' for @res;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 102 96 198
1817 140 return @res;
1818 }
1819  
1820 408 ▶sub _chunkify_order_by {
Subroutine _chunkify_order_by: called CC=8 SCAR=20.8
1821 408 my ($self, $expanded) = @_;
1822  
1823 96 return grep length, @{ $self->render_aqt($expanded) }
1824 408 ▶ if $expanded->{-ident} or @{$expanded->{-literal}||[]} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $expanded->{"-ident"} or @{$expanded->{"-literal"} or [];} == 1 96 312 408
Condition100%5/5
$expanded->{"-ident"} or @{$expanded->{"-literal"} or [];} == 1
l!l&&r!l&&!r
924312
$expanded->{'-literal'} || []
l!l
12304
MC/DC100%2/2
$expanded->{"-ident"} or @{$expanded->{"-literal"} or [];} == 1
$expanded->{"-ident"} @{$expanded->{"-literal"} or [];} == 1
MC/DC100%1/1
$expanded->{'-literal'} || []
$expanded->{'-literal'}
Truth table100%3/3
A or B
A= $expanded->{"-ident"}B= @{$expanded->{"-literal"} or [];} == 1
ABresult
1X1
011
000
Truth table100%2/2
A
A= $expanded->{'-literal'}
Aresult
11
00
1825  
1826 312 for ($expanded) {
1827 312 ▶ if (ref() eq 'HASH' and $_->{-op} and $_->{-op}[0] eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq "HASH" and $_->{"-op"} and $_->{"-op"}[0] eq ",") 210 102 312
Condition83%5/6
ref $_ eq "HASH" and $_->{"-op"} and $_->{"-op"}[0] eq ","
!ll&&!rl&&r
894210
ref $_ eq "HASH" and $_->{"-op"}
!ll&&!rl&&r
08304
MC/DC66%2/3
ref $_ eq "HASH" and $_->{"-op"} and $_->{"-op"}[0] eq ","
ref $_ eq "HASH" $_->{"-op"} $_->{"-op"}[0] eq ","
Truth table75%3/4
A and B and C
A= ref $_ eq "HASH"B= $_->{"-op"}C= $_->{"-op"}[0] eq ","
ABCresult
0XX0
10X0
1100
1111
1828 210 my ($comma, @list) = @{$_->{-op}};
1829 210 return map $self->_chunkify_order_by($_), @list;
1830 }
1831 102 return $self->render_aqt($_);
1832 }
1833 }
1834  
1835 #======================================================================
1836 # DATASOURCE (FOR NOW, JUST PLAIN TABLE OR LIST OF TABLES)
1837 #======================================================================
1838  
1839 0 ▶sub _table {
Subroutine _table: not called CC=1 SCAR=6.9
1840 0 my $self = shift;
1841 0 my $from = shift;
1842 0 $self->render_aqt(
1843 $self->expand_expr({ -list => $from }, -ident)
1844 )->[0];
1845 }
1846  
1847  
1848 #======================================================================
1849 # UTILITY FUNCTIONS
1850 #======================================================================
1851  
1852 # highly optimized, as it's called way too often
1853 1811 ▶sub _quote {
Subroutine _quote: called CC=14 SCAR=28.3
1854 # my ($self, $label) = @_;
1855  
1856 1811 ▶ return '' unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 0 1811 1811
1857 0 ▶ return ${$_[1]} if ref($_[1]) eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "SCALAR" 0 1811 1811
1858 1811 ▶ puke 'Identifier cannot be hashref' if ref($_[1]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "HASH" 0 1811 1811
1859  
1860 1811 ▶ unless ($_[0]->{quote_char}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0]{"quote_char"}) 1244 567 1811
1861 1244 ▶ if (ref($_[1]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq 'ARRAY') { } 1219 25 1244
1862 1219 ▶ return join($_[0]->{name_sep}||'.', @{$_[1]});
Condition50%1/2
$_[0]{'name_sep'} || '.'
l!l
01219
MC/DC0%0/1
$_[0]{'name_sep'} || '.'
$_[0]{'name_sep'}
Truth table50%1/2
A
A= $_[0]{'name_sep'}
Aresult
11
00
1863 } else {
1864 25 $_[0]->_assert_pass_injection_guard($_[1]);
1865 25 return $_[1];
1866 }
1867 }
1868  
1869 567 my $qref = ref $_[0]->{quote_char};
1870 my ($l, $r) =
1871 !$qref ? ($_[0]->{quote_char}, $_[0]->{quote_char})
1872 0 ▶ : ($qref eq 'ARRAY') ? @{$_[0]->{quote_char}}
Branch: 2 branches
BranchTrueFalseTotal
!$qref ? : 567 0 567
$qref eq 'ARRAY' ? : 0 0 0
1873 : puke "Unsupported quote_char format: $_[0]->{quote_char}";
1874  
1875 567 ▶ my $esc = $_[0]->{escape_char} || $r;
Condition66%2/3
$_[0]{'escape_char'} || $r
l!l&&r!l&&!r
25650
MC/DC0%0/2
$_[0]{'escape_char'} || $r
$_[0]{'escape_char'} $r
Truth table66%2/3
A || B
A= $_[0]{'escape_char'}B= $r
ABresult
1X1
011
000
1876  
1877 # parts containing * are naturally unquoted
1878 return join(
1879 $_[0]->{name_sep}||'',
1880 map +(
1881 $_ eq '*'
1882 ? $_
1883 581 ▶ : do { (my $n = $_) =~ s/(\Q$esc\E|\Q$r\E)/$esc$1/g; $l . $n . $r }
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] eq 'ARRAY' ? : 552 15 567
1884 ),
1885 (ref($_[1]) eq 'ARRAY'
1886 552 ▶ ? @{$_[1]}
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'name_sep'} ? : 15 0 15
1887 : (
1888 $_[0]->{name_sep}
1889 567 ▶ ? split (/\Q$_[0]->{name_sep}\E/, $_[1] )
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '*' ? : 1 581 582
Condition100%2/2
$_[0]{'name_sep'} || ''
l!l
450117
MC/DC100%1/1
$_[0]{'name_sep'} || ''
$_[0]{'name_sep'}
Truth table100%2/2
A
A= $_[0]{'name_sep'}
Aresult
11
00
1890 : $_[1]
1891 )
1892 )
1893 );
1894 }
1895  
1896  
1897 # Conversion, if applicable
1898 3 ▶sub _convert {
Subroutine _convert: called CC=2 SCAR=9.2
1899 #my ($self, $arg) = @_;
1900 3 ▶ if (my $conv = $_[0]->{convert_where}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conv = $_[0]{"convert_where"}) 0 3 3
1901 0 return @{ $_[0]->join_query_parts('',
1902 $_[0]->_sqlcase($conv),
1903 '(' , $_[1] , ')'
1904 ) };
1905 }
1906 3 return $_[1];
1907 }
1908  
1909 # And bindtype
1910 1122 ▶sub _bindtype {
Subroutine _bindtype: called CC=2 SCAR=6.9
1911 #my ($self, $col, @vals) = @_;
1912 # called often - tighten code
1913 return $_[0]->{bindtype} eq 'columns'
1914 1122 ▶ ? map {[$_[1], $_]} @_[2 .. $#_]
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'bindtype'} eq 'columns' ? : 112 1010 1122
1915 : @_[2 .. $#_]
1916 ;
1917 }
1918  
1919 # Dies if any element of @bind is not in [colname => value] format
1920 # if bindtype is 'columns'.
1921 363 ▶sub _assert_bindval_matches_bindtype {
Subroutine _assert_bindval_matches_bindtype: called CC=6 SCAR=18.1
1922 # my ($self, @bind) = @_;
1923 363 my $self = shift;
1924 363 ▶ if ($self->{bindtype} eq 'columns') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bindtype"} eq "columns") 68 295 363
1925 68 for (@_) {
1926 40 ▶ if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_ or ref $_ ne "ARRAY" or @$_ != 2) 10 30 40
Condition66%4/6
not defined $_ or ref $_ ne "ARRAY" or @$_ != 2
l!l&&r!l&&!r
10030
not defined $_ or ref $_ ne "ARRAY"
l!l&&r!l&&!r
01030
MC/DC33%1/3
not defined $_ or ref $_ ne "ARRAY" or @$_ != 2
not defined $_ ref $_ ne "ARRAY" @$_ != 2
Truth table50%2/4
not(A) or B or C
A= not defined $_B= ref $_ ne "ARRAY"C= @$_ != 2
ABCresult
1XX1
01X1
0011
0000
1927 10 puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]"
1928 }
1929 }
1930 }
1931 }
1932  
1933 # Fix SQL case, if so requested
1934 2773 ▶sub _sqlcase {
Subroutine _sqlcase: called CC=2 SCAR=6.9
1935 # LDNOTE: if $self->{case} is true, then it contains 'lower', so we
1936 # don't touch the argument ... crooked logic, but let's not change it!
1937 2773 ▶ return $_[0]->{case} ? $_[1] : uc($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'case'} ? : 42 2731 2773
1938 }
1939  
1940 #======================================================================
1941 # DISPATCHING FROM REFKIND
1942 #======================================================================
1943  
1944 50 ▶sub _refkind {
Subroutine _refkind: called CC=8 SCAR=20.8
1945 50 my ($self, $data) = @_;
1946  
1947 50 ▶ return 'UNDEF' unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 4 46 50
1948  
1949 # blessed objects are treated like scalars
1950 46 ▶ my $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($data) ? : 1 45 46
1951  
1952 46 ▶ return 'SCALAR' unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 32 14 46
1953  
1954 14 my $n_steps = 1;
1955 14 ▶ while ($ref eq 'REF') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "REF") 11 14 25
1956 11 $data = $$data;
1957 11 ▶ $ref = (Scalar::Util::blessed $data) ? '' : ref $data;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($data) ? : 2 9 11
1958 11 ▶ $n_steps++ if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 9 2 11
1959 }
1960  
1961 14 ▶ return ($ref||'SCALAR') . ('REF' x $n_steps);
Condition100%2/2
$ref || 'SCALAR'
l!l
122
MC/DC100%1/1
$ref || 'SCALAR'
$ref
Truth table100%2/2
A
A= $ref
Aresult
11
00
1962 }
1963  
1964 37 ▶sub _try_refkind {
Subroutine _try_refkind: called CC=3 SCAR=11.0
1965 37 my ($self, $data) = @_;
1966 37 my @try = ($self->_refkind($data));
1967 37 ▶ push @try, 'SCALAR_or_UNDEF' if $try[0] eq 'SCALAR' || $try[0] eq 'UNDEF';
Branch: 1 branch
BranchTrueFalseTotal
if $try[0] eq "SCALAR" or $try[0] eq "UNDEF" 33 4 37
Condition100%3/3
$try[0] eq "SCALAR" or $try[0] eq "UNDEF"
l!l&&r!l&&!r
3034
MC/DC100%2/2
$try[0] eq "SCALAR" or $try[0] eq "UNDEF"
$try[0] eq "SCALAR" $try[0] eq "UNDEF"
Truth table100%3/3
A or B
A= $try[0] eq "SCALAR"B= $try[0] eq "UNDEF"
ABresult
1X1
011
000
1968 37 push @try, 'FALLBACK';
1969 37 return \@try;
1970 }
1971  
1972 0 ▶sub _METHOD_FOR_refkind {
Subroutine _METHOD_FOR_refkind: not called CC=4 SCAR=30.0
1973 0 my ($self, $meth_prefix, $data) = @_;
1974  
1975 0 my $method;
1976 0 for (@{$self->_try_refkind($data)}) {
1977 0 ▶ $method = $self->can($meth_prefix."_".$_)
Branch: 1 branch
BranchTrueFalseTotal
$method = $self->can($meth_prefix . '_' . $_) and last 0 0 0
1978 and last;
1979 }
1980  
1981 0 ▶ return $method || puke "cannot dispatch on '$meth_prefix' for ".$self->_refkind($data);
Condition0%0/3
$method || &puke("cannot dispatch on '${meth_prefix}' for " . $self->_refkind($data))
l!l&&r!l&&!r
000
MC/DC0%0/2
$method || &puke("cannot dispatch on '${meth_prefix}' for " . $self->_refkind($data))
$method &puke("cannot dispatch on '${meth_prefix}' for " . $self->_refkind($data))
Truth table0%0/3
A || B
A= $methodB= &puke("cannot dispatch on '${meth_prefix}' for " . $self->_refkind($data))
ABresult
1X1
011
000
1982 }
1983  
1984  
1985 37 ▶sub _SWITCH_refkind {
Subroutine _SWITCH_refkind: called CC=4 SCAR=13.9
1986 37 my ($self, $data, $dispatch_table) = @_;
1987  
1988 37 my $coderef;
1989 37 for (@{$self->_try_refkind($data)}) {
1990 70 ▶ $coderef = $dispatch_table->{$_}
Branch: 1 branch
BranchTrueFalseTotal
$coderef = $dispatch_table->{$_} and last 37 33 70
1991 and last;
1992 }
1993  
1994 37 ▶ puke "no dispatch entry for ".$self->_refkind($data)
Branch: 1 branch
BranchTrueFalseTotal
unless $coderef 0 37 37
1995 unless $coderef;
1996  
1997 37 $coderef->();
1998 }
1999  
2000  
2001  
2002  
2003 #======================================================================
2004 # VALUES, GENERATE, AUTOLOAD
2005 #======================================================================
2006  
2007 # LDNOTE: original code from nwiger, didn't touch code in that section
2008 # I feel the AUTOLOAD stuff should not be the default, it should
2009 # only be activated on explicit demand by user.
2010  
2011 6 ▶sub values {
Subroutine values: called CC=4 SCAR=14.0
Pod documented
2012 6 my $self = shift;
2013 6 ▶ my $data = shift || return;
Condition50%1/2
shift() || (return)
l!l
60
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
2014 6 ▶ puke "Argument to ", __PACKAGE__, "->values must be a \\%hash"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 6 6
2015 unless ref $data eq 'HASH';
2016  
2017 6 my @all_bind;
2018 6 foreach my $k (sort keys %$data) {
2019 37 my $v = $data->{$k};
2020 $self->_SWITCH_refkind($v, {
2021 1 ▶ ARRAYREF => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
2022 1 ▶ if ($self->{array_datatypes}) { # array datatype
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'array_datatypes'}) { } 0 1 1
2023 0 push @all_bind, $self->_bindtype($k, $v);
2024 }
2025 else { # literal SQL with bind
2026 1 my ($sql, @bind) = @$v;
2027 1 $self->_assert_bindval_matches_bindtype(@bind);
2028 1 push @all_bind, @bind;
2029 }
2030 },
2031 1 ▶ ARRAYREFREF => sub { # literal SQL with bind
Subroutine __ANON__: called CC=1 SCAR=0.0
2032 1 my ($sql, @bind) = @${$v};
2033 1 $self->_assert_bindval_matches_bindtype(@bind);
2034 1 push @all_bind, @bind;
2035 },
2036 2 ▶ SCALARREF => sub { # literal SQL without bind
Subroutine __ANON__: called CC=1 SCAR=0.0
2037 },
2038 33 ▶ SCALAR_or_UNDEF => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2039 33 push @all_bind, $self->_bindtype($k, $v);
2040 },
2041 37 });
2042 }
2043  
2044 6 return @all_bind;
2045 }
2046  
2047 0 ▶sub generate {
Subroutine generate: not called CC=14 SCAR=53.5
Pod documented
2048 0 my $self = shift;
2049  
2050 0 my(@sql, @sqlq, @sqlv);
2051  
2052 0 for (@_) {
2053 0 my $ref = ref $_;
2054 0 ▶ if ($ref eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'SCALAR') { } 0 0 0
2055 0 for my $k (sort keys %$_) {
2056 0 my $v = $_->{$k};
2057 0 my $r = ref $v;
2058 0 my $label = $self->_quote($k);
2059 0 ▶ if ($r eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 0 0 0
elsif ($r eq 'SCALAR') { } 0 0 0
2060 # literal SQL with bind
2061 0 my ($sql, @bind) = @$v;
2062 0 $self->_assert_bindval_matches_bindtype(@bind);
2063 0 push @sqlq, "$label = $sql";
2064 0 push @sqlv, @bind;
2065 } elsif ($r eq 'SCALAR') {
2066 # literal SQL without bind
2067 0 push @sqlq, "$label = $$v";
2068 } else {
2069 0 push @sqlq, "$label = ?";
2070 0 push @sqlv, $self->_bindtype($k, $v);
2071 }
2072 }
2073 0 push @sql, $self->_sqlcase('set'), join ', ', @sqlq;
2074 } elsif ($ref eq 'ARRAY') {
2075 # unlike insert(), assume these are ONLY the column names, i.e. for SQL
2076 0 for my $v (@$_) {
2077 0 my $r = ref $v;
2078 0 ▶ if ($r eq 'ARRAY') { # literal SQL with bind
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 0 0 0
elsif ($r eq 'SCALAR') { } 0 0 0
2079 0 my ($sql, @bind) = @$v;
2080 0 $self->_assert_bindval_matches_bindtype(@bind);
2081 0 push @sqlq, $sql;
2082 0 push @sqlv, @bind;
2083 } elsif ($r eq 'SCALAR') { # literal SQL without bind
2084 # embedded literal SQL
2085 0 push @sqlq, $$v;
2086 } else {
2087 0 push @sqlq, '?';
2088 0 push @sqlv, $v;
2089 }
2090 }
2091 0 push @sql, '(' . join(', ', @sqlq) . ')';
2092 } elsif ($ref eq 'SCALAR') {
2093 # literal SQL
2094 0 push @sql, $$_;
2095 } else {
2096 # strings get case twiddled
2097 0 push @sql, $self->_sqlcase($_);
2098 }
2099 }
2100  
2101 0 my $sql = join ' ', @sql;
2102  
2103 # this is pretty tricky
2104 # if ask for an array, return ($stmt, @bind)
2105 # otherwise, s/?/shift @sqlv/ to put it inline
2106 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
2107 0 return ($sql, @sqlv);
2108 } else {
2109 0 ▶ 1 while $sql =~ s/\?/my $d = shift(@sqlv);
Branch: 1 branch
BranchTrueFalseTotal
$sql =~ s/\?/my $d = shift @sqlv; ref $d ? $d->[1] : $d;/e and do { '???' } 0 0 0
2110 0 ▶ ref $d ? $d->[1] : $d/e;
Branch: 1 branch
BranchTrueFalseTotal
ref $d ? : 0 0 0
2111 0 return $sql;
2112 }
2113 }
2114  
2115  
2116 780 ▶sub DESTROY { 1 }
Subroutine DESTROY: called CC=1 SCAR=0.0
2117  
2118 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
2119 # This allows us to check for a local, then _form, attr
2120 0 my $self = shift;
2121 0 my($name) = $AUTOLOAD =~ /.*::(.+)/;
2122 0 ▶ puke "AUTOLOAD invoked for method name ${name} and allow_autoload option not set" unless $self->{allow_autoload};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"allow_autoload"} 0 0 0
2123 0 return $self->generate($name, @_);
2124 }
2125  
2126 16 1;
2127  
2128  
2129  
2130 __END__
« SQL/Abstract/Tree.pm ↑ Summary SQL/Abstract/Test.pm »