SQL/Interp.pm

filter: Statementstmt 90.7% 205 / 226 Branchbran 80.7% 105 / 130 Conditioncond 80.0% 20 / 25 MC/DCmcdc 66.6% 10 / 15 Subroutinesub 88.0% 22 / 25 Podpod 60.0% 3 / 5 totaltotal 85.6% 365 / 426 CC 81 SCAR 32.0
CC
81
Cov
87%
_sql_interp
47.5
_sql_interp_resultset
13.2
_error_item
12.0
CRAP 24.4
↑ Summary DBIx/Interp.pm »
1 package SQL::Interp;
2  
3 4 our $VERSION = '1.28';
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 # Custom import magic to support Sub::Exporter '-as' style function renaming.
11 # (for backwards compatibility with older versions without adding an additional dependency)
12 6 ▶sub import {
Subroutine import: called CC=6 SCAR=17.9
13 6 my $pkg = shift;
14 6 my $caller = caller;
15 6 my %export = qw/sql_interp 1 sql_interp_strict 1 sql_type 1 sql 1/;
16 6 ▶ while(my $fn = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fn = shift()) 27 6 33
17 27 ▶ if($fn eq ':all') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn eq ":all") 5 22 27
18 5 push @_, keys %export;
19 5 next;
20 }
21 22 ▶ croak "Symbol '$fn' is not exported by $pkg" if !$export{$fn};
Branch: 1 branch
BranchTrueFalseTotal
unless $export{$fn} 0 22 22
22 22 my $as = $fn;
23 22 ▶ if(ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 2 20 22
24 2 my $arg = shift;
25 2 ▶ $as = $arg->{'-as'} if defined $arg->{'-as'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg->{"-as"} 2 0 2
26 }
27 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 22 *{$caller.'::'.$as} = *{$pkg.'::'.$fn};
29 }
30 }
31  
32  
33 # whether TRACE_SQL is enabled
34 4 ▶my $trace_sql_enabled = $ENV{TRACE_SQL} || 0;
Condition50%1/2
$ENV{'TRACE_SQL'} || 0
l!l
04
MC/DC0%0/1
$ENV{'TRACE_SQL'} || 0
$ENV{'TRACE_SQL'}
Truth table50%1/2
A
A= $ENV{'TRACE_SQL'}
Aresult
11
00
35  
36 # regexes
37 4 my $id_match = qr/(?:[a-zA-Z_][a-zA-Z0-9_\$\.]*|"[^"]+"|`[^`]+`)/;
38  
39  
40 # next ID to use for table alias
41 # [local to sql_interp functions]
42 4 my $alias_id = 0;
43  
44 # current index in interpolation list
45 # [local to sql_interp functions]
46 4 my $idx = 0;
47  
48 # current interpolation list
49 # [local to sql_interp functions]
50 4 my $items_ref = undef;
51  
52 # whether typed sql_type() ever used (if so,
53 # format of @bind result is more complicated)
54 # [local to sql_interp functions]
55 4 my $is_var_used = 0;
56  
57 # bind elements in interpolation
58 # [local to sql_interp functions]
59 4 my @bind;
60  
61 # only used by DBIx::Interp, so not further documented here.
62 # Doesn't do anything, but may accept options to influence sql_interp()'s behavior in the future.
63 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
64 2 bless {}, shift;
65 }
66  
67  
68 # note: sql_interp is not reentrant.
69 164 ▶sub sql_interp {
Subroutine sql_interp: called CC=7 SCAR=19.5
Pod documented
70 164 my @items = @_;
71  
72 # clear call state
73 164 $alias_id = 0;
74 164 $idx = 0;
75 164 $items_ref = undef;
76 164 $is_var_used = 0;
77 164 @bind = ();
78  
79 # Legacy: We may be called with an object as first argument; it's unused so throw it away
80 164 ▶ shift @items if UNIVERSAL::isa($items[0], 'SQL::Interp') || UNIVERSAL::isa($items[0], 'DBI::db');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($items[0], "SQL::Interp") or UNIVERSAL::isa($items[0], "DBI::db") 83 81 164
Condition66%2/3
UNIVERSAL::isa($items[0], "SQL::Interp") or UNIVERSAL::isa($items[0], "DBI::db")
l!l&&r!l&&!r
83081
MC/DC50%1/2
UNIVERSAL::isa($items[0], "SQL::Interp") or UNIVERSAL::isa($items[0], "DBI::db")
UNIVERSAL::isa($items[0], "SQL::Interp") UNIVERSAL::isa($items[0], "DBI::db")
Truth table66%2/3
A or B
A= UNIVERSAL::isa($items[0], "SQL::Interp")B= UNIVERSAL::isa($items[0], "DBI::db")
ABresult
1X1
011
000
81  
82 164 $items_ref = \@items;
83  
84 # interpolate!
85 164 my $sql = _sql_interp(@items);
86  
87 # convert bind values to complex format (if needed)
88 159 ▶ if ($is_var_used) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_var_used) 12 147 159
89 12 for my $val (@bind) {
90 36 my $valcopy = $val;
91 36 ▶ ! ref $val and $val = [$val, sql_type(\$valcopy)];
Branch: 1 branch
BranchTrueFalseTotal
ref $val or $val = [$val, sql_type(\$valcopy)] 20 16 36
92 }
93 }
94  
95 $trace_sql_enabled
96 159 ▶ and print STDERR "DEBUG:interp[sql=$sql,bind="
Branch: 1 branch
BranchTrueFalseTotal
$trace_sql_enabled and print STDERR "DEBUG:interp[sql=$sql,bind=" . join(":", @bind) . "]\n" 0 159 159
97 . join(':', @bind) . "]\n";
98  
99 159 return ($sql, @bind);
100 }
101  
102 # Prevent accidental SQL injection holes
103 # By enforcing the rule that two non-references cannot be used
104 # in a row. If you really mean that, concatanate the strings instead.
105 1 ▶sub sql_interp_strict {
Subroutine sql_interp_strict: called CC=4 SCAR=14.3
Pod undocumented
106 1 my @items = @_;
107  
108 1 my $adjacent_string_cnt = 0;
109 1 for my $item (@items) {
110 # If we have a reference, reset the counter and move to the next element.
111 2 ▶ if (ref $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item) { } 0 2 2
112 0 $adjacent_string_cnt = 0;
113 }
114 else {
115 2 $adjacent_string_cnt++;
116 2 ▶ if ($adjacent_string_cnt == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($adjacent_string_cnt == 2) 1 1 2
117 1 croak "failed sql_interp_strict check. Refactor to concatenate adjacent strings in sql_interp array";
118 }
119 }
120  
121 }
122  
123 0 return sql_interp(@_);
124 }
125  
126 # helper called by sql_interp()
127 # @items - interpolation list
128 252 ▶sub _sql_interp {
Subroutine _sql_interp: called CC=46 SCAR=38.6
129 252 my (@items) = @_;
130  
131 252 my $sql = '';
132  
133 252 foreach my $item (@items) {
134 446 my $varobj;
135 446 my $bind_size = @bind;
136 446 ▶ if (ref $item eq 'SQL::Interp::Variable') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq "SQL::Interp::Variable") 18 428 446
137 18 ▶ unless (keys %$item == 1 && defined($item->{value})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$item == 1 and defined $item->{"value"}) 16 2 18
Condition66%2/3
keys %$item == 1 and defined $item->{"value"}
!ll&&!rl&&r
1602
MC/DC50%1/2
keys %$item == 1 and defined $item->{"value"}
keys %$item == 1 defined $item->{"value"}
Truth table66%2/3
A and B
A= keys %$item == 1B= defined $item->{"value"}
ABresult
0X0
100
111
138 16 $varobj = $item;
139 16 $is_var_used = 1;
140 }
141 18 $item = $item->{value};
142 }
143  
144 446 ▶ if (ref $item eq 'SQL::Interp::SQL') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $item eq 'SQL::Interp::SQL') { } 50 396 446
elsif (ref $item) { } 191 205 396
145 50 my ($sql2, @bind2) = _sql_interp(@$item);
146 50 ▶ $sql .= ' ' if $sql ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $sql ne "" 4 46 50
147 50 $sql .= $sql2;
148 50 push @bind, @bind2;
149 }
150 elsif (ref $item) {
151 191 ▶ if ($sql =~ /\b(NOT\s+)?IN\s*$/si) {
Branch: 8 branches
BranchTrueFalseTotal
if ($sql =~ /\b(NOT\s+)?IN\s*$/is) { } 30 161 191
elsif ($sql =~ /\bARRAY\s*$/is) { } 8 153 161
elsif ($sql =~ /\b(?:ON\s+DUPLICATE\s+KEY\s+UPDATE|SET)\s*$/is and ref $item eq 'HASH') { } 4 149 153
elsif ($sql =~ /\b(REPLACE|INSERT)[\w\s]*\sINTO\s*$id_match\s*$/is) { } 26 123 149
elsif ($sql =~ /(?:\bFROM|JOIN)\s*$/is and not $sql =~ /DISTINCT\s+FROM\s*$/) { } 33 90 123
elsif (ref $item eq 'SCALAR') { } 54 36 90
elsif (ref $item eq 'HASH') { } 22 14 36
elsif (ref $item eq 'ARRAY') { } 14 0 14
152 30 ▶ my $not = quotemeta($1 || '');
Condition100%2/2
$1 || ''
l!l
228
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
153  
154 30 ▶ $item = [ $$item ] if ref $item eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $item eq "SCALAR" 2 28 30
155  
156 # allow double references
157 30 ▶ $item = $$item if ref $item eq 'REF' ;
Branch: 1 branch
BranchTrueFalseTotal
if ref $item eq "REF" 2 28 30
158  
159 30 ▶ if (ref $item eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 30 0 30
160 30 ▶ if (@$item == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$item == 0) { } 12 18 30
161 12 ▶ my $dummy_expr = $not ? '1=1' : '1=0';
Branch: 1 branch
BranchTrueFalseTotal
$not ? : 2 10 12
162 12 ▶ $sql =~ s/$id_match\s+${not}IN\s*$/$dummy_expr/si or croak 'ASSERT';
Branch: 1 branch
BranchTrueFalseTotal
$sql =~ s/$id_match\s+${not}IN\s*$/$dummy_expr/is or croak("ASSERT") 12 0 12
163 }
164 else {
165 $sql .= " (" . join(', ', map {
166 18 _sql_interp_data($_);
167 } @$item) . ")";
168 }
169 }
170 else {
171 0 _error_item($idx, \@items);
172 }
173 }
174 elsif ($sql =~ /\bARRAY\s*$/si) {
175 8 ▶ $item = [ $$item ] if ref $item eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $item eq "SCALAR" 2 6 8
176  
177 # allow double references
178 8 ▶ $item = $$item if ref $item eq 'REF' ;
Branch: 1 branch
BranchTrueFalseTotal
if ref $item eq "REF" 4 4 8
179  
180 8 ▶ if (ref $item eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 8 0 8
181 $sql .= '[' . join(', ', map {
182 8 _sql_interp_data($_);
183 } @$item) . ']';
184 }
185 else {
186 0 _error_item($idx, \@items);
187 }
188 }
189 ▶ elsif ($sql =~ /\b(?:ON\s+DUPLICATE\s+KEY\s+UPDATE|SET)\s*$/si && ref $item eq 'HASH') {
Condition66%2/3
$sql =~ /\b(?:ON\s+DUPLICATE\s+KEY\s+UPDATE|SET)\s*$/is and ref $item eq "HASH"
!ll&&!rl&&r
14904
MC/DC50%1/2
$sql =~ /\b(?:ON\s+DUPLICATE\s+KEY\s+UPDATE|SET)\s*$/is and ref $item eq "HASH"
$sql =~ /\b(?:ON\s+DUPLICATE\s+KEY\s+UPDATE|SET)\s*$/is ref $item eq "HASH"
Truth table66%2/3
A and B
A= $sql =~ /\b(?:ON\s+DUPLICATE\s+KEY\s+UPDATE|SET)\s*$/isB= ref $item eq "HASH"
ABresult
0X0
100
111
190 4 ▶ _error('Hash has zero elements.') if keys %$item == 0;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$item == 0 0 4 4
191 $sql .= " " . join(', ', map {
192 4 my $key = $_;
193 10 my $val = $item->{$key};
194 10 "$key=" .
195 _sql_interp_data($val);
196 } (sort keys %$item));
197 }
198 elsif ($sql =~ /\b(REPLACE|INSERT)[\w\s]*\sINTO\s*$id_match\s*$/si) {
199 26 ▶ $item = [ $$item ] if ref $item eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $item eq "SCALAR" 8 18 26
200 26 ▶ if (ref $item eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 18 8 26
elsif (ref $item eq 'HASH') { } 8 0 8
201 $sql .= " VALUES(" . join(', ', map {
202 18 _sql_interp_data($_);
203 } @$item) . ")";
204 }
205 elsif (ref $item eq 'HASH') {
206 8 my @keyseq = sort keys %$item;
207 $sql .=
208 " (" . join(', ', @keyseq) . ")" .
209 " VALUES(" . join(', ', map {
210 8 _sql_interp_data($item->{$_});
211 } @keyseq) . ")";
212 }
213 0 else { _error_item($idx, \@items); }
214 }
215 ▶ elsif ($sql =~ /(?:\bFROM|JOIN)\s*$/si && $sql !~ /DISTINCT\s+FROM\s*$/) {
Condition100%3/3
$sql =~ /(?:\bFROM|JOIN)\s*$/is and not $sql =~ /DISTINCT\s+FROM\s*$/
!ll&&!rl&&r
88233
MC/DC100%2/2
$sql =~ /(?:\bFROM|JOIN)\s*$/is and not $sql =~ /DISTINCT\s+FROM\s*$/
$sql =~ /(?:\bFROM|JOIN)\s*$/is not $sql =~ /DISTINCT\s+FROM\s*$/
Truth table100%3/3
A and not(B)
A= $sql =~ /(?:\bFROM|JOIN)\s*$/isB= not $sql =~ /DISTINCT\s+FROM\s*$/
ABresult
0X0
100
111
216 # table reference
217  
218 # get alias for table
219 33 my $table_alias = undef; # alias given to table
220 33 my $next_item = $items[$idx + 1];
221 33 ▶ if(defined $next_item && ref $next_item eq '' &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next_item and ref $next_item eq '' and $next_item =~ /\s*AS\b/is) { } 2 31 33
Condition83%5/6
defined $next_item and ref $next_item eq "" and $next_item =~ /\s*AS\b/is
!ll&&!rl&&r
2922
defined $next_item and ref $next_item eq ""
!ll&&!rl&&r
2904
MC/DC66%2/3
defined $next_item and ref $next_item eq "" and $next_item =~ /\s*AS\b/is
defined $next_item ref $next_item eq "" $next_item =~ /\s*AS\b/is
Truth table75%3/4
A and B and C
A= defined $next_itemB= ref $next_item eq ""C= $next_item =~ /\s*AS\b/is
ABCresult
0XX0
10X0
1100
1111
222 $next_item =~ /\s*AS\b/is)
223 {
224 2 $table_alias = undef; # provided by client
225 }
226 else {
227 31 $table_alias = 'tbl' . $alias_id++;
228 }
229  
230 33 ▶ $sql .= ' ' unless $sql eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $sql eq "" 33 0 33
231 33 $sql .= _sql_interp_resultset($item);
232 30 ▶ $sql .= " AS $table_alias" if defined $table_alias;
Branch: 1 branch
BranchTrueFalseTotal
if defined $table_alias 28 2 30
233 }
234 elsif (ref $item eq 'SCALAR') {
235 54 push @bind, $$item;
236 54 $sql .= ' ?';
237 }
238 elsif (ref $item eq 'HASH') { # e.g. WHERE {x = 3, y = 4}
239 22 ▶ if (keys %$item == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$item == 0) { } 2 20 22
240 2 $sql .= ' 1=1';
241 }
242 else {
243 my $s = join ' AND ', map {
244 20 my $key = $_;
245 34 my $val = $item->{$key};
246 34 ▶ if (! defined $val) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val) { } 2 32 34
elsif (ref $val eq 'ARRAY') { } 8 24 32
247 2 "$key IS NULL";
248 }
249 elsif (ref $val eq 'ARRAY') {
250 8 _sql_interp_list($key, $val);
251 }
252 else {
253 24 "$key=" .
254 _sql_interp_data($val);
255 }
256 } (sort keys %$item);
257 20 ▶ $s = "($s)" if keys %$item > 1;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$item > 1 14 6 20
258 20 $s = " $s";
259 20 $sql .= $s;
260 }
261 }
262 elsif (ref $item eq 'ARRAY') { # result set
263 14 ▶ $sql .= ' ' unless $sql eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $sql eq "" 0 14 14
264 14 $sql .= _sql_interp_resultset($item);
265 }
266 0 else { _error_item($idx, \@items); }
267 }
268 else {
269 205 ▶ $sql .= ' ' unless $sql =~ /(^|\s)$/ || $item =~ /^\s/; # style
Branch: 1 branch
BranchTrueFalseTotal
unless $sql =~ /(^|\s)$/ or $item =~ /^\s/ 27 178 205
Condition100%3/3
$sql =~ /(^|\s)$/ or $item =~ /^\s/
l!l&&r!l&&!r
176227
MC/DC100%2/2
$sql =~ /(^|\s)$/ or $item =~ /^\s/
$sql =~ /(^|\s)$/ $item =~ /^\s/
Truth table100%3/3
A or B
A= $sql =~ /(^|\s)$/B= $item =~ /^\s/
ABresult
1X1
011
000
270 205 $sql .= $item;
271 }
272  
273 # attach $varobj to any bind values it generates
274 441 ▶ if ($varobj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varobj) 16 425 441
275 16 my $num_pushed = @bind - $bind_size;
276 16 for my $val (@bind[-$num_pushed..-1]) {
277 20 $val = [$val, $varobj];
278 }
279 }
280 441 $idx++;
281 }
282  
283 247 return $sql;
284 }
285  
286 # sql_interp helper function.
287 # Interpolate data element in aggregate variable (hashref or arrayref).
288 # $ele - raw input element from aggregate.
289 # returns $sql
290 203 ▶sub _sql_interp_data {
Subroutine _sql_interp_data: called CC=3 SCAR=11.0
291 203 my ($ele) = @_;
292 203 ▶ if (ref $ele) { # e.g. sql()
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ele) { } 38 165 203
293 38 my ($sql2, @bind2) = _sql_interp($ele);
294 38 push @bind, @bind2;
295 38 ▶ $is_var_used = 1 if ref $bind2[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $bind2[0] 0 38 38
296 38 return $sql2;
297 }
298 else {
299 165 push @bind, $ele;
300 165 return '?';
301 }
302 }
303  
304 # sql_interp helper function to interpolate "key IN list",
305 # assuming context ("WHERE", {key => $list, ...}).
306 8 ▶sub _sql_interp_list {
Subroutine _sql_interp_list: called CC=3 SCAR=11.0
307 8 my ($key, $list) = @_;
308 8 ▶ if (@$list == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list == 0) { } 2 6 8
309 2 return "1=0";
310 }
311 else {
312 6 my @sqle;
313 6 for my $ele (@$list) {
314 12 my $sqle
315 = _sql_interp_data($ele);
316 12 push @sqle, $sqle;
317 }
318 6 my $sql2 = $key . " IN (" . join(', ', @sqle) . ")";
319 6 return $sql2;
320 }
321 }
322 # sql_interp helper function to interpolate result set,
323 # e.g. [[1,2],[3,4]] or [{a=>1,b=>2},{a=>3,b=>4}].
324 47 ▶sub _sql_interp_resultset {
Subroutine _sql_interp_resultset: called CC=13 SCAR=25.8
325 47 my($item) = @_;
326 47 my $sql = '';
327 47 ▶ if (ref $item eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 47 0 47
328 47 ▶ _error("table reference has zero rows") # improve?
Branch: 1 branch
BranchTrueFalseTotal
if @$item == 0 1 46 47
329 if @$item == 0;
330 46 my $sql2 = '';
331 46 ▶ if(ref $item->[0] eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $item->[0] eq 'ARRAY') { } 28 18 46
elsif (ref $item->[0] eq 'HASH') { } 18 0 18
332 _error("table reference has zero columns") # improve?
333 28 ▶ if @{ $item->[0] } == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$item->[0];} == 0 2 26 28
334 26 for my $row ( @$item ) {
335 30 my $is_first_row = ($sql2 eq '');
336 30 ▶ $sql2 .= ' UNION ALL ' unless $is_first_row;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_first_row 4 26 30
337 $sql2 .=
338 "SELECT " .
339 join(', ', map {
340 30 _sql_interp_data($_)
341 } @$row);
342 }
343 }
344 elsif(ref $item->[0] eq 'HASH') {
345 _error("table reference has zero columns") # improve?
346 18 ▶ if keys %{ $item->[0] } == 0;
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$item[0];} == 0 2 16 18
347 16 my $first_row = $item->[0];
348 16 for my $row ( @$item ) {
349 20 my $is_first_row = ($sql2 eq '');
350 20 ▶ $sql2 .= ' UNION ALL ' unless $is_first_row;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_first_row 4 16 20
351 $sql2 .=
352 "SELECT " .
353 join(', ', map {
354 20 my($key, $val) = ($_, $row->{$_});
355 32 my $sql3 = _sql_interp_data($val);
356 32 ▶ $sql3 .= " AS $key" if $is_first_row;
Branch: 1 branch
BranchTrueFalseTotal
if $is_first_row 24 8 32
357 32 $sql3;
358 } (sort keys %$first_row));
359 }
360 }
361 else {
362 0 _error_item($idx, $items_ref);
363 }
364 42 ▶ $sql .= ' ' unless $sql eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $sql eq "" 0 42 42
365 42 $sql .= "($sql2)";
366 }
367 0 else { _error_item($idx, $items_ref); }
368 42 return $sql;
369 }
370  
371 23 ▶sub sql {
Subroutine sql: called CC=1 SCAR=0.0
Pod documented
372 23 return SQL::Interp::SQL->new(@_);
373 }
374  
375 29 ▶sub sql_type {
Subroutine sql_type: called CC=1 SCAR=0.0
Pod documented
376 29 return SQL::Interp::Variable->new(@_);
377 }
378  
379 # helper function to throw error
380 0 ▶sub _error_item {
Subroutine _error_item: not called CC=3 SCAR=24.8
381 0 my ($idx, $items_ref) = @_;
382 0 ▶ my $prev = $idx > 0 ? $items_ref->[$idx-1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$idx > 0 ? : 0 0 0
383 0 ▶ my $prev_text = defined($prev) ? " following '$prev'" : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $prev ? : 0 0 0
384 0 my $cur = $items_ref->[$idx];
385 0 _error("SQL::Interp error: Unrecognized "
386 . "'$cur'$prev_text in interpolation list.");
387 0 return;
388 }
389  
390 5 ▶sub _error {
Subroutine _error: called CC=1 SCAR=0.0
391 5 croak "SQL::Interp error: $_[0]";
392 }
393  
394 4 1;
395  
396 package SQL::Interp::Variable;
397 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
398 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
399  
400 29 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
401 29 my ($class, $value, %params) = @_;
402 29 ▶ SQL::Interp::_error(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 0 29 29
403 "Value '$value' in sql_type constructor is not a reference")
404 if ! ref $value;
405 29 my $self = bless {value => $value, %params}, $class;
406 29 return $self;
407 }
408  
409 4 1;
410  
411  
412 package SQL::Interp::SQL;
413 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
414 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
415 4 ▶use overload '.' => \&concat, '""' => \&stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
416  
417 24 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
418 24 my ($class, @list) = @_;
419  
420 24 my $self = \@list;
421 24 bless $self, $class;
422 24 return $self;
423 }
424  
425 # Concatenate SQL object with another expression.
426 # An SQL object can be concatenated with another SQL object,
427 # variable reference, or an SQL string.
428 0 ▶sub concat {
Subroutine concat: not called CC=3 SCAR=24.8
429 0 my ($a, $b, $inverted) = @_;
430  
431 0 ▶ my @params = ( @$a, ref $b eq __PACKAGE__ ? @$b : $b );
Branch: 1 branch
BranchTrueFalseTotal
ref $b eq __PACKAGE__ ? : 0 0 0
432 0 ▶ @params = reverse @params if $inverted;
Branch: 1 branch
BranchTrueFalseTotal
if $inverted 0 0 0
433 0 my $o = SQL::Interp::SQL->new(@params);
434 0 return $o;
435 }
436  
437 0 ▶sub stringify {
Subroutine stringify: not called CC=1 SCAR=6.9
438 0 my ($a) = @_;
439 0 return $a;
440 }
441  
442 4 1;
443  
444 __END__
↑ Summary DBIx/Interp.pm »