DBIx/Interp.pm

filter: Statementstmt 61.6% 146 / 237 Branchbran 32.8% 23 / 70 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 61.5% 32 / 52 Podpod 100% 4 / 4 totaltotal 54.7% 207 / 378 CC 43 SCAR 25.6
CC
43
Cov
54%
selectcol_arrayref_i
90.0
connect
12.0
_do_selectrow_i
12.0
CRAP 12.9
« SQL/Interp.pm ↑ Summary
1 package DBIx::Interp;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use SQL::Interp ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use base 'DBI';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 2 our $VERSION = '1.28';
12  
13 2 our %EXPORT_TAGS = (all => [qw(
14 attr dbi_interp key_field
15 sql_interp sql_interp_strict sql_type sql
16 )]);
17 2 our @EXPORT_OK = @{$EXPORT_TAGS{all}};
18  
19 2 our @CARP_NOT =
20 qw(DBIx::Interp DBIx::Interp::db DBIx::Interp::STX);
21  
22 0 ▶sub key_field {
Subroutine key_field: not called CC=1 SCAR=6.9
Pod documented
23 0 my $key = shift;
24 0 return bless \$key, "DBIx::Interp::Key";
25 }
26  
27 0 ▶sub attr {
Subroutine attr: not called CC=1 SCAR=6.9
Pod documented
28 0 return bless {@_}, "DBIx::Interp::Attr";
29 }
30  
31 0 ▶sub dbi_interp {
Subroutine dbi_interp: not called CC=1 SCAR=6.9
Pod documented
32 0 return DBIx::Interp::db::dbi_interp(@_);
33 }
34  
35 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
36 1 shift;
37 1 return DBIx::Interp::db->new(@_);
38 }
39  
40 16 ▶sub _wrap(&) {
Subroutine _wrap: called CC=4 SCAR=13.9
41 16 my $code = shift;
42 16 my $x;
43 my @x;
44 16 my $want = wantarray();
45 16 eval {
46 16 ▶ if ($want) { @x = $code->(); }
Branch: 1 branch
BranchTrueFalseTotal
if ($want) { } 4 12 16
47 12 else { $x = $code->(); }
48 };
49 0 ▶ if ($@) { croak $@; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 16 16
50 16 ▶ return $want ? @x : $x;
Branch: 1 branch
BranchTrueFalseTotal
$want ? : 4 12 16
51 }
52  
53 #####
54 package DBIx::Interp::db;
55 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ▶use base 'DBI::db';
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 2 our @CARP_NOT = @DBIx::Interp::CARP_NOT;
62  
63 2 my $priv = 'private_DBIxInterpolate';
64  
65 1 ▶sub new {
Subroutine new: called CC=4 SCAR=16.3
66 1 my $class = shift;
67 1 my $dbh;
68  
69 1 ▶ if (UNIVERSAL::isa($_[0], 'DBI::db')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'DBI::db')) { } 1 0 1
elsif (ref $_[0] eq 'ARRAY') { } 0 0 0
70 1 $dbh = shift;
71 }
72 elsif (ref $_[0] eq 'ARRAY') {
73 0 $dbh = DBI->connect(@{shift @_});
74 0 ▶ return if ! defined $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dbh 0 0 0
75 }
76 else {
77 0 croak 'DBIx::Interp::db::new() not passed database connection';
78 }
79  
80 1 my $interp = SQL::Interp->new(@_);
81 1 my $self = $dbh;
82 1 bless $self, $class;
83 1 my $private = $self->{$priv} = {};
84 1 $private->{stx} = $self->prepare_i();
85 1 $private->{interp} = $interp;
86  
87 # weaken circular references to allow garbage collection
88 1 weaken $private->{stx}->{dbx};
89  
90 1 return $self;
91 }
92  
93 #sub DESTROY {
94 # my ($self) = @_;
95 # $self->SUPER::DESTROY();
96 #}
97  
98 0 ▶sub connect {
Subroutine connect: not called CC=3 SCAR=24.8
99 0 my $class = shift;
100 0 my $self;
101 0 eval {
102 0 my $dbh = DBI->connect(@_);
103 0 ▶ return if ! $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 0 0
104 0 $self = DBIx::Interp->new($dbh); #Q: OK?
105 };
106 0 ▶ if ($@) { croak $@; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
107 0 return $self;
108 }
109  
110 # removed in 0.40:
111 #sub dbh
112  
113  
114 # new in 0.31
115 0 ▶sub stx {
Subroutine stx: not called CC=1 SCAR=6.9
116 0 my $self = shift;
117 0 return $self->{$priv}->{stx};
118 }
119  
120 # new in 0.40
121 8 ▶sub interp {
Subroutine interp: called CC=1 SCAR=0.0
122 8 my $self = shift;
123 8 return $self->{$priv}->{interp};
124 }
125  
126 8 ▶sub dbi_interp {
Subroutine dbi_interp: called CC=4 SCAR=15.4
127 8 my $key;
128 my $attr;
129 my @args = grep {
130 8 my $save = 1;
131 36 ▶ if (ref($_) eq 'DBIx::Interp::Key') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ eq 'DBIx::Interp::Key') { } 0 36 36
elsif (ref $_ eq 'DBIx::Interp::Attr') { } 0 36 36
132 0 $key = $_; $save = 0;
133 }
134 elsif (ref($_) eq 'DBIx::Interp::Attr') {
135 0 $attr = {%$_}; $save = 0;
136 }
137 36 $save;
138 } @_;
139 8 my ($sql, @bind) = sql_interp(@args);
140 8 my @params = ($sql);
141 8 ▶ push @params, $$key if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 0 8 8
142 8 push @params, $attr, @bind;
143 8 return @params;
144 }
145  
146 8 ▶sub sql_interp {
Subroutine sql_interp: called CC=2 SCAR=7.4
147 8 my (@params) = @_;
148 8 ▶ if (UNIVERSAL::isa($_[0], 'DBIx::Interp::db')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($_[0], 'DBIx::Interp::db')) { } 8 0 8
149 8 my $self = shift;
150 8 return SQL::Interp::sql_interp($self->interp(), @_);
151 }
152 else {
153 0 return SQL::Interp::sql_interp(@_);
154 }
155 }
156  
157 # based on function in DBI
158 0 ▶sub _do_selectrow_i {
Subroutine _do_selectrow_i: not called CC=3 SCAR=24.8
159 0 my ($self, $method, @list) = @_;
160  
161 #my $sth = $dbh->prepare($stmt, $attr) or return;
162 #_do_execute($sth, @bind) or return;
163 0 my $stx = $self->{$priv}->{stx};
164 0 ▶ $stx->execute_i(@list) or return;
Branch: 1 branch
BranchTrueFalseTotal
$stx->execute_i(@list) or return 0 0 0
165 0 my $sth = $stx->sth();
166 0 ▶ my $row = $sth->$method() and $sth->finish;
Branch: 1 branch
BranchTrueFalseTotal
my $row = $sth->$method and $sth->finish 0 0 0
167 0 return $row;
168 }
169  
170 # new in 0.40
171 2 ▶sub prepare_i {
Subroutine prepare_i: called CC=1 SCAR=0.0
172 2 my ($self) = @_;
173 2 return DBIx::Interp::STX->new($self);
174 }
175  
176 # new in 0.40
177 0 ▶sub do_i {
Subroutine do_i: not called CC=3 SCAR=24.8
178 0 my ($self, @list) = @_;
179 0 return _wrap {
180 # based on DBI::do
181 # my $sth = $dbh->prepare($sql, $attr) or return undef;
182 # _do_execute(@bind) or return undef;
183 0 my $stx = $self->{$priv}->{stx};
184 0 ▶ $stx->execute_i(@list) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$stx->execute_i(@list) or return undef 0 0 0
185 0 my $sth = $stx->sth();
186 0 my $rows = $sth->rows;
187 0 ▶ return ($rows == 0) ? "0E0" : $rows;
Branch: 1 branch
BranchTrueFalseTotal
$rows == 0 ? : 0 0 0
188 };
189 }
190  
191 # new in 0.40
192 0 ▶sub selectrow_array_i {
Subroutine selectrow_array_i: not called CC=3 SCAR=24.8
193 0 my ($self, @list) = @_;
194 0 my $want = wantarray;
195 0 return _wrap {
196 # based on DBI::selectrow_array
197  
198 0 ▶ my $row = $self->_do_selectrow_i('fetchrow_arrayref', @list)
Branch: 1 branch
BranchTrueFalseTotal
my $row = $self->_do_selectrow_i("fetchrow_arrayref", @list) or return 0 0 0
199 or return;
200 0 ▶ return $row->[0] unless $want;
Branch: 1 branch
BranchTrueFalseTotal
unless $want 0 0 0
201 0 return @$row;
202 };
203 }
204  
205 # new in 0.40
206 0 ▶sub selectrow_arrayref_i {
Subroutine selectrow_arrayref_i: not called CC=1 SCAR=6.9
207 0 my ($self, @list) = @_;
208 0 return _wrap {
209 # based on DBI::selectrow_arrayref
210  
211 0 return $self->_do_selectrow_i('fetchrow_arrayref', @list);
212 };
213 }
214  
215 # new in 0.40
216 0 ▶sub selectrow_hashref_i {
Subroutine selectrow_hashref_i: not called CC=1 SCAR=6.9
217 0 my ($self, @list) = @_;
218 0 return _wrap {
219 # based on DBI::selectrow_hashref
220  
221 0 return $self->_do_selectrow_i('fetchrow_hashref', @list);
222 };
223 }
224  
225 # new in 0.40
226 2 ▶sub selectall_arrayref_i {
Subroutine selectall_arrayref_i: called CC=7 SCAR=24.2
227 2 my ($self, @list) = @_;
228 2 return _wrap {
229 # improve: no need to to a full dbi_interp call here and elsewhere
230 2 my ($sql, $attr, @bind) = $self->dbi_interp(@list); # need $attr
231  
232 # based on DBI::selectall_arrayref
233 # my $sth = $dbh->prepare($sql, $attr) or return;
234 # _do_execute($sth, @bind) or return;
235  
236 2 my $stx = $self->{$priv}->{stx};
237 2 ▶ $stx->execute_i(@list) or return;
Branch: 1 branch
BranchTrueFalseTotal
$stx->execute_i(@list) or return 2 0 2
238 2 my $sth = $stx->sth();
239 # typically undef, else hash or array ref
240 2 my $slice = $attr->{Slice};
241 2 ▶ if (!$slice and $slice=$attr->{Columns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $slice and $slice = $attr->{"Columns"}) 0 2 2
Condition33%1/3
not $slice and $slice = $attr->{"Columns"}
!ll&&!rl&&r
020
MC/DC0%0/2
not $slice and $slice = $attr->{"Columns"}
not $slice $slice = $attr->{"Columns"}
Truth table33%1/3
not(A) and B
A= not $sliceB= $slice = $attr->{"Columns"}
ABresult
0X0
100
111
242 0 ▶ if (ref $slice eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $slice eq "ARRAY") 0 0 0
243 0 $slice = [ @{$attr->{Columns}} ];
244 0 for (@$slice) { $_-- }
245 }
246 }
247 my $rows = $sth->fetchall_arrayref(
248 2 $slice, my $MaxRows = $attr->{MaxRows});
249 2 ▶ $sth->finish if defined $MaxRows;
Branch: 1 branch
BranchTrueFalseTotal
if defined $MaxRows 0 2 2
250 2 return $rows;
251 };
252 }
253  
254 # new in 0.40
255 0 ▶sub selectall_hashref_i {
Subroutine selectall_hashref_i: not called CC=2 SCAR=17.9
256 0 my ($self, @list) = @_;
257 0 return _wrap {
258 #need $key_field
259 0 my ($sql, $key_field, $attr, @bind) = $self->dbi_interp(@list);
260  
261 # based on DBI::selectall_hashref
262 # my $sth = $dbh->prepare($sql, $attr);
263 # return unless $sth;
264 # _do_execute($sth, @bind) or return;
265  
266 0 my $stx = $self->{$priv}->{stx};
267 0 ▶ $stx->execute_i(@list) or return;
Branch: 1 branch
BranchTrueFalseTotal
$stx->execute_i(@list) or return 0 0 0
268 0 my $sth = $stx->sth();
269 0 return $sth->fetchall_hashref($key_field);
270 };
271 }
272  
273 # new in 0.40
274 0 ▶sub selectcol_arrayref_i {
Subroutine selectcol_arrayref_i: not called CC=9 SCAR=45.0
275 0 my ($self, @list) = @_;
276 0 return _wrap {
277 0 my ($sql, $attr, @bind) = $self->dbi_interp(@list); # need $attr
278  
279 # based on DBI::selectcol_arrayref
280 # my $sth = $dbh->prepare($sql, $attr);
281 # return unless $sth;
282 # _do_execute($sth, @bind) or return;
283  
284 0 my $stx = $self->{$priv}->{stx};
285 0 ▶ $stx->execute_i(@list) or return;
Branch: 1 branch
BranchTrueFalseTotal
$stx->execute_i(@list) or return 0 0 0
286 0 ▶ my @columns = ($attr->{Columns}) ? @{$attr->{Columns}} : (1);
Branch: 1 branch
BranchTrueFalseTotal
$attr->{'Columns'} ? : 0 0 0
287 0 my @values = (undef) x @columns;
288 0 my $idx = 0;
289 0 my $sth = $stx->sth();
290 0 for (@columns) {
291 0 ▶ $sth->bind_col($_, \$values[$idx++]) or return;
Branch: 1 branch
BranchTrueFalseTotal
$sth->bind_col($_, \$values[$idx++]) or return 0 0 0
292 }
293 0 my @col;
294 0 ▶ if (my $max = $attr->{MaxRows}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $max = $attr->{'MaxRows'}) { } 0 0 0
295 0 ▶ push @col, @values while @col<$max && $sth->fetch;
Branch: 1 branch
BranchTrueFalseTotal
@col < $max and $sth->fetch and do { push @col, @values } 0 0 0
Condition0%0/3
@col < $max and $sth->fetch
!ll&&!rl&&r
000
MC/DC0%0/2
@col < $max and $sth->fetch
@col < $max $sth->fetch
Truth table0%0/3
A and B
A= @col < $maxB= $sth->fetch
ABresult
0X0
100
111
296 }
297 else {
298 0 ▶ push @col, @values while $sth->fetch;
Branch: 1 branch
BranchTrueFalseTotal
$sth->fetch and do { push @col, @values } 0 0 0
299 }
300 0 return \@col;
301 };
302 }
303  
304 2 1;
305  
306 #####
307 package DBIx::Interp::STX;
308 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
309 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
310 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
311  
312 2 our @CARP_NOT = @DBIx::Interp::CARP_NOT;
313  
314 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
315 2 my ($class, $dbx) = @_;
316 2 my $self = bless {
317 # active sth
318 sth => undef,
319  
320 # map: SQL --> sth (sth cache)
321 sths => {},
322  
323 # queue of SQL. used to select sth to delete if cache is full
324 sql_queue => [],
325  
326 # DBIx::Interp
327 dbx => $dbx,
328  
329 # max sths allowed in the cache
330 max_sths => 1
331 }, $class;
332 2 return $self;
333 }
334  
335 2 ▶sub max_sths {
Subroutine max_sths: called CC=2 SCAR=6.9
336 2 my ($self, $max_sths) = @_;
337 2 ▶ if (defined $max_sths) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_sths) { } 1 1 2
338 1 $self->{max_sths} = $max_sths;
339 }
340 else {
341 1 return $self->{max_sths};
342 }
343 }
344  
345 8 ▶sub sth {
Subroutine sth: called CC=1 SCAR=0.0
346 8 my $self = shift;
347 8 return $self->{sth};
348 }
349  
350 2 ▶sub sths {
Subroutine sths: called CC=1 SCAR=0.0
351 2 my $self = shift;
352 2 return {%{$self->{sths}}};
353 }
354  
355 # renamed execute --> execute_i in 0.40
356 6 ▶sub execute_i {
Subroutine execute_i: called CC=1 SCAR=0.0
357 6 my ($self, @list) = @_;
358  
359 6 ▶ return DBIx::Interp::_wrap {
Subroutine __ANON__: called CC=6 SCAR=18.1
360 6 my ($sql, @bind) = $self->{dbx}->dbi_interp(@list);
361 6 ▶ shift @bind if defined $bind[0] && ref $bind[0] eq ''; # remove any key_field()
Branch: 1 branch
BranchTrueFalseTotal
if defined $bind[0] and ref $bind[0] eq "" 0 6 6
Condition33%1/3
defined $bind[0] and ref $bind[0] eq ""
!ll&&!rl&&r
600
MC/DC0%0/2
defined $bind[0] and ref $bind[0] eq ""
defined $bind[0] ref $bind[0] eq ""
Truth table33%1/3
A and B
A= defined $bind[0]B= ref $bind[0] eq ""
ABresult
0X0
100
111
362 6 my $attr = shift @bind;
363 6 my $sth = $self->{sths}->{$sql};
364 6 ▶ if (! defined $sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sth) 5 1 6
365 5 ▶ $sth = $self->{dbx}->prepare($sql, $attr) or return;
Branch: 1 branch
BranchTrueFalseTotal
$sth = $self->{"dbx"}->prepare($sql, $attr) or return 5 0 5
366 5 ▶ if (@{$self->{sql_queue}} + 1 > $self->{max_sths}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"sql_queue"};} + 1 > $self->{"max_sths"}) 2 3 5
367 2 my $sql_remove = shift @{$self->{sql_queue}};
368 2 delete $self->{sths}->{$sql_remove};
369 }
370 5 $self->{sths}->{$sql} = $sth;
371 5 push @{$self->{sql_queue}}, $sql;
372 }
373 6 $self->{sth} = $sth;
374 6 _bind_params($sth, @bind);
375 6 return $sth->execute();
376 6 };
377 }
378  
379 6 ▶sub _bind_params {
Subroutine _bind_params: called CC=1 SCAR=0.0
380 6 my ($sth, @bind) = @_;
381 6 my $num = 1;
382 6 ▶ return DBIx::Interp::_wrap {
Subroutine __ANON__: called CC=4 SCAR=13.9
383 6 ▶ if (ref($bind[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $bind[0] eq 'ARRAY') { } 1 5 6
384 1 for my $val (@bind) {
385 6 $sth->bind_param($num++, $val->[0], $val->[1]->{type});
386 }
387 }
388 else {
389 5 for my $val (@bind) {
390 13 $sth->bind_param($num++, $val);
391 }
392 }
393 6 };
394 }
395  
396 0 ▶sub fetchrow_arrayref {
Subroutine fetchrow_arrayref: not called CC=1 SCAR=6.9
397 0 my $self = shift;
398 0 ▶ return DBIx::Interp::_wrap {
Subroutine __ANON__: not called CC=1 SCAR=6.9
399 0 return $self->{sth}->fetchrow_arrayref();
400 0 };
401 }
402  
403 0 ▶sub fetchrow_array {
Subroutine fetchrow_array: not called CC=1 SCAR=6.9
404 0 my $self = shift;
405 0 ▶ return DBIx::Interp::_wrap {
Subroutine __ANON__: not called CC=1 SCAR=6.9
406 0 return $self->{sth}->fetchrow_array();
407 0 };
408 }
409  
410 0 ▶sub fetchrow_hashref {
Subroutine fetchrow_hashref: not called CC=1 SCAR=6.9
411 0 my ($self, @params) = @_;
412 0 ▶ return DBIx::Interp::_wrap {
Subroutine __ANON__: not called CC=1 SCAR=6.9
413 0 return $self->{sth}->fetchrow_hashref(@params);
414 0 };
415 }
416  
417 4 ▶sub fetchall_arrayref {
Subroutine fetchall_arrayref: called CC=1 SCAR=0.0
418 4 my ($self, @params) = @_;
419 4 ▶ return DBIx::Interp::_wrap {
Subroutine __ANON__: called CC=1 SCAR=0.0
420 4 return $self->{sth}->fetchall_arrayref(@params);
421 4 };
422 }
423  
424 0 ▶sub fetchall_hashref {
Subroutine fetchall_hashref: not called CC=1 SCAR=6.9
425 0 my ($self, @params) = @_;
426 0 ▶ return DBIx::Interp::_wrap {
Subroutine __ANON__: not called CC=1 SCAR=6.9
427 0 return $self->{sth}->fetchall_hashref(@params);
428 0 };
429 }
430  
431 2 1;
432  
433 __END__
« SQL/Interp.pm ↑ Summary