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 30 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package DBIx::Squirrel::Placeholders;
4  
5 30 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use List::Util qw(any max uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Ref::Util qw(is_arrayref is_plain_hashref is_plain_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 30 ▶use DBIx::Squirrel::Errors qw(croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 30 ▶use namespace::clean -except => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 30 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 30 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 1666 ▶sub trim_sql ($statement) {
Subroutine trim_sql: called CC=2 SCAR=6.9
Pod documented
30 1666 ▶ return ($statement // '') =~ s( \A \s+ | \s+ \z )()gxr;
Condition100%2/2
$statement // ""
l!l
16624
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 1653 ▶sub study_sql ($statement, $options = {}) {
Subroutine study_sql: called CC=16 SCAR=27.7
Pod documented
41 1653 my $trimmed = trim_sql($statement);
42 1653 ▶ return unless length $trimmed;
Branch: 1 branch
BranchTrueFalseTotal
unless length $trimmed 6 1647 1653
43 1647 ▶ my $dialect = $options->{ekorn_dialect} // '';
Condition100%2/2
$options->{"ekorn_dialect"} // ""
l!l
161037
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 1647 state $STRING = qr( ' (?: [^'] | '' )* ' )x;
50 1647 state $E_STRING = qr( (?<! \w ) [Ee] ' (?: [^'\\] | \\. | '' )* ' )x;
51 1647 state $IDENT = qr( " (?: [^"] | "" )* " | ` [^`]* ` )x;
52 1647 state $COMMENT = qr( -- [^\n]* | /\* .*? \*/ )xs;
53 1647 state $DOLLARS = qr( \$ (?<tag> (?: [A-Za-z_]\w* )? ) \$ .*? \$ \k<tag> \$ )xs;
54 1647 state $ESCAPED = qr( \\ [?:\$] )x;
55 1647 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 1647 state $MYSQL_STRING
62 = qr( ' (?: [^'\\] | \\. | '' )* ' | " (?: [^"\\] | \\. | "" )* " )x;
63 1647 state $MYSQL_NOT_SQL = qr( $MYSQL_STRING | ` [^`]* ` | $COMMENT | \# [^\n]* )x;
64 1647 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 1647 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 1647 state $DOLLAR_ONLY = qr( (?<! [:\w\$] ) \$ [0-9]+ )x;
75 1647 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 1647 my $pg = $dialect eq 'Pg';
80 1647 ▶ my $dollar_only = $pg && $options->{pg_placeholder_dollaronly};
Condition100%3/3
$pg && $options->{'pg_placeholder_dollaronly'}
!ll&&!rl&&r
163386
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 1647 ▶ : $pg && $options->{pg_placeholder_nocolons} ? $NO_COLONS
Branch: 2 branches
BranchTrueFalseTotal
$dollar_only ? : 6 1641 1647
$pg && $options->{'pg_placeholder_nocolons'} ? : 2 1639 1641
Condition100%3/3
$pg and $options->{"pg_placeholder_nocolons"}
!ll&&!rl&&r
163362
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 1647 ▶ my $not_sql = $NOT_SQL_FOR{$dialect} // $NOT_SQL;
Condition66%2/3
$NOT_SQL_FOR{$dialect} // $NOT_SQL
l!l&&r!l&&!r
816390
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 1647 my $splat_or_not
90 = qr( (?<! [:\w\$] ) (?<splat> \.\.\. )? (?<placeholder> $placeholder ) )x;
91 1647 my(@placeholders, @spans, %lists);
92 1647 my $normalised = $trimmed =~ s(
93 (?<not_sql> $not_sql ) | $splat_or_not
94 )(
95 533 ▶ if (exists $+{not_sql}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $+{'not_sql'}) { } 51 482 533
96 51 $+{not_sql};
97 }
98 else {
99 482 push @placeholders, $+{placeholder};
100 482 push @spans, [$-[0], $+[0]];
101 482 ▶ $lists{ scalar @placeholders } = 1 if defined $+{splat};
Branch: 1 branch
BranchTrueFalseTotal
if defined $+{"splat"} 43 439 482
102 482 '?';
103 }
104 )gerx;
105 1647 ▶ $normalised = $trimmed if $dollar_only;
Branch: 1 branch
BranchTrueFalseTotal
if $dollar_only 6 1641 1647
106  
107 # One style of placeholder to a statement.
108 1647 my @styles = uniq map { _style($_) } @placeholders;
109 1647 ▶ croakf 'Expected one placeholder style, not several (%s)', join ', ', @styles
Branch: 1 branch
BranchTrueFalseTotal
if @styles > 1 8 1639 1647
110 if @styles > 1;
111  
112 1639 my %positions = map { ($_ + 1 => $placeholders[$_]) } keys @placeholders;
113 1639 ▶ return \%positions, $normalised, $trimmed
Branch: 1 branch
BranchTrueFalseTotal
unless %lists 1601 38 1639
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 38 my($first) = sort { $a <=> $b } keys %lists;
119 38 ▶ croakf 'Expected no list placeholder with pg_placeholder_dollaronly (%s)',
Branch: 1 branch
BranchTrueFalseTotal
if $dollar_only 1 37 38
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 37 my($from, @pieces) = (0);
126 37 for my $span (@spans) {
127 53 push @pieces, substr $trimmed, $from, $span->[0] - $from;
128 53 $from = $span->[1];
129 }
130 37 push @pieces, substr $trimmed, $from;
131 37 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 451 ▶sub key_of ($placeholder, $position = undef) {
Subroutine key_of: called CC=4 SCAR=13.9
Pod documented
138 451 ▶ return $position if $placeholder eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $placeholder eq "?" 41 410 451
139 410 my $key = $placeholder =~ s( \A [?:\$] )()xr;
140 410 ▶ return $key =~ m( \A \d+ \z )xa ? 0+ $key : $key;
Branch: 1 branch
BranchTrueFalseTotal
$key =~ / \A \d+ \z /ax ? : 117 293 410
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 451 ▶sub is_numbered ($binds) {
Subroutine is_numbered: called CC=1 SCAR=0.0
Pod documented
151 451 keys %$binds; # resets the iterator, for each
152 451 my $key = each %$binds;
153 451 keys %$binds; # and again, leaving it as it was found
154 451 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 120 ▶sub values_taken ($binds) {
Subroutine values_taken: called CC=2 SCAR=6.9
Pod documented
161 120 ▶ return is_numbered($binds) ? max keys %$binds : 0;
Branch: 1 branch
BranchTrueFalseTotal
is_numbered($binds) ? : 118 2 120
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 250 ▶sub _values_for ($binds, $args) {
Subroutine _values_for: called CC=14 SCAR=26.4
180 250 ▶ if (is_numbered($binds)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_numbered($binds)) 73 177 250
181 73 my $needed = values_taken($binds);
182 73 ▶ croakf 'Expected %d bind values, not %d', $needed, scalar @$args
Branch: 1 branch
BranchTrueFalseTotal
unless @$args == $needed 5 68 73
183 unless @$args == $needed;
184 68 return {map { ($_ => $args->[$_ - 1]) } keys %$binds};
185 }
186 177 my %values;
187 177 ▶ if (@$args == 1 && is_plain_hashref($args->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args == 1 and Ref::Util::XS::is_plain_hashref($args->[0])) { } 16 161 177
Condition100%3/3
@$args == 1 and Ref::Util::XS::is_plain_hashref($args->[0])
!ll&&!rl&&r
160116
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 16 %values = %{ $args->[0] };
189 }
190 else {
191 161 ▶ croakf 'Expected names and values in pairs'
Branch: 1 branch
BranchTrueFalseTotal
if @$args % 2 3 158 161
192 if @$args % 2;
193 158 %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 174 ▶ && !grep { !exists $binds->{$_} } keys %values;
Branch: 1 branch
BranchTrueFalseTotal
if keys %values == keys %$binds and not grep({not exists $binds->{$_};} keys %values) 158 16 174
Condition100%3/3
keys %values == keys %$binds and not grep({not exists $binds->{$_};} keys %values)
!ll&&!rl&&r
79158
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 16 my(%given, @unknown);
201 16 for my $name (keys %values) {
202 # A name as the statement keys it, as most are, is its own key.
203 24 ▶ my $key = exists $binds->{$name} ? $name : key_of($name);
Branch: 1 branch
BranchTrueFalseTotal
exists $binds->{$name} ? : 10 14 24
204 24 $given{$key} = $values{$name};
205 24 ▶ push @unknown, $name unless $binds->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $binds->{$key} 5 19 24
206 }
207 16 ▶ croakf 'Expected a placeholder of the statement (%s)', join ', ', sort @unknown
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 5 11 16
208 if @unknown;
209 # Each key given is the statement's, so as many as it has are all of
210 # them.
211 11 ▶ return \%given if keys %given == keys %$binds;
Branch: 1 branch
BranchTrueFalseTotal
if keys %given == keys %$binds 7 4 11
212 4 my @missing = sort grep { !exists $given{$_} } keys %$binds;
213 4 ▶ croakf 'Expected a value for each placeholder (none for %s)',
Branch: 1 branch
BranchTrueFalseTotal
if @missing 4 0 4
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 72 ▶sub expand_lists ($state, $values) {
Subroutine expand_lists: called CC=3 SCAR=11.0
Pod documented
226 72 my($lists, $pieces, $placeholders) = @$state{qw(Lists Pieces Placeholders)};
227 my %lengths
228 72 = map { ($_ => scalar _list($lists->{$_}, $values->{$_})) } keys %$lists;
229 63 my($statement, $taken, %binds) = ($pieces->[0], 0);
230 63 for my $position (1 .. keys %$placeholders) {
231 71 my $key = key_of($placeholders->{$position}, $position);
232 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
233 71 push @{ $binds{$key} }, $taken + 1 .. $taken + $count;
234 71 $taken += $count;
235 71 $statement .= join(', ', ('?') x $count) . $pieces->[$position];
236 }
237 63 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 12 ▶sub place_values ($state, %values) {
Subroutine place_values: called CC=1 SCAR=0.0
Pod documented
246 12 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 95 ▶sub _place_values ($state, $values) {
Subroutine _place_values: called CC=5 SCAR=16.1
253 95 my($binds, $lists, $lengths) = @$state{qw(Binds Lists Lengths)};
254 95 my @placed;
255 95 for my $key (keys %$values) {
256 104 my $positions = $binds->{$key};
257 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
258 56 push @placed, map { ($_ => $values->{$key}) } @$positions;
259 56 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 92 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 120 ▶sub _list ($name, $list) {
Subroutine _list: called CC=4 SCAR=13.9
274 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
275 unless is_arrayref($list);
276 116 ▶ croakf 'Expected at least one value in a list (%s)', $name
Branch: 1 branch
BranchTrueFalseTotal
unless @$list 4 112 116
277 unless @$list;
278 croakf 'Expected plain values in a list (%s)', $name
279 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
280 110 return @$list;
281 }
282  
283 # Return a placeholder's style, as an example of it: "?", "?1", ":1", "$1",
284 # ":name" or "$name".
285 482 ▶sub _style ($placeholder) {
Subroutine _style: called CC=3 SCAR=11.0
286 482 my($sigil, $rest) = $placeholder =~ m( \A (.) (.*) \z )xs;
287 482 ▶ return $sigil if $rest eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $rest eq "" 183 299 482
288 299 ▶ return "${sigil}1" if $rest =~ m( \A \d+ \z )xa;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ / \A \d+ \z /ax 101 198 299
289 198 return "${sigil}name";
290 }
291  
292 30 1;
293 __END__
↑ Summary blib/lib/DBIx/Squirrel/db.pm »