blib/lib/DBIx/Squirrel/db.pm

filter: Statementstmt 97.3% 217 / 223 Branchbran 94.4% 102 / 108 Conditioncond 94.7% 72 / 76 MC/DCmcdc 92.6% 38 / 41 Subroutinesub 96.2% 51 / 53 Podpod 100% 12 / 12 totaltotal 95.9% 492 / 513 CC 91 SCAR 19.0
CC
91
Cov
96%
_study
17.0
prepare_cached
12.0
_study_afresh
12.0
CRAP 6.7
« blib/lib/DBIx/Squirrel/Placeholders.pm ↑ Summary blib/lib/DBIx/Squirrel/st.pm »
1 30 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package DBIx::Squirrel::db;
4  
5 30 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use DBI qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use List::Util qw(all max);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use Ref::Util qw(is_arrayref is_coderef is_hashref is_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 30 ▶use Ekorn::Pipeline qw(is_stage);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 30 ▶use DBIx::Squirrel::Errors qw(at_caller croakf failed in_method);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 30 ▶use DBIx::Squirrel::Placeholders qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
14 expand_lists key_of study_sql values_taken
15 30 );
16 30 ▶use Ekorn::Pipeline::Pipes qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 30 ▶use DBIx::Squirrel::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
18 canonical iterator_going shift_attrs split_attrs split_stages unwrap_values
19 30 );
20  
21 30 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 30 our $VERSION = 'v4.3.1';
24  
25 30 our @ISA = ('DBI::db', 'Ekorn::Pipeline::Pipes');
26  
27 # Croak at the line that called DBIx::Squirrel.
28 30 our @CARP_NOT = @DBIx::Squirrel::Errors::INTERNAL;
29  
30 # DBD::Pg's options that change which placeholders it reads.
31 30 my @PG_OPTIONS = qw(pg_placeholder_dollaronly pg_placeholder_nocolons);
32  
33 # A study _prepare_as hands on to the method it calls, while it calls it:
34 # the handle, the statement and the attributes it's for, and what _study
35 # returned. See _prepare_as.
36 30 our $HANDED;
37  
38 # Prepare a statement, with placeholders in any style, and return its
39 # handle.
40 1725 ▶sub prepare ($self, $statement, $attrs = undef, @args) {
Subroutine prepare: called CC=3 SCAR=11.0
Pod documented
41 1725 ▶ return _prepare($self, $statement, $attrs, @args)
Condition100%2/2
_prepare($self, $statement, $attrs, @args) || croak(failed($self, 'prepare'))
l!l
170010
MC/DC100%1/1
_prepare($self, $statement, $attrs, @args) || croak(failed($self, 'prepare'))
_prepare($self, $statement, $attrs, @args)
Truth table100%2/2
A
A= _prepare($self, $statement, $attrs, @args)
Aresult
11
00
42 || croak failed($self, 'prepare');
43 }
44  
45 # Prepare a statement, as prepare does, and return its handle, or
46 # nothing, as the DBI's prepare does, if it can't be prepared. A
47 # statement with list placeholders can't be prepared without its values.
48 1725 ▶sub _prepare ($self, $statement, $attrs, @args) {
Subroutine _prepare: called CC=1 SCAR=0.0
49 1725 my($normalised, $state, $dbi_attrs) = _study($self, $statement, $attrs);
50 1717 _no_lists($state);
51 1716 return _prepare_studied($self, $normalised, $state, $dbi_attrs, @args);
52 }
53  
54 # Prepare a statement, as _prepare does, given the bind values it's to be
55 # executed with, so that a statement with list placeholders is prepared
56 # with a "?" for each value in each list.
57 188 ▶sub _prepare_for ($self, $statement, $attrs, @values) {
Subroutine _prepare_for: called CC=1 SCAR=0.0
58 188 my($normalised, $state, $dbi_attrs) = _study($self, $statement, $attrs);
59 187 return _prepare_studied(
60 $self, _for_values($self, $normalised, $state, $dbi_attrs, @values),
61 $dbi_attrs,
62 );
63 }
64  
65 # Return a statement handle for a statement with list placeholders, to be
66 # executed with the bind values given: the handle given, if each list has
67 # as many values as it was prepared for, or a new one, prepared for them,
68 # croaking if it can't be prepared. Without values, the handle executes
69 # with those bound already. An iterator calls it from its reset.
70 20 ▶sub _prepare_again ($self, $sth, @values) { ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
Subroutine _prepare_again: called CC=4 SCAR=13.9
71 20 my $state = $sth->{private_ekorn};
72 20 ▶ return $sth
Branch: 1 branch
BranchTrueFalseTotal
unless @values 1 19 20
73 unless @values;
74 my($normalised, $expanded)
75 19 = _for_values($self, undef, $state, $state->{DBIAttrs}, @values);
76 18 my($was, $now) = ($state->{Lengths}, $expanded->{Lengths});
77 return $sth
78 18 ▶ if all { $was->{$_} == $now->{$_} } keys %$now;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &all(sub { $was->{$_} == $now->{$_}; } , keys %$now) 14 4 18
79 return _prepare_studied($self, $normalised, $expanded, $state->{DBIAttrs})
80 4 ▶ || croak failed($self, 'prepare');
Condition100%2/2
_prepare_studied($self, $normalised, $expanded, $state->{'DBIAttrs'}) || croak(failed($self, 'prepare'))
l!l
31
MC/DC100%1/1
_prepare_studied($self, $normalised, $expanded, $state->{'DBIAttrs'}) || croak(failed($self, 'prepare'))
_prepare_studied($self, $normalised, $expanded, $state->{'DBIAttrs'})
Truth table100%2/2
A
A= _prepare_studied($self, $normalised, $expanded, $state->{'DBIAttrs'})
Aresult
11
00
81 }
82  
83 # Return a statement studied already, and what its handle is to keep
84 # about it, as they are, unless it has list placeholders: then, given the
85 # bind values it's to be executed with, return it with a "?" for each
86 # value in each list, as expand_lists does, and what its handle is to keep
87 # about that, starting afresh from what was studied.
88 216 ▶sub _for_values ($self, $normalised, $state, $dbi_attrs, @values) {
Subroutine _for_values: called CC=2 SCAR=6.9
89 return $normalised, $state
90 216 ▶ unless $state->{Lists};
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"Lists"} 145 71 216
91 @values = unwrap_values(
92 $self->{Driver}{Name},
93 16 ▶ sub { values_taken($state->{Binds}) },
Subroutine __ANON__: called CC=1 SCAR=0.0
94 71 $state->{Lists}, @values,
95 );
96 my($expanded, $binds, $lengths) = expand_lists(
97 $state,
98 71 DBIx::Squirrel::Placeholders::_values_for($state->{Binds}, \@values), ## no critic (Subroutines::ProtectPrivateSubs)
99 );
100 62 my %kept = map { ($_ => $state->{$_}) }
101 qw(Placeholders OriginalStatement Options Lists Pieces);
102 62 return $expanded, {
103 %kept,
104 Binds => $binds,
105 Lengths => $lengths,
106 DBIAttrs => $dbi_attrs,
107 };
108 }
109  
110 # Croak if a statement has list placeholders, which take their values
111 # before it's prepared.
112 1935 ▶sub _no_lists ($state) {
Subroutine _no_lists: called CC=2 SCAR=6.9
113 1935 my $lists = $state->{Lists};
114 1935 ▶ croakf 'Expected bind values with a statement taking a list (%s)',
Branch: 1 branch
BranchTrueFalseTotal
if $lists 2 1933 1935
115 join ', ', sort values %$lists
116 if $lists;
117 1933 return;
118 }
119  
120 # Prepare a statement studied already, as _prepare does.
121 1909 ▶sub _prepare_studied ($self, $normalised, $state, $dbi_attrs, @args) {
Subroutine _prepare_studied: called CC=2 SCAR=6.9
122 1909 ▶ my $sth = at_caller { $self->SUPER::prepare($normalised, $dbi_attrs, @args) }
Subroutine __ANON__: called CC=1 SCAR=0.0
123 1909 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = &at_caller(sub { $self->SUPER::prepare($normalised, $dbi_attrs, @args); } ) or return 1864 31 1895
124 1864 $sth->{private_ekorn} = $state;
125 1864 return $sth;
126 }
127  
128 # Prepare a statement, with placeholders in any style, using the DBI's
129 # cache of statement handles, and return its handle.
130 #
131 # The DBI caches a handle by its statement and attributes, and different
132 # statements can be rewritten the same, as "SELECT :a, :b" and
133 # "SELECT :b, :a" both are, so the original statement goes in a private
134 # attribute, to keep their handles apart, with DBIx::Squirrel's own
135 # attributes, which the DBI doesn't see, as a string.
136 #
137 # A cached handle an iterator is part way through is never handed out, or
138 # finished, as the DBI would finish an active one: that would cut the
139 # iterator's results short. A new handle is made, and cached in its place,
140 # unless $if_active is 2, which asks for the handle as it is. Otherwise,
141 # $if_active means what it does to the DBI.
142 218 ▶sub prepare_cached ($self, $statement, $attrs = undef, $if_active = undef) {
Subroutine prepare_cached: called CC=12 SCAR=24.9
Pod documented
143 218 my($normalised, $state, $dbi_attrs, $ekorn) = _study($self, $statement, $attrs);
144 218 _no_lists($state);
145 $dbi_attrs = {
146 217 ▶ %{ $dbi_attrs // {} },
Branch: 1 branch
BranchTrueFalseTotal
%$ekorn ? : 7 210 217
Condition100%2/2
$dbi_attrs // {}
l!l
7210
MC/DC100%1/1
$dbi_attrs // {}
$dbi_attrs
Truth table100%2/2
A
A= $dbi_attrs
Aresult
11
00
147 private_ekorn_statement => $state->{OriginalStatement},
148 217 (%$ekorn ? (private_ekorn_options => canonical($ekorn)) : ()),
149 };
150 224 ▶ my $cached = sub (@if_active) {
Subroutine __ANON__: called CC=1 SCAR=0.1
151 224 ▶ in_method $self, 'prepare_cached', sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
152 224 ▶ at_caller {
Subroutine __ANON__: called CC=1 SCAR=0.0
153 224 $self->SUPER::prepare_cached($normalised, $dbi_attrs, @if_active)
154 224 ▶ } or croak failed($self, 'prepare_cached');
Branch: 1 branch
BranchTrueFalseTotal
&at_caller(sub { $self->SUPER::prepare_cached($normalised, $dbi_attrs, @if_active); } ) or croak(failed($self, "prepare_cached")) 218 0 218
155 224 };
156 217 };
157  
158 # Look at the cached handle, without finishing it.
159 217 my $sth = $cached->(2);
160 211 ▶ my $as_is = defined $if_active && $if_active == 2;
Condition100%3/3
defined $if_active && $if_active == 2
!ll&&!rl&&r
191902
MC/DC100%2/2
defined $if_active && $if_active == 2
defined $if_active $if_active == 2
Truth table100%3/3
A && B
A= defined $if_activeB= $if_active == 2
ABresult
0X0
100
111
161 211 ▶ if (!$as_is && iterator_going($sth)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $as_is and iterator_going($sth)) 4 207 211
Condition100%3/3
not $as_is and iterator_going($sth)
!ll&&!rl&&r
22054
MC/DC100%2/2
not $as_is and iterator_going($sth)
not $as_is iterator_going($sth)
Truth table100%3/3
not(A) and B
A= not $as_isB= iterator_going($sth)
ABresult
0X0
100
111
162 4 _uncache($self, $sth);
163 4 $sth = $cached->();
164 }
165  
166 # One that's active, with no iterator, is the DBI's to deal with.
167 $sth = $cached->(defined $if_active ? $if_active : ())
168 211 ▶ if !$as_is && $sth->{Active};
Branch: 2 branches
BranchTrueFalseTotal
if not $as_is and $sth->{"Active"} 3 208 211
defined $if_active ? : 2 1 3
Condition100%3/3
not $as_is and $sth->{"Active"}
!ll&&!rl&&r
22063
MC/DC100%2/2
not $as_is and $sth->{"Active"}
not $as_is $sth->{"Active"}
Truth table100%3/3
not(A) and B
A= not $as_isB= $sth->{"Active"}
ABresult
0X0
100
111
169  
170 # A cached handle keeps what it had, such as the iterator using it,
171 # updated in place: a copy would make the weak reference to the
172 # iterator strong, and the two would never be freed.
173 211 my $kept = $sth->{private_ekorn};
174 211 ▶ if ($kept) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kept) { } 211 0 211
175 211 @$kept{keys %$state} = values %$state;
176 }
177 else {
178 0 $sth->{private_ekorn} = $state;
179 }
180 211 return $sth;
181 }
182  
183 # Prepare a statement by calling the method named, prepare or
184 # prepare_cached, which a subclass may have of its own, with $if_active
185 # for prepare_cached; or, if the statement takes a list, for the bind
186 # values given, as _prepare_for does, and not cached, croaking as the
187 # method does if it can't be prepared. iterate, results and
188 # DBIx::Squirrel::Sugar's functions prepare their statements with it.
189 #
190 # What it studied is handed on, in $HANDED, to the study the method
191 # makes, so the statement is studied once: twice in full, with
192 # attributes, cost about 32 µs each (roadmap 2026-0028).
193 661 ▶sub _prepare_as ($self, $method, $statement, $attrs, $if_active, @values) {
Subroutine _prepare_as: called CC=5 SCAR=16.1
194 661 my @studied = _study($self, $statement, $attrs);
195 # A statement given as a reference, such as code to call, is studied
196 # each time, as before.
197 660 ▶ local $HANDED
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_ref($statement) ? : 1 659 660
198 = is_ref($statement) ? undef : [$self, $statement, $attrs, @studied];
199 return $self->$method($statement, $attrs, defined $if_active ? $if_active : ())
200 660 ▶ unless $studied[1]{Lists};
Branch: 2 branches
BranchTrueFalseTotal
unless $studied[1]{"Lists"} 640 20 660
defined $if_active ? : 189 451 640
201 20 ▶ return _prepare_for($self, $statement, $attrs, @values)
Condition100%2/2
_prepare_for($self, $statement, $attrs, @values) || croak(failed($self, $method))
l!l
173
MC/DC100%1/1
_prepare_for($self, $statement, $attrs, @values) || croak(failed($self, $method))
_prepare_for($self, $statement, $attrs, @values)
Truth table100%2/2
A
A= _prepare_for($self, $statement, $attrs, @values)
Aresult
11
00
202 || croak failed($self, $method);
203 }
204  
205 # Take a statement handle out of the DBI's cache, so the next
206 # prepare_cached of its statement makes a new one.
207 4 ▶sub _uncache ($self, $sth) {
Subroutine _uncache: called CC=2 SCAR=7.0
208 4 ▶ my $cache = $self->{CachedKids} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cache = $self->{"CachedKids"} or return 4 0 4
209 4 delete @$cache{grep { $cache->{$_} == $sth } keys %$cache};
210 4 return;
211 }
212  
213 # Run a statement, and return what the DBI's do returns. Unlike the DBI's,
214 # the attributes may be left out. A statement DBIx::Squirrel doesn't
215 # rewrite goes to the driver's own do, as with plain DBI, so the driver's
216 # row counts and options, such as DBD::SQLite's multiple statements,
217 # apply; any other is prepared and executed, returning undef, as the DBI
218 # does, if it can't be prepared.
219 1235 ▶sub do ($self, $statement, @args) {
Subroutine do: called CC=2 SCAR=6.9
Pod documented
220 1235 my $attrs = shift_attrs(\@args);
221 1234 my($normalised, $state, $dbi_attrs) = _study($self, $statement, $attrs);
222 1234 ▶ if ($state->{Binds}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"Binds"}) 10 1224 1234
223 10 ▶ return in_method $self, 'do', sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
224 10 ▶ my $sth = _prepare_studied(
Branch: 1 branch
BranchTrueFalseTotal
my $sth = _prepare_studied($self, _for_values($self, $normalised, $state, $dbi_attrs, @args), $dbi_attrs) or return 8 2 10
225 $self, _for_values($self, $normalised, $state, $dbi_attrs, @args),
226 $dbi_attrs,
227 ) or return;
228 8 return $sth->execute(@args);
229 10 };
230 }
231 @args = unwrap_values(
232 $self->{Driver}{Name},
233 0 ▶ sub { _values_taken($state->{Placeholders}) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
234 1224 undef, @args,
235 );
236 1224 ▶ return at_caller { $self->SUPER::do($normalised, $dbi_attrs, @args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
237 }
238  
239 # Return how many values a statement the DBI binds as written takes: one
240 # for each "?", or the highest number of those DBD::Pg reads as "$1".
241 0 ▶sub _values_taken ($placeholders) {
Subroutine _values_taken: not called CC=3 SCAR=24.8
242 0 my @placeholders = values %$placeholders;
243 return scalar @placeholders
244 0 ▶ unless @placeholders && $placeholders[0] =~ m( \A \$ )x;
Branch: 1 branch
BranchTrueFalseTotal
unless @placeholders and $placeholders[0] =~ / \A \$ /ux 0 0 0
Condition0%0/3
@placeholders and $placeholders[0] =~ / \A \$ /ux
!ll&&!rl&&r
000
MC/DC0%0/2
@placeholders and $placeholders[0] =~ / \A \$ /ux
@placeholders $placeholders[0] =~ / \A \$ /ux
Truth table0%0/3
A and B
A= @placeholdersB= $placeholders[0] =~ / \A \$ /ux
ABresult
0X0
100
111
245 0 return max map { s( \A \$ )()xr } @placeholders;
246 }
247  
248 # Prepare a statement, and return an iterator over its results, executed
249 # with the bind values given, and run through the stages given after
250 # them. As with do, the attributes may be left out.
251 239 ▶sub iterate ($self, $statement, @args) {
Subroutine iterate: called CC=1 SCAR=0.0
Pod documented
252 239 return _prepare_to_iterate($self, $statement, \@args)->iterate(@args);
253 }
254  
255 # Prepare a statement, and return a result set over its results, as
256 # iterate does.
257 41 ▶sub results ($self, $statement, @args) {
Subroutine results: called CC=1 SCAR=0.0
Pod documented
258 41 return _prepare_to_iterate($self, $statement, \@args)->results(@args);
259 }
260  
261 # Prepare a statement for iterate or results, as _prepare_as does with
262 # prepare, for the bind values in their arguments, taking the attributes
263 # off the front of them, unless a stage is there.
264 280 ▶sub _prepare_to_iterate ($self, $statement, $args) {
Subroutine _prepare_to_iterate: called CC=3 SCAR=11.0
265 280 ▶ my $attrs = @$args && is_stage($args->[0]) ? undef : shift_attrs($args);
Branch: 1 branch
BranchTrueFalseTotal
@$args && is_stage($args->[0]) ? : 139 141 280
Condition100%3/3
@$args and is_stage($args->[0])
!ll&&!rl&&r
6180139
MC/DC100%2/2
@$args and is_stage($args->[0])
@$args is_stage($args->[0])
Truth table100%3/3
A and B
A= @$argsB= is_stage($args->[0])
ABresult
0X0
100
111
266 280 my @values = @$args;
267 280 split_stages(\@values);
268 280 return _prepare_as($self, 'prepare', $statement, $attrs, undef, @values);
269 }
270  
271 # The DBI's select methods, each preparing the statement and binding its
272 # values as DBIx::Squirrel does. Drivers built on the DBI's own C code,
273 # such as DBD::SQLite, DBD::mysql and DBD::Pg, bind and execute in C, not
274 # through DBIx::Squirrel's methods, so each is given a statement handle
275 # with its values already bound.
276 45 ▶sub selectrow_array ($self, $statement, @args) {
Subroutine selectrow_array: called CC=1 SCAR=0.0
Pod documented
277 45 return _select($self, 'selectrow_array', $statement, [], @args);
278 }
279  
280  
281 9 ▶sub selectrow_arrayref ($self, $statement, @args) {
Subroutine selectrow_arrayref: called CC=1 SCAR=0.0
Pod documented
282 9 return _select($self, 'selectrow_arrayref', $statement, [], @args);
283 }
284  
285  
286 9 ▶sub selectrow_hashref ($self, $statement, @args) {
Subroutine selectrow_hashref: called CC=1 SCAR=0.0
Pod documented
287 9 return _select($self, 'selectrow_hashref', $statement, [], @args);
288 }
289  
290  
291 48 ▶sub selectall_arrayref ($self, $statement, @args) {
Subroutine selectall_arrayref: called CC=1 SCAR=0.0
Pod documented
292 48 return _select($self, 'selectall_arrayref', $statement, [], @args);
293 }
294  
295  
296 7 ▶sub selectall_hashref ($self, $statement, $key_field, @args) {
Subroutine selectall_hashref: called CC=1 SCAR=0.0
Pod documented
297 7 return _select($self, 'selectall_hashref', $statement, [$key_field], @args);
298 }
299  
300  
301 37 ▶sub selectcol_arrayref ($self, $statement, @args) {
Subroutine selectcol_arrayref: called CC=1 SCAR=0.0
Pod documented
302 37 return _select($self, 'selectcol_arrayref', $statement, [], @args);
303 }
304  
305 # The DBI's selectall_array: the rows selectall_arrayref gives, as a
306 # list; or, with stages after the values, each row pushed through them,
307 # and what comes out, as a pipeline called with values gives it. The
308 # DBI's own would call selectall_arrayref with the stages, and take what
309 # it gives for one value, a count, for its rows.
310 16 ▶sub selectall_array ($self, $statement, @args) {
Subroutine selectall_array: called CC=5 SCAR=16.1
Pod documented
311 16 ▶ my $attrs = @args && is_stage($args[0]) ? undef : shift_attrs(\@args);
Branch: 1 branch
BranchTrueFalseTotal
@args && is_stage($args[0]) ? : 5 11 16
Condition100%3/3
@args and is_stage($args[0])
!ll&&!rl&&r
295
MC/DC100%2/2
@args and is_stage($args[0])
@args is_stage($args[0])
Truth table100%3/3
A and B
A= @argsB= is_stage($args[0])
ABresult
0X0
100
111
312 16 my @stages = split_stages(\@args);
313 16 my $rows = _select($self, 'selectall_arrayref', $statement, [], $attrs, @args);
314 16 ▶ my @rows = $rows ? @$rows : ();
Branch: 1 branch
BranchTrueFalseTotal
$rows ? : 16 0 16
315 16 ▶ return @rows unless @stages;
Branch: 1 branch
BranchTrueFalseTotal
unless @stages 9 7 16
316 7 return Ekorn::Pipeline->new(@stages)->(@rows);
317 }
318  
319 # Call one of the DBI's select methods with a statement handle, prepared,
320 # unless it was given one, and with any values bound. Return what the
321 # DBI's method returns; or, with stages after the values, push what it
322 # returns through them, and return what comes out, as a pipeline called
323 # with values does. The attributes may be left out, as with do.
324 #
325 # A statement that can't be prepared, or have its values bound, returns
326 # what the DBI's method does, without RaiseError: undef from those
327 # returning a reference to one row or all of them, and nothing from the
328 # others.
329 181 ▶sub _select ($self, $method, $statement, $before, @args) {
Subroutine _select: called CC=4 SCAR=13.9
330 181 state %UNDEF = map { ($_ => 1) } qw(
331 selectrow_arrayref selectrow_hashref selectall_arrayref
332 );
333 181 ▶ my $attrs = @args && is_stage($args[0]) ? undef : shift_attrs(\@args);
Branch: 1 branch
BranchTrueFalseTotal
@args && is_stage($args[0]) ? : 7 174 181
Condition100%3/3
@args and is_stage($args[0])
!ll&&!rl&&r
461287
MC/DC100%2/2
@args and is_stage($args[0])
@args is_stage($args[0])
Truth table100%3/3
A and B
A= @argsB= is_stage($args[0])
ABresult
0X0
100
111
334 180 my @stages = split_stages(\@args);
335 180 ▶ return _select_through(
Branch: 1 branch
BranchTrueFalseTotal
if @stages 10 170 180
336 $self, $method, $statement, $before, $attrs, \@stages,
337 @args,
338 ) if @stages;
339 170 ▶ return in_method $self, $method, sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
340 170 ▶ my $sth
Branch: 1 branch
BranchTrueFalseTotal
$statement isa 'DBI::st' ? : 2 168 170
341 = $statement isa 'DBI::st'
342 ? $statement
343 : _prepare_for($self, $statement, $attrs, @args);
344 153 ▶ return $UNDEF{$method} ? undef : ()
Branch: 2 branches
BranchTrueFalseTotal
unless $sth 15 138 153
$UNDEF{$method} ? : 8 7 15
345 unless $sth;
346 # Values for ? placeholders go to the DBI, which checks them as
347 # plain DBI does; those for numbered or named ones are bound, or,
348 # without any, must have been.
349 138 my @values = DBIx::Squirrel::st::_unwrap($sth, @args); ## no critic (Subroutines::ProtectPrivateSubs)
350 138 ▶ if ($sth->{private_ekorn}{Binds}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->{"private_ekorn"}{"Binds"}) 68 70 138
351 68 ▶ DBIx::Squirrel::st::_check_bound($sth) unless @values; ## no critic (Subroutines::ProtectPrivateSubs)
Branch: 1 branch
BranchTrueFalseTotal
unless @values 4 64 68
352 65 ▶ return $UNDEF{$method} ? undef : ()
Branch: 2 branches
BranchTrueFalseTotal
if @values and not $sth->bind(@values) 5 58 63
$UNDEF{$method} ? : 3 2 5
Condition100%3/3
@values and not $sth->bind(@values)
!ll&&!rl&&r
1595
MC/DC100%2/2
@values and not $sth->bind(@values)
@values not $sth->bind(@values)
Truth table100%3/3
A and not(B)
A= @valuesB= not $sth->bind(@values)
ABresult
0X0
100
111
353 if @values && !$sth->bind(@values);
354 58 @values = ();
355 }
356 128 my $super = "SUPER::$method";
357 128 ▶ at_caller { $self->$super($sth, @$before, $attrs, @values) };
Subroutine __ANON__: called CC=1 SCAR=0.0
358 170 };
359 }
360  
361 # Call a select method as _select does, without stages, and push what it
362 # returns through the stages: a reference, from most, or a row's values,
363 # from selectrow_array. What comes out is what a pipeline called with
364 # values gives: every value, or, where one is asked for, a sink's result,
365 # or how many.
366 10 ▶sub _select_through (
Subroutine _select_through: called CC=1 SCAR=0.0
367 $self, $method, $statement, $before, $attrs, $stages,
368 @args,
369 ) {
370 10 my @got = _select($self, $method, $statement, $before, $attrs, @args);
371 10 return Ekorn::Pipeline->new(@$stages)->(@got);
372 }
373  
374 # Return a statement with its placeholders rewritten as "?", for the DBI
375 # to prepare, what its statement handle keeps about it, the attributes to
376 # prepare it with, without DBIx::Squirrel's own, and DBIx::Squirrel's own,
377 # those given, apart from the handle's defaults. Study it with the
378 # driver's name, DBD::Pg's placeholder options from the handle, the
379 # handle's own defaults, from connect, and the attributes given, each
380 # overriding the last.
381 #
382 # A statement given as a string, without attributes, as most are, is
383 # studied once for each handle, which keeps what was found in its
384 # private_ekorn_studied attribute, keyed by the statement and, with
385 # DBD::Pg, its placeholder options, as they can be changed on the
386 # handle. Each statement handle has a copy of its own of what the
387 # statement handle keeps, as an iterator adds to it. The handle keeps no
388 # more than $STUDIED statements, so statements made as the program runs
389 # can't fill memory: when it has that many, it evicts $EVICTED of those
390 # used least, as _evict says (roadmap 2026-0030).
391 5048 ▶sub _study ($self, $statement, $attrs) {
Subroutine _study: called CC=17 SCAR=28.3
392 5048 state $STUDIED = 1000;
393 5048 state $EVICTED = 125;
394 # A study _prepare_as handed on, taken once, and only for the handle,
395 # statement and attributes it was made for: a subclass's prepare that
396 # changes them has them studied afresh.
397 5048 ▶ if ($HANDED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DBIx::Squirrel::db::HANDED) 665 4383 5048
398 665 my($dbh, $text, $given, @studied) = @$HANDED;
399 665 ▶ if ( $dbh == $self
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh == $self and $text eq ($statement // "") and ($given // 0) == ($attrs // 0)) 659 6 665
Condition100%12/12
$dbh == $self and $text eq ($statement // "") and ($given // 0) == ($attrs // 0)
!ll&&!rl&&r
42659
$dbh == $self and $text eq ($statement // "")
!ll&&!rl&&r
13661
$statement // ""
l!l
6631
$given // 0
l!l
46615
$attrs // 0
l!l
45616
MC/DC100%3/3
$dbh == $self and $text eq ($statement // "") and ($given // 0) == ($attrs // 0)
$dbh == $self $text eq ($statement // "") $given
MC/DC100%1/1
$statement // ""
$statement
MC/DC100%1/1
$attrs // 0
$attrs
Truth table100%4/4
A and B and C
A= $dbh == $selfB= $text eq ($statement // "")C= $given
ABCresult
0XX0
10X0
1100
1111
Truth table100%2/2
A
A= $statement
Aresult
11
00
Truth table100%2/2
A
A= $attrs
Aresult
11
00
400 && $text eq ($statement // '')
401 && ($given // 0) == ($attrs // 0))
402 {
403 659 $HANDED = undef;
404 659 return @studied;
405 }
406 }
407 4389 ▶ return _study_afresh($self, $statement, $attrs)
Branch: 1 branch
BranchTrueFalseTotal
if $attrs and %$attrs or not defined $statement or Ref::Util::XS::is_ref($statement) 112 4277 4389
Condition100%9/9
$attrs and %$attrs or not defined $statement or Ref::Util::XS::is_ref($statement)
l!l&&r!l&&!r
10574277
$attrs and %$attrs or not defined $statement
l!l&&r!l&&!r
10414284
$attrs and %$attrs
!ll&&!rl&&r
42823104
MC/DC100%4/4
$attrs and %$attrs or not defined $statement or Ref::Util::XS::is_ref($statement)
$attrs %$attrs not defined $statement Ref::Util::XS::is_ref($statement)
Truth table71%5/7
A and B or not(C) or D
A= $attrsB= %$attrsC= not defined $statementD= Ref::Util::XS::is_ref($statement)
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
408 if ($attrs && %$attrs)
409 || !defined $statement
410 || is_ref($statement);
411 my $cache = $self->{private_ekorn_studied} //= {
412 4277 ▶ Pg => $self->{Driver}{Name} eq 'Pg',
Condition100%2/2
$self->{"private_ekorn_studied"} //= {"Pg", $self->{"Driver"}{"Name"} eq "Pg", "Statements", {}, "Order", []}
l!l
4144133
MC/DC100%1/1
$self->{"private_ekorn_studied"} //= {"Pg", $self->{"Driver"}{"Name"} eq "Pg", "Statements", {}, "Order", []}
$self->{"private_ekorn_studied"}
Truth table100%2/2
A
A= $self->{"private_ekorn_studied"}
Aresult
11
00
413 Statements => {},
414 Order => [],
415 };
416 my $key
417 = $cache->{Pg}
418 4277 ▶ ? join("\0", $statement, map { $self->{$_} // '' } @PG_OPTIONS)
Branch: 1 branch
BranchTrueFalseTotal
$cache->{'Pg'} ? : 4 4273 4277
Condition100%2/2
$self->{$_} // ""
l!l
44
MC/DC100%1/1
$self->{$_} // ""
$self->{$_}
Truth table100%2/2
A
A= $self->{$_}
Aresult
11
00
419 : $statement;
420 4277 my $studied = $cache->{Statements};
421 4277 my $found = $studied->{$key};
422 4277 ▶ if ($found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found) { } 2783 1494 4277
423 2783 $found->[2]++;
424 }
425 else {
426 1494 my($normalised, $kept) = _study_afresh($self, $statement, undef);
427 1488 ▶ _evict($studied, $cache->{Order}, $EVICTED)
Branch: 1 branch
BranchTrueFalseTotal
if keys %$studied >= $STUDIED 1 1487 1488
428 if keys %$studied >= $STUDIED;
429 1488 $found = $studied->{$key} = [$normalised, $kept, 0];
430 1488 push @{ $cache->{Order} }, $key;
431 }
432 4271 return $found->[0], {%{ $found->[1] }}, undef, {};
433 }
434  
435 # Evict the number of studies given, walking the queue of their keys,
436 # in the order they were first studied: a study unused since the walk
437 # last passed it is evicted, and one used has its count of uses halved,
438 # so it ages, and goes to the back. It touches only the studies it
439 # passes. A program with more statements than the cache holds keeps
440 # those it uses, and takes 1 to 4% less time than when the cache was
441 # emptied (roadmap 2026-0030).
442 1 ▶sub _evict ($studied, $order, $number) {
Subroutine _evict: called CC=3 SCAR=11.0
443 1 ▶ while ($number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number) 127 1 128
444 127 my $key = shift @$order;
445 127 my $entry = $studied->{$key};
446 127 ▶ if ($entry->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->[2]) { } 2 125 127
447 2 $entry->[2] >>= 1;
448 2 push @$order, $key;
449 }
450 else {
451 125 delete $studied->{$key};
452 125 $number--;
453 }
454 }
455 1 return;
456 }
457  
458 # Study a statement, as _study does, without keeping what was found.
459 1606 ▶sub _study_afresh ($self, $statement, $attrs) {
Subroutine _study_afresh: called CC=12 SCAR=24.9
460 1606 my $driver = $self->{Driver}{Name};
461 1606 my $state = $self->{private_ekorn};
462 my %options = (
463 ekorn_dialect => $driver,
464 10 ($driver eq 'Pg' ? map { ($_ => $self->{$_}) } @PG_OPTIONS : ()),
465 1606 ▶ %{ $state && $state->{Options} // {} },
Condition80%4/5
$state && $state->{"Options"} // {}
l!l
5951011
$state and $state->{"Options"}
!ll&&!rl&&r
10110595
MC/DC50%1/2
$state && $state->{"Options"} // {}
$state $state->{"Options"}
Truth table66%2/3
A and B
A= $stateB= $state->{"Options"}
ABresult
111
0X0
100
466 1606 ▶ %{ $attrs // {} },
Branch: 1 branch
BranchTrueFalseTotal
$driver eq 'Pg' ? : 5 1601 1606
Condition100%2/2
$attrs // {}
l!l
1041502
MC/DC100%1/1
$attrs // {}
$attrs
Truth table100%2/2
A
A= $attrs
Aresult
11
00
467 );
468 1606 my $slice = $options{ekorn_slice};
469 1606 ▶ croakf 'Expected ekorn_slice to be a reference to an ARRAY or HASH'
Branch: 1 branch
BranchTrueFalseTotal
if defined $slice and not Ref::Util::XS::is_arrayref($slice) and not Ref::Util::XS::is_hashref($slice) 1 1605 1606
Condition100%6/6
defined $slice and not Ref::Util::XS::is_arrayref($slice) and not Ref::Util::XS::is_hashref($slice)
!ll&&!rl&&r
1552531
defined $slice and not Ref::Util::XS::is_arrayref($slice)
!ll&&!rl&&r
1548454
MC/DC100%3/3
defined $slice and not Ref::Util::XS::is_arrayref($slice) and not Ref::Util::XS::is_hashref($slice)
defined $slice not Ref::Util::XS::is_arrayref($slice) not Ref::Util::XS::is_hashref($slice)
Truth table100%4/4
A and not(B) and not(C)
A= defined $sliceB= not Ref::Util::XS::is_arrayref($slice)C= not Ref::Util::XS::is_hashref($slice)
ABCresult
0XX0
10X0
1100
1111
470 if defined $slice && !is_arrayref($slice) && !is_hashref($slice);
471 1605 ▶ my($placeholders, $normalised, $original, $lists, $pieces)
Branch: 1 branch
BranchTrueFalseTotal
() = study_sql(_statement_text($statement), \%options) or croakf("Expected a statement") 1598 4 1602
472 = study_sql(_statement_text($statement), \%options)
473 or croakf 'Expected a statement';
474 1598 my %state = (
475 Placeholders => $placeholders,
476 OriginalStatement => $original,
477 Options => \%options,
478 Binds => scalar _binds($placeholders, $normalised, $original),
479 );
480 1597 ▶ @state{qw(Lists Pieces)} = (_lists($placeholders, $lists), $pieces)
Branch: 1 branch
BranchTrueFalseTotal
if $lists 28 1569 1597
481 if $lists;
482 $state{Order} = _order($state{Binds})
483 1595 ▶ if $state{Binds} && !$lists;
Branch: 1 branch
BranchTrueFalseTotal
if $state{"Binds"} and not $lists 124 1471 1595
Condition100%3/3
$state{"Binds"} and not $lists
!ll&&!rl&&r
144526124
MC/DC100%2/2
$state{"Binds"} and not $lists
$state{"Binds"} not $lists
Truth table100%3/3
A and not(B)
A= $state{"Binds"}B= not $lists
ABresult
0X0
100
111
484 1595 return $normalised, \%state, split_attrs($attrs);
485 }
486  
487 # Return the number or name of the placeholder at each position, in
488 # order, given a statement's map of its placeholders' numbers or names to
489 # their positions, so its values can be put in order with a slice
490 # (roadmap 2026-0028).
491 124 ▶sub _order ($binds) {
Subroutine _order: called CC=3 SCAR=11.0
492 124 my @order;
493 124 for my $key (keys %$binds) {
494 181 $order[$_ - 1] = $key for @{ $binds->{$key} };
495 }
496 124 return \@order;
497 }
498  
499 # Return a map of each number or name a statement's placeholders use to
500 # the positions using it, from 1, or nothing if the statement goes to the
501 # DBI as written: with "?" placeholders or none, or with placeholders left
502 # for DBD::Pg to read. With list placeholders, "?" placeholders are
503 # numbered by their positions.
504 1598 ▶sub _binds ($placeholders, $normalised, $original) {
Subroutine _binds: called CC=5 SCAR=16.1
505 1598 ▶ return if $normalised eq $original;
Branch: 1 branch
BranchTrueFalseTotal
if $normalised eq $original 1445 153 1598
506 153 my %binds;
507 153 for my $position (sort { $a <=> $b } keys %$placeholders) {
508 253 my $placeholder = $placeholders->{$position};
509 253 my $key = key_of($placeholder, $position);
510 253 ▶ croakf 'Expected placeholders numbered from 1 (%s)', $placeholder
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ / \A \d+ \z /ax and $key == 0 1 252 253
Condition100%3/3
$key =~ / \A \d+ \z /ax and $key == 0
!ll&&!rl&&r
166861
MC/DC100%2/2
$key =~ / \A \d+ \z /ax and $key == 0
$key =~ / \A \d+ \z /ax $key == 0
Truth table100%3/3
A and B
A= $key =~ / \A \d+ \z /axB= $key == 0
ABresult
0X0
100
111
511 if $key =~ m( \A \d+ \z )xa && $key == 0;
512 252 push @{ $binds{$key} }, $position;
513 }
514 152 return \%binds;
515 }
516  
517 # Return a map of the number or name of each list placeholder to the
518 # placeholder as written, with "...", for its messages, and a "?" with
519 # its position too, as "...? at 2", given the statement's map of its
520 # placeholders and of the positions taking a list. Croak if a placeholder
521 # takes a list in one place and not another.
522 28 ▶sub _lists ($placeholders, $lists) {
Subroutine _lists: called CC=5 SCAR=16.1
523 28 my(%listed, %plain);
524 28 for my $position (keys %$placeholders) {
525 40 my $placeholder = $placeholders->{$position};
526 40 my $key = key_of($placeholder, $position);
527 40 ▶ if ($lists->{$position}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lists->{$position}) { } 31 9 40
528 31 ▶ $listed{$key} = $placeholder eq '?' ? "...? at $key" : "...$placeholder";
Branch: 1 branch
BranchTrueFalseTotal
$placeholder eq '?' ? : 9 22 31
529 }
530 else {
531 9 $plain{$key} = $placeholder;
532 }
533 }
534 28 my @both = sort map { $plain{$_} } grep { exists $plain{$_} } keys %listed;
535 28 ▶ croakf 'Expected a placeholder to take a list everywhere or nowhere (%s)',
Branch: 1 branch
BranchTrueFalseTotal
if @both 2 26 28
536 join ', ', @both
537 if @both;
538 26 return \%listed;
539 }
540  
541 # Return the text of a statement given as a string, as lines in an array
542 # reference, as a code reference returning either, or as a statement
543 # handle.
544 1605 ▶sub _statement_text ($statement) {
Subroutine _statement_text: called CC=6 SCAR=17.9
545 1605 ▶ $statement = $statement->() if is_coderef($statement);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_coderef($statement) 4 1601 1605
546 1605 ▶ return join ' ', @$statement if is_arrayref($statement);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_arrayref($statement) 2 1603 1605
547 1603 ▶ if ($statement isa 'DBI::st') {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement isa "DBI::st") 2 1601 1603
548 2 my $state = $statement->{private_ekorn};
549 2 ▶ return $state ? $state->{OriginalStatement} : $statement->{Statement};
Branch: 1 branch
BranchTrueFalseTotal
$state ? : 1 1 2
550 }
551 1601 ▶ croakf 'Expected a statement' if is_ref($statement);
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_ref($statement) 2 1599 1601
552 1599 return $statement;
553 }
554  
555 30 1;
556 __END__
« blib/lib/DBIx/Squirrel/Placeholders.pm ↑ Summary blib/lib/DBIx/Squirrel/st.pm »