blib/lib/DBIx/PreQL.pm

filter: Statementstmt 81.6% 151 / 185 Branchbran 71.4% 70 / 98 Conditioncond 56.2% 27 / 48 MC/DCmcdc 33.3% 10 / 30 Subroutinesub 58.8% 20 / 34 Podpod 100% 1 / 1 totaltotal 70.4% 279 / 396 CC 70 SCAR 23.0
CC
70
Cov
75%
_select_line
22.1
build_query
15.1
_is_ref
12.0
CRAP 10.0
↑ Summary
1 package DBIx::PreQL;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp qw< croak carp >;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 require overload; # Just for Overloaded()
6  
7 3 ▶use constant TAG_ALWAYS => '*';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use constant TAG_NEVER => '#';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use constant TAG_IF_ALL_EXIST => '&';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use constant TAG_IF_ANY_EXIST => '|';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use constant TAG_IF_PREFIXED => qr/^([&|])(?![&|])(\S*)$/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Substitution types:
14 # !ifset!, !~ifnotset!
15 # ?param?, ?@array_ref?
16 # ?=same_as?, ?!distinct?
17 # ?"literal_sql?
18  
19  
20 30 ▶sub _parse_data {
Subroutine _parse_data: called CC=4 SCAR=14.2
21 30 my( $data ) = @_;
22  
23 30 ▶ croak "data must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $data or ref $data eq "HASH" 2 28 30
Condition66%2/3
not defined $data or ref $data eq "HASH"
l!l&&r!l&&!r
0282
MC/DC50%1/2
not defined $data or ref $data eq "HASH"
not defined $data ref $data eq "HASH"
Truth table66%2/3
not(A) or B
A= not defined $dataB= ref $data eq "HASH"
ABresult
1X1
011
000
24 unless ! defined($data)
25 || ref($data) eq 'HASH';
26  
27 28 ▶ return $data || {};
Condition50%1/2
$data || {}
l!l
280
MC/DC0%0/1
$data || {}
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
28 }
29  
30  
31 30 ▶sub _parse_wanted {
Subroutine _parse_wanted: called CC=4 SCAR=13.9
32 30 my( $w ) = @_;
33  
34 30 ▶ return if ! defined $w;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w 18 12 30
35  
36 12 ▶ return $w if ref $w eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $w eq "CODE" 6 6 12
37  
38 6 ▶ if( ref $w eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $w eq "ARRAY") 5 1 6
39 5 my %w; @w{@$w} = (1) x @$w;
40  
41 5 ▶ return sub { return $w{$_[0]} };
Subroutine __ANON__: called CC=1 SCAR=0.0
42 }
43  
44 1 croak "wanted must be an array ref or code ref";
45 }
46  
47  
48 27 ▶sub _split_query {
Subroutine _split_query: called CC=8 SCAR=21.5
49 27 my( $q ) = @_;
50  
51 27 ▶ croak "No query specified"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $q and length $q 0 27 27
Condition33%1/3
defined $q and length $q
!ll&&!rl&&r
0027
MC/DC0%0/2
defined $q and length $q
defined $q length $q
Truth table33%1/3
A and B
A= defined $qB= length $q
ABresult
0X0
100
111
52 if ! defined($q) || ! length($q);
53  
54 my @lines = grep /\S/, # Remove blank lines
55 map {
56 # Split into lines, removing trailing whitespace:
57 27 ▶ split /[^\S\n]*\n/, $_
Branch: 1 branch
BranchTrueFalseTotal
ref $q eq 'ARRAY' ? : 2 25 27
58 } ref $q eq 'ARRAY' ? @$q : ($q);
59 27 my $indent;
60 27 for( @lines ) {
61 next # Lines with nothing after the tag have no indentation
62 161 ▶ if ! /^(\s*\S+\s+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^(\s*\S+\s+)/ 0 161 161
63 161 my $l = length( $1 ); # Measure how much the SQL is indented
64 161 ▶ $indent = $l # Track the least amount of indentation
Branch: 1 branch
BranchTrueFalseTotal
if not $indent or $l < $indent 27 134 161
Condition66%2/3
not $indent or $l < $indent
l!l&&r!l&&!r
270134
MC/DC50%1/2
not $indent or $l < $indent
not $indent $l < $indent
Truth table66%2/3
not(A) or B
A= not $indentB= $l < $indent
ABresult
1X1
011
000
65 if ! $indent || $l < $indent;
66 }
67 27 return( $indent, @lines );
68 }
69  
70  
71 154 ▶sub _parse_line {
Subroutine _parse_line: called CC=4 SCAR=14.0
72 154 my( $line, $indent ) = @_;
73  
74 154 ▶ my( $pre, $tag, $post, $sql ) = $line =~ /^(\s*)(\S+)(\s*)(.*)$/
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /^(\s*)(\S+)(\s*)(.*)$/ or die "Impossible: SQL template line w/o tag:\n$line\nfound" 154 0 154
75 or die "Impossible: SQL template line w/o tag:\n$line\nfound";
76  
77 154 my $context = ":\n $tag $sql\n"; # Used for error messages.
78 # Preserve indentation (using minimal spaces):
79 154 $indent = ' ' x( length("$pre$tag$post") - $indent );
80  
81  
82 154 my $prefix = '';
83 154 ▶ if( $tag =~ TAG_IF_PREFIXED ) { # '&', '|', '&FOO', or '|BAR' tag:
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^([&|])(?![&|])(\S*)$/) 61 93 154
84 61 ( $prefix, $tag ) = ( $1, $2 );
85  
86 # Note that '&*' and '|*' act like '&' and '|' (respectively)
87 61 ▶ $tag = TAG_ALWAYS
Branch: 1 branch
BranchTrueFalseTotal
if "" eq $tag 61 0 61
88 if '' eq $tag;
89 }
90  
91 154 return $prefix, $tag, $indent, $sql, $context;
92 }
93  
94 151 ▶sub _find_named_placeholders {
Subroutine _find_named_placeholders: called CC=3 SCAR=11.0
95 151 my( $sql, $data ) = @_;
96  
97 151 my( @pholders, @found, @missing );
98 151 ▶ while( $sql =~ /\?[\@=!"]?(\w+)\?/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /\?[\@=!"]?(\w+)\?/g) 76 151 227
99 76 my $name = $1;
100 76 push @pholders, $name;
101  
102 76 ▶ if( defined $data->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{$name}) { } 56 20 76
103 56 push @found, $name;
104 }
105 else {
106 20 push @missing, $name
107 }
108 }
109  
110 return {
111 151 all => \@pholders,
112 found => \@found,
113 missing => \@missing,
114 };
115 }
116  
117 151 ▶sub _find_dependencies {
Subroutine _find_dependencies: called CC=4 SCAR=13.9
118 151 my( $sql, $data ) = @_;
119  
120 151 my( @deps, @found, @lost );
121 151 ▶ while( $sql =~ /!(~?)(\w+)!/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /!(~?)(\w+)!/g) 44 151 195
122 44 my( $negated, $name ) = ( $1, $2 );
123  
124 44 push @deps, $name;
125  
126 44 my $found = defined $data->{$name};
127 44 ▶ $found = ! $found
Branch: 1 branch
BranchTrueFalseTotal
if $negated 0 44 44
128 if $negated;
129  
130 44 ▶ if( $found ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) { } 21 23 44
131 21 push @found, $name;
132 } else {
133 23 push @lost, $name;
134 }
135 }
136  
137 return {
138 151 all => \@deps,
139 found => \@found,
140 missing => \@lost,
141 };
142 }
143  
144 154 ▶sub _select_line {
Subroutine _select_line: called CC=21 SCAR=31.0
145 154 my( $line, $base_indent, $data, $want, $known_tags ) = @_;
146  
147 154 my( $prefix, $tag, $indent, $sql, $context ) = _parse_line( $line, $base_indent );
148  
149 return
150 154 ▶ if TAG_NEVER eq $tag;
Branch: 1 branch
BranchTrueFalseTotal
if "#" eq $tag 3 151 154
151  
152 151 my $nph = _find_named_placeholders( $sql, $data );
153 151 my $deps = _find_dependencies( $sql, $data );
154  
155 return
156 151 ▶ if @{$nph->{missing}}
Branch: 1 branch
BranchTrueFalseTotal
if @{$nph->{"missing"};} and $prefix 14 137 151
Condition100%3/3
@{$nph->{"missing"};} and $prefix
!ll&&!rl&&r
131614
MC/DC100%2/2
@{$nph->{"missing"};} and $prefix
@{$nph->{"missing"};} $prefix
Truth table100%3/3
A and B
A= @{$nph->{"missing"};}B= $prefix
ABresult
0X0
100
111
157 && $prefix; # $tag starts with '&' or '|'
158  
159 137 ▶ croak "Missing tag?$context" # Catch "* SELECT\n *\n* FROM\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $sql =~ /\S/ 0 137 137
160 if $sql !~ /\S/; # ^ missing tag
161  
162 #TAG_IF_ALL_EXIST, {
163 # die_msg => "No parameters nor dependency markers specified",
164 # die_unless => [ $nph->{all}, $deps->{all} ],
165 # skip_if => [ $deps->{missing} ],
166 #},
167 #
168 #TAG_IF_ANY_EXIST, {
169 # die_msg => "No dependency markers specified",
170 # die_unless => [ $deps->{all} ],
171 # skip_unless => [ $deps->{found} ],
172 #},
173 #TAG_ALWAYS, {
174 # die_msg => "Dependency markers ({$deps->{all}}) used with wrong tag type"
175 # die_if => [ $deps->{all} ],
176 #}
177  
178  
179  
180  
181 137 ▶ if( TAG_IF_ALL_EXIST eq $prefix ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('&' eq $prefix) { } 37 100 137
elsif ('|' eq $prefix) { } 10 90 100
182 croak "No parameters nor dependency markers specified$context"
183 37 if ! @{$nph->{all}}
184 37 ▶ && ! @{$deps->{all}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$nph->{"all"};} or @{$deps->{"all"};} 1 36 37
Condition100%3/3
@{$nph->{"all"};} or @{$deps->{"all"};}
l!l&&r!l&&!r
16201
MC/DC100%2/2
@{$nph->{"all"};} or @{$deps->{"all"};}
@{$nph->{"all"};} @{$deps->{"all"};}
Truth table100%3/3
A or B
A= @{$nph->{"all"};}B= @{$deps->{"all"};}
ABresult
1X1
011
000
185 return
186 36 ▶ if @{$deps->{missing}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$deps->{"missing"};} 11 25 36
187 } elsif( TAG_IF_ANY_EXIST eq $prefix ) {
188 croak "No dependency markers specified$context"
189 10 ▶ if ! @{$deps->{all}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$deps->{"all"};} 1 9 10
190 return
191 9 ▶ if ! @{$deps->{found}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$deps->{"found"};} 4 5 9
192 } else {
193 0 croak "Dependency markers (@{$deps->{all}}) used with wrong tag type$context"
194 90 ▶ if @{$deps->{all}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$deps->{"all"};} 0 90 90
195  
196 90 ▶ if( TAG_ALWAYS ne $tag ) { # Handle custom tags:
Branch: 1 branch
BranchTrueFalseTotal
if ("*" ne $tag) 39 51 90
197 39 ▶ if( $known_tags ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($known_tags) { } 0 39 39
198 croak "Unknown tag found$context"
199 0 ▶ if ! $known_tags->{$tag}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $known_tags->{$tag}++ 0 0 0
200 } else {
201 39 ▶ croak "Missing tag?$context"
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /,$/ or grep(($_ eq uc $tag), ("SELECT", "FROM", "LEFT", "RIGHT", "INNER", "OUTER", "JOIN", "USING", "ON", "WHERE", "AND", "OR", "ORDER", "GROUP", "BY", "LIMIT", "OFFSET", "HAVING", "UNION", "ALL", "DISTINCT", "INSERT", "UPDATE", "ALTER", "CREATE", "DROP", "WITH", "OVER", "BETWEEN")) 0 39 39
Condition33%1/3
$tag =~ /,$/ or grep(($_ eq uc $tag), ("SELECT", "FROM", "LEFT", "RIGHT", "INNER", "OUTER", "JOIN", "USING", "ON", "WHERE", "AND", "OR", "ORDER", "GROUP", "BY", "LIMIT", "OFFSET", "HAVING", "UNION", "ALL", "DISTINCT", "INSERT", "UPDATE", "ALTER", "CREATE", "DROP", "WITH", "OVER", "BETWEEN"))
l!l&&r!l&&!r
0039
MC/DC0%0/2
$tag =~ /,$/ or grep(($_ eq uc $tag), ("SELECT", "FROM", "LEFT", "RIGHT", "INNER", "OUTER", "JOIN", "USING", "ON", "WHERE", "AND", "OR", "ORDER", "GROUP", "BY", "LIMIT", "OFFSET", "HAVING", "UNION", "ALL", "DISTINCT", "INSERT", "UPDATE", "ALTER", "CREATE", "DROP", "WITH", "OVER", "BETWEEN"))
$tag =~ /,$/ grep(($_ eq uc $tag), ("SELECT", "FROM", "LEFT", "RIGHT", "INNER", "OUTER", "JOIN", "USING", "ON", "WHERE", "AND", "OR", "ORDER", "GROUP", "BY", "LIMIT", "OFFSET", "HAVING", "UNION", "ALL", "DISTINCT", "INSERT", "UPDATE", "ALTER", "CREATE", "DROP", "WITH", "OVER", "BETWEEN"))
Truth table33%1/3
A or B
A= $tag =~ /,$/B= grep(($_ eq uc $tag), ("SELECT", "FROM", "LEFT", "RIGHT", "INNER", "OUTER", "JOIN", "USING", "ON", "WHERE", "AND", "OR", "ORDER", "GROUP", "BY", "LIMIT", "OFFSET", "HAVING", "UNION", "ALL", "DISTINCT", "INSERT", "UPDATE", "ALTER", "CREATE", "DROP", "WITH", "OVER", "BETWEEN"))
ABresult
1X1
011
000
202 if $tag =~ /,$/
203 || grep( $_ eq uc $tag, qw<
204 SELECT FROM LEFT RIGHT INNER OUTER JOIN USING ON
205 WHERE AND OR
206 ORDER GROUP BY LIMIT OFFSET HAVING
207 UNION ALL DISTINCT
208 INSERT UPDATE ALTER CREATE DROP
209 WITH OVER BETWEEN
210 > );
211 }
212 39 ▶ croak "No wanted function provided when using custom tag$context"
Branch: 1 branch
BranchTrueFalseTotal
unless $want 1 38 39
213 if ! $want;
214 return
215 38 ▶ if ! $want->( $tag, $data );
Branch: 1 branch
BranchTrueFalseTotal
unless &$want($tag, $data) 12 26 38
216 }
217 }
218  
219 2 croak "Missing named place-holders (@{$nph->{missing}})$context"
220 107 ▶ if @{$nph->{missing}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$nph->{"missing"};} 2 105 107
221  
222 105 return( $indent . $sql, $context );
223 }
224  
225  
226 0 ▶sub _bad_type {
Subroutine _bad_type: not called CC=1 SCAR=6.9
227 0 my( $sigil, $name, $val, $context ) = @_;
228  
229 0 my $type = ref($val);
230 0 croak "Invalid type ($type) for ?$sigil$name?$context";
231 }
232  
233 {
234 #SIGIL => { VALTYPE => sub { param => ?, repl => ? } }
235 # sub takes args $name, $value.
236 # returns kvp list of param and repl to override defaults.
237 my %NPH_HANDLER = (
238 50 ▶ '' => { '' => sub { param => [$_[0]] },
Subroutine __ANON__: called CC=1 SCAR=0.0
239 0 ▶ SCALAR => sub { repl => ${$_[1]} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
240 },
241 0 ▶ '@' => { ARRAY => sub { param => [$_[0]] } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
242 0 ▶ '"' => { '' => sub { repl => $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
243 0 ▶ SCALAR => sub { repl => ${$_[1]} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
244 },
245 0 ▶ '=' => { '' => sub { param => [$_[0]], repl => '= ?' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
246 0 ▶ SCALAR => sub { repl => "= ${$_[1]}" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
247 0 ▶ NULL => sub { repl => 'IS NULL' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
248 },
249 0 ▶ '!' => { '' => sub { param => [$_[0]], repl => '<> ?' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
250 0 ▶ SCALAR => sub { repl => "<> ${$_[1]}" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
251 0 ▶ NULL => sub { repl => 'IS NOT NULL' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
252 },
253 3 );
254  
255 50 ▶ sub _process_named_placeholder {
Subroutine _process_named_placeholder: called CC=6 SCAR=20.9
256 50 my ($sigil, $name, $value) = @_;
257  
258 50 ▶ my $type = ref $value || '';
Condition50%1/2
ref $value || ''
l!l
050
MC/DC0%0/1
ref $value || ''
ref $value
Truth table50%1/2
A
A= ref $value
Aresult
11
00
259 50 ▶ $type = 'NULL'
Branch: 1 branch
BranchTrueFalseTotal
if $type and _is_null($type) 0 50 50
Condition33%1/3
$type and _is_null($type)
!ll&&!rl&&r
5000
MC/DC0%0/2
$type and _is_null($type)
$type _is_null($type)
Truth table33%1/3
A and B
A= $typeB= _is_null($type)
ABresult
0X0
100
111
260 if $type && _is_null( $type );
261  
262 return
263 unless exists( $NPH_HANDLER{$sigil} )
264 50 ▶ && exists( $NPH_HANDLER{$sigil}{$type} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $NPH_HANDLER{$sigil} and exists $NPH_HANDLER{$sigil}{$type} 0 50 50
Condition33%1/3
exists $NPH_HANDLER{$sigil} and exists $NPH_HANDLER{$sigil}{$type}
!ll&&!rl&&r
0050
MC/DC0%0/2
exists $NPH_HANDLER{$sigil} and exists $NPH_HANDLER{$sigil}{$type}
exists $NPH_HANDLER{$sigil} exists $NPH_HANDLER{$sigil}{$type}
Truth table33%1/3
A and B
A= exists $NPH_HANDLER{$sigil}B= exists $NPH_HANDLER{$sigil}{$type}
ABresult
0X0
100
111
265  
266 50 my $handler = $NPH_HANDLER{$sigil}{$type};
267  
268 50 return { repl => '?', param => [], $handler->($name, $value) };
269 }
270  
271 }
272  
273 3 ▶sub _substitute_line {
Subroutine _substitute_line: called CC=5 SCAR=16.4
274 105 my( $sql, $context, $data ) = @_;
275  
276 # Remove dependency markers and extra whitespace:
277 # "a !b! !c!" => "a"
278 # "a!b!c" => "ac"
279 # "a !b! c" or "a !b!c" => "a c"
280 105 $sql =~ s/(\s*)!~?\w+!(?=(\s*)(\S?))/
281 21 ▶ $1 && !$2 && length $3 ? ' ' : '';
Branch: 1 branch
BranchTrueFalseTotal
$1 && !$2 && length $3 ? : 0 21 21
Condition66%4/6
$1 and not $2 and length $3
!ll&&!rl&&r
1200
$1 and not $2
!ll&&!rl&&r
0120
MC/DC0%0/3
$1 and not $2 and length $3
$1 not $2 length $3
Truth table50%2/4
A and not(B) and C
A= $1B= not $2C= length $3
ABCresult
0XX0
10X0
1100
1111
282 /ge;
283  
284 # Replace named parameters with computed text
285 # Fill param array.
286 105 my @params;
287 105 $sql =~ s{\?([\@=!"]?)(\w+)\?}{
288 50 my( $sigil, $name ) = ( $1, $2 );
289 50 my $value = $data->{$name};
290  
291 50 ▶ my $np = _process_named_placeholder( $sigil, $name, $value )
Branch: 1 branch
BranchTrueFalseTotal
my $np = _process_named_placeholder($sigil, $name, $value) or _bad_type($sigil, $name, $value, $context) 50 0 50
292 or _bad_type( $sigil, $name, $value, $context );
293  
294 50 push @params, @{$np->{param}};
295  
296 50 $np->{repl};
297 }ge;
298  
299 105 return( $sql, @params );
300 }
301  
302  
303 27 ▶sub build_query {
Subroutine build_query: called CC=14 SCAR=27.1
Pod documented
304 27 my( $class, %a ) = @_;
305 27 ▶ croak "You must call build_query() as a class method"
Branch: 1 branch
BranchTrueFalseTotal
if $class ne __PACKAGE__ 0 27 27
306 if $class ne __PACKAGE__;
307  
308 27 my $data = _parse_data( delete $a{data} );
309 27 my $want = _parse_wanted( delete $a{wanted} );
310 27 my @query_lines = _split_query( delete $a{query} );
311 27 my $indent = shift @query_lines;
312 27 my $keep_keys = delete $a{keep_keys};
313 27 my $known_tags = delete $a{known_tags};
314 0 ▶ $known_tags &&= { map { $_ => 1 } @$known_tags };
Condition50%1/2
$known_tags &&= {map({$_, 1;} @$known_tags)}
!ll
270
MC/DC0%0/1
$known_tags &&= {map({$_, 1;} @$known_tags)}
$known_tags
Truth table50%1/2
A
A= $known_tags
Aresult
00
11
315  
316 27 my @unexpected_keys = sort keys %a;
317 27 ▶ croak "Unexpected arguments given to build_query(): @unexpected_keys"
Branch: 1 branch
BranchTrueFalseTotal
if @unexpected_keys 0 27 27
318 if @unexpected_keys;
319  
320 27 my @query; # Accumulate lines in loop
321 my @binds; # Build list in loop
322  
323 27 for ( @query_lines ) {
324 # Determine if we should include this line:
325 154 ▶ my( $line, $context ) = _select_line(
Branch: 1 branch
BranchTrueFalseTotal
() = _select_line($_, $indent, $data, $want, $known_tags) or next 105 44 149
326 $_, $indent, $data, $want, $known_tags,
327 )
328 or next;
329  
330 105 ( $line, my @params ) = _substitute_line( $line, $context, $data );
331  
332 # Remove trailing comma if in front of 'FROM':
333 105 ▶ $query[-1] =~ s/,\s*$//
Branch: 1 branch
BranchTrueFalseTotal
if @query and $line =~ /^\s*FROM\b/i 7 98 105
Condition100%3/3
@query and $line =~ /^\s*FROM\b/i
!ll&&!rl&&r
22767
MC/DC100%2/2
@query and $line =~ /^\s*FROM\b/i
@query $line =~ /^\s*FROM\b/i
Truth table100%3/3
A and B
A= @queryB= $line =~ /^\s*FROM\b/i
ABresult
0X0
100
111
334 if @query && $line =~ /^\s*FROM\b/i;
335  
336 # Remove leading 'AND' if behind 'WHERE':
337 105 ▶ $line =~ s/^(\s*)AND\b/$1 /
Branch: 1 branch
BranchTrueFalseTotal
if @query and $query[-1] =~ /\bWHERE\s*$/i 8 97 105
Condition100%3/3
@query and $query[-1] =~ /\bWHERE\s*$/i
!ll&&!rl&&r
22758
MC/DC100%2/2
@query and $query[-1] =~ /\bWHERE\s*$/i
@query $query[-1] =~ /\bWHERE\s*$/i
Truth table100%3/3
A and B
A= @queryB= $query[-1] =~ /\bWHERE\s*$/i
ABresult
0X0
100
111
338 if @query && $query[-1] =~ /\bWHERE\s*$/i;
339  
340 105 push @query, $line;
341 105 push @binds, @params;
342 }
343  
344 22 my $query = join "\n", @query;
345  
346 22 ▶ if( ! $keep_keys ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($keep_keys) 17 5 22
347 $_ = $data->{$_}
348 17 for @binds;
349 }
350  
351 22 ▶ if( $known_tags ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($known_tags) 0 22 22
352 0 my @unused = grep 1==$known_tags->{$_}, sort keys %$known_tags;
353 0 ▶ carp "Some known tags never used (@unused)"
Branch: 1 branch
BranchTrueFalseTotal
if @unused 0 0 0
354 if @unused;
355 }
356  
357 22 return $query, @binds;
358 }
359  
360  
361 0 ▶sub _is_ref {
Subroutine _is_ref: not called CC=3 SCAR=24.8
362 0 my( $val ) = @_;
363 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if not ref $val or overload::Method($val, "\"\"") 0 0 0
Condition0%0/3
not ref $val or overload::Method($val, "\"\"")
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $val or overload::Method($val, "\"\"")
not ref $val overload::Method($val, "\"\"")
Truth table0%0/3
not(A) or B
A= not ref $valB= overload::Method($val, "\"\"")
ABresult
1X1
011
000
364 if ! ref $val
365 || overload::Method( $val, '""' );
366 0 return 1;
367 }
368  
369  
370 0 ▶sub _is_a {
Subroutine _is_a: not called CC=3 SCAR=24.8
371 0 my( $val, $type ) = @_;
372 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless _is_ref($val) 0 0 0
373 if ! _is_ref( $val );
374 0 my $isa = eval { $val->isa($type) };
375 0 ▶ $isa = UNIVERSAL::isa( $val, $type )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $isa 0 0 0
376 if ! defined $isa;
377 0 return $isa;
378 }
379  
380  
381 0 ▶sub _is_null {
Subroutine _is_null: not called CC=3 SCAR=24.8
382 0 my( $val ) = @_;
383 0 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if _is_a($val, "SCALAR") and $$val =~ /^\s*NULL\s*$/i 0 0 0
Condition0%0/3
_is_a($val, "SCALAR") and $$val =~ /^\s*NULL\s*$/i
!ll&&!rl&&r
000
MC/DC0%0/2
_is_a($val, "SCALAR") and $$val =~ /^\s*NULL\s*$/i
_is_a($val, "SCALAR") $$val =~ /^\s*NULL\s*$/i
Truth table0%0/3
A and B
A= _is_a($val, "SCALAR")B= $$val =~ /^\s*NULL\s*$/i
ABresult
0X0
100
111
384 if _is_a( $val, 'SCALAR' )
385 && $$val =~ /^\s*NULL\s*$/i;
386 0 return 0;
387 }
388  
389  
390 3 1;
391  
392 __END__
↑ Summary