Librarian/Statement.pm

filter: Statementstmt 23.5% 28 / 119 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 40.0% 6 / 15 Podpod 33.3% 2 / 6 totaltotal 18.2% 36 / 197 CC 26 SCAR 37.9
CC
26
Cov
17%
_bind
90.0
new
56.0
_substitutions
56.0
CRAP 44.2
↑ Summary Librarian.pm »
1 package DBIx::Librarian::Statement;
2  
3 1 require 5.005;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw(Class::Virtual);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $VERSION = '0.4';
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Time::HiRes qw( gettimeofday tv_interval );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Log::Channel;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 {
14 1 my $execlog = new Log::Channel "exec";
15 0 ▶ sub execlog { $execlog->(@_) }
Subroutine execlog: not called CC=1 SCAR=6.9
Pod undocumented
16 1 my $perflog = new Log::Channel "perf";
17 0 ▶ sub perflog { $perflog->(@_) }
Subroutine perflog: not called CC=1 SCAR=6.9
Pod undocumented
18 1 my $rowlog = new Log::Channel "rows";
19 0 ▶ sub rowlog { $rowlog->(@_) }
Subroutine rowlog: not called CC=1 SCAR=6.9
Pod undocumented
20 1 my $bindlog = new Log::Channel "bind";
21 0 ▶ sub bindlog { $bindlog->(@_) }
Subroutine bindlog: not called CC=1 SCAR=6.9
Pod undocumented
22 }
23  
24 1 __PACKAGE__->virtual_methods(qw(fetch));
25  
26 =head1 NAME
27  
28 DBIx::Librarian::Statement - an active SQL statement in a Librarian
29  
30 =head1 SYNOPSIS
31  
32 Internal class used by DBIx::Librarian. Implementation of BUILDER
33 pattern (Librarian is the Director, Statement is the Builder).
34  
35 Recognizes the following extensions to the SQL SELECT directive:
36  
37 SELECT* return zero or more rows as an array
38 SELECT? return zero or one rows as a scalar
39 SELECT1 return exactly one row as a scalar
40  
41 For the SELECT? and SELECT1 flavors, an exception will be raised if
42 more than one row is returned. For the SELECT1 flavor, an exception
43 will be raised if no rows are found.
44  
45 The default behavior for an unadorned SELECT is multi-row SELECT*.
46  
47 =head1 METHODS
48  
49 =cut
50  
51 1 my %select_mode = (
52 "*" => "SelectMany",
53 "?" => "SelectOne",
54 "1" => "SelectExactlyOne",
55 "" => "SelectMany",
56 );
57  
58 =item B<new>
59  
60 my $stmt = new DBIx::Librarian::Statement ($dbh, $sql);
61  
62 Prepares the SQL statement in $sql against the database connection
63 in $dbh. Handles bind variables and direct substitution.
64  
65 =cut
66  
67 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
68 0 my ($proto, $dbh, $sql, %config) = @_;
69  
70 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
71 0 my $self = {
72 DBH => $dbh
73 };
74 0 ▶ while (my ($key, $val) = each %config) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %config) 0 0 0
75 0 $self->{$key} = $val;
76 }
77  
78  
79 # WARNING: Oracle does not like ? placeholders inside comments.
80 # If Statement thinks that the ? in the comment is a bind value
81 # and includes a value for it in the execute() list, Oracle receives
82 # more values than it expects.
83 # mysql seems to handle this correctly.
84 # May need to strip comments from SQL before converting placeholders.
85 # Yuck. Is there a cross-platform way to do this?
86  
87  
88 0 my @bindvars = $sql =~ /[^A-Za-z0-9:]:([[:word:].]+)/mog;
89 0 ▶ if (@bindvars) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bindvars) 0 0 0
90 0 $sql =~ s/([^A-Za-z0-9:]):[[:word:].]+/$1?/og;
91 }
92 0 $self->{BINDVARS} = \@bindvars;
93  
94 0 ▶ if ($sql =~ /^select/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /^select/io) 0 0 0
95 0 my ($mode) = $sql =~ /^select(\S*)/io;
96  
97 croak "Unrecognized select mode $mode in\n$sql\n"
98 0 ▶ unless $select_mode{$mode};
Branch: 1 branch
BranchTrueFalseTotal
unless $select_mode{$mode} 0 0 0
99  
100 # delegate SELECT processing based on expected rows returned
101  
102 0 $class = __PACKAGE__ . "::$select_mode{$mode}";
103 0 my $classpath = $class;
104 0 $classpath =~ s{::}{/}g;
105 0 require "$classpath.pm";
106  
107 0 $sql =~ s/^select\S*/select/io;
108  
109 0 $self->{IS_SELECT} = 1;
110 }
111  
112  
113 0 bless ($self, $class);
114  
115  
116 0 my @directvars;
117 0 ▶ if ($sql =~ /\$\w+/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql =~ /\$\w+/o) { } 0 0 0
118 # requires on-demand parsing
119 0 @directvars = $sql =~ /\$(\w+)/og;
120 0 $self->{DIRECTVARS} = \@directvars;
121 # $self->{SQL} = $sql;
122 }
123 else {
124 # can prepare in advance
125 0 $self->_prepare($sql);
126 }
127  
128 0 $self->{SQL} = $sql;
129  
130 0 return $self;
131 }
132  
133  
134 0 ▶sub _prepare {
Subroutine _prepare: not called CC=2 SCAR=17.9
135 0 my ($self, $sql) = @_;
136  
137 0 execlog "PREPARE SQL:\n", $sql, "\n====================\n";
138  
139 0 my $sth = $self->{DBH}->prepare($sql);
140 0 ▶ if (!$sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
141 0 croak $self->{DBH}->errstr;
142 }
143 0 $self->{STH} = $sth;
144 }
145  
146  
147 =item B<execute>
148  
149 $stmt->execute($data);
150  
151 Returns the number of rows affected for INSERTs, UPDATEs and DELETES;
152 zero for SELECTs.
153 Croaks on any database error or if any SELECT criteria are violated.
154  
155 =cut
156  
157 0 ▶sub execute {
Subroutine execute: not called CC=5 SCAR=34.0
Pod documented
158 0 my ($self, $data) = @_;
159  
160 0 execlog "EXECUTE SQL:\n", $self->{SQL}, "\n==========\n";
161  
162 0 $self->_substitutions($data);
163  
164 0 my @bindlist = $self->_bind($data);
165  
166 0 ▶ if ($self->{IS_SELECT} && !$data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"IS_SELECT"} and not $data) 0 0 0
Condition0%0/3
$self->{"IS_SELECT"} and not $data
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"IS_SELECT"} and not $data
$self->{"IS_SELECT"} not $data
Truth table0%0/3
A and not(B)
A= $self->{"IS_SELECT"}B= not $data
ABresult
0X0
100
111
167 # caller(0) gives this line here in Statement.pm
168 # caller(1) gives line in _execute() in Librarian.pm
169 # caller(2) gives line in execute() in Librarian.pm
170 # caller(3) gives the application code invoking Librarian::execute()
171 0 my (undef, $filename, $line) = caller(3);
172 0 croak "Missing required data parameter for SQL execute(\"$self->{NAME}\")\n at $filename line $line\n";
173 }
174  
175 0 my $t0 = [ gettimeofday ];
176  
177 0 ▶ if (! $self->{STH}->execute(@bindlist)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"STH"}->execute(@bindlist)) 0 0 0
178 0 croak $self->{DBH}->errstr. " in SQL\n$self->{STH}->{Statement}\n";
179 }
180  
181 0 my $elapsed = tv_interval ($t0, [ gettimeofday ]);
182 0 perflog "ELAPSED TIME: ", $elapsed, "\n";
183  
184 0 my $rows;
185 0 ▶ if ($self->{IS_SELECT}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'IS_SELECT'}) { } 0 0 0
186 0 $rows = $self->fetch($data);
187 0 rowlog "ROWS RETRIEVED: ", $rows, "\n";
188 }
189 else {
190 0 rowlog "ROWS ALTERED: ", $self->{STH}->rows, "\n";
191 0 $rows = $self->{STH}->rows;
192 }
193  
194 0 return $rows;
195 }
196  
197  
198 0 ▶sub _substitutions {
Subroutine _substitutions: not called CC=7 SCAR=40.3
199 0 my ($self, $data) = @_;
200  
201 0 ▶ return unless $self->{DIRECTVARS};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"DIRECTVARS"} 0 0 0
202  
203 # The SQL contains "$parameter" substitutions.
204 # Must be re-prepared before every execution.
205  
206 0 my $sql = $self->{SQL};
207 0 foreach my $directvar (@{$self->{DIRECTVARS}}) {
208 0 my $val;
209 0 ▶ if ($self->{ALLARRAYS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ALLARRAYS'}) { } 0 0 0
210 0 $val = $data->{$directvar}[0];
211 } else {
212 0 ▶ croak "Expected scalar for $directvar" if ref($data->{$directvar});
Branch: 1 branch
BranchTrueFalseTotal
if ref $data->{$directvar} 0 0 0
213 0 $val = $data->{$directvar};
214 }
215 0 ▶ bindlog sprintf ("\tSUB \$%s = %s\n",
Condition0%0/2
$val || '(null)'
l!l
00
MC/DC0%0/1
$val || '(null)'
$val
Truth table0%0/2
A
A= $val
Aresult
11
00
216 $directvar,
217 $val || '(null)');
218 0 $sql =~ s/\$$directvar(\W|$)/$val$1/g;
219 }
220  
221 0 my $sth = $self->{DBH}->prepare($sql);
222 0 ▶ if (!$sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
223 0 croak $self->{DBH}->errstr . " in SQL\n$sql\n";
224 }
225 0 $self->{STH} = $sth;
226 }
227  
228  
229 0 ▶sub _bind {
Subroutine _bind: not called CC=9 SCAR=45.0
230 0 my ($self, $data) = @_;
231  
232 0 ▶ return unless $self->{BINDVARS};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"BINDVARS"} 0 0 0
233  
234 # The SQL contains ":parameter" placeholders, which have already
235 # been converted to standard ? markers by prepare(). Pull the
236 # list of bind variables.
237  
238 0 my @bindlist;
239  
240 0 foreach my $bindvar (@{$self->{BINDVARS}}) {
241 0 my $val;
242 0 my $node = $data;
243 0 my $key = $bindvar;
244 0 ▶ while ($key =~ /\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\./) 0 0 0
245 # drill down into the hierarchy as needed to find sub-elements
246 0 my ($base, $subkey) = $key =~ /(.*?)\.(.*)/;
247 0 ▶ croak "Expected scalar for $key" if !defined $node->{$base} || !ref($node->{$base});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node->{$base} and ref $node->{$base} 0 0 0
Condition0%0/3
defined $node->{$base} and ref $node->{$base}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $node->{$base} and ref $node->{$base}
defined $node->{$base} ref $node->{$base}
Truth table0%0/3
A and B
A= defined $node->{$base}B= ref $node->{$base}
ABresult
0X0
100
111
248 0 $node = $node->{$base};
249 0 $key = $subkey;
250 }
251 0 ▶ if ($self->{ALLARRAYS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ALLARRAYS'}) { } 0 0 0
252 0 $val = $node->{$key}[0];
253 } else {
254 0 ▶ croak "Expected scalar for $key" if ref($data->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if ref $data->{$key} 0 0 0
255 0 $val = $node->{$key};
256 }
257  
258 0 ▶ bindlog sprintf ("\tBIND :%s = %s\n",
Condition0%0/2
$val || '(null)'
l!l
00
MC/DC0%0/1
$val || '(null)'
$val
Truth table0%0/2
A
A= $val
Aresult
11
00
259 $bindvar,
260 $val || '(null)');
261 0 push @bindlist, $val;
262 }
263  
264 0 return @bindlist;
265 }
266  
267 1 1;
268  
269 =head1 AUTHOR
270  
271 Jason W. May <jmay@pobox.com>
272  
273 =head1 COPYRIGHT
274  
275 Copyright (C) 2001-2003 Jason W. May. All rights reserved.
276 This module is free software; you can redistribute it and/or
277 modify it under the same terms as Perl itself.
278  
279 =cut
↑ Summary Librarian.pm »