blib/lib/DBIx/Squirrel/Sugar.pm

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