blib/lib/DBIx/Squirrel/Placeholders.pm

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