blib/lib/WebDyne/Cloudflare/Hyperdrive/Statement.pm

filter: Statementstmt 96.4% 137 / 142 Branchbran 70.2% 59 / 84 Conditioncond 59.5% 25 / 42 MC/DCmcdc 30.4% 7 / 23 Subroutinesub 96.1% 25 / 26 Podpod 0.0% 0 / 17 totaltotal 75.7% 253 / 334 CC 59 SCAR 20.3
CC
59
Cov
82%
parameter
22.4
rows
12.0
bind_param
8.5
CRAP 7.6
« blib/lib/WebDyne/Cloudflare/R2.pm ↑ Summary lib/WebDyne/Cloudflare/DurableObject/Bytes.pm »
1 package WebDyne::Cloudflare::Hyperdrive::Statement;
2  
3 1 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use WebDyne::Cloudflare::Hyperdrive::Codec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use WebDyne::Cloudflare::Hyperdrive::Blob;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION='0.001';
13  
14 15 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
15 15 my ($class, %opt)=@_;
16 15 return bless({ database => $opt{'database'}, sql => $opt{'sql'}, bindings => {}, hints => {}, position => 0 }, $class);
17 }
18  
19  
20 73 ▶sub available {
Subroutine available: called CC=2 SCAR=6.9
Pod undocumented
21 73 my ($self)=@_;
22 73 $self->{'database'}->available();
23 70 ▶ die "Hyperdrive statement execution is in progress\n" if $self->{'busy'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"busy"} 2 68 70
24 }
25  
26  
27 11 ▶sub parameter {
Subroutine parameter: called CC=14 SCAR=31.1
Pod undocumented
28 11 my ($value_ref, $type)=@_;
29 11 ▶ if (defined($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 6 5 11
30 6 ▶ die "Unsupported Hyperdrive parameter type\n" if ref($type)||($type!~/\A(?:text|boolean|bytea)\z/);
Branch: 1 branch
BranchTrueFalseTotal
if ref $type or not $type =~ /\A(?:text|boolean|bytea)\z/u 1 5 6
Condition66%2/3
ref $type or not $type =~ /\A(?:text|boolean|bytea)\z/u
l!l&&r!l&&!r
015
MC/DC50%1/2
ref $type or not $type =~ /\A(?:text|boolean|bytea)\z/u
ref $type not $type =~ /\A(?:text|boolean|bytea)\z/u
Truth table66%2/3
A or not(B)
A= ref $typeB= not $type =~ /\A(?:text|boolean|bytea)\z/u
ABresult
1X1
011
000
31 5 ▶ if (defined($value_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value_ref) 5 0 5
32 5 ▶ if ($type eq 'bytea') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'bytea') { } 3 2 5
elsif ($type eq 'boolean') { } 2 0 2
33 3 ▶ $value_ref=WebDyne::Cloudflare::Hyperdrive::Blob->new($value_ref)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value_ref) and $value_ref->isa("WebDyne::Cloudflare::Hyperdrive::Blob") 3 0 3
Condition33%1/3
builtin::blessed($value_ref) and $value_ref->isa("WebDyne::Cloudflare::Hyperdrive::Blob")
!ll&&!rl&&r
300
MC/DC0%0/2
builtin::blessed($value_ref) and $value_ref->isa("WebDyne::Cloudflare::Hyperdrive::Blob")
builtin::blessed($value_ref) $value_ref->isa("WebDyne::Cloudflare::Hyperdrive::Blob")
Truth table33%1/3
A and B
A= builtin::blessed($value_ref)B= $value_ref->isa("WebDyne::Cloudflare::Hyperdrive::Blob")
ABresult
0X0
100
111
34 unless blessed($value_ref)&&$value_ref->isa('WebDyne::Cloudflare::Hyperdrive::Blob');
35 }
36 elsif ($type eq 'boolean') {
37 2 ▶ die "Hyperdrive boolean requires 0, 1 or a JSON boolean\n"
Branch: 1 branch
BranchTrueFalseTotal
unless JSON::PP::is_bool($value_ref) or not ref $value_ref and $value_ref =~ /\A[01]\z/u 0 2 2
Condition33%2/6
JSON::PP::is_bool($value_ref) or not ref $value_ref and $value_ref =~ /\A[01]\z/u
l!l&&r!l&&!r
020
not ref $value_ref and $value_ref =~ /\A[01]\z/u
!ll&&!rl&&r
002
MC/DC0%0/3
JSON::PP::is_bool($value_ref) or not ref $value_ref and $value_ref =~ /\A[01]\z/u
JSON::PP::is_bool($value_ref) not ref $value_ref $value_ref =~ /\A[01]\z/u
Truth table25%1/4
A or not(B) and C
A= JSON::PP::is_bool($value_ref)B= not ref $value_refC= $value_ref =~ /\A[01]\z/u
ABCresult
1XX1
0111
00X0
0100
38 unless JSON::PP::is_bool($value_ref)||(!ref($value_ref)&&($value_ref=~/\A[01]\z/));
39 2 ▶ $value_ref=$value_ref ? JSON::PP::true() : JSON::PP::false();
Branch: 1 branch
BranchTrueFalseTotal
$value_ref ? : 1 1 2
40 }
41 0 ▶ else { die "Hyperdrive text requires a scalar\n" if ref($value_ref) }
Branch: 1 branch
BranchTrueFalseTotal
if ref $value_ref 0 0 0
42 }
43 }
44 9 return WebDyne::Cloudflare::Hyperdrive::Codec::parameters($value_ref)->[0];
45 }
46  
47  
48 9 ▶sub bind_param {
Subroutine bind_param: called CC=8 SCAR=21.3
Pod undocumented
49 9 my ($self, $position, $value_ref, $type)=@_;
50 9 $self->available();
51 9 ▶ die "Hyperdrive bind_param requires position, value and optional type\n" unless (@_==3)||(@_==4);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 3 or @_ == 4 0 9 9
Condition66%2/3
@_ == 3 or @_ == 4
l!l&&r!l&&!r
630
MC/DC0%0/2
@_ == 3 or @_ == 4
@_ == 3 @_ == 4
Truth table66%2/3
A or B
A= @_ == 3B= @_ == 4
ABresult
1X1
011
000
52 9 ▶ die "Hyperdrive parameter positions are integers from 1 to 65535\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $position and not ref $position and $position =~ /\A[1-9][0-9]*\z/u and $position <= 65535 5 4 9
Condition66%6/9
defined $position and not ref $position and $position =~ /\A[1-9][0-9]*\z/u and $position <= 65535
!ll&&!rl&&r
414
defined $position and not ref $position and $position =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
045
defined $position and not ref $position
!ll&&!rl&&r
009
MC/DC50%2/4
defined $position and not ref $position and $position =~ /\A[1-9][0-9]*\z/u and $position <= 65535
defined $position not ref $position $position =~ /\A[1-9][0-9]*\z/u $position <= 65535
Truth table60%3/5
A and not(B) and C and D
A= defined $positionB= not ref $positionC= $position =~ /\A[1-9][0-9]*\z/uD= $position <= 65535
ABCDresult
0XXX0
10XX0
110X0
11100
11111
53 unless defined($position)&&!ref($position)&&($position=~/\A[1-9][0-9]*\z/)&&($position<=65535);
54 4 ▶ my $hint=defined($type) ? $type : $self->{'hints'}{$position};
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 3 1 4
55 4 $self->{'bindings'}{$position}=parameter($value_ref, $hint);
56 3 $self->{'hints'}{$position}=$hint;
57 3 return 1;
58 }
59  
60  
61 24 ▶sub execute {
Subroutine execute: called CC=6 SCAR=17.9
Pod undocumented
62 24 my ($self, @bind)=@_;
63 24 $self->available();
64 23 delete($self->{'result'});
65 23 $self->{'position'}=0;
66 23 ▶ if (@bind) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bind) 4 19 23
67 4 ▶ die "Hyperdrive accepts at most 65535 parameters\n" if @bind>65535;
Branch: 1 branch
BranchTrueFalseTotal
if @bind > 65535 0 4 4
68 4 my %bindings;
69 4 foreach my $index (0..$#bind) { $bindings{$index+1}=parameter($bind[$index], $self->{'hints'}{$index+1}) }
70 3 $self->{'bindings'}=\%bindings;
71 }
72 22 my @positions=sort { $a<=>$b } keys(%{$self->{'bindings'}});
73 22 ▶ die "Hyperdrive bound parameter positions must be contiguous\n" if @positions&&($positions[-1]!=@positions);
Branch: 1 branch
BranchTrueFalseTotal
if @positions and $positions[-1] != @positions 1 21 22
Condition100%3/3
@positions and $positions[-1] != @positions
!ll&&!rl&&r
1381
MC/DC100%2/2
@positions and $positions[-1] != @positions
@positions $positions[-1] != @positions
Truth table100%3/3
A and B
A= @positionsB= $positions[-1] != @positions
ABresult
0X0
100
111
74 21 my $params_ar=[map { $self->{'bindings'}{$_} } @positions];
75 21 $self->{'busy'}=1;
76 21 my $future_or=$self->run($params_ar);
77 21 ▶ $future_or->on_ready(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
78 21 $self->{'busy'}=0;
79 21 ▶ delete($self->{'result'}) unless $future_or->is_done();
Branch: 1 branch
BranchTrueFalseTotal
unless $future_or->is_done 2 19 21
80 21 });
81 21 return $future_or;
82 }
83  
84  
85 21 ▶async sub run {
Subroutine run: called CC=3 SCAR=11.6
Pod undocumented
86 21 my ($self, $params_ar)=@_;
87 21 my $result_hr=await $self->{'database'}->request('query', sql => $self->{'sql'}, params => $params_ar);
88 19 ▶ die "Hyperdrive query omitted row metadata\n" unless (ref($result_hr->{'rows'}) eq 'ARRAY')&&(ref($result_hr->{'columns'}) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result_hr->{"rows"} eq "ARRAY" and ref $result_hr->{"columns"} eq "ARRAY" 0 19 19
Condition33%1/3
ref $result_hr->{"rows"} eq "ARRAY" and ref $result_hr->{"columns"} eq "ARRAY"
!ll&&!rl&&r
0019
MC/DC0%0/2
ref $result_hr->{"rows"} eq "ARRAY" and ref $result_hr->{"columns"} eq "ARRAY"
ref $result_hr->{"rows"} eq "ARRAY" ref $result_hr->{"columns"} eq "ARRAY"
Truth table33%1/3
A and B
A= ref $result_hr->{"rows"} eq "ARRAY"B= ref $result_hr->{"columns"} eq "ARRAY"
ABresult
0X0
100
111
89 19 $self->{'result'}=$result_hr;
90 19 return count_value($result_hr->{'count'});
91 1 }
92  
93  
94 0 ▶sub rows {
Subroutine rows: not called CC=3 SCAR=24.8
Pod undocumented
95 0 my ($self)=@_;
96 0 ▶ die "Hyperdrive rows takes no arguments\n" unless @_==1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 0 0
97 0 $self->available();
98 0 ▶ return count_value($self->{'result'} ? $self->{'result'}{'count'} : undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'result'} ? : 0 0 0
99 }
100  
101  
102 19 ▶sub count_value {
Subroutine count_value: called CC=5 SCAR=16.7
Pod undocumented
103 19 my ($count)=@_;
104 19 ▶ return -1 unless defined($count);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 1 18 19
105 18 ▶ die "Invalid Hyperdrive affected row count\n" if ref($count)||($count!~/\A[0-9]+\z/);
Branch: 1 branch
BranchTrueFalseTotal
if ref $count or not $count =~ /\A[0-9]+\z/u 0 18 18
Condition33%1/3
ref $count or not $count =~ /\A[0-9]+\z/u
l!l&&r!l&&!r
0018
MC/DC0%0/2
ref $count or not $count =~ /\A[0-9]+\z/u
ref $count not $count =~ /\A[0-9]+\z/u
Truth table33%1/3
A or not(B)
A= ref $countB= not $count =~ /\A[0-9]+\z/u
ABresult
1X1
011
000
106 18 ▶ return $count ? 0+$count : '0E0';
Branch: 1 branch
BranchTrueFalseTotal
$count ? : 13 5 18
107 }
108  
109  
110 24 ▶sub fetchrow_arrayref {
Subroutine fetchrow_arrayref: called CC=4 SCAR=13.9
Pod undocumented
111 24 my ($self)=@_;
112 24 ▶ die "Hyperdrive fetchrow_arrayref takes no arguments\n" unless @_==1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 24 24
113 24 $self->available();
114 20 ▶ return undef unless $self->{'result'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"result"} 4 16 20
115 16 my $row_ar=$self->{'result'}{'rows'}[$self->{'position'}];
116 16 ▶ return undef unless defined($row_ar);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row_ar 7 9 16
117 9 $self->{'position'}++;
118 9 return [@{$row_ar}];
119 }
120  
121  
122 10 ▶sub fetchrow_hashref {
Subroutine fetchrow_hashref: called CC=4 SCAR=13.9
Pod undocumented
123 10 my ($self)=@_;
124 10 ▶ die "Hyperdrive fetchrow_hashref takes no arguments\n" unless @_==1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 10 10
125 10 my $row_ar=$self->fetchrow_arrayref();
126 10 ▶ return undef unless defined($row_ar);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row_ar 4 6 10
127 6 my %row;
128 6 my $columns_ar=$self->{'result'}{'columns'};
129 6 foreach my $index (0..$#{$columns_ar}) { $row{$columns_ar->[$index]{'name'}}=$row_ar->[$index] }
130 6 return \%row;
131 }
132  
133  
134 6 ▶sub fetchall_arrayref {
Subroutine fetchall_arrayref: called CC=7 SCAR=19.5
Pod undocumented
135 6 my ($self, $slice_hr)=@_;
136 6 $self->available();
137 die "Hyperdrive fetchall_arrayref accepts only undef or an empty hash\n"
138 6 ▶ if (@_>2)||(defined($slice_hr)&&((ref($slice_hr) ne 'HASH')||keys(%{$slice_hr})));
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 or defined $slice_hr and ref $slice_hr ne "HASH" || keys %{$slice_hr;} 1 5 6
Condition77%7/9
@_ > 2 or defined $slice_hr and ref $slice_hr ne "HASH" || keys %{$slice_hr;}
l!l&&r!l&&!r
015
defined $slice_hr and ref $slice_hr ne "HASH" || keys %{$slice_hr;}
!ll&&!rl&&r
321
ref $slice_hr ne "HASH" or keys %{$slice_hr;}
l!l&&r!l&&!r
102
MC/DC50%2/4
@_ > 2 or defined $slice_hr and ref $slice_hr ne "HASH" || keys %{$slice_hr;}
@_ > 2 defined $slice_hr ref $slice_hr ne "HASH" keys %{$slice_hr;}
Truth table60%3/5
A or B and C or D
A= @_ > 2B= defined $slice_hrC= ref $slice_hr ne "HASH"D= keys %{$slice_hr;}
ABCDresult
1XXX1
011X1
01011
00XX0
01000
139 5 my @rows;
140 5 ▶ while (defined(my $row_ref=defined($slice_hr) ? $self->fetchrow_hashref() : $self->fetchrow_arrayref())) { push(@rows, $row_ref) }
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $row_ref = defined $slice_hr ? $self->fetchrow_hashref : $self->fetchrow_arrayref)) 4 5 9
defined $slice_hr ? : 6 3 9
141 5 return \@rows;
142 }
143  
144  
145 2 ▶sub columns {
Subroutine columns: called CC=3 SCAR=11.0
Pod undocumented
146 2 my ($self)=@_;
147 2 ▶ die "Hyperdrive columns takes no arguments\n" unless @_==1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 2 2
148 2 $self->available();
149 2 ▶ return [] unless $self->{'result'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"result"} 1 1 2
150 1 return [map { { %{$_} } } @{$self->{'result'}{'columns'}}];
151 }
152  
153  
154 1 ▶sub command {
Subroutine command: called CC=3 SCAR=11.4
Pod undocumented
155 1 my ($self)=@_;
156 1 ▶ die "Hyperdrive command takes no arguments\n" unless @_==1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 1 1
157 1 $self->available();
158 1 ▶ return $self->{'result'} ? $self->{'result'}{'command'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'result'} ? : 1 0 1
159 }
160  
161  
162 3 ▶sub insert_id {
Subroutine insert_id: called CC=3 SCAR=11.0
Pod undocumented
163 3 my ($self)=@_;
164 3 ▶ die "Hyperdrive insert_id takes no arguments\n" unless @_==1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 3 3
165 3 $self->available();
166 3 ▶ return $self->{'result'} ? $self->{'result'}{'insert_id'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'result'} ? : 2 1 3
167 }
168  
169  
170 1 ▶sub affected_rows {
Subroutine affected_rows: called CC=3 SCAR=11.4
Pod undocumented
171 1 my ($self)=@_;
172 1 ▶ die "Hyperdrive affected_rows takes no arguments\n" unless @_==1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 1 1
173 1 $self->available();
174 1 ▶ return $self->{'result'} ? $self->{'result'}{'affected_rows'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'result'} ? : 1 0 1
175 }
176  
177  
178 1 ▶sub warning_count {
Subroutine warning_count: called CC=3 SCAR=11.4
Pod undocumented
179 1 my ($self)=@_;
180 1 ▶ die "Hyperdrive warning_count takes no arguments\n" unless @_==1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 1 1
181 1 $self->available();
182 1 ▶ return $self->{'result'} ? $self->{'result'}{'warning_count'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'result'} ? : 1 0 1
183 }
184  
185  
186 2 ▶sub finish {
Subroutine finish: called CC=2 SCAR=7.0
Pod undocumented
187 2 my ($self)=@_;
188 2 ▶ die "Hyperdrive finish takes no arguments\n" unless @_==1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 2 2
189 2 $self->available();
190 2 delete($self->{'result'});
191 2 $self->{'position'}=0;
192 2 return 1;
193 }
194  
195 1 1;
« blib/lib/WebDyne/Cloudflare/R2.pm ↑ Summary lib/WebDyne/Cloudflare/DurableObject/Bytes.pm »