blib/lib/DBIx/Squirrel/Sugar.pm

filter: Statementstmt 100% 111 / 111 Branchbran 100% 50 / 50 Conditioncond 92.3% 36 / 39 MC/DCmcdc 86.3% 19 / 22 Subroutinesub 100% 26 / 26 Podpod 100% 2 / 2 totaltotal 97.6% 244 / 250 CC 40 SCAR 16.3
CC
40
Cov
98%
import
11.0
_define_statement
9.0
database
8.0
CRAP 5.1
« blib/lib/DBIx/Squirrel/Iterator.pm ↑ Summary blib/lib/DBIx/Squirrel/Stages/Rows.pm »
1 10 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package DBIx::Squirrel::Sugar;
4  
5 10 ▶use Ref::Util qw(is_arrayref is_hashref is_plain_hashref is_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use Ekorn::Pipeline qw(is_stage);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Ekorn::Pipeline::Sugar qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use DBIx::Squirrel qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use DBIx::Squirrel::Errors qw(at_caller croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use DBIx::Squirrel::Util qw(install_sub is_name shift_attrs split_stages);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 our $VERSION = 'v4.3.1';
17  
18 # Croak at the line that called DBIx::Squirrel.
19 10 our @CARP_NOT = @DBIx::Squirrel::Errors::INTERNAL;
20  
21 # Each database defined, by name: the arguments to connect with, and the
22 # handle, once it's connected.
23 10 my %connect_args;
24 my %handle;
25  
26 # Give the calling package database or statement, and define the
27 # databases or statements named in a hash reference after each, with
28 # their arguments in an array reference. The pipelines after each
29 # pipeline go to Ekorn::Pipeline::Sugar first, wherever they're given,
30 # so a statement can name one (roadmap 2026-0027).
31 23 ▶sub import ($class, @args) {
Subroutine import: called CC=11 SCAR=24.0
32 23 state %define = (database => \&_define, statement => \&_define_statement);
33 23 state %what = (database => 'arguments to connect to', statement => 'statement');
34 23 my $package = caller;
35 23 my(@pipelines, @rest);
36 23 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 38 23 61
37 38 my $arg = shift @args;
38 38 ▶ if (defined $arg && $arg eq 'pipeline') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg and $arg eq "pipeline") 6 32 38
Condition66%2/3
defined $arg and $arg eq "pipeline"
!ll&&!rl&&r
0326
MC/DC50%1/2
defined $arg and $arg eq "pipeline"
defined $arg $arg eq "pipeline"
Truth table66%2/3
A and B
A= defined $argB= $arg eq "pipeline"
ABresult
0X0
100
111
39 6 push @pipelines, $arg, shift @args;
40 6 next;
41 }
42 32 push @rest, $arg;
43 }
44 6 ▶ at_caller { Ekorn::Pipeline::Sugar->import_into($package, @pipelines) }
Subroutine __ANON__: called CC=1 SCAR=0.0
45 23 ▶ if @pipelines;
Branch: 1 branch
BranchTrueFalseTotal
if @pipelines 6 17 23
46 19 @args = @rest;
47 19 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 24 15 39
48 24 my $arg = shift @args;
49 croakf 'Expected database, pipeline or statement, not %s', $arg
50 24 ▶ unless $define{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless $define{$arg} 1 23 24
51 23 _install($package, $arg => $class->can($arg));
52 23 ▶ next unless @args && is_hashref($args[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless @args and Ref::Util::XS::is_hashref($args[0]) 15 8 23
Condition100%3/3
@args and Ref::Util::XS::is_hashref($args[0])
!ll&&!rl&&r
968
MC/DC100%2/2
@args and Ref::Util::XS::is_hashref($args[0])
@args Ref::Util::XS::is_hashref($args[0])
Truth table100%3/3
A and B
A= @argsB= Ref::Util::XS::is_hashref($args[0])
ABresult
0X0
100
111
53 8 my $definitions = shift @args;
54 8 for my $name (sort keys %$definitions) {
55 croakf 'Expected the %s %s in an ARRAY', $what{$arg}, $name
56 14 ▶ unless is_arrayref($definitions->{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_arrayref($definitions->{$name}) 2 12 14
57 12 $define{$arg}->($package, $name, @{ $definitions->{$name} });
58 }
59 }
60 15 return;
61 }
62  
63 # Return the handle of the database named, or, given the arguments to
64 # connect with, define it, give the calling package a function of its
65 # name, and return that.
66 #
67 # The handle is kept, and given again without asking the database, which
68 # would cost a round trip each time, unless it's been disconnected, or its
69 # last call failed, when it's pinged, and replaced if it doesn't answer.
70 # Neither DBD::mysql nor DBD::Pg turns Active off when the server drops
71 # the connection, but each leaves the failure in err.
72 456 ▶sub database ($name, @connect) {
Subroutine database: called CC=8 SCAR=20.8
Pod documented
73 456 ▶ return _define(scalar caller, $name, @connect) if @connect;
Branch: 1 branch
BranchTrueFalseTotal
if @connect 20 436 456
74 croakf 'Expected a database named %s', $name
75 436 ▶ unless $connect_args{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $connect_args{$name} 2 434 436
76 434 my $dbh = $handle{$name};
77 return $dbh
78 434 ▶ if $dbh && $dbh->{Active} && (!$dbh->err || _answers($dbh));
Branch: 1 branch
BranchTrueFalseTotal
if $dbh and $dbh->{"Active"} and !$dbh->err || _answers($dbh) 413 21 434
Condition100%9/9
$dbh and $dbh->{"Active"} and !$dbh->err || _answers($dbh)
!ll&&!rl&&r
201413
$dbh and $dbh->{"Active"}
!ll&&!rl&&r
191414
not $dbh->err or _answers($dbh)
l!l&&r!l&&!r
40671
MC/DC100%4/4
$dbh and $dbh->{"Active"} and !$dbh->err || _answers($dbh)
$dbh $dbh->{"Active"} not $dbh->err _answers($dbh)
Truth table100%5/5
A and B and not(C) or D
A= $dbhB= $dbh->{"Active"}C= not $dbh->errD= _answers($dbh)
ABCDresult
0XXX0
10XX0
11000
111X1
11011
79 21 my($source, @rest) = @{ $connect_args{$name} };
80 21 ▶ my $method = $source isa 'DBI::db' ? 'connect' : 'connect_cached';
Branch: 1 branch
BranchTrueFalseTotal
$source isa 'DBI::db' ? : 2 19 21
81 21 return $handle{$name} = DBIx::Squirrel->$method($source, @rest);
82 }
83  
84 # Return true if a database answers a ping. A ping that dies, as the DBI
85 # allows, is no answer.
86 8 ▶sub _answers ($dbh) {
Subroutine _answers: called CC=1 SCAR=0.0
87 8 my $answered = eval { $dbh->ping };
88 8 return $answered;
89 }
90  
91 # Define a statement on a database, with the attributes to prepare it
92 # with and stages, if given, give the calling package a function of its
93 # name, and return that.
94 23 ▶sub statement ($name, $database, $statement, @rest) {
Subroutine statement: called CC=1 SCAR=0.0
Pod documented
95 22 return _define_statement(scalar caller, $name, $database, $statement, @rest);
96 }
97  
98 # Define a database, and give a package a function of its name, returning
99 # the function.
100 22 ▶sub _define ($package, $name, @connect) {
Subroutine _define: called CC=3 SCAR=11.0
101 22 ▶ croakf 'Expected a name for a database (%s)', $name
Branch: 1 branch
BranchTrueFalseTotal
unless is_name($name) 1 21 22
102 unless is_name($name);
103 croakf 'Expected a database defined once (%s)', $name
104 21 ▶ if $connect_args{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $connect_args{$name} 1 20 21
105 20 ▶ my $function = sub (@args) { _run($name, @args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine hushed: called CC=1 SCAR=0.0
106 20 _install($package, $name, $function);
107 19 $connect_args{$name} = \@connect;
108 19 return $function;
109 }
110  
111 # Define a statement, and give a package a function of its name, returning
112 # the function.
113 32 ▶sub _define_statement ($package, $name, @definition) {
Subroutine _define_statement: called CC=9 SCAR=22.0
114 32 my($database, $statement, @rest) = @definition;
115 32 ▶ croakf 'Expected a name for a statement (%s)', $name
Branch: 1 branch
BranchTrueFalseTotal
unless is_name($name) 1 31 32
116 unless is_name($name);
117 31 ▶ croakf 'Expected a database and a statement for %s', $name
Branch: 1 branch
BranchTrueFalseTotal
unless defined $database and defined $statement 1 30 31
Condition66%2/3
defined $database and defined $statement
!ll&&!rl&&r
1030
MC/DC50%1/2
defined $database and defined $statement
defined $database defined $statement
Truth table66%2/3
A and B
A= defined $databaseB= defined $statement
ABresult
0X0
100
111
118 unless defined $database && defined $statement;
119 # Only a plain hash is the attributes: a pipeline, or any other
120 # object, straight after the statement is a stage, or a mistake.
121 30 ▶ my $attrs
Branch: 1 branch
BranchTrueFalseTotal
@rest && (!defined($rest[0]) || Ref::Util::XS::is_plain_hashref($rest[0])) ? : 5 25 30
Condition100%6/6
@rest and !defined($rest[0]) || Ref::Util::XS::is_plain_hashref($rest[0])
!ll&&!rl&&r
6195
not defined $rest[0] or Ref::Util::XS::is_plain_hashref($rest[0])
l!l&&r!l&&!r
1419
MC/DC100%3/3
@rest and !defined($rest[0]) || Ref::Util::XS::is_plain_hashref($rest[0])
@rest not defined $rest[0] Ref::Util::XS::is_plain_hashref($rest[0])
Truth table100%4/4
A and not(B) or C
A= @restB= not defined $rest[0]C= Ref::Util::XS::is_plain_hashref($rest[0])
ABCresult
0XX0
1000
11X1
1011
122 = @rest && (!defined $rest[0] || is_plain_hashref($rest[0]))
123 ? shift @rest
124 : undef;
125 croakf
126 "Expected only stages after the statement for %s: code references, pipelines or their names, or objects offering a stage's role method",
127 $name
128 30 ▶ if grep { !is_stage($_) && !_is_pipeline_name($_) } @rest;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not is_stage($_) || _is_pipeline_name($_);} @rest 4 26 30
Condition100%3/3
is_stage($_) || _is_pipeline_name($_)
l!l&&r!l&&!r
1874
MC/DC100%2/2
is_stage($_) || _is_pipeline_name($_)
is_stage($_) _is_pipeline_name($_)
Truth table100%3/3
A || B
A= is_stage($_)B= _is_pipeline_name($_)
ABresult
1X1
011
000
129 26 @rest = map { _resolved($package, $_) } @rest;
130 25 my %definition = (
131 database => $database,
132 statement => $statement,
133 attrs => $attrs,
134 stages => \@rest,
135 );
136 25 ▶ my $function = sub (@args) { _run_statement(\%definition, @args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
137 25 _install($package, $name, $function);
138 23 return $function;
139 }
140  
141 # Return true if a stage given in a definition is a pipeline's name:
142 # words joined by ::, as Ekorn::Pipeline names them.
143 36 ▶sub _is_pipeline_name ($stage) {
Subroutine _is_pipeline_name: called CC=3 SCAR=11.1
144 return
145 36 ▶ defined $stage
Condition83%5/6
defined $stage && !Ref::Util::XS::is_ref($stage) && $stage =~ / \A [^\W\d]\w* (?: :: [^\W\d]\w* )* \z /ax
!ll&&!rl&&r
21114
defined $stage and not Ref::Util::XS::is_ref($stage)
!ll&&!rl&&r
02115
MC/DC66%2/3
defined $stage && !Ref::Util::XS::is_ref($stage) && $stage =~ / \A [^\W\d]\w* (?: :: [^\W\d]\w* )* \z /ax
defined $stage not Ref::Util::XS::is_ref($stage) $stage =~ / \A [^\W\d]\w* (?: :: [^\W\d]\w* )* \z /ax
Truth table75%3/4
A and not(B) && C
A= defined $stageB= not Ref::Util::XS::is_ref($stage)C= $stage =~ / \A [^\W\d]\w* (?: :: [^\W\d]\w* )* \z /ax
ABCresult
0XX0
10X0
1100
1111
146 && !is_ref($stage)
147 && $stage =~ m( \A [^\W\d]\w* (?: :: [^\W\d]\w* )* \z )xa;
148 }
149  
150 # Return a stage given in a definition: a pipeline's name resolved now, in
151 # the package making the definition, as Ekorn::Pipeline's pipeline word
152 # resolves one in its caller's, each on its own, so the other stages keep
153 # their places; anything else as it is.
154 25 ▶sub _resolved ($package, $stage) {
Subroutine _resolved: called CC=2 SCAR=6.9
155 25 ▶ return $stage unless _is_pipeline_name($stage);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_pipeline_name($stage) 18 7 25
156 7 ▶ return at_caller { Ekorn::Pipeline->pipeline_in($package, $stage) };
Subroutine __ANON__: called CC=1 SCAR=0.0
157 }
158  
159 # Run a statement's function: execute the statement with the bind values
160 # given, and return its handle, or, with stages, its own or those given
161 # after the bind values, return an iterator over the results, or, if the
162 # last stage is a sink, its result. The statement is prepared once for
163 # each handle, with prepare_cached, which makes a new one, cached in its
164 # place, when the one prepared is in use; one taking a list is prepared
165 # for the values given, and not cached.
166 195 ▶sub _run_statement ($definition, @args) {
Subroutine _run_statement: called CC=1 SCAR=0.0
167 195 my $dbh = database($definition->{database});
168 194 my @statement = @$definition{qw(statement attrs)};
169 194 my @stages = (@{ $definition->{stages} }, split_stages(\@args));
170  
171 194 my $sth = DBIx::Squirrel::db::_prepare_as( ## no critic (Subroutines::ProtectPrivateSubs)
172 $dbh, prepare_cached => @statement, 3, @args,
173 );
174 193 return _execute($sth, @args, @stages);
175 }
176  
177 # Run what a database's function is given: nothing, for the handle; a
178 # statement and its bind values, for the statement handle, executed; or
179 # those and stages after them, for an iterator, or a sink's result.
180 224 ▶sub _run ($name, @args) {
Subroutine _run: called CC=4 SCAR=13.9
181 224 my $dbh = database($name);
182 223 ▶ return $dbh unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 23 200 223
183 200 my $statement = shift @args;
184 200 ▶ return _execute($statement, @args) if $statement isa 'DBI::st';
Branch: 1 branch
BranchTrueFalseTotal
if $statement isa "DBI::st" 2 198 200
185 198 ▶ return $dbh->iterate($statement, @args) if _ends_in_stages(\@args);
Branch: 1 branch
BranchTrueFalseTotal
if _ends_in_stages(\@args) 11 187 198
186 187 my $attrs = shift_attrs(\@args);
187  
188 187 my $sth = DBIx::Squirrel::db::_prepare_as( ## no critic (Subroutines::ProtectPrivateSubs)
189 $dbh, prepare => $statement, $attrs, undef, @args,
190 );
191 185 return _execute($sth, @args);
192 }
193  
194 # Execute a statement handle with the bind values given, and return it,
195 # or nothing if it couldn't be executed; or, with stages after the bind
196 # values, return an iterator over the results, or, if the last stage is a
197 # sink, its result.
198 380 ▶sub _execute ($sth, @args) {
Subroutine _execute: called CC=3 SCAR=11.0
199 380 ▶ return $sth->iterate(@args) if _ends_in_stages(\@args);
Branch: 1 branch
BranchTrueFalseTotal
if _ends_in_stages(\@args) 187 193 380
200 193 ▶ defined $sth->execute(@args) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $sth->execute(@args) or return 191 2 193
201 191 return $sth;
202 }
203  
204 # Return true if a list of arguments ends in a stage.
205 578 ▶sub _ends_in_stages ($args) {
Subroutine _ends_in_stages: called CC=2 SCAR=6.9
206 578 ▶ return @$args && is_stage($args->[-1]);
Condition100%3/3
@$args && is_stage($args->[-1])
!ll&&!rl&&r
27353198
MC/DC100%2/2
@$args && is_stage($args->[-1])
@$args is_stage($args->[-1])
Truth table100%3/3
A && B
A= @$argsB= is_stage($args->[-1])
ABresult
0X0
100
111
207 }
208  
209 # Give a package a named function, croaking if it has one of that name,
210 # unless it's the same.
211 68 ▶sub _install ($package, $name, $code) {
Subroutine _install: called CC=4 SCAR=13.9
212 68 my $have = $package->can($name);
213 68 ▶ return if $have && $have == $code;
Branch: 1 branch
BranchTrueFalseTotal
if $have and $have == $code 3 65 68
Condition100%3/3
$have and $have == $code
!ll&&!rl&&r
6233
MC/DC100%2/2
$have and $have == $code
$have $have == $code
Truth table100%3/3
A and B
A= $haveB= $have == $code
ABresult
0X0
100
111
214 65 ▶ croakf 'Expected no function of the name in %s already (%s)', $package, $name
Branch: 1 branch
BranchTrueFalseTotal
if $have 3 62 65
215 if $have;
216 62 install_sub("${package}::$name", $code);
217 62 return;
218 }
219  
220 10 1;
221 __END__
« blib/lib/DBIx/Squirrel/Iterator.pm ↑ Summary blib/lib/DBIx/Squirrel/Stages/Rows.pm »