SqlObject.pm

filter: Statementstmt 22.5% 79 / 350 Branchbran 17.6% 35 / 198 Conditioncond 9.2% 5 / 54 MC/DCmcdc 8.3% 3 / 36 Subroutinesub 42.1% 16 / 38 Podpod 68.0% 17 / 25 totaltotal 22.1% 155 / 701 CC 122 SCAR 40.9
CC
122
Cov
20%
connect_string
272.0
cond_insert_select
110.0
process_constructor_args
58.8
CRAP 59.5
↑ Summary SqlObject/ODBC.pm »
1 package SQL::SqlObject;
2  
3 #use 5.006;
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use SQL::SqlObject::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # 110803 - VERSION incremented to 0.02 for first CPAN release
11 # 101903 - fixed argument verification for insert_select
12 # - added connection arg cacheing
13 # - we now cache db_name, db_dsn and db_pre if they
14 # are provided as arguments to connect_string() -cz
15 # 011003 - arguments for constructor now come from
16 # SQL::SqlObject::Config -cz
17 # 010803 - added constuctor_args/process_contructror args -cz
18 # 010603 - added AUTOLOAD to pass through method calls -cz
19 # 052699 - Minor fix to Hashes Sub -cz
20  
21 3 our $VERSION = '0.02';
22 3 our %SqlConfig; # read from SQL::SqlObject::Config
23  
24 sub new
25 5 ▶{
Subroutine new: called CC=5 SCAR=16.1
Pod undocumented
26  
27 # install confguration, if necessary
28 5 ▶ unless (%SqlConfig)
Branch: 1 branch
BranchTrueFalseTotal
unless (%SQL::SqlObject::SqlConfig) 3 2 5
29 {
30 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 *SQL::SqlObject::SqlConfig = \%SQL::SqlObject::Config::SqlConfig;
32 }
33  
34 5 my ($class) = @_;
35 5 my $self;
36  
37  
38 # we don't really support clones...
39 5 ▶ if (ref $class)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 1 4 5
40 {
41 # clones must have us somwhere in their ancestory
42 1 ▶ unless (UNIVERSAL::isa($class, __PACKAGE__))
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($class, __PACKAGE__)) 0 1 1
43 {
44 0 Carp::confess (
45 "process_constructor_args: attemp to clone",
46 " from unrelated class: ", ref $class
47 );
48 }
49  
50 1 ($class) = $class =~ /(.*?)=/;
51 }
52  
53 5 $self = bless {}, $class;
54 5 $self->process_constructor_args(@_);
55 # post construction initialization
56 5 ▶ $self->_init if $self->can('_init');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_init") 1 4 5
57  
58 5 return $self;
59 }
60  
61 0 ▶sub clone { shift->new(@_) }
Subroutine clone: not called CC=1 SCAR=6.9
Pod undocumented
62  
63 5 ▶sub DESTROY { $_[0]->disconnect }
Subroutine DESTROY: called CC=1 SCAR=0.0
64  
65 sub process_constructor_args
66 5 ▶{
Subroutine process_constructor_args: called CC=22 SCAR=40.7
Pod undocumented
67  
68 5 ▶ unless (@_>1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ > 1) 0 5 5
69 0 Carp::confess "process_constructor_args: wrong number of parameters"
70 }
71  
72 5 my $self = shift;
73 5 my $class = shift;
74  
75 # coerse input to hash reference
76 5 my $args = {};
77 5 ▶ if ($_ = ref $_[0])
Branch: 2 branches
BranchTrueFalseTotal
if ($_ = ref $_[0]) { } 0 5 5
elsif (@_) { } 1 4 5
78 {
79 0 ▶ if (/HASH/)
Branch: 2 branches
BranchTrueFalseTotal
if (/HASH/) { } 0 0 0
elsif (/ARRAY/) { } 0 0 0
80 {
81 0 ▶ while (my ($k, $v) = each %{ $_[0] })
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$_[0];}) 0 0 0
82 {
83 0 my $arg_ix = SQL::SqlObject::Config::arg_index($k);
84 0 $k =~ y/A-Z-/a-z/d;
85 0 $args->{$SqlConfig{ARGS}[ $arg_ix ][0]} = $v;
86 }
87 }
88  
89 elsif (/ARRAY/)
90 {
91 $args = {
92 0 map { $SqlConfig{ARGS}[$_][0] => $_[0][$_] } 0..$#{$_[0]}
93 };
94 }
95  
96 else
97 {
98 0 Carp::Confess (q(process_constructor_args encountered ),
99 q(unexpected reference type ), $_);
100 }
101 }
102  
103 elsif (@_)
104 {
105 1 ▶ if ($_[0] =~ /^-/)
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^-/) { } 0 1 1
106 {
107 # input aleardy in key-value format
108 0 ▶ if (@_ % 2)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 0 0
109 {
110 0 Carp::confess ('process_constructor_args uneven number ',
111 'of input parameters')
112 }
113  
114 0 ▶ for (my $input_ix = 0; $input_ix < @_; $input_ix += 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($input_ix < @_) 0 0 0
115 {
116 0 my $arg_ix = SQL::SqlObject::Config::arg_index($_[$input_ix] );
117 0 $args->{ $SqlConfig{ARGS}[ $arg_ix ][0] } = $_[ $input_ix + 1 ];
118 }
119 }
120  
121 else
122 {
123 # input in ordered list format
124 1 for (0..$#_)
125 {
126 3 $args->{ $SqlConfig{ARGS}[$_][0] } = $_[$_];
127 }
128 }
129 }
130  
131 # now try to supply values for all args
132 5 ARG: for my $arg_id ( 0..$#{ $SqlConfig{ARGS} })
133 {
134 29 my $name = $SqlConfig{ARGS}[$arg_id][0];
135  
136 # see value if value was supplied as an argument
137 29 ▶ if (exists $args->{$name})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{$name}) 3 26 29
138 {
139 3 $self->$name( $args->{$name} );
140 3 next ARG;
141 }
142  
143 # if we're cloning look at the parent object first
144 26 ▶ if (ref $class and defined $class->$name)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class and defined $class->$name) 4 22 26
Condition100%3/3
ref $class and defined $class->$name
!ll&&!rl&&r
2114
MC/DC100%2/2
ref $class and defined $class->$name
ref $class defined $class->$name
Truth table100%3/3
A and B
A= ref $classB= defined $class->$name
ABresult
0X0
100
111
145 {
146 4 $self->$name($class->$name);
147 4 next ARG;
148 }
149  
150 # search env variables if any we're supplied
151 22 ▶ if ($SqlConfig{ARGS}[$arg_id][2])
Branch: 1 branch
BranchTrueFalseTotal
if ($SQL::SqlObject::SqlConfig{"ARGS"}[$arg_id][2]) 7 15 22
152 {
153 7 my $env = $SqlConfig{ARGS}[$arg_id][2];
154  
155 7 ▶ if (ref $env)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $env) { } 3 4 7
156 {
157 3 ENV: for (@$env)
158 {
159 3 ▶ next ENV unless exists $ENV{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ENV{$_} 3 0 3
160 0 $self->$name( $ENV{$_} );
161 0 next ARG;
162 }
163 }
164  
165 else
166 {
167 4 ▶ if (exists $ENV{$env})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{$env}) 0 4 4
168 {
169 0 $self->$name( $ENV{$env} );
170 0 next ARG;
171 }
172 }
173 }
174  
175 # use default if one was provided
176 22 ▶ if ($SqlConfig{ARGS}[$arg_id][3] and
Branch: 1 branch
BranchTrueFalseTotal
if ($SQL::SqlObject::SqlConfig{"ARGS"}[$arg_id][3] and $_ = $SQL::SqlObject::SqlConfig{$SQL::SqlObject::SqlConfig{"ARGS"}[$arg_id][3]}) 14 8 22
Condition66%2/3
$SQL::SqlObject::SqlConfig{"ARGS"}[$arg_id][3] and $_ = $SQL::SqlObject::SqlConfig{$SQL::SqlObject::SqlConfig{"ARGS"}[$arg_id][3]}
!ll&&!rl&&r
0814
MC/DC50%1/2
$SQL::SqlObject::SqlConfig{"ARGS"}[$arg_id][3] and $_ = $SQL::SqlObject::SqlConfig{$SQL::SqlObject::SqlConfig{"ARGS"}[$arg_id][3]}
$SQL::SqlObject::SqlConfig{"ARGS"}[$arg_id][3] $_ = $SQL::SqlObject::SqlConfig{$SQL::SqlObject::SqlConfig{"ARGS"}[$arg_id][3]}
Truth table66%2/3
A and B
A= $SQL::SqlObject::SqlConfig{"ARGS"}[$arg_id][3]B= $_ = $SQL::SqlObject::SqlConfig{$SQL::SqlObject::SqlConfig{"ARGS"}[$arg_id][3]}
ABresult
0X0
100
111
177 $_ = $SqlConfig{ $SqlConfig{ARGS}[$arg_id][3] })
178 {
179 14 $self->$name( $_ );
180 14 next ARG;
181 }
182 }
183  
184 5 return $self;
185 }
186  
187 # is_connected: toggled on by dbh
188 # off by disconnect
189 2 ▶sub is_connected : lvalue { $_[0]->{connected_P} }
Subroutine is_connected: called CC=1 SCAR=0.0
Pod documented
190  
191 # primary flock of accessors
192 11 ▶sub db_name : lvalue { delete $_[0]->{__connection_args};
Subroutine db_name: called CC=2 SCAR=6.9
Pod documented
193 11 ▶ $#_ and $_[0]->{name} = $_[1]; $_[0]->{name}}
Branch: 1 branch
BranchTrueFalseTotal
$#_ and $_[0]{"name"} = $_[1] 5 6 11
194 8 ▶sub db_name_prefix : lvalue { delete $_[0]->{__connection_args};
Subroutine db_name_prefix: called CC=2 SCAR=6.9
Pod undocumented
195 8 ▶ $#_ and $_[0]->{pre} = $_[1]; $_[0]->{pre}}
Branch: 1 branch
BranchTrueFalseTotal
$#_ and $_[0]{"pre"} = $_[1] 5 3 8
196 11 ▶sub db_dsn : lvalue { delete $_[0]->{__connection_args};
Subroutine db_dsn: called CC=2 SCAR=6.9
Pod documented
197 11 ▶ $#_ and $_[0]->{dsn} = $_[1]; $_[0]->{dsn}}
Branch: 1 branch
BranchTrueFalseTotal
$#_ and $_[0]{"dsn"} = $_[1] 5 6 11
198 11 ▶sub db_user : lvalue { delete $_[0]->{__connection_args};
Subroutine db_user: called CC=2 SCAR=6.9
Pod documented
199 11 ▶ $#_ and $_[0]->{user} = $_[1]; $_[0]->{user}}
Branch: 1 branch
BranchTrueFalseTotal
$#_ and $_[0]{"user"} = $_[1] 5 6 11
200 6 ▶sub db_password : lvalue { delete $_[0]->{__connection_args};
Subroutine db_password: called CC=2 SCAR=6.9
Pod documented
201 6 ▶ $#_ and $_[0]->{passwd} = $_[1]; $_[0]->{passwd}}
Branch: 1 branch
BranchTrueFalseTotal
$#_ and $_[0]{"passwd"} = $_[1] 1 5 6
202 0 ▶sub Error : lvalue { $#_ and $DBI::errstr = $_[1]; $DBI::errstr}
Subroutine Error: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$#_ and $DBI::errstr = $_[1] 0 0 0
203 0 ▶sub dbh : lvalue
Subroutine dbh: not called CC=2 SCAR=17.9
Pod documented
204 {
205 0 ▶ unless ($_[0]->is_connected) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0]->is_connected) 0 0 0
206 0 $_[0]->{dbh} = $_[0]->connect;
207 0 $_[0]->is_connected = 1;
208 }
209  
210 $_[0]->{dbh}
211 0 }
212  
213 sub connect
214 0 ▶{
Subroutine connect: not called CC=6 SCAR=37.4
Pod undocumented
215  
216 0 my $self = shift;
217  
218 0 ▶ if (@_)
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 0 0
elsif ($self->{'__connection_args'}) { } 0 0 0
219 # cache the arguments, in case we have to reconnect
220 # as seems to be needed for insert_select
221 {
222 0 $self->{__connection_args} = [ @_ ];
223 }
224  
225 elsif ($self->{__connection_args})
226 # load the connection args from the cache
227 {
228 0 @_ = @{ $self->{__connection_args} }
229 }
230  
231 0 my $connect_string = $self->connect_string(@_);
232  
233 0 ▶ $self->is_connected and $self->disconnect;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_connected and $self->disconnect 0 0 0
234  
235 0 my $dbh = DBI->connect($connect_string,$self->db_user,$self->db_password);
236  
237 0 my $err = $self->Error;
238 0 ▶ $err and Carp::confess (
Branch: 1 branch
BranchTrueFalseTotal
$err and Carp::confess("Whoops - couldn\\'t connect to ", $_[0] || $self->db_name, "\n", "Error:", $err) 0 0 0
Condition0%0/3
$_[0] || $self->db_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0] || $self->db_name
$_[0] $self->db_name
Truth table0%0/3
A || B
A= $_[0]B= $self->db_name
ABresult
1X1
011
000
239 q(Whoops - couldn\'t connect to ),
240 $_[0] || $self->db_name, "\n",
241 'Error:', $err
242 );
243  
244 0 return $dbh;
245 }
246  
247 # build the connect string
248 sub connect_string
249 0 ▶{
Subroutine connect_string: not called CC=16 SCAR=56.1
Pod undocumented
250 0 my ($self, $name, $dsn, $pre) = @_;
251  
252 0 ▶ $self->db_name ||= $name if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
Condition0%0/3
$self->db_name ||= $name
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->db_name ||= $name
$self->db_name $name
Truth table0%0/3
A ||= B
A= $self->db_nameB= $name
ABresult
1X1
011
000
253 0 ▶ $self->db_dsn ||= $dsn if $dsn;
Branch: 1 branch
BranchTrueFalseTotal
if $dsn 0 0 0
Condition0%0/3
$self->db_dsn ||= $dsn
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->db_dsn ||= $dsn
$self->db_dsn $dsn
Truth table0%0/3
A ||= B
A= $self->db_dsnB= $dsn
ABresult
1X1
011
000
254 0 ▶ $self->db_name_prefix ||= $pre if $pre;
Branch: 1 branch
BranchTrueFalseTotal
if $pre 0 0 0
Condition0%0/3
$self->db_name_prefix ||= $pre
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->db_name_prefix ||= $pre
$self->db_name_prefix $pre
Truth table0%0/3
A ||= B
A= $self->db_name_prefixB= $pre
ABresult
1X1
011
000
255  
256 0 ▶ defined $name or $name = $self->db_name;
Branch: 1 branch
BranchTrueFalseTotal
defined $name or $name = $self->db_name 0 0 0
257 0 ▶ defined $dsn or $dsn = $self->db_dsn
Branch: 1 branch
BranchTrueFalseTotal
defined $dsn or $dsn = $self->db_dsn or Carp::confess("No data source named for SqlObject connect string") 0 0 0
Condition0%0/3
defined $dsn or $dsn = $self->db_dsn
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $dsn or $dsn = $self->db_dsn
defined $dsn $dsn = $self->db_dsn
Truth table0%0/3
A or B
A= defined $dsnB= $dsn = $self->db_dsn
ABresult
1X1
011
000
258 or Carp::confess "No data source named for SqlObject connect string";
259 0 ▶ defined $pre or $pre = $self->db_name_prefix;
Branch: 1 branch
BranchTrueFalseTotal
defined $pre or $pre = $self->db_name_prefix 0 0 0
260  
261 0 my $other = '';
262 0 ▶ if ($SqlConfig{OTHER_ARGS})
Branch: 1 branch
BranchTrueFalseTotal
if ($SQL::SqlObject::SqlConfig{"OTHER_ARGS"}) 0 0 0
263 {
264 0 ▶ if (ref $SqlConfig{OTHER_ARGS})
Branch: 1 branch
BranchTrueFalseTotal
if (ref $SQL::SqlObject::SqlConfig{'OTHER_ARGS'}) { } 0 0 0
265 {
266 0 for (@{$SqlConfig{OTHER_ARGS}})
267 {
268 0 ▶ my $val = $self->$_ or next;
Branch: 1 branch
BranchTrueFalseTotal
my $val = $self->$_ or next 0 0 0
269 0 my ($key) = (/^(?:.*?_)?(.*)$/);
270 0 $other .= "$SqlConfig{OTHER_ARG_SEP}$key=$val";
271 }
272 }
273  
274 else
275 {
276 0 my $meth = $SqlConfig{OTHER_ARGS};
277 0 my $val = $self->$meth;
278 0 ▶ $other = "$SqlConfig{OTHER_ARG_SEP}$SqlConfig{OTHER_ARGS}=$val"
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
279 if defined $val;
280 }
281 }
282  
283 0 return "$dsn:$pre$name$other";
284 }
285  
286 sub disconnect
287 5 ▶{
Subroutine disconnect: called CC=2 SCAR=10.1
Pod undocumented
288 5 my $self = shift;
289  
290 5 ▶ if(exists $self->{'dbh'})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"dbh"}) 0 5 5
291 {
292 0 $self->dbh->disconnect;
293 0 $self->is_connected = '';
294 0 delete $self->{'dbh'};
295 }
296 }
297  
298 sub hash
299 0 ▶{
Subroutine hash: not called CC=4 SCAR=30.0
Pod documented
300 0 ▶ unless (@_==2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == 2) 0 0 0
301 0 Carp::confess qq(SqlObject: wrong number of arguments for hash);
302 }
303  
304 0 my ($self,$query) = @_;
305  
306 0 my $sth = $self->prepare($query);
307 0 $sth->execute();
308  
309 0 my $href = $sth->fetchrow_hashref;
310 0 $sth->finish;
311  
312 0 ▶ return unless defined $href;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $href 0 0 0
313 0 ▶ return wantarray ? %$href : $href;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
314 }
315  
316 sub hashes
317 0 ▶{
Subroutine hashes: not called CC=6 SCAR=37.4
Pod documented
318 0 ▶ if (@_<2 or @_>3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2 or @_ > 3) 0 0 0
Condition0%0/3
@_ < 2 or @_ > 3
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ < 2 or @_ > 3
@_ < 2 @_ > 3
Truth table0%0/3
A or B
A= @_ < 2B= @_ > 3
ABresult
1X1
011
000
319 0 Carp::confess "SqlObject: wrong number of arguments for hashes"
320 }
321 0 my ($self,$query,$cref) = @_;
322  
323 0 my $sth = $self->prepare($query); $sth->execute();
324  
325 0 ▶ if($cref)
Branch: 1 branch
BranchTrueFalseTotal
if ($cref) { } 0 0 0
326 {
327 0 my $href;
328 0 ▶ $cref->($href) while $href = $sth->fetchrow_hashref;
Branch: 1 branch
BranchTrueFalseTotal
$href = $sth->fetchrow_hashref and do { &$cref($href) } 0 0 0
329  
330 0 $sth->finish();
331 0 return;
332 } else
333 {
334 0 my @AoH = @{ $sth->fetchall_arrayref( {} ) };
335 0 $sth->finish();
336  
337 0 ▶ return wantarray ? @AoH : \@AoH;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
338 }
339 }
340  
341 sub array
342 0 ▶{
Subroutine array: not called CC=4 SCAR=30.0
Pod documented
343 0 ▶ unless (@_==2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == 2) 0 0 0
344 0 Carp::confess "SqlObject: wrong number of arguments for array"
345 }
346  
347 0 my $self = shift;
348 0 my $query = shift;
349  
350 0 my $sth = $self->prepare($query);
351  
352 0 ▶ if (my $err = $self->Error)
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $self->Error) 0 0 0
353 {
354 0 Carp::confess ("SqlObject: bad SQL for array \n",
355 "Query: '$query'\nError: '$err'")
356 }
357  
358 0 $sth->execute();
359  
360 0 my @arr = $sth->fetchrow_array();
361 0 $sth->finish;
362  
363 0 ▶ return wantarray ? @arr : \@arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
364 }
365  
366 #
367 # Execute the query and return a single element
368 #
369 sub value
370 0 ▶{
Subroutine value: not called CC=3 SCAR=24.8
Pod documented
371 0 ▶ unless (@_==2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == 2) 0 0 0
372 0 Carp::confess "SqlObject: wrong number of arguments for value"
373 }
374  
375 0 my $self = shift;
376 0 my $query = shift;
377  
378 0 my $sth = $self->prepare($query);
379  
380 0 ▶ if (my $err = $self->Error)
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $self->Error) 0 0 0
381 {
382 0 Carp::confess ("SqlObject: bad SQL for value \n",
383 "Query: '$query'\nError: '$err'")
384 }
385  
386 0 $sth->execute();
387  
388 0 my ($val) = $sth->fetchrow_array;
389 0 $sth->finish;
390  
391 0 return $val;
392 }
393  
394 #
395 # Execute the query and return the first element of each result row
396 #
397 0 ▶sub list {
Subroutine list: not called CC=5 SCAR=34.0
Pod documented
398 0 ▶ unless (@_==2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == 2) 0 0 0
399 0 Carp::confess "SqlObject: wrong number of arguments for list"
400 }
401  
402 0 my $self = shift;
403 0 my $query = shift;
404  
405 0 my $sth = $self->prepare($query);
406  
407 0 ▶ if (my $err = $self->Error)
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $self->Error) 0 0 0
408 {
409 0 Carp::confess ("SqlObject: bad SQL for list \n",
410 "Query: '$query'\nError: '$err'")
411 }
412  
413 0 $sth->execute();
414  
415 0 my @vals;
416 0 ▶ push @vals, $_ while ($_) = $sth->fetchrow_array;
Branch: 1 branch
BranchTrueFalseTotal
($_) = $sth->fetchrow_array and do { push @vals, $_ } 0 0 0
417 0 $sth->finish;
418  
419 0 ▶ return wantarray ? @vals : \@vals;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
420 }
421  
422  
423 0 ▶sub delete {
Subroutine delete: not called CC=6 SCAR=37.4
Pod documented
424  
425 0 ▶ unless (@_>1 and @_<4)
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ > 1 and @_ < 4) 0 0 0
Condition0%0/3
@_ > 1 and @_ < 4
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 1 and @_ < 4
@_ > 1 @_ < 4
Truth table0%0/3
A and B
A= @_ > 1B= @_ < 4
ABresult
0X0
100
111
426 {
427 0 Carp::confess "SqlObject: wrong number of arguments for delete"
428 }
429  
430 0 my ($self,$table,$href) = @_;
431 0 my ($err, $query);
432  
433 0 ▶ $self->_sql_quote_hash($href) if defined $href and ref $href;
Branch: 1 branch
BranchTrueFalseTotal
if defined $href and ref $href 0 0 0
Condition0%0/3
defined $href and ref $href
!ll&&!rl&&r
000
MC/DC0%0/2
defined $href and ref $href
defined $href ref $href
Truth table0%0/3
A and B
A= defined $hrefB= ref $href
ABresult
0X0
100
111
434  
435 0 $query = $self->_sql_delete_query($table,$href);
436 0 $self->do($query);
437 0 $err = $self->Error();
438  
439 0 ▶ if ($err = $self->Error)
Branch: 1 branch
BranchTrueFalseTotal
if ($err = $self->Error) 0 0 0
440 {
441 0 Carp::confess ("SqlObject: delete error\n",
442 "Query: '$query' Error: '$err'")
443 }
444 }
445  
446 sub insert
447 0 ▶{
Subroutine insert: not called CC=3 SCAR=24.8
Pod documented
448 0 ▶ unless (@_==3)
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == 3) 0 0 0
449 {
450 0 Carp::confess "SqlObject: wrong number of arguments for insert"
451 }
452  
453 0 my ($self, $table, $href) = @_;
454 0 my ($err, $query);
455  
456 0 $self->_sql_quote_hash($href);
457  
458 0 $query = $self->_sql_insert_query($table,$href);
459 0 $self->do($query); $err = $self->Error();
460  
461 0 ▶ if ($err = $self->Error)
Branch: 1 branch
BranchTrueFalseTotal
if ($err = $self->Error) 0 0 0
462 {
463 0 Carp::confess ("SqlObject: insert error\n",
464 "Query: '$query'\nError: '$err'")
465 }
466 }
467  
468 0 ▶sub cond_insert {
Subroutine cond_insert: not called CC=7 SCAR=40.3
Pod documented
469 0 ▶ unless (@_>2 and @_<4)
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ > 2 and @_ < 4) 0 0 0
Condition0%0/3
@_ > 2 and @_ < 4
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 2 and @_ < 4
@_ > 2 @_ < 4
Truth table0%0/3
A and B
A= @_ > 2B= @_ < 4
ABresult
0X0
100
111
470 {
471 0 Carp::confess "SqlObject: wrong number of arguments for insert"
472 }
473  
474 0 my ($self, $table, $href, $whref) = @_;
475 0 my ($found,$err, $exists_query);
476  
477 0 $self->_sql_quote_hash($href);
478  
479 0 ▶ if ($whref)
Branch: 1 branch
BranchTrueFalseTotal
if ($whref) { } 0 0 0
480 {
481 0 $self->_sql_quote_hash($href);
482 0 $exists_query =
483 $self->_sql_select_query($table,[keys %$whref], $whref);
484 } else
485 {
486 0 $exists_query =
487 $self->_sql_select_query($table, [keys %$href], $href);
488 }
489  
490 0 $found = $self->value($exists_query);
491 0 $err = $self->Error();
492  
493 0 ▶ if ($err)
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
494 {
495 0 Carp::confess ("SqlObject cond_insert exists error\n",
496 "Query: $exists_query\nError: $err")
497 }
498  
499 0 ▶ return if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 0 0 0
500  
501 0 my $insert_query = $self->_sql_insert_query($table,$href);
502 0 $self->do($insert_query);
503 0 $err = $self->Error();
504 0 ▶ if ($err)
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
505 {
506 0 Carp::confess ("SqlObject insert error\n",
507 "Query: $insert_query\nError: $err")
508 }
509  
510 0 return 1;
511 }
512  
513  
514 0 ▶sub insert_select {
Subroutine insert_select: not called CC=5 SCAR=34.0
Pod documented
515  
516 0 ▶ unless (@_>2 and @_ < 5)
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ > 2 and @_ < 5) 0 0 0
Condition0%0/3
@_ > 2 and @_ < 5
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 2 and @_ < 5
@_ > 2 @_ < 5
Truth table0%0/3
A and B
A= @_ > 2B= @_ < 5
ABresult
0X0
100
111
517 {
518 0 Carp::confess "SqlObject: wrong number of arguments for insert_select"
519 }
520  
521 0 my ($self, $table, $href, $column) = @_;
522 0 my ($err, $insert_query);
523  
524 0 ▶ $column ||= join '_', $table, 'id';
Condition0%0/3
$column ||= join("_", $table, "id")
l!l&&r!l&&!r
000
MC/DC0%0/2
$column ||= join("_", $table, "id")
$column join("_", $table, "id")
Truth table0%0/3
A ||= B
A= $columnB= join("_", $table, "id")
ABresult
1X1
011
000
525 0 $column =~ s/^.*?(\w+)_id$/$1_id/;
526  
527 0 $self->_sql_quote_hash($href);
528  
529 0 $insert_query = $self->_sql_insert_query($table,$href);
530 0 $self->do($insert_query);
531 0 $err = $self->Error();
532  
533 0 ▶ if ($err)
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
534 {
535 0 Carp::confess ("SqlObject insert_select insert error\n",
536 "Query: $insert_query\nError: $err")
537 }
538  
539 0 my $select_query = $self->_sql_select_query($table,[$column],$href);
540 0 return $self->value($select_query);
541 }
542  
543 0 ▶sub cond_insert_select {
Subroutine cond_insert_select: not called CC=10 SCAR=47.0
Pod documented
544  
545 0 ▶ unless (@_>2 and @_<6)
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ > 2 and @_ < 6) 0 0 0
Condition0%0/3
@_ > 2 and @_ < 6
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 2 and @_ < 6
@_ > 2 @_ < 6
Truth table0%0/3
A and B
A= @_ > 2B= @_ < 6
ABresult
0X0
100
111
546 {
547 0 Carp::confess "SqlObject: wrong number of arguments for cond_insert_select"
548 }
549  
550 0 my ($self, $table, $href, $arg4, $arg5) = splice @_, 0, 3;
551 0 my ($whref,$column, $found, $err, $exists_query);
552  
553 0 $self->_sql_quote_hash($href);
554  
555 0 ▶ if (@_ == 1)
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@_ == 2) { } 0 0 0
556 {
557 # we have whref or column but not both
558 0 ▶ if (ref $_[0])
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
559 {
560 0 $whref = shift;
561 }
562  
563 else
564 {
565 0 $column = shift;
566 }
567 }
568 elsif (@_ ==2)
569 {
570 # we have both $whref = shift;
571 0 $column = shift;
572 0 last;
573 }
574 else
575 {
576 # we have neither
577 0 $column = join '_',$table,'id';
578 }
579  
580 0 ▶ if ($whref)
Branch: 1 branch
BranchTrueFalseTotal
if ($whref) { } 0 0 0
581 {
582 0 $self->_sql_quote_hash($href);
583 0 $exists_query = $self->_sql_select_query($table, [$column], $whref);
584 }
585 else
586 {
587 0 $exists_query = $self->_sql_select_query($table, [$column], $href);
588 }
589  
590 0 $found = $self->value($exists_query);
591 0 $err = $self->Error();
592  
593 0 ▶ if ($err)
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
594 {
595 0 Carp::confess ("SqlObject cond_insert_select exists error\n",
596 "Query: $exists_query\nError: $err")
597 }
598  
599 0 ▶ return $found if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 0 0 0
600  
601 0 my $insert_query = $self->_sql_insert_query($table,$href);
602 0 my $select_query = $self->_sql_select_query($table,[$column],$href);
603  
604 0 $self->do($insert_query);
605 0 $err = $self->Error();
606  
607 0 ▶ if ($err)
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
608 {
609 0 Carp::confess ("SqlObject insert_select insert error\n",
610 "Query: $insert_query\nError: $err")
611 }
612  
613 0 return $self->value($select_query);
614 }
615  
616 sub update
617 0 ▶{
Subroutine update: not called CC=6 SCAR=37.4
Pod documented
618  
619 0 ▶ unless (@_>2 and @_<5)
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ > 2 and @_ < 5) 0 0 0
Condition0%0/3
@_ > 2 and @_ < 5
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 2 and @_ < 5
@_ > 2 @_ < 5
Truth table0%0/3
A and B
A= @_ > 2B= @_ < 5
ABresult
0X0
100
111
620 {
621 0 Carp::confess "SqlObject: wrong number of arguments for update"
622 }
623  
624 0 my ($self, $table, $shref, $whref) = @_;
625 0 my($err, $query);
626  
627  
628 0 $self->_sql_quote_hash($shref);
629 0 ▶ $self->_sql_quote_hash($whref) if defined $whref && ref $whref;
Branch: 1 branch
BranchTrueFalseTotal
if defined $whref and ref $whref 0 0 0
Condition0%0/3
defined $whref and ref $whref
!ll&&!rl&&r
000
MC/DC0%0/2
defined $whref and ref $whref
defined $whref ref $whref
Truth table0%0/3
A and B
A= defined $whrefB= ref $whref
ABresult
0X0
100
111
630  
631 0 $query = $self->_sql_update_query($table,$shref,$whref);
632 0 $self->do($query);
633 0 $err = $self->Error();
634  
635 0 ▶ if ($err)
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
636 {
637 0 Carp::confess ("SqlObject update error\n",
638 "Query: $query\nError: $err")
639 }
640 }
641  
642  
643 ## Here be the private methods
644  
645 sub _sql_quote_hash
646 0 ▶{
Subroutine _sql_quote_hash: not called CC=5 SCAR=34.0
647 0 my $href = pop;
648  
649 0 ▶ while ( my ($k,$v) = each %$href)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$href) 0 0 0
650 {
651 0 $v =~ s|^'||;
652 0 $v =~ s|'$||;
653 0 $v =~ s|'|''|g;
654 0 ▶ $v = qq('') if $v=~/^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /^\s*$/ 0 0 0
655 0 ▶ $v = qq('$v') if $v=~/[^0-9]/ and $v!~/^null$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /[^0-9]/ and not $v =~ /^null$/i 0 0 0
Condition0%0/3
$v =~ /[^0-9]/ and not $v =~ /^null$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$v =~ /[^0-9]/ and not $v =~ /^null$/i
$v =~ /[^0-9]/ not $v =~ /^null$/i
Truth table0%0/3
A and not(B)
A= $v =~ /[^0-9]/B= not $v =~ /^null$/i
ABresult
0X0
100
111
656 0 $href->{$k}=$v;
657 }
658 }
659  
660 sub _sql_insert_query
661 0 ▶{
Subroutine _sql_insert_query: not called CC=3 SCAR=24.8
662 0 ▶ return unless @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 2 0 0 0
663  
664 0 my ($self, $table, $href) = @_;
665  
666 0 my ($columns,$values);
667 0 ▶ while (my ($k,$v)=each %$href)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$href) 0 0 0
668 {
669 0 $columns .= "$k,";
670 0 $values .= "$v,";
671 }
672 0 $columns =~ s|,$||;
673 0 $values =~ s|,$||;
674  
675 0 return qq(insert into $table ($columns) values($values));
676 }
677  
678 0 ▶sub _sql_select_query {
Subroutine _sql_select_query: not called CC=3 SCAR=24.8
679 0 ▶ return unless @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 2 0 0 0
680  
681 0 my ($self,$table,$aref,$href) = @_;
682 0 my $columns = join ',',@$aref;
683 0 my $where;
684  
685 0 ▶ while (my ($k,$v)=each %$href)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$href) 0 0 0
686 {
687 0 $where .= qq($k = $v and );
688 }
689 0 $where =~ s|\s*and\s*$||;
690  
691 0 return qq(select $columns from $table where $where);
692 }
693  
694 sub _sql_delete_query
695 0 ▶{
Subroutine _sql_delete_query: not called CC=4 SCAR=30.0
696 0 ▶ return unless @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 0 0
697  
698 0 my ($self, $table, $href) = @_;
699 0 ▶ return "delete from $table" if $href == undef;
Branch: 1 branch
BranchTrueFalseTotal
if $href == undef 0 0 0
700  
701 0 my $where;
702 0 ▶ while (my ($k,$v)=each %$href) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$href) 0 0 0
703 0 $where .= qq($k = $v and );
704 }
705 0 $where =~ s|\s*and\s*$||;
706  
707 0 return qq(delete from $table where $where);
708 }
709  
710 0 ▶sub _sql_update_query {
Subroutine _sql_update_query: not called CC=5 SCAR=34.0
711 0 ▶ return unless @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 2 0 0 0
712  
713 0 my ($self, $table, $shref, $whref) = @_;
714  
715 0 my ($set,$where);
716 0 ▶ while (my ($k,$v)=each %$shref)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$shref) 0 0 0
717 {
718 0 $set .= qq($k = $v, );
719 }
720 0 $set =~ s|\s*,\s*$||;
721  
722 0 ▶ if (ref $whref)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $whref) 0 0 0
723 {
724 0 ▶ while (my ($k,$v)=each %$whref)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$whref) 0 0 0
725 {
726 0 $where .= qq($k = $v and );
727 }
728 0 $where=~ s|\s*and\s*$||;
729 0 $where = "where $where";
730 }
731  
732 0 return qq(update $table set $set $where);
733 }
734  
735 3 our $AUTOLOAD;
736 sub AUTOLOAD
737 0 ▶{
Subroutine AUTOLOAD: not called CC=6 SCAR=37.4
738 0 $AUTOLOAD =~ /::([a-zA-Z_][a-zA-Z_0-9]+)$/;
739 my $func = $1 or do
740 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
my $func = $1 or do { my(@caller) = caller; die "Database Handle unable to dispatch.\n", "Method $SQL::SqlObject::AUTOLOAD called by $caller[1] line $caller[2].\n" } 0 0 0
741 0 my @caller = caller;
742 0 die (qq[Database Handle unable to dispatch.\n],
743 qq[Method $AUTOLOAD called by $caller[1] line $caller[2].\n]);
744 };
745  
746 0 my $self = shift;
747 0 my $result;
748  
749 0 ▶ if (my $dbh = eval { $self->dbh })
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbh = eval { do { $self->dbh } }) 0 0 0
750 {
751 0 $result = eval { $dbh->$func(@_) };
752 }
753  
754 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
755 {
756 # error while calling DBI function
757 0 my @caller = caller;
758 0 ▶ my $err = ref $_[0] && UNIVERSAL::isa($_[0],__PACKAGE__)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
Condition0%0/3
ref $_[0] and UNIVERSAL::isa($_[0], __PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and UNIVERSAL::isa($_[0], __PACKAGE__)
ref $_[0] UNIVERSAL::isa($_[0], __PACKAGE__)
Truth table0%0/3
A and B
A= ref $_[0]B= UNIVERSAL::isa($_[0], __PACKAGE__)
ABresult
0X0
100
111
759 ? $_[0]->Error
760 : 'no object';
761 0 die (qq[Database Handle encountered an error executing $func.\n],
762 qq[$AUTOLOAD called by $caller[1] line $caller[2].\n],
763 qq[Error: $@.\n],
764 qq[DBI Error: $err.\n]);
765 }
766  
767 0 return $result;
768 }
769  
770 3 1;
771  
772 __END__
↑ Summary SqlObject/ODBC.pm »