blib/lib/DBIx/Squirrel/db.pm

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