blib/lib/Zuzu/Module/DB.pm

filter: Statementstmt 16.3% 47 / 287 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 54 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 11.2% 7 / 62 Podpod 100% 1 / 1 totaltotal 10.9% 55 / 504 CC 64 SCAR 43.6
CC
64
Cov
11%
_coerce_value
420.0
_connection_options
72.0
_normalize_path
30.0
CRAP 78.3
« blib/lib/Zuzu/AST/Visitor/TypeCheckHints.pm ↑ Summary blib/lib/Zuzu/AST/Visitor/LexicalEnvHints.pm »
1 package Zuzu::Module::DB;
2  
3 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 our $VERSION = '0.007002';
6  
7 2 ▶use DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Zuzu::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Zuzu::Value::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Zuzu::Util::NativeHelpers qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 native_class
14 native_function
15 native_object
16 perl_to_zuzu
17 zuzu_bool
18 zuzu_to_perl
19 2 );
20  
21 0 ▶sub _normalize_path {
Subroutine _normalize_path: not called CC=5 SCAR=34.0
22 0 my ( $value ) = @_;
23  
24 0 ▶ return '' if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
25  
26 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->isa("Zuzu::Value::Object") and exists $value->slots->{"_path_tiny"}) 0 0 0
Condition0%0/6
builtin::blessed($value) and $value->isa("Zuzu::Value::Object") and exists $value->slots->{"_path_tiny"}
!ll&&!rl&&r
000
builtin::blessed($value) and $value->isa("Zuzu::Value::Object")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Object") and exists $value->slots->{"_path_tiny"}
builtin::blessed($value) $value->isa("Zuzu::Value::Object") exists $value->slots->{"_path_tiny"}
Truth table0%0/4
A and B and C
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Object")C= exists $value->slots->{"_path_tiny"}
ABCresult
0XX0
10X0
1100
1111
27 blessed($value)
28 and $value->isa('Zuzu::Value::Object')
29 and exists $value->slots->{_path_tiny}
30 ) {
31 0 return $value->slots->{_path_tiny}->stringify;
32 }
33  
34 0 return "$value";
35 }
36  
37 0 ▶sub _connection_options {
Subroutine _connection_options: not called CC=8 SCAR=42.8
38 0 my ( $settings ) = @_;
39  
40 0 my $opts = {};
41 0 ▶ if ( defined $settings ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $settings) 0 0 0
42 0 $opts = zuzu_to_perl( $settings );
43 0 ▶ $opts = {} if ref($opts) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts ne "HASH" 0 0 0
44 }
45  
46 my $dbi_opts = {
47 RaiseError => exists $opts->{raise_error}
48 ? zuzu_bool( $opts->{raise_error}, 1 )
49 : 1,
50 PrintError => exists $opts->{print_error}
51 ? zuzu_bool( $opts->{print_error}, 0 )
52 : 0,
53 AutoCommit => exists $opts->{auto_commit}
54 0 ▶ ? zuzu_bool( $opts->{auto_commit}, 1 )
Branch: 3 branches
BranchTrueFalseTotal
exists $opts->{'raise_error'} ? : 0 0 0
exists $opts->{'print_error'} ? : 0 0 0
exists $opts->{'auto_commit'} ? : 0 0 0
55 : 1,
56 };
57  
58 0 ▶ if ( exists $opts->{sqlite_unicode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"sqlite_unicode"}) 0 0 0
59 $dbi_opts->{sqlite_unicode}
60 0 = zuzu_bool( $opts->{sqlite_unicode}, 1 );
61 }
62  
63 return {
64 dbi => $dbi_opts,
65 isolation_level => exists $opts->{isolation_level}
66 0 ▶ ? lc( "$opts->{isolation_level}" )
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'isolation_level'} ? : 0 0 0
67 : undef,
68 };
69 }
70  
71 0 ▶sub _connect {
Subroutine _connect: not called CC=1 SCAR=6.9
72 0 my ( $dsn, $settings ) = @_;
73 0 my $connect_options = _connection_options( $settings );
74  
75 return _dbi_call(
76 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 return DBI->connect(
78 $dsn,
79 undef,
80 undef,
81 $connect_options->{dbi},
82 0 );
83 },
84 0 "connect failed for DSN '$dsn'",
85 );
86 }
87  
88 0 ▶sub _dbi_call {
Subroutine _dbi_call: not called CC=4 SCAR=30.0
89 0 my ( $code, $prefix ) = @_;
90  
91 0 my $result;
92 eval {
93 0 $result = $code->();
94 0 1;
95 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $result = &$code(); 1 } } or do { my $err = $@; $err = "$err"; chomp $err; my $message = $err; if (defined $prefix and $prefix ne "") { $message = "${prefix}: $err"; } ; die "Zuzu::Error"->new_runtime("message", $message, "file", "<std/db>", "line", 0) } 0 0 0
96 0 my $err = $@;
97 0 $err = "$err";
98 0 chomp $err;
99 0 my $message = $err;
100 0 ▶ if ( defined $prefix and $prefix ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix and $prefix ne "") 0 0 0
Condition0%0/3
defined $prefix and $prefix ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $prefix and $prefix ne ""
defined $prefix $prefix ne ""
Truth table0%0/3
A and B
A= defined $prefixB= $prefix ne ""
ABresult
0X0
100
111
101 0 $message = "$prefix: $err";
102 }
103 0 die Zuzu::Error->new_runtime(
104 message => $message,
105 file => '<std/db>',
106 line => 0,
107 );
108 };
109  
110 0 return $result;
111 }
112  
113 0 ▶sub _track_native_object {
Subroutine _track_native_object: not called CC=3 SCAR=24.8
114 0 my ( $runtime, $object ) = @_;
115  
116 0 ▶ return $object
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($object) and $object->isa("Zuzu::Value::Object") 0 0 0
Condition0%0/3
builtin::blessed($object) and $object->isa("Zuzu::Value::Object")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($object) and $object->isa("Zuzu::Value::Object")
builtin::blessed($object) $object->isa("Zuzu::Value::Object")
Truth table0%0/3
A and B
A= builtin::blessed($object)B= $object->isa("Zuzu::Value::Object")
ABresult
0X0
100
111
117 if !blessed($object) or !$object->isa('Zuzu::Value::Object');
118  
119 0 push @{ $runtime->{_demolish_objects} }, $object;
120 0 weaken( $runtime->{_demolish_objects}[-1] );
121  
122 0 return $object;
123 }
124  
125 0 ▶sub _new_dbh_object {
Subroutine _new_dbh_object: not called CC=1 SCAR=6.9
126 0 my ( $runtime, $dbh_class, $dbh, $settings ) = @_;
127  
128 my $object = native_object(
129 class => $dbh_class,
130 slots => {
131 _dbh => $dbh,
132 _isolation_level => $settings->{isolation_level},
133 },
134 0 const => {
135 _dbh => 1,
136 },
137 );
138 $object->demolish_hook(
139 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
140 0 my ( $target ) = @_;
141 0 my $dbh = delete $target->slots->{_dbh};
142 0 delete $target->slots->{_isolation_level};
143 0 ▶ return if !defined $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dbh 0 0 0
144 0 local $@;
145 0 eval {
146 0 ▶ $dbh->disconnect if $dbh->{Active};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"Active"} 0 0 0
147 0 1;
148 };
149 0 return;
150 },
151 0 );
152  
153 0 return _track_native_object( $runtime, $object );
154 }
155  
156 0 ▶sub _new_sth_object {
Subroutine _new_sth_object: not called CC=1 SCAR=6.9
157 0 my ( $runtime, $sth_class, $sth ) = @_;
158  
159 0 my $object = native_object(
160 class => $sth_class,
161 slots => {
162 _sth => $sth,
163 },
164 const => {
165 _sth => 1,
166 },
167 );
168 $object->demolish_hook(
169 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
170 0 my ( $target ) = @_;
171 0 my $sth = delete $target->slots->{_sth};
172 0 ▶ return if !defined $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sth 0 0 0
173 0 local $@;
174 0 eval {
175 0 ▶ $sth->finish if $sth->{Active};
Branch: 1 branch
BranchTrueFalseTotal
if $sth->{"Active"} 0 0 0
176 0 1;
177 };
178 0 return;
179 },
180 0 );
181  
182 0 return _track_native_object( $runtime, $object );
183 }
184  
185 0 ▶sub _columns_metadata {
Subroutine _columns_metadata: not called CC=4 SCAR=30.0
186 0 my ( $sth ) = @_;
187 0 ▶ my $names = eval { $sth->{NAME} } // [];
Condition0%0/2
eval { do { $sth->{"NAME"} } } // []
l!l
00
MC/DC0%0/1
eval { do { $sth->{"NAME"} } } // []
eval { do { $sth->{"NAME"} } }
Truth table0%0/2
A
A= eval { do { $sth->{"NAME"} } }
Aresult
11
00
188 0 ▶ my $types = eval { $sth->{TYPE} } // [];
Condition0%0/2
eval { do { $sth->{"TYPE"} } } // []
l!l
00
MC/DC0%0/1
eval { do { $sth->{"TYPE"} } } // []
eval { do { $sth->{"TYPE"} } }
Truth table0%0/2
A
A= eval { do { $sth->{"TYPE"} } }
Aresult
11
00
189  
190 0 my @columns;
191 0 for my $index ( 0 .. $#{ $names } ) {
192 0 push @columns, {
193 name => $names->[$index],
194 type_code => $types->[$index],
195 type_name => undef,
196 };
197 }
198  
199 0 return \@columns;
200 }
201  
202 0 ▶sub _coerce_value {
Subroutine _coerce_value: not called CC=20 SCAR=60.4
203 0 my ( $value, $meta ) = @_;
204  
205 0 ▶ return undef if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
206 0 ▶ return $value if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 0 0
207  
208 0 ▶ my $type_name = lc( $meta->{type_name} // '' );
Condition0%0/2
$meta->{"type_name"} // ""
l!l
00
MC/DC0%0/1
$meta->{"type_name"} // ""
$meta->{"type_name"}
Truth table0%0/2
A
A= $meta->{"type_name"}
Aresult
11
00
209 my $type_code = defined $meta->{type_code}
210 ? 0 + $meta->{type_code}
211 0 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $meta->{'type_code'} ? : 0 0 0
212  
213 0 ▶ if ( $type_name =~ /\b(bool|boolean)\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_name =~ /\b(bool|boolean)\b/) 0 0 0
214 0 ▶ return Zuzu::Value::Boolean->new(
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
215 value => $value ? 1 : 0
216 );
217 }
218  
219 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($type_name =~ /\b(int|integer|real|float|double|decimal|numeric)\b/ or defined $type_code and $type_code == &DBI::SQL_INTEGER() || $type_code == &DBI::SQL_SMALLINT() || $type_code == &DBI::SQL_TINYINT() || $type_code == &DBI::SQL_BIGINT() || $type_code == &DBI::SQL_DECIMAL() || $type_code == &DBI::SQL_NUMERIC() || $type_code == &DBI::SQL_REAL() || $type_code == &DBI::SQL_FLOAT() || $type_code == &DBI::SQL_DOUBLE()) 0 0 0
Condition0%0/30
$type_name =~ /\b(int|integer|real|float|double|decimal|numeric)\b/ or defined $type_code and $type_code == &DBI::SQL_INTEGER() || $type_code == &DBI::SQL_SMALLINT() || $type_code == &DBI::SQL_TINYINT() || $type_code == &DBI::SQL_BIGINT() || $type_code == &DBI::SQL_DECIMAL() || $type_code == &DBI::SQL_NUMERIC() || $type_code == &DBI::SQL_REAL() || $type_code == &DBI::SQL_FLOAT() || $type_code == &DBI::SQL_DOUBLE()
l!l&&r!l&&!r
000
defined $type_code and $type_code == &DBI::SQL_INTEGER() || $type_code == &DBI::SQL_SMALLINT() || $type_code == &DBI::SQL_TINYINT() || $type_code == &DBI::SQL_BIGINT() || $type_code == &DBI::SQL_DECIMAL() || $type_code == &DBI::SQL_NUMERIC() || $type_code == &DBI::SQL_REAL() || $type_code == &DBI::SQL_FLOAT() || $type_code == &DBI::SQL_DOUBLE()
!ll&&!rl&&r
000
$type_code == &DBI::SQL_INTEGER() or $type_code == &DBI::SQL_SMALLINT() or $type_code == &DBI::SQL_TINYINT() or $type_code == &DBI::SQL_BIGINT() or $type_code == &DBI::SQL_DECIMAL() or $type_code == &DBI::SQL_NUMERIC() or $type_code == &DBI::SQL_REAL() or $type_code == &DBI::SQL_FLOAT() or $type_code == &DBI::SQL_DOUBLE()
l!l&&r!l&&!r
000
$type_code == &DBI::SQL_INTEGER() or $type_code == &DBI::SQL_SMALLINT() or $type_code == &DBI::SQL_TINYINT() or $type_code == &DBI::SQL_BIGINT() or $type_code == &DBI::SQL_DECIMAL() or $type_code == &DBI::SQL_NUMERIC() or $type_code == &DBI::SQL_REAL() or $type_code == &DBI::SQL_FLOAT()
l!l&&r!l&&!r
000
$type_code == &DBI::SQL_INTEGER() or $type_code == &DBI::SQL_SMALLINT() or $type_code == &DBI::SQL_TINYINT() or $type_code == &DBI::SQL_BIGINT() or $type_code == &DBI::SQL_DECIMAL() or $type_code == &DBI::SQL_NUMERIC() or $type_code == &DBI::SQL_REAL()
l!l&&r!l&&!r
000
$type_code == &DBI::SQL_INTEGER() or $type_code == &DBI::SQL_SMALLINT() or $type_code == &DBI::SQL_TINYINT() or $type_code == &DBI::SQL_BIGINT() or $type_code == &DBI::SQL_DECIMAL() or $type_code == &DBI::SQL_NUMERIC()
l!l&&r!l&&!r
000
$type_code == &DBI::SQL_INTEGER() or $type_code == &DBI::SQL_SMALLINT() or $type_code == &DBI::SQL_TINYINT() or $type_code == &DBI::SQL_BIGINT() or $type_code == &DBI::SQL_DECIMAL()
l!l&&r!l&&!r
000
$type_code == &DBI::SQL_INTEGER() or $type_code == &DBI::SQL_SMALLINT() or $type_code == &DBI::SQL_TINYINT() or $type_code == &DBI::SQL_BIGINT()
l!l&&r!l&&!r
000
$type_code == &DBI::SQL_INTEGER() or $type_code == &DBI::SQL_SMALLINT() or $type_code == &DBI::SQL_TINYINT()
l!l&&r!l&&!r
000
$type_code == &DBI::SQL_INTEGER() or $type_code == &DBI::SQL_SMALLINT()
l!l&&r!l&&!r
000
MC/DC0%0/11
$type_name =~ /\b(int|integer|real|float|double|decimal|numeric)\b/ or defined $type_code and $type_code == &DBI::SQL_INTEGER() || $type_code == &DBI::SQL_SMALLINT() || $type_code == &DBI::SQL_TINYINT() || $type_code == &DBI::SQL_BIGINT() || $type_code == &DBI::SQL_DECIMAL() || $type_code == &DBI::SQL_NUMERIC() || $type_code == &DBI::SQL_REAL() || $type_code == &DBI::SQL_FLOAT() || $type_code == &DBI::SQL_DOUBLE()
$type_name =~ /\b(int|integer|real|float|double|decimal|numeric)\b/ defined $type_code $type_code == &DBI::SQL_INTEGER() $type_code == &DBI::SQL_SMALLINT() $type_code == &DBI::SQL_TINYINT() $type_code == &DBI::SQL_BIGINT() $type_code == &DBI::SQL_DECIMAL() $type_code == &DBI::SQL_NUMERIC() $type_code == &DBI::SQL_REAL() $type_code == &DBI::SQL_FLOAT() $type_code == &DBI::SQL_DOUBLE()
Truth table0%0/12
A or B and C or D or E or F or G or H or I or J or K
A= $type_name =~ /\b(int|integer|real|float|double|decimal|numeric)\b/B= defined $type_codeC= $type_code == &DBI::SQL_INTEGER()D= $type_code == &DBI::SQL_SMALLINT()E= $type_code == &DBI::SQL_TINYINT()F= $type_code == &DBI::SQL_BIGINT()G= $type_code == &DBI::SQL_DECIMAL()H= $type_code == &DBI::SQL_NUMERIC()I= $type_code == &DBI::SQL_REAL()J= $type_code == &DBI::SQL_FLOAT()K= $type_code == &DBI::SQL_DOUBLE()
ABCDEFGHIJKresult
1XXXXXXXXXX1
011XXXXXXXX1
0101XXXXXXX1
01001XXXXXX1
010001XXXXX1
0100001XXXX1
01000001XXX1
010000001XX1
0100000001X1
010000000011
00XXXXXXXXX0
010000000000
220 $type_name =~ /\b(int|integer|real|float|double|decimal|numeric)\b/
221 or (
222 defined $type_code
223 and (
224 $type_code == DBI::SQL_INTEGER()
225 or $type_code == DBI::SQL_SMALLINT()
226 or $type_code == DBI::SQL_TINYINT()
227 or $type_code == DBI::SQL_BIGINT()
228 or $type_code == DBI::SQL_DECIMAL()
229 or $type_code == DBI::SQL_NUMERIC()
230 or $type_code == DBI::SQL_REAL()
231 or $type_code == DBI::SQL_FLOAT()
232 or $type_code == DBI::SQL_DOUBLE()
233 )
234 )
235 ) {
236 0 ▶ if ( "$value" =~ /\A-?\d+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$value" =~ /\A-?\d+\z/) 0 0 0
237 0 return int( $value );
238 }
239 0 ▶ if ( "$value" =~ /\A-?(?:\d+\.\d*|\d*\.\d+)(?:[eE]-?\d+)?\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$value" =~ /\A-?(?:\d+\.\d*|\d*\.\d+)(?:[eE]-?\d+)?\z/) 0 0 0
240 0 return 0.0 + $value;
241 }
242 }
243  
244 0 return $value;
245 }
246  
247 0 ▶sub _coerce_row_array {
Subroutine _coerce_row_array: not called CC=3 SCAR=24.8
248 0 my ( $row, $columns ) = @_;
249 0 my @out;
250 0 for my $index ( 0 .. $#{ $row } ) {
251 0 ▶ push @out, _coerce_value( $row->[$index], $columns->[$index] // {} );
Condition0%0/2
$columns->[$index] // {}
l!l
00
MC/DC0%0/1
$columns->[$index] // {}
$columns->[$index]
Truth table0%0/2
A
A= $columns->[$index]
Aresult
11
00
252 }
253 0 return \@out;
254 }
255  
256 0 ▶sub _coerce_row_dict {
Subroutine _coerce_row_dict: not called CC=3 SCAR=24.8
257 0 my ( $row, $columns ) = @_;
258 0 my %column_by_name = map { $_->{name} => $_ } @{ $columns };
259 0 my %out;
260 0 for my $name ( CORE::keys %{ $row } ) {
261 0 ▶ $out{$name} = _coerce_value( $row->{$name}, $column_by_name{$name} // {} );
Condition0%0/2
$column_by_name{$name} // {}
l!l
00
MC/DC0%0/1
$column_by_name{$name} // {}
$column_by_name{$name}
Truth table0%0/2
A
A= $column_by_name{$name}
Aresult
11
00
262 }
263 0 return \%out;
264 }
265  
266 2 ▶sub IMPORT {
Subroutine IMPORT: called CC=1 SCAR=5.0
Pod documented
267 2 my ( $class, $runtime ) = @_;
268  
269 2 my $db_class = native_class(
270 name => 'DB',
271 );
272 2 my $dbh_class = native_class(
273 name => 'DatabaseHandle',
274 );
275 2 my $sth_class = native_class(
276 name => 'StatementHandle',
277 );
278  
279 $db_class->static_methods->{connect} = native_function(
280 name => 'connect',
281 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
282 0 my ( $self, $dsn, $settings ) = @_;
283 0 my $connect_settings = _connection_options( $settings );
284 0 ▶ my $dbh = _connect( defined $dsn ? "$dsn" : '', $settings );
Branch: 1 branch
BranchTrueFalseTotal
defined $dsn ? : 0 0 0
285 0 return _new_dbh_object(
286 $runtime,
287 $dbh_class,
288 $dbh,
289 $connect_settings,
290 );
291 },
292 2 );
293  
294 $db_class->static_methods->{temp} = native_function(
295 name => 'temp',
296 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
297 0 my ( $self, $settings ) = @_;
298 0 my $connect_settings = _connection_options( $settings );
299 0 my $dbh = _connect( 'dbi:SQLite:dbname=:memory:', $settings );
300 0 return _new_dbh_object(
301 $runtime,
302 $dbh_class,
303 $dbh,
304 $connect_settings,
305 );
306 },
307 2 );
308  
309 $db_class->static_methods->{open} = native_function(
310 name => 'open',
311 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
312 0 my ( $self, $path, $settings ) = @_;
313 0 $runtime->assert_capability( 'fs', "DB.open is denied by runtime policy" );
314 0 my $string_path = _normalize_path( $path );
315 0 my $dsn = "dbi:SQLite:dbname=$string_path";
316 0 my $connect_settings = _connection_options( $settings );
317 0 my $dbh = _connect( $dsn, $settings );
318 0 return _new_dbh_object(
319 $runtime,
320 $dbh_class,
321 $dbh,
322 $connect_settings,
323 );
324 },
325 2 );
326  
327 $dbh_class->methods->{prepare} = native_function(
328 name => 'prepare',
329 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
330 0 my ( $self, $sql ) = @_;
331 0 ▶ my $stmt = defined $sql ? "$sql" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $sql ? : 0 0 0
332 my $sth = _dbi_call(
333 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
334 0 return $self->slots->{_dbh}->prepare( $stmt );
335 },
336 0 'prepare failed',
337 );
338 0 return _new_sth_object( $runtime, $sth_class, $sth );
339 },
340 2 );
341  
342 $dbh_class->methods->{quote} = native_function(
343 name => 'quote',
344 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
345 0 my ( $self, $value ) = @_;
346 return _dbi_call(
347 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
348 0 ▶ return $self->slots->{_dbh}->quote( defined $value ? "$value" : undef );
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
349 },
350 0 'quote failed',
351 );
352 },
353 2 );
354  
355 $dbh_class->methods->{begin} = native_function(
356 name => 'begin',
357 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
358 0 my ( $self ) = @_;
359 0 ▶ my $mode = $self->slots->{_isolation_level} // '';
Condition0%0/2
$self->slots->{"_isolation_level"} // ""
l!l
00
MC/DC0%0/1
$self->slots->{"_isolation_level"} // ""
$self->slots->{"_isolation_level"}
Truth table0%0/2
A
A= $self->slots->{"_isolation_level"}
Aresult
11
00
360 0 ▶ if ( $mode =~ /\A(?:immediate|exclusive|deferred)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode =~ /\A(?:immediate|exclusive|deferred)\z/) { } 0 0 0
361 _dbi_call(
362 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
363 $self->slots->{_dbh}->do(
364 0 "begin $mode transaction"
365 );
366 0 return 1;
367 },
368 0 'begin failed',
369 );
370 }
371 else {
372 _dbi_call(
373 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
374 0 $self->slots->{_dbh}->begin_work;
375 0 return 1;
376 },
377 0 'begin failed',
378 );
379 }
380 0 return $self;
381 },
382 2 );
383  
384 $dbh_class->methods->{commit} = native_function(
385 name => 'commit',
386 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
387 0 my ( $self ) = @_;
388 _dbi_call(
389 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
390 0 $self->slots->{_dbh}->commit;
391 0 return 1;
392 },
393 0 'commit failed',
394 );
395 0 return $self;
396 },
397 2 );
398  
399 $dbh_class->methods->{rollback} = native_function(
400 name => 'rollback',
401 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
402 0 my ( $self ) = @_;
403 _dbi_call(
404 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
405 0 $self->slots->{_dbh}->rollback;
406 0 return 1;
407 },
408 0 'rollback failed',
409 );
410 0 return $self;
411 },
412 2 );
413  
414 $dbh_class->methods->{execute_batch} = native_function(
415 name => 'execute_batch',
416 0 ▶ native => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
417 0 my ( $self, $sql, $rows ) = @_;
418 0 ▶ my $stmt = defined $sql ? "$sql" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $sql ? : 0 0 0
419 0 my $bind_rows = zuzu_to_perl( $rows );
420 0 ▶ $bind_rows = [] if ref($bind_rows) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $bind_rows ne "ARRAY" 0 0 0
421 _dbi_call(
422 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
423 0 my $sth = $self->slots->{_dbh}->prepare( $stmt );
424 0 for my $bind ( @{ $bind_rows } ) {
425 my @args = ref($bind) eq 'ARRAY'
426 0 ▶ ? @{ $bind }
Branch: 1 branch
BranchTrueFalseTotal
ref $bind eq 'ARRAY' ? : 0 0 0
427 : ( $bind );
428 0 $sth->execute( @args );
429 }
430 0 return 1;
431 },
432 0 'execute_batch failed',
433 );
434 0 return $self;
435 },
436 2 );
437  
438 $sth_class->methods->{execute} = native_function(
439 name => 'execute',
440 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
441 0 my ( $self, @bind ) = @_;
442 _dbi_call(
443 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
444 0 $self->slots->{_sth}->execute( @bind );
445 0 return 1;
446 },
447 0 'execute failed',
448 );
449 0 return $self;
450 },
451 2 );
452  
453 $sth_class->methods->{next_array} = native_function(
454 name => 'next_array',
455 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
456 0 my ( $self ) = @_;
457 my $row = _dbi_call(
458 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
459 0 return $self->slots->{_sth}->fetchrow_arrayref;
460 },
461 0 'fetch row failed',
462 );
463 0 ▶ return undef if not defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
464 0 return perl_to_zuzu( [ @{ $row } ] );
465 },
466 2 );
467  
468 $sth_class->methods->{next_dict} = native_function(
469 name => 'next_dict',
470 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
471 0 my ( $self ) = @_;
472 my $row = _dbi_call(
473 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
474 0 return $self->slots->{_sth}->fetchrow_hashref;
475 },
476 0 'fetch row failed',
477 );
478 0 ▶ return undef if not defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
479 0 return perl_to_zuzu( { %{ $row } } );
480 },
481 2 );
482  
483 $sth_class->methods->{all_array} = native_function(
484 name => 'all_array',
485 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
486 0 my ( $self ) = @_;
487 my $rows = _dbi_call(
488 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
489 0 return $self->slots->{_sth}->fetchall_arrayref( [] );
490 },
491 0 'fetch all rows failed',
492 );
493 0 return perl_to_zuzu( $rows );
494 },
495 2 );
496  
497 $sth_class->methods->{all_dict} = native_function(
498 name => 'all_dict',
499 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
500 0 my ( $self ) = @_;
501 my $rows = _dbi_call(
502 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
503 0 return $self->slots->{_sth}->fetchall_arrayref( {} );
504 },
505 0 'fetch all rows failed',
506 );
507 0 return perl_to_zuzu( $rows );
508 },
509 2 );
510  
511 $sth_class->methods->{execute_batch} = native_function(
512 name => 'execute_batch',
513 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
514 0 my ( $self, $rows ) = @_;
515 0 my $bind_rows = zuzu_to_perl( $rows );
516 0 ▶ $bind_rows = [] if ref($bind_rows) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $bind_rows ne "ARRAY" 0 0 0
517 _dbi_call(
518 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
519 0 for my $bind ( @{ $bind_rows } ) {
520 my @args = ref($bind) eq 'ARRAY'
521 0 ▶ ? @{ $bind }
Branch: 1 branch
BranchTrueFalseTotal
ref $bind eq 'ARRAY' ? : 0 0 0
522 : ( $bind );
523 0 $self->slots->{_sth}->execute( @args );
524 }
525 0 return 1;
526 },
527 0 'execute_batch failed',
528 );
529 0 return $self;
530 },
531 2 );
532  
533 $sth_class->methods->{column_names} = native_function(
534 name => 'column_names',
535 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
536 0 my ( $self ) = @_;
537 0 my $meta = _columns_metadata( $self->slots->{_sth} );
538 return perl_to_zuzu(
539 0 [ map { $_->{name} } @{ $meta } ]
540 );
541 },
542 2 );
543  
544 $sth_class->methods->{column_types} = native_function(
545 name => 'column_types',
546 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
547 0 my ( $self ) = @_;
548 0 my $meta = _columns_metadata( $self->slots->{_sth} );
549 return perl_to_zuzu(
550 [
551 map {
552 {
553 code => $_->{type_code},
554 name => $_->{type_name},
555 }
556 0 } @{ $meta }
557 ]
558 );
559 },
560 2 );
561  
562 $sth_class->methods->{next_typed_array} = native_function(
563 name => 'next_typed_array',
564 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
565 0 my ( $self ) = @_;
566 my $row = _dbi_call(
567 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
568 0 return $self->slots->{_sth}->fetchrow_arrayref;
569 },
570 0 'fetch row failed',
571 );
572 0 ▶ return undef if not defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
573 0 my $meta = _columns_metadata( $self->slots->{_sth} );
574 0 return perl_to_zuzu( _coerce_row_array( $row, $meta ) );
575 },
576 2 );
577  
578 $sth_class->methods->{next_typed_dict} = native_function(
579 name => 'next_typed_dict',
580 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
581 0 my ( $self ) = @_;
582 my $row = _dbi_call(
583 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
584 0 return $self->slots->{_sth}->fetchrow_hashref;
585 },
586 0 'fetch row failed',
587 );
588 0 ▶ return undef if not defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 0 0
589 0 my $meta = _columns_metadata( $self->slots->{_sth} );
590 0 return perl_to_zuzu( _coerce_row_dict( $row, $meta ) );
591 },
592 2 );
593  
594 $sth_class->methods->{all_typed_array} = native_function(
595 name => 'all_typed_array',
596 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
597 0 my ( $self ) = @_;
598 my $rows = _dbi_call(
599 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
600 0 return $self->slots->{_sth}->fetchall_arrayref( [] );
601 },
602 0 'fetch all rows failed',
603 );
604 0 my $meta = _columns_metadata( $self->slots->{_sth} );
605 my @coerced = map {
606 0 _coerce_row_array( $_, $meta )
607 0 } @{ $rows };
608 0 return perl_to_zuzu( \@coerced );
609 },
610 2 );
611  
612 $sth_class->methods->{all_typed_dict} = native_function(
613 name => 'all_typed_dict',
614 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
615 0 my ( $self ) = @_;
616 my $rows = _dbi_call(
617 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
618 0 return $self->slots->{_sth}->fetchall_arrayref( {} );
619 },
620 0 'fetch all rows failed',
621 );
622 0 my $meta = _columns_metadata( $self->slots->{_sth} );
623 my @coerced = map {
624 0 _coerce_row_dict( $_, $meta )
625 0 } @{ $rows };
626 0 return perl_to_zuzu( \@coerced );
627 },
628 2 );
629  
630 $sth_class->methods->{to_Iterator} = native_function(
631 name => 'to_Iterator',
632 0 ▶ native => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
633 0 my ( $self ) = @_;
634 0 my $sth = $self->slots->{_sth};
635 0 my $meta = _columns_metadata( $sth );
636 return native_function(
637 name => 'iterator',
638 0 ▶ native => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
639 my $row = _dbi_call(
640 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
641 0 return $sth->fetchrow_hashref;
642 },
643 0 'fetch row failed',
644 );
645 0 ▶ if ( not defined $row ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row) 0 0 0
646 my $exhausted = $runtime->_instantiate_builtin_object(
647 $runtime->{_builtin_classes}{ExhaustedException},
648 {
649 0 message => 'iterator exhausted',
650 file => '<std/db>',
651 line => 0,
652 },
653 );
654 0 die {
655 _zuzu_throw => 1,
656 value => $exhausted,
657 };
658 }
659 0 return perl_to_zuzu( _coerce_row_dict( $row, $meta ) );
660 },
661 0 );
662 },
663 2 );
664  
665 return {
666 2 DB => $db_class,
667 DatabaseHandle => $dbh_class,
668 StatementHandle => $sth_class,
669 };
670 }
671  
672 2 1;
673  
674 =pod
675  
676 =head1 NAME
677  
678 Zuzu::Module::DB - C<std/db> builtin module bridge.
679  
680 =head1 DESCRIPTION
681  
682 This package implements the runtime side of C<std/db>.
683 It exposes DBI-backed classes to ZuzuScript code:
684  
685 =over
686  
687 =item * C<DB>
688  
689 Static constructor helpers:
690  
691 =over
692  
693 =item * C<connect(dsn)>
694  
695 Creates a database handle from a DBI DSN.
696  
697 =item * C<temp()>
698  
699 Creates an in-memory SQLite connection.
700  
701 =item * C<open(path)>
702  
703 Opens a SQLite database by file path.
704 Accepts either a string or a C<std/io> C<Path> object.
705  
706 =back
707  
708 =item * C<DatabaseHandle>
709  
710 Provides C<prepare(sql)>, C<quote(value)>,
711 C<begin>, C<commit>, C<rollback>, and
712 C<execute_batch(sql, rows)>.
713  
714 =item * C<StatementHandle>
715  
716 Provides C<execute(...)>, C<execute_batch(rows)>,
717 C<column_names>, C<column_types>, C<next_array>,
718 C<next_dict>, C<all_array>, C<all_dict>,
719 C<next_typed_array>, C<next_typed_dict>,
720 C<all_typed_array>, C<all_typed_dict>, and
721 C<to_Iterator> (for C<for> loops yielding
722 typed C<Dict> rows).
723  
724 =back
725  
726 All DBI operations are wrapped so DBI failures become
727 runtime errors, which are catchable as exceptions
728 from ZuzuScript.
729  
730 =head1 METHODS
731  
732 =head2 IMPORT
733  
734 Returns module exports for the runtime importer.
735  
736 =head1 COPYRIGHT AND LICENCE
737  
738 B<< Zuzu::Module::DB >> is copyright Toby Inkster.
739  
740 It is free software; you may redistribute it and/or modify it under
741 the terms of either the Artistic License 1.0 or the GNU General Public
742 License version 2.
743  
744 =cut
« blib/lib/Zuzu/AST/Visitor/TypeCheckHints.pm ↑ Summary blib/lib/Zuzu/AST/Visitor/LexicalEnvHints.pm »