blib/lib/DBIx/Squirrel/Placeholders.pm

filter: Statementstmt 99.2% 138 / 139 Branchbran 98.2% 57 / 58 Conditioncond 95.8% 23 / 24 MC/DCmcdc 86.6% 13 / 15 Subroutinesub 100% 19 / 19 Podpod 100% 8 / 8 totaltotal 98.1% 258 / 263 CC 45 SCAR 21.6
CC
45
Cov
99%
study_sql
16.0
_values_for
14.0
_place_values
5.0
CRAP 8.7
↑ Summary blib/lib/DBIx/Squirrel/db.pm »
1 29 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package DBIx::Squirrel::Placeholders;
4  
5 29 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 29 ▶use List::Util qw(any max uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use Ref::Util qw(is_arrayref is_plain_hashref is_plain_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 29 ▶use DBIx::Squirrel::Errors qw(croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 29 ▶use namespace::clean -except => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 29 our @EXPORT_OK = qw(
14 expand_lists
15 is_numbered
16 key_of
17 place_values
18 study_sql
19 trim_sql
20 values_for
21 values_taken
22 );
23  
24 # Croak at the line that called DBIx::Squirrel.
25 29 our @CARP_NOT = @DBIx::Squirrel::Errors::INTERNAL;
26  
27 # Remove the blank space from around a statement; return what's left, or
28 # an empty string if it was all blank.
29 1574 ▶sub trim_sql ($statement) {
Subroutine trim_sql: called CC=2 SCAR=6.9
Pod documented
30 1574 ▶ return ($statement // '') =~ s( \A \s+ | \s+ \z )()gxr;
Condition100%2/2
$statement // ""
l!l
15704
MC/DC100%1/1
$statement // ""
$statement
Truth table100%2/2
A
A= $statement
Aresult
11
00
31 }
32  
33 # Study a statement, with the options given; return a map of its
34 # placeholders' positions, from 1, to their names, the statement with each
35 # placeholder rewritten as "?", and the statement as trimmed. Return
36 # nothing if the statement is blank. If a placeholder takes a list,
37 # written with "..." before it, also return a map of each position taking
38 # a list, and the pieces of the statement around its placeholders, to
39 # expand the lists with expand_lists.
40 1561 ▶sub study_sql ($statement, $options = {}) {
Subroutine study_sql: called CC=16 SCAR=27.7
Pod documented
41 1561 my $trimmed = trim_sql($statement);
42 1561 ▶ return unless length $trimmed;
Branch: 1 branch
BranchTrueFalseTotal
unless length $trimmed 6 1555 1561
43 1555 ▶ my $dialect = $options->{ekorn_dialect} // '';
Condition100%2/2
$options->{"ekorn_dialect"} // ""
l!l
151837
MC/DC100%1/1
$options->{"ekorn_dialect"} // ""
$options->{"ekorn_dialect"}
Truth table100%2/2
A
A= $options->{"ekorn_dialect"}
Aresult
11
00
44  
45 # What isn't SQL code, and is stepped over unchanged: quoted strings and
46 # identifiers, comments, PostgreSQL's dollar-quoted bodies, and a
47 # backslash before "?", ":" or "$", which DBD::Pg takes to mean the
48 # character isn't a placeholder, and removes.
49 1555 state $STRING = qr( ' (?: [^'] | '' )* ' )x;
50 1555 state $E_STRING = qr( (?<! \w ) [Ee] ' (?: [^'\\] | \\. | '' )* ' )x;
51 1555 state $IDENT = qr( " (?: [^"] | "" )* " | ` [^`]* ` )x;
52 1555 state $COMMENT = qr( -- [^\n]* | /\* .*? \*/ )xs;
53 1555 state $DOLLARS = qr( \$ (?<tag> (?: [A-Za-z_]\w* )? ) \$ .*? \$ \k<tag> \$ )xs;
54 1555 state $ESCAPED = qr( \\ [?:\$] )x;
55 1555 state $NOT_SQL
56 = qr( $E_STRING | $STRING | $IDENT | $COMMENT | $DOLLARS | $ESCAPED )x;
57  
58 # The same for MySQL and MariaDB, where a backslash escapes a character
59 # in a string, "..." is a string, not an identifier, and "#" starts a
60 # comment.
61 1555 state $MYSQL_STRING
62 = qr( ' (?: [^'\\] | \\. | '' )* ' | " (?: [^"\\] | \\. | "" )* " )x;
63 1555 state $MYSQL_NOT_SQL = qr( $MYSQL_STRING | ` [^`]* ` | $COMMENT | \# [^\n]* )x;
64 1555 state %NOT_SQL_FOR = (mysql => $MYSQL_NOT_SQL, MariaDB => $MYSQL_NOT_SQL);
65  
66 # A placeholder: "?", "?" and a position, or ":" or "$" and a position or
67 # a name; not after a colon, as in a cast, or inside an identifier. A
68 # position is in the digits 0-9, as \d matches a digit of any script;
69 # the /a flag would keep \w to ASCII too, and a name can be in any.
70 1555 state $PLACEHOLDER = qr( (?<! [:\w\$] ) (?: \? [0-9]* | [:\$] \w+ ) )x;
71  
72 # The same, for DBD::Pg's options: only "$" and a position, or anything
73 # but ":" and a position or a name.
74 1555 state $DOLLAR_ONLY = qr( (?<! [:\w\$] ) \$ [0-9]+ )x;
75 1555 state $NO_COLONS = qr( (?<! [:\w\$] ) (?: \? [0-9]* | \$ \w+ ) )x;
76  
77 # With pg_placeholder_dollaronly, DBD::Pg reads the placeholders itself,
78 # so the statement is left as it is.
79 1555 my $pg = $dialect eq 'Pg';
80 1555 ▶ my $dollar_only = $pg && $options->{pg_placeholder_dollaronly};
Condition100%3/3
$pg && $options->{'pg_placeholder_dollaronly'}
!ll&&!rl&&r
154186
MC/DC100%2/2
$pg && $options->{'pg_placeholder_dollaronly'}
$pg $options->{'pg_placeholder_dollaronly'}
Truth table100%3/3
A && B
A= $pgB= $options->{'pg_placeholder_dollaronly'}
ABresult
0X0
100
111
81 my $placeholder
82 = $dollar_only ? $DOLLAR_ONLY
83 1555 ▶ : $pg && $options->{pg_placeholder_nocolons} ? $NO_COLONS
Branch: 2 branches
BranchTrueFalseTotal
$dollar_only ? : 6 1549 1555
$pg && $options->{'pg_placeholder_nocolons'} ? : 2 1547 1549
Condition100%3/3
$pg and $options->{"pg_placeholder_nocolons"}
!ll&&!rl&&r
154162
MC/DC100%2/2
$pg and $options->{"pg_placeholder_nocolons"}
$pg $options->{"pg_placeholder_nocolons"}
Truth table100%3/3
A and B
A= $pgB= $options->{"pg_placeholder_nocolons"}
ABresult
0X0
100
111
84 : $PLACEHOLDER;
85  
86 # Each placeholder found, where it is in the trimmed statement, and
87 # whether "..." before it makes it take a list.
88 1555 ▶ my $not_sql = $NOT_SQL_FOR{$dialect} // $NOT_SQL;
Condition66%2/3
$NOT_SQL_FOR{$dialect} // $NOT_SQL
l!l&&r!l&&!r
815470
MC/DC0%0/2
$NOT_SQL_FOR{$dialect} // $NOT_SQL
$NOT_SQL_FOR{$dialect} $NOT_SQL
Truth table66%2/3
A // B
A= $NOT_SQL_FOR{$dialect}B= $NOT_SQL
ABresult
1X1
011
000
89 1555 my $splat_or_not
90 = qr( (?<! [:\w\$] ) (?<splat> \.\.\. )? (?<placeholder> $placeholder ) )x;
91 1555 my(@placeholders, @spans, %lists);
92 1555 my $normalised = $trimmed =~ s(
93 (?<not_sql> $not_sql ) | $splat_or_not
94 )(
95 439 ▶ if (exists $+{not_sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $+{'not_sql'}) { } 51 388 439
96 51 $+{not_sql};
97 }
98 else {
99 388 push @placeholders, $+{placeholder};
100 388 push @spans, [$-[0], $+[0]];
101 388 ▶ $lists{ scalar @placeholders } = 1 if defined $+{splat};
Branch: 1 branch
BranchTrueFalseTotal
if defined $+{"splat"} 42 346 388
102 388 '?';
103 }
104 )gerx;
105 1555 ▶ $normalised = $trimmed if $dollar_only;
Branch: 1 branch
BranchTrueFalseTotal
if $dollar_only 6 1549 1555
106  
107 # One style of placeholder to a statement.
108 1555 my @styles = uniq map { _style($_) } @placeholders;
109 1555 ▶ croakf 'Expected one placeholder style, not several (%s)', join ', ', @styles
Branch: 1 branch
BranchTrueFalseTotal
if @styles > 1 8 1547 1555
110 if @styles > 1;
111  
112 1547 my %positions = map { ($_ + 1 => $placeholders[$_]) } keys @placeholders;
113 1547 ▶ return \%positions, $normalised, $trimmed
Branch: 1 branch
BranchTrueFalseTotal
unless %lists 1510 37 1547
114 unless %lists;
115  
116 # With pg_placeholder_dollaronly, the statement goes to DBD::Pg as
117 # written, with no way to expand a list.
118 37 my($first) = sort { $a <=> $b } keys %lists;
119 37 ▶ croakf 'Expected no list placeholder with pg_placeholder_dollaronly (%s)',
Branch: 1 branch
BranchTrueFalseTotal
if $dollar_only 1 36 37
120 "...$positions{$first}"
121 if $dollar_only;
122  
123 # The pieces around the placeholders, the first before the first, and
124 # the last after the last.
125 36 my($from, @pieces) = (0);
126 36 for my $span (@spans) {
127 52 push @pieces, substr $trimmed, $from, $span->[0] - $from;
128 52 $from = $span->[1];
129 }
130 36 push @pieces, substr $trimmed, $from;
131 36 return \%positions, $normalised, $trimmed, \%lists, \@pieces;
132 }
133  
134 # Return a placeholder's number or name, as a statement's map of its
135 # placeholders keys it: without its sigil, and a number as a number. A
136 # bare "?" is keyed by its position, given with it.
137 307 ▶sub key_of ($placeholder, $position = undef) {
Subroutine key_of: called CC=4 SCAR=13.9
Pod documented
138 307 ▶ return $position if $placeholder eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $placeholder eq "?" 41 266 307
139 266 my $key = $placeholder =~ s( \A [?:\$] )()xr;
140 266 ▶ return $key =~ m( \A \d+ \z )xa ? 0+ $key : $key;
Branch: 1 branch
BranchTrueFalseTotal
$key =~ / \A \d+ \z /ax ? : 67 199 266
141 }
142  
143 # Return true if a statement's placeholders are numbered, not named,
144 # given its map of their numbers or names to their positions.
145 #
146 # It takes one key with each, not a list of every key, which costs more
147 # with each key the statement has (roadmap 2026-0028). The hash's
148 # iterator is reset before and after, with keys in void context, which
149 # makes no list, so nothing else finds it moved.
150 320 ▶sub is_numbered ($binds) {
Subroutine is_numbered: called CC=1 SCAR=0.0
Pod documented
151 320 keys %$binds; # resets the iterator, for each
152 320 my $key = each %$binds;
153 320 keys %$binds; # and again, leaving it as it was found
154 320 return $key =~ m( \A \d+ \z )xa;
155 }
156  
157 # Return how many values a statement takes in order, given its map of
158 # its placeholders' numbers or names to their positions: the highest
159 # number, or none if they're named.
160 91 ▶sub values_taken ($binds) {
Subroutine values_taken: called CC=2 SCAR=6.9
Pod documented
161 91 ▶ return is_numbered($binds) ? max keys %$binds : 0;
Branch: 1 branch
BranchTrueFalseTotal
is_numbered($binds) ? : 89 2 91
162 }
163  
164 # Return each placeholder's number or name, with the value given for it,
165 # croaking unless there's a value for each, and no other, given the
166 # statement's map of its placeholders' numbers or names to their
167 # positions.
168 #
169 # It has no signature, and passes its arguments on as a reference, as
170 # copying them costs about 0.3 µs a call.
171 5 ▶sub values_for { ## no critic (Subroutines::RequireArgUnpacking)
Subroutine values_for: called CC=1 SCAR=0.0
Pod documented
172 5 my $binds = shift;
173 5 return %{ _values_for($binds, \@_) };
174 }
175  
176 # Return what values_for does, as a hash reference, given the values as
177 # an array reference, which the handles use: a hash and its values passed
178 # by reference cost less than as lists (roadmap 2026-0028).
179 225 ▶sub _values_for ($binds, $args) {
Subroutine _values_for: called CC=14 SCAR=26.4
180 225 ▶ if (is_numbered($binds)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_numbered($binds)) 56 169 225
181 56 my $needed = values_taken($binds);
182 56 ▶ croakf 'Expected %d bind values, not %d', $needed, scalar @$args
Branch: 1 branch
BranchTrueFalseTotal
unless @$args == $needed 4 52 56
183 unless @$args == $needed;
184 52 return {map { ($_ => $args->[$_ - 1]) } keys %$binds};
185 }
186 169 my %values;
187 169 ▶ if (@$args == 1 && is_plain_hashref($args->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args == 1 and Ref::Util::XS::is_plain_hashref($args->[0])) { } 11 158 169
Condition100%3/3
@$args == 1 and Ref::Util::XS::is_plain_hashref($args->[0])
!ll&&!rl&&r
157111
MC/DC100%2/2
@$args == 1 and Ref::Util::XS::is_plain_hashref($args->[0])
@$args == 1 Ref::Util::XS::is_plain_hashref($args->[0])
Truth table100%3/3
A and B
A= @$args == 1B= Ref::Util::XS::is_plain_hashref($args->[0])
ABresult
0X0
100
111
188 11 %values = %{ $args->[0] };
189 }
190 else {
191 158 ▶ croakf 'Expected names and values in pairs'
Branch: 1 branch
BranchTrueFalseTotal
if @$args % 2 3 155 158
192 if @$args % 2;
193 155 %values = @$args;
194 }
195 # The commonest case: a value for each placeholder, each named as the
196 # statement keys it, so the values are as they were given.
197 return \%values
198 if keys %values == keys %$binds
199 166 ▶ && !grep { !exists $binds->{$_} } keys %values;
Branch: 1 branch
BranchTrueFalseTotal
if keys %values == keys %$binds and not grep({not exists $binds->{$_};} keys %values) 153 13 166
Condition100%3/3
keys %values == keys %$binds and not grep({not exists $binds->{$_};} keys %values)
!ll&&!rl&&r
58153
MC/DC100%2/2
keys %values == keys %$binds and not grep({not exists $binds->{$_};} keys %values)
keys %values == keys %$binds not grep({not exists $binds->{$_};} keys %values)
Truth table100%3/3
A and not(B)
A= keys %values == keys %$bindsB= not grep({not exists $binds->{$_};} keys %values)
ABresult
0X0
100
111
200 13 my(%given, @unknown);
201 13 for my $name (keys %values) {
202 # A name as the statement keys it, as most are, is its own key.
203 18 ▶ my $key = exists $binds->{$name} ? $name : key_of($name);
Branch: 1 branch
BranchTrueFalseTotal
exists $binds->{$name} ? : 7 11 18
204 18 $given{$key} = $values{$name};
205 18 ▶ push @unknown, $name unless $binds->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $binds->{$key} 4 14 18
206 }
207 13 ▶ croakf 'Expected a placeholder of the statement (%s)', join ', ', sort @unknown
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 4 9 13
208 if @unknown;
209 # Each key given is the statement's, so as many as it has are all of
210 # them.
211 9 ▶ return \%given if keys %given == keys %$binds;
Branch: 1 branch
BranchTrueFalseTotal
if keys %given == keys %$binds 6 3 9
212 3 my @missing = sort grep { !exists $given{$_} } keys %$binds;
213 3 ▶ croakf 'Expected a value for each placeholder (none for %s)',
Branch: 1 branch
BranchTrueFalseTotal
if @missing 3 0 3
214 join(', ', @missing)
215 if @missing;
216 0 return \%given;
217 }
218  
219 # Return a statement with list placeholders, as a statement handle keeps
220 # what was studied, with each list's "?" written once for each of its
221 # values, given the values for its placeholders' numbers or names, as
222 # values_for returns them; the map of each number or name to its
223 # positions in the statement returned; and the number of values in each
224 # list.
225 71 ▶sub expand_lists ($state, $values) {
Subroutine expand_lists: called CC=3 SCAR=11.0
Pod documented
226 71 my($lists, $pieces, $placeholders) = @$state{qw(Lists Pieces Placeholders)};
227 my %lengths
228 71 = map { ($_ => scalar _list($lists->{$_}, $values->{$_})) } keys %$lists;
229 62 my($statement, $taken, %binds) = ($pieces->[0], 0);
230 62 for my $position (1 .. keys %$placeholders) {
231 70 my $key = key_of($placeholders->{$position}, $position);
232 70 ▶ my $count = $lengths{$key} // 1;
Condition100%2/2
$lengths{$key} // 1
l!l
646
MC/DC100%1/1
$lengths{$key} // 1
$lengths{$key}
Truth table100%2/2
A
A= $lengths{$key}
Aresult
11
00
233 70 push @{ $binds{$key} }, $taken + 1 .. $taken + $count;
234 70 $taken += $count;
235 70 $statement .= join(', ', ('?') x $count) . $pieces->[$position];
236 }
237 62 return $statement, \%binds, \%lengths;
238 }
239  
240 # Return each position of a statement's placeholders, from 1, with the
241 # value for it, given what the statement handle keeps about it and the
242 # values for its placeholders' numbers or names. A list placeholder's
243 # positions take the values in its list in turn, and the list must have
244 # as many as the statement was prepared for.
245 11 ▶sub place_values ($state, %values) {
Subroutine place_values: called CC=1 SCAR=0.0
Pod documented
246 11 return @{ _place_values($state, \%values) };
247 }
248  
249 # Return what place_values does, as an array reference of its pairs,
250 # given the values as a hash reference, which the handles use (roadmap
251 # 2026-0028).
252 94 ▶sub _place_values ($state, $values) {
Subroutine _place_values: called CC=5 SCAR=16.1
253 94 my($binds, $lists, $lengths) = @$state{qw(Binds Lists Lengths)};
254 94 my @placed;
255 94 for my $key (keys %$values) {
256 103 my $positions = $binds->{$key};
257 103 ▶ unless ($lists && $lists->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lists and $lists->{$key}) 55 48 103
Condition100%3/3
$lists and $lists->{$key}
!ll&&!rl&&r
49648
MC/DC100%2/2
$lists and $lists->{$key}
$lists $lists->{$key}
Truth table100%3/3
A and B
A= $listsB= $lists->{$key}
ABresult
0X0
100
111
258 55 push @placed, map { ($_ => $values->{$key}) } @$positions;
259 55 next;
260 }
261 48 my @list = _list($lists->{$key}, $values->{$key});
262 croakf 'Expected %d values in a list, as prepared, not %d (%s)',
263 $lengths->{$key}, scalar @list, $lists->{$key}
264 47 ▶ unless @list == $lengths->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless @list == $lengths->{$key} 2 45 47
265 45 push @placed, map { ($positions->[$_] => $list[$_ % @list]) } keys @$positions;
266 }
267 91 return \@placed;
268 }
269  
270 # Return the values in a list given for a list placeholder, named as it's
271 # written, croaking unless it's a reference to an ARRAY of at least one
272 # plain value.
273 119 ▶sub _list ($name, $list) {
Subroutine _list: called CC=4 SCAR=13.9
274 119 ▶ croakf 'Expected a reference to an ARRAY for a list (%s)', $name
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_arrayref($list) 4 115 119
275 unless is_arrayref($list);
276 115 ▶ croakf 'Expected at least one value in a list (%s)', $name
Branch: 1 branch
BranchTrueFalseTotal
unless @$list 4 111 115
277 unless @$list;
278 croakf 'Expected plain values in a list (%s)', $name
279 111 ▶ if any { is_plain_ref($_) } @$list;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { Ref::Util::XS::is_plain_ref($_); } , @$list) 2 109 111
280 109 return @$list;
281 }
282  
283 # Return a placeholder's style, as an example of it: "?", "?1", ":1", "$1",
284 # ":name" or "$name".
285 388 ▶sub _style ($placeholder) {
Subroutine _style: called CC=3 SCAR=11.0
286 388 my($sigil, $rest) = $placeholder =~ m( \A (.) (.*) \z )xs;
287 388 ▶ return $sigil if $rest eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $rest eq "" 174 214 388
288 214 ▶ return "${sigil}1" if $rest =~ m( \A \d+ \z )xa;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ / \A \d+ \z /ax 72 142 214
289 142 return "${sigil}name";
290 }
291  
292 29 1;
293 __END__
↑ Summary blib/lib/DBIx/Squirrel/db.pm »