DBI/st.pm

filter: Statementstmt 27.9% 19 / 68 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 45.4% 5 / 11 Podpod 25.0% 1 / 4 totaltotal 22.7% 25 / 110 SCAR 45.9
CC
13
Cov
20%
initialize
30.0
execute
30.0
strip
20.0
CRAP 98.1
↑ Summary DBI/db.pm »
1  
2 package Tao::DBI::st;
3  
4 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 require Exporter;
9  
10 2 our @ISA = qw(Exporter);
11 2 our @EXPORT = qw();
12  
13 2 our $VERSION = '0.011';
14  
15 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # the instance variables:
18 # DBH
19 # SQL
20 # PLACES, (the mapping between anonymous placholders and named placeholders)
21 # ARGNS (the current argument names)
22 # STMT
23 #
24 # NAME
25  
26 # creates a Tao::DBI::st object (the statement is
27 # prepared during initialization).
28 sub new {
29 0 ▶ my $proto = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
30 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
31 0 my $obj = bless {}, $class;
32 0 return $obj->initialize(@_);
33  
34 }
35  
36 # { dbh => , sql => }
37 sub initialize {
38 0 ▶ my ( $self, $args ) = @_;
Subroutine initialize: not called CC=5 SCAR=34.0
Pod undocumented
39 0 ▶ croak "argument 'sql' undefined" unless defined $args->{sql};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"sql"} 0 0 0
40 0 my $sql = $self->{SQL} = $args->{sql};
41 0 ▶ croak "argument 'dbh' is required" unless $args->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"dbh"} 0 0 0
42 0 $self->{DBH} = $args->{dbh};
43 0 my ( $ssql, $places, $argns ) = strip($sql);
44 0 $self->{PLACES} = $places;
45 0 $self->{ARGNS} = $argns;
46  
47 0 ▶ if ( $self->{DBH}->isa('Tao::DBI::db') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'DBH'}->isa('Tao::DBI::db')) { } 0 0 0
48 0 $self->{STMT} = $self->{DBH}->{DBH}->prepare($ssql);
49  
50 # FIXME: needs to support optional args
51 # FIXME: knows too much on Tao::DBI::db
52 }
53 else {
54 0 $self->{STMT} = $self->{DBH}->prepare($ssql);
55 }
56 0 ▶ unless ( $self->{STMT} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"STMT"}) 0 0 0
57 0 %$self = ();
58 0 return undef;
59 }
60 0 return $self;
61 }
62  
63 # ($ssql, $places, $argns) = strip($sql);
64 sub strip {
65 0 ▶ my $sql = shift;
Subroutine strip: not called CC=4 SCAR=30.0
Pod undocumented
66 0 my $ssql = '';
67 0 my @places = ();
68 0 my %args = ();
69  
70 0 for ( $_ = $sql ; ; ) {
71 0 ▶ $ssql .= ':', next
Branch: 1 branch
BranchTrueFalseTotal
if /\G::/cg 0 0 0
72 if /\G::/gc;
73 0 ▶ $ssql .= "?", push( @places, $1 ), $args{$1} = 1, next
Branch: 1 branch
BranchTrueFalseTotal
if /\G:(\w+)/cg 0 0 0
74 if /\G:(\w+)/gc;
75 0 ▶ $ssql .= $1, next
Branch: 1 branch
BranchTrueFalseTotal
if /\G(:?[^:]*)/cg 0 0 0
76 if /\G(:?[^:]*)/gc;
77 0 last;
78 }
79  
80 # if not at the end of string, invalid use of :[^\w:] -> not yet implemented
81  
82 0 my @argns = keys %args;
83 0 return ( $ssql, \@places, \@argns );
84 }
85  
86 # $stmt->execute($hash_ref)
87 # $stmt->execute($scalar)
88 # $stmt->execute
89 sub execute {
90 0 ▶ my $self = shift;
Subroutine execute: not called CC=5 SCAR=34.0
Pod documented
91 0 my $args = shift;
92  
93 0 ▶ if ( !$args ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $args) { } 0 0 0
elsif (ref $args) { } 0 0 0
94 0 ▶ if ( @{ $self->{ARGNS} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"ARGNS"};}) 0 0 0
95 0 croak "execute on SQL::Statement missing arguments";
96 }
97 0 return $self->{STMT}->execute;
98  
99 }
100 elsif ( ref $args ) {
101 0 return $self->{STMT}->execute( @{$args}{ @{ $self->{PLACES} } }, @_ );
102 }
103 else {
104 0 ▶ if ( @{ $self->{ARGNS} } != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"ARGNS"};} != 1) 0 0 0
105 0 croak
106 "execute on SQL::Statement with a single non-ref argument only for one-parameter statements";
107 }
108 0 return $self->{STMT}->execute( ($args) x @{ $self->{PLACES} }, @_ );
109 }
110 }
111  
112 # fetch*
113  
114 2 ▶use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
115  
116 # If method wasn't found, delegates to STMT instance variable.
117 # This way, instances of this class behaves like DBI statements.
118 sub AUTOLOAD {
119 0 ▶ my $self = shift;
Subroutine AUTOLOAD: not called CC=1 SCAR=6.9
120 0 my $meth = $AUTOLOAD;
121 0 $meth =~ s/.*:://;
122 0 return $self->{STMT}->$meth(@_);
123 }
124  
125 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
126  
127 2 1;
128  
129 # NOTE.
130 # In SQL statements, ':' has a special meaning as the prefix of a placeholder.
131 # If you need to include ':' within a statement to be literally interpreted,
132 # double it: '::'.
133  
134 __END__
↑ Summary DBI/db.pm »