blib/lib/DBIx/Squirrel/Util.pm

filter: Statementstmt 100% 56 / 56 Branchbran 100% 18 / 18 Conditioncond 100% 22 / 22 MC/DCmcdc 100% 13 / 13 Subroutinesub 100% 17 / 17 Podpod 100% 8 / 8 totaltotal 100% 134 / 134 CC 18 SCAR 12.1
CC
18
Cov
100%
shift_attrs
6.0
unwrap_values
6.0
iterator_going
3.0
CRAP 3.4
« blib/lib/DBIx/Squirrel.pm ↑ Summary blib/lib/DBIx/Squirrel/Result.pm »
1 30 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package DBIx::Squirrel::Util;
4  
5 30 ▶use Data::Dumper qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Ref::Util qw(is_plain_arrayref is_plain_hashref is_plain_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use Sub::Util qw(set_subname subname);
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(croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 30 ▶use namespace::clean -except => 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 30 our $VERSION = 'v4.3.0';
17  
18 30 our @EXPORT_OK = qw(
19 canonical
20 install_sub
21 is_name
22 iterator_going
23 shift_attrs
24 split_attrs
25 split_stages
26 unwrap_values
27 );
28  
29 # Croak at the line that called DBIx::Squirrel.
30 30 our @CARP_NOT = @DBIx::Squirrel::Errors::INTERNAL;
31  
32 # Return options as a string, the same for the same options.
33 17 ▶sub canonical ($options) {
Subroutine canonical: called CC=1 SCAR=0.0
Pod documented
34 17 return Data::Dumper->new([$options])->Indent(0)->Sortkeys(1)->Terse(1)->Dump;
35 }
36  
37 # Install a subroutine by its full name. An anonymous one is given that
38 # name, for stack traces; a named one keeps its own.
39 92 ▶sub install_sub ($name, $code) {
Subroutine install_sub: called CC=2 SCAR=6.9
Pod documented
40 92 ▶ set_subname($name, $code)
Branch: 1 branch
BranchTrueFalseTotal
if subname($code) =~ / :: __ANON__ \z /ux 72 20 92
41 if subname($code) =~ m( :: __ANON__ \z )x;
42 30 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
43 92 *{ $name } = $code;
44 92 return;
45 }
46  
47 # Return true if a name can be a subroutine's.
48 89 ▶sub is_name ($name) {
Subroutine is_name: called CC=1 SCAR=0.0
Pod documented
49 89 return $name =~ m( \A [^\W\d] \w* \z )x;
50 }
51  
52 # Return the iterator going through a statement handle's results, if one
53 # has results left to hand out.
54 211 ▶sub iterator_going ($sth) {
Subroutine iterator_going: called CC=3 SCAR=11.0
Pod documented
55 211 ▶ my $iterator = $sth->{private_ekorn}{Iterator} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $iterator = $sth->{"private_ekorn"}{"Iterator"} or return 6 205 211
56 6 ▶ return $iterator->{done} ? () : $iterator;
Branch: 1 branch
BranchTrueFalseTotal
$iterator->{'done'} ? : 1 5 6
57 }
58  
59 # Take the attributes off the front of a list of arguments, if they're
60 # there: a hash reference, or undef, as the DBI allows. An array reference
61 # or a plain value there is a bind value, and is left in place, and so is
62 # an object, whatever it's made of, as the DBI binds one as a value.
63 1747 ▶sub shift_attrs ($args) {
Subroutine shift_attrs: called CC=6 SCAR=17.9
Pod documented
64 1747 ▶ return unless @$args;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args 255 1492 1747
65 1492 my $first = $args->[0];
66 1492 ▶ return shift @$args if !defined $first || is_plain_hashref($first);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $first or Ref::Util::XS::is_plain_hashref($first) 237 1255 1492
Condition100%3/3
not defined $first or Ref::Util::XS::is_plain_hashref($first)
l!l&&r!l&&!r
187501255
MC/DC100%2/2
not defined $first or Ref::Util::XS::is_plain_hashref($first)
not defined $first Ref::Util::XS::is_plain_hashref($first)
Truth table100%3/3
not(A) or B
A= not defined $firstB= Ref::Util::XS::is_plain_hashref($first)
ABresult
1X1
011
000
67 1255 ▶ croakf 'Expected a reference to a HASH or ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
if Ref::Util::XS::is_plain_ref($first) and not Ref::Util::XS::is_plain_arrayref($first) 2 1253 1255
Condition100%3/3
Ref::Util::XS::is_plain_ref($first) and not Ref::Util::XS::is_plain_arrayref($first)
!ll&&!rl&&r
124492
MC/DC100%2/2
Ref::Util::XS::is_plain_ref($first) and not Ref::Util::XS::is_plain_arrayref($first)
Ref::Util::XS::is_plain_ref($first) not Ref::Util::XS::is_plain_arrayref($first)
Truth table100%3/3
A and not(B)
A= Ref::Util::XS::is_plain_ref($first)B= not Ref::Util::XS::is_plain_arrayref($first)
ABresult
0X0
100
111
68 if is_plain_ref($first) && !is_plain_arrayref($first);
69 1253 return;
70 }
71  
72 # Split attributes into the DBI's and DBIx::Squirrel's own, those starting
73 # "ekorn_", and return a reference to each. With no attributes, there are
74 # none for the DBI: undef, as the DBI takes.
75 1759 ▶sub split_attrs ($attrs) {
Subroutine split_attrs: called CC=3 SCAR=11.0
Pod documented
76 1759 ▶ my %dbi = %{ $attrs // {} };
Condition100%2/2
$attrs // {}
l!l
2661493
MC/DC100%1/1
$attrs // {}
$attrs
Truth table100%2/2
A
A= $attrs
Aresult
11
00
77 1759 my %ekorn = map { ($_ => delete $dbi{$_}) } grep { m( \A ekorn_ )x } keys %dbi;
78 1759 ▶ return $attrs && \%dbi, \%ekorn;
Condition100%2/2
$attrs && \%dbi
!ll
1493266
MC/DC100%1/1
$attrs && \%dbi
$attrs
Truth table100%2/2
A
A= $attrs
Aresult
00
11
79 }
80  
81 # Take the stages off the end of a list of arguments, and return them. A
82 # stage is a code reference or a pipeline.
83 1159 ▶sub split_stages ($args) {
Subroutine split_stages: called CC=3 SCAR=11.0
Pod documented
84 1159 my $n = @$args;
85 1159 ▶ $n -= 1 while $n && is_stage($args->[$n - 1]);
Branch: 1 branch
BranchTrueFalseTotal
$n and is_stage($$args[$n - 1]) and do { $n -= 1 } 1101 1159 2260
Condition100%3/3
$n and is_stage($$args[$n - 1])
!ll&&!rl&&r
5895701101
MC/DC100%2/2
$n and is_stage($$args[$n - 1])
$n is_stage($$args[$n - 1])
Truth table100%3/3
A and B
A= $nB= is_stage($$args[$n - 1])
ABresult
0X0
100
111
86 1159 return splice @$args, $n;
87 }
88  
89 # Return the bind values given, or those in an array reference given
90 # alone, unless it's a value itself. An object is a value, whatever it's
91 # made of. Given to a statement taking one value, an array reference is
92 # that value if the statement takes a list, as a list placeholder does,
93 # or if the driver is DBD::Pg, which binds an array reference as a
94 # PostgreSQL array, as it would with the DBI. The driver's name is given,
95 # code returning how many values the statement takes, and the
96 # statement's map of its list placeholders, if it has any.
97 1325 ▶sub unwrap_values ($driver, $taken, $lists, @args) {
Subroutine unwrap_values: called CC=6 SCAR=17.9
Pod documented
98 return @args
99 1325 ▶ unless @args == 1 && is_plain_arrayref($args[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless @args == 1 and Ref::Util::XS::is_plain_arrayref($args[0]) 1278 47 1325
Condition100%3/3
@args == 1 and Ref::Util::XS::is_plain_arrayref($args[0])
!ll&&!rl&&r
46681247
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
100 return @args
101 47 ▶ if ($lists || $driver eq 'Pg') && $taken->() == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $lists || $driver eq "Pg" and &$taken() == 1 30 17 47
Condition100%6/6
$lists || $driver eq "Pg" and &$taken() == 1
!ll&&!rl&&r
12530
$lists or $driver eq "Pg"
l!l&&r!l&&!r
31412
MC/DC100%3/3
$lists || $driver eq "Pg" and &$taken() == 1
$lists $driver eq "Pg" &$taken() == 1
Truth table100%5/5
A or B and C
A= $listsB= $driver eq "Pg"C= &$taken() == 1
ABCresult
00X0
1X00
0100
1X11
0111
102 17 return @{ $args[0] };
103 }
104  
105 30 1;
106 __END__
« blib/lib/DBIx/Squirrel.pm ↑ Summary blib/lib/DBIx/Squirrel/Result.pm »