blib/lib/DBIx/Squirrel/st.pm

filter: Statementstmt 100% 104 / 104 Branchbran 94.7% 36 / 38 Conditioncond 84.6% 22 / 26 MC/DCmcdc 75.0% 12 / 16 Subroutinesub 100% 28 / 28 Podpod 100% 5 / 5 totaltotal 95.3% 207 / 217 CC 32 SCAR 15.2
CC
32
Cov
96%
execute
10.0
bind_param
9.0
_unwrap
4.0
CRAP 4.6
« blib/lib/DBIx/Squirrel/Stages/Rows.pm ↑ Summary blib/lib/DBIx/Squirrel.pm »
1 28 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package DBIx::Squirrel::st;
4  
5 28 ▶use DBI qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶use List::Util qw(pairs);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use Ref::Util qw(is_plain_arrayref);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 28 ▶use DBIx::Squirrel::Errors qw(at_caller croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use DBIx::Squirrel::Iterator qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use DBIx::Squirrel::Placeholders qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 is_numbered key_of place_values values_taken
13 28 );
14 28 ▶use DBIx::Squirrel::Results qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 28 ▶use Ekorn::Pipeline::Pipes qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 28 ▶use DBIx::Squirrel::Util qw(unwrap_values);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 28 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 28 our @ISA = ('DBI::st', 'Ekorn::Pipeline::Pipes');
21  
22 # Croak at the line that called DBIx::Squirrel.
23 28 our @CARP_NOT = @DBIx::Squirrel::Errors::INTERNAL;
24  
25 # Bind values to the statement's placeholders, without executing it, and
26 # return the handle, or nothing if the DBI couldn't bind one. Numbered
27 # placeholders take values in order, the first to every use of 1, and so
28 # on; named ones take a hash reference or a list of names and values. An
29 # array reference stands for the values in it.
30 69 ▶sub bind ($self, @args) {
Subroutine bind: called CC=1 SCAR=0.0
Pod documented
31 69 return _bind($self, _binds($self), _unwrap($self, @args));
32 }
33  
34 # Bind values, as bind does, given as a list, and the statement's map of
35 # placeholders to positions, if it has one. Each value is bound to its
36 # positions directly, with the DBI's bind_param.
37 69 ▶sub _bind ($self, $binds, @args) {
Subroutine _bind: called CC=2 SCAR=6.9
38 69 my @placed;
39 69 ▶ if ($binds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binds) { } 64 5 69
40 64 my $given = DBIx::Squirrel::Placeholders::_values_for($binds, \@args); ## no critic (Subroutines::ProtectPrivateSubs)
41 61 my $state = $self->{private_ekorn};
42 61 @placed = @{ DBIx::Squirrel::Placeholders::_place_values($state, $given) }; ## no critic (Subroutines::ProtectPrivateSubs)
43 }
44 else {
45 5 @placed = map { ($_ => $args[$_ - 1]) } 1 .. @args;
46 }
47 66 ▶ return at_caller {
Subroutine __ANON__: called CC=4 SCAR=13.9
48 66 for my $pair (pairs @placed) {
49 111 ▶ $self->SUPER::bind_param(@$pair) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::bind_param(@$pair) or return 110 1 111
50 }
51 65 ▶ $self->{private_ekorn}{AllBound} = 1 if $binds;
Branch: 1 branch
BranchTrueFalseTotal
if $binds 61 4 65
52 65 $self;
53 66 };
54 }
55  
56 # Bind a value to a placeholder, as the DBI's bind_param does, given its
57 # number or its name, with or without the sigil, and bind it to every use
58 # of that placeholder in the statement; for a list placeholder, bind each
59 # value in the list to a position of its own. Return what the DBI returns.
60 17 ▶sub bind_param ($self, $param, $value, @attrs) {
Subroutine bind_param: called CC=9 SCAR=22.0
Pod documented
61 my $binds = _binds($self)
62 17 ▶ or return at_caller { $self->SUPER::bind_param($param, $value, @attrs) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
my $binds = _binds($self) or return &at_caller(sub { $self->SUPER::bind_param($param, $value, @attrs); } ) 15 2 17
63 15 my $key = key_of($param);
64 15 ▶ unless ($binds->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($binds->{$key}) 6 9 15
65  
66 # A position, for a statement with named placeholders.
67 6 ▶ if ($param =~ m( \A \d+ \z )xa && !is_numbered($binds)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ / \A \d+ \z /ax and not is_numbered($binds)) 3 3 6
Condition100%3/3
$param =~ / \A \d+ \z /ax and not is_numbered($binds)
!ll&&!rl&&r
213
MC/DC100%2/2
$param =~ / \A \d+ \z /ax and not is_numbered($binds)
$param =~ / \A \d+ \z /ax not is_numbered($binds)
Truth table100%3/3
A and not(B)
A= $param =~ / \A \d+ \z /axB= not is_numbered($binds)
ABresult
0X0
100
111
68 3 ▶ my $rv = at_caller { $self->SUPER::bind_param($param, $value, @attrs) };
Subroutine __ANON__: called CC=1 SCAR=0.0
69 3 ▶ $self->{private_ekorn}{Bound}{$param} = 1 if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 3 0 3
70 3 return $rv;
71 }
72 3 croakf 'Expected a placeholder of the statement (%s)', $param;
73 }
74 9 my @placed = pairs place_values($self->{private_ekorn}, $key => $value);
75 8 my $rv;
76 8 for my $pair (@placed) {
77 14 ▶ $rv = at_caller { $self->SUPER::bind_param(@$pair, @attrs) }
Subroutine __ANON__: called CC=1 SCAR=0.0
78 14 ▶ or return $rv;
Branch: 1 branch
BranchTrueFalseTotal
$rv = &at_caller(sub { $self->SUPER::bind_param(@$pair, @attrs); } ) or return $rv 14 0 14
79 }
80 8 $self->{private_ekorn}{Bound}{$_->[0]} = 1 for @placed;
81 8 return $rv;
82 }
83  
84 # Execute the statement, as the DBI's execute does, with any values given
85 # bound as bind binds them. Values for numbered or named placeholders go
86 # to the DBI's execute in the order of their positions, which binds them
87 # as bind_param would, in one call. With the ekorn_finish_active
88 # attribute, a statement still active from an earlier execute is finished
89 # first.
90 1918 ▶sub execute ($self, @args) {
Subroutine execute: called CC=10 SCAR=23.1
Pod documented
91 1918 my $state = $self->{private_ekorn};
92 $self->finish
93 1918 ▶ if $state && $state->{Options}{ekorn_finish_active} && $self->{Active};
Branch: 1 branch
BranchTrueFalseTotal
if $state and $state->{"Options"}{"ekorn_finish_active"} and $self->{"Active"} 2 1916 1918
Condition83%5/6
$state and $state->{"Options"}{"ekorn_finish_active"} and $self->{"Active"}
!ll&&!rl&&r
191422
$state and $state->{"Options"}{"ekorn_finish_active"}
!ll&&!rl&&r
019144
MC/DC66%2/3
$state and $state->{"Options"}{"ekorn_finish_active"} and $self->{"Active"}
$state $state->{"Options"}{"ekorn_finish_active"} $self->{"Active"}
Truth table75%3/4
A and B and C
A= $stateB= $state->{"Options"}{"ekorn_finish_active"}C= $self->{"Active"}
ABCresult
0XX0
10X0
1100
1111
94 # Only values in an array reference given alone need unwrapping, and
95 # most aren't: the call, copying them in and out, is left out for
96 # them (roadmap 2026-0028).
97 1918 ▶ @args = _unwrap($self, @args) if @args == 1 && is_plain_arrayref($args[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @args == 1 and Ref::Util::XS::is_plain_arrayref($args[0]) 9 1909 1918
Condition100%3/3
@args == 1 and Ref::Util::XS::is_plain_arrayref($args[0])
!ll&&!rl&&r
70612039
MC/DC100%2/2
@args == 1 and Ref::Util::XS::is_plain_arrayref($args[0])
@args == 1 Ref::Util::XS::is_plain_arrayref($args[0])
Truth table100%3/3
A and B
A= @args == 1B= Ref::Util::XS::is_plain_arrayref($args[0])
ABresult
0X0
100
111
98 1918 ▶ my $binds = $state && $state->{Binds};
Condition66%2/3
$state && $state->{'Binds'}
!ll&&!rl&&r
01786132
MC/DC50%1/2
$state && $state->{'Binds'}
$state $state->{'Binds'}
Truth table66%2/3
A && B
A= $stateB= $state->{'Binds'}
ABresult
0X0
100
111
99 1918 ▶ if ($binds) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binds) 132 1786 1918
100 132 ▶ _check_bound($self) unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 46 86 132
101 127 ▶ @args = _in_order($state, \@args) if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 86 41 127
102 118 $state->{AllBound} = 1;
103 }
104 1904 ▶ return at_caller { $self->SUPER::execute(@args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
105 }
106  
107 # Return an iterator over the statement's results, executed with the bind
108 # values given, and run through the stages given after them; or, if the
109 # last stage is a sink, the sink's result.
110 421 ▶sub iterate ($self, @args) {
Subroutine iterate: called CC=1 SCAR=0.0
Pod documented
111 421 return DBIx::Squirrel::Iterator->new($self, @args)->_or_sink; ## no critic (Subroutines::ProtectPrivateSubs)
112 }
113  
114 # Return a result set over the statement's results: an iterator handing
115 # out rows as objects; or, if the last stage is a sink, the sink's result.
116 44 ▶sub results ($self, @args) {
Subroutine results: called CC=1 SCAR=0.0
Pod documented
117 44 return DBIx::Squirrel::Results->new($self, @args)->_or_sink; ## no critic (Subroutines::ProtectPrivateSubs)
118 }
119  
120 # Return the values given, or those in an array reference given alone,
121 # as unwrap_values does. Values not given in an array reference alone,
122 # as most aren't, are returned at once, without reading the driver's name
123 # through the handles' attributes.
124 194 ▶sub _unwrap ($self, @args) {
Subroutine _unwrap: called CC=4 SCAR=13.9
125 return @args
126 194 ▶ unless @args == 1 && is_plain_arrayref($args[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless @args == 1 and Ref::Util::XS::is_plain_arrayref($args[0]) 164 30 194
Condition100%3/3
@args == 1 and Ref::Util::XS::is_plain_arrayref($args[0])
!ll&&!rl&&r
1224230
MC/DC100%2/2
@args == 1 and Ref::Util::XS::is_plain_arrayref($args[0])
@args == 1 Ref::Util::XS::is_plain_arrayref($args[0])
Truth table100%3/3
A and B
A= @args == 1B= Ref::Util::XS::is_plain_arrayref($args[0])
ABresult
0X0
100
111
127 30 my $state = $self->{private_ekorn};
128 return unwrap_values(
129 $self->{Database}{Driver}{Name},
130 19 ▶ sub { _values_taken($self) },
Subroutine __ANON__: called CC=1 SCAR=0.0
131 30 ▶ $state && $state->{Lists}, @args,
Condition66%2/3
$state && $state->{'Lists'}
!ll&&!rl&&r
01515
MC/DC50%1/2
$state && $state->{'Lists'}
$state $state->{'Lists'}
Truth table66%2/3
A && B
A= $stateB= $state->{'Lists'}
ABresult
0X0
100
111
132 );
133 }
134  
135 # Return how many values the statement takes in order: one for each
136 # placeholder the DBI sees, or each number, or none if they're named.
137 19 ▶sub _values_taken ($self) {
Subroutine _values_taken: called CC=2 SCAR=6.9
138 my $binds = _binds($self)
139 19 ▶ or return $self->{NUM_OF_PARAMS};
Branch: 1 branch
BranchTrueFalseTotal
my $binds = _binds($self) or return $self->{"NUM_OF_PARAMS"} 17 2 19
140 17 return values_taken($binds);
141 }
142  
143 # Return the statement's map of each placeholder's number or name to its
144 # positions, or nothing if the statement went to the DBI as written.
145 105 ▶sub _binds ($self) {
Subroutine _binds: called CC=2 SCAR=7.1
146 105 my $state = $self->{private_ekorn};
147 105 ▶ return $state && $state->{Binds};
Condition66%2/3
$state && $state->{'Binds'}
!ll&&!rl&&r
0996
MC/DC50%1/2
$state && $state->{'Binds'}
$state $state->{'Binds'}
Truth table66%2/3
A && B
A= $stateB= $state->{'Binds'}
ABresult
0X0
100
111
148 }
149  
150 # Croak unless a value has been bound to every use of each of the
151 # placeholders of a statement with numbered or named ones: given to
152 # execute or bind, which bind them all, or to bind_param. Unbound, the
153 # driver would read them as NULL, or fail.
154 50 ▶sub _check_bound ($self) {
Subroutine _check_bound: called CC=4 SCAR=13.9
155 50 my $state = $self->{private_ekorn};
156 50 ▶ return if $state->{AllBound};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"AllBound"} 39 11 50
157 11 my $binds = $state->{Binds};
158 11 ▶ my $bound = $state->{Bound} // {};
Condition100%2/2
$state->{"Bound"} // {}
l!l
56
MC/DC100%1/1
$state->{"Bound"} // {}
$state->{"Bound"}
Truth table100%2/2
A
A= $state->{"Bound"}
Aresult
11
00
159 my @missing = sort grep {
160 11 grep { !$bound->{$_} }
161 17 @{ $binds->{$_} }
162 } keys %$binds;
163 11 ▶ croakf 'Expected a value for each placeholder (none for %s)',
Branch: 1 branch
BranchTrueFalseTotal
if @missing 8 3 11
164 join(', ', @missing)
165 if @missing;
166 3 return;
167 }
168  
169 # Return the values for a statement's placeholders, as values_for finds
170 # them, in the order of their positions, each as often as it's used, and
171 # each value in a list at a position of its own, given what the statement
172 # handle keeps about the statement.
173 #
174 # A statement without list placeholders keeps their order, so its values
175 # are put in it with a slice (roadmap 2026-0028).
176 86 ▶sub _in_order ($state, $args) {
Subroutine _in_order: called CC=3 SCAR=11.0
177 86 my $given = DBIx::Squirrel::Placeholders::_values_for($state->{Binds}, $args); ## no critic (Subroutines::ProtectPrivateSubs)
178 78 ▶ if (my $order = $state->{Order}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $order = $state->{"Order"}) 56 22 78
179 56 return @$given{@$order};
180 }
181 # A list's values go to positions of their own, each pair a position
182 # and its value, put in order without a hash of them.
183 22 my $placed = DBIx::Squirrel::Placeholders::_place_values($state, $given); ## no critic (Subroutines::ProtectPrivateSubs)
184 21 my @in_order;
185 21 ▶ for (my $i = 0; $i < @$placed; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$placed) 39 21 60
186 39 $in_order[$placed->[$i] - 1] = $placed->[$i + 1];
187 }
188 21 return @in_order;
189 }
190  
191 28 1;
192 __END__
« blib/lib/DBIx/Squirrel/Stages/Rows.pm ↑ Summary blib/lib/DBIx/Squirrel.pm »