blib/lib/DBIx/Cursor.pm

filter: Statementstmt 7.6% 14 / 182 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 20.0% 4 / 20 Podpod 100% 15 / 15 totaltotal 11.0% 33 / 300 CC 34 SCAR 39.1
CC
34
Cov
6%
update
156.0
new
42.0
fetch
42.0
CRAP 49.7
↑ Summary
1 ########################################################################
2 #
3 # Copyright (c) 2001,2002 by Tommi Mäkitalo
4 #
5 # This package is free software; you can redistribute it
6 # and/or modify it under the same terms as Perl itself.
7 #
8 ########################################################################
9  
10 package DBIx::Cursor;
11  
12 1 ▶use 5.6.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $VERSION = '0.14';
18  
19 1 my %cache;
20  
21 # ----------------------------------------------------------------------
22 sub new
23 0 ▶{
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
24 0 shift;
25 0 my $self = {};
26 0 my $dbh = shift;
27 0 my $table = shift;
28 0 my $pk = \@_;
29  
30 # read names and types of columns
31 0 ▶ unless ($cache{$table})
Branch: 1 branch
BranchTrueFalseTotal
if ($cache{$table}) { } 0 0 0
32 {
33 0 my $sth = $dbh->prepare("select * from $table where 0=1");
34 0 ▶ $sth->execute or croak('error fetching columns');
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or croak("error fetching columns") 0 0 0
35  
36 0 $self->{NAME} = $sth->{NAME};
37 0 $self->{TYPE} = $sth->{TYPE};
38 0 $self->{type} = {};
39 0 ▶ for (my $i = 0; $i < @{$self->{NAME}}; ++$i)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"NAME"};}) 0 0 0
40 {
41 0 $self->{type}{$self->{NAME}[$i]} = $self->{TYPE}[$i];
42 }
43  
44 # read primary-key if not given
45 0 ▶ unless ($pk)
Branch: 1 branch
BranchTrueFalseTotal
unless ($pk) 0 0 0
46 {
47 0 my @pk = $dbh->primary_key(undef, undef, $table);
48 0 ▶ if (@pk)
Branch: 1 branch
BranchTrueFalseTotal
if (@pk) { } 0 0 0
49 {
50 0 $pk = \@pk;
51 }
52 else
53 {
54 0 croak ('primary key not known');
55 }
56 }
57  
58 0 $self->{pk} = $pk;
59  
60 0 $cache{$table} = [$self->{NAME}, $self->{TYPE}, $self->{type}, $self->{pk}];
61 }
62 else
63 {
64 0 ($self->{NAME}, $self->{TYPE}, $self->{type}, $self->{pk}) = @{$cache{$table}};
65 }
66  
67 # initialize values
68 0 $self->{dbh} = $dbh;
69 0 $self->{table} = $table;
70  
71 # initialize data
72 0 $self->{data} = {};
73 0 $self->{olddata} = {};
74  
75 # return value
76 0 bless $self;
77 0 return $self;
78 }
79  
80 # ----------------------------------------------------------------------
81 sub DESTROY
82 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
83 0 my $self = shift;
84 0 $self->reset;
85 }
86  
87 # ----------------------------------------------------------------------
88 sub dbh
89 0 ▶{
Subroutine dbh: not called CC=1 SCAR=6.9
Pod documented
90 0 my $self = shift;
91 0 return $self->{dbh};
92 }
93  
94 # ----------------------------------------------------------------------
95 sub get_type
96 0 ▶{
Subroutine get_type: not called CC=1 SCAR=6.9
Pod documented
97 0 my $self = shift;
98 0 my $colname = shift;
99  
100 0 return $self->{type}{$colname} or croak ("column $colname not found");
101 }
102  
103 # ----------------------------------------------------------------------
104 sub get_columns
105 0 ▶{
Subroutine get_columns: not called CC=1 SCAR=6.9
Pod documented
106 0 my $self = shift;
107 0 return @{$self->{NAME}};
108 }
109  
110 # ----------------------------------------------------------------------
111 sub read
112 0 ▶{
Subroutine read: not called CC=4 SCAR=30.0
Pod documented
113 0 my $self = shift;
114 0 my @values = @_;
115  
116 0 my @pk = @{$self->{pk}};
117  
118 0 ▶ croak ('invalid number of parameters') unless @values == @pk;
Branch: 1 branch
BranchTrueFalseTotal
unless @values == @pk 0 0 0
119  
120 0 my $dbh = $self->{dbh};
121 0 my $table = $self->{table};
122  
123 0 my $sth = $self->{sth_select};
124 0 ▶ unless ($sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
125 {
126 my $sql = "select * from $table where "
127 0 . join(' and ', map { "$_ = ?" } @pk);
128  
129 0 $self->reset;
130 0 $sth = $self->{sth_select} = $dbh->prepare($sql);
131 }
132 0 $sth->execute(@values);
133  
134 0 ▶ if ($self->{data} = $sth->fetchrow_hashref)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'data'} = $sth->fetchrow_hashref) { } 0 0 0
135 {
136 0 %{$self->{olddata}} = %{$self->{data}};
137 0 return $self;
138 }
139 else
140 {
141 0 return undef;
142 }
143 }
144  
145 # ----------------------------------------------------------------------
146 sub set
147 0 ▶{
Subroutine set: not called CC=3 SCAR=24.8
Pod documented
148 0 my $self = shift;
149 0 my %newval = @_;
150  
151 0 ▶ while (my ($key, $value) = each (%newval) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %newval) 0 0 0
152 {
153 0 ▶ croak("column $key not found") unless $self->{type}{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"type"}{$key} 0 0 0
154 0 $self->{data}{$key} = $value;
155 }
156  
157 0 return $self;
158 }
159  
160 # ----------------------------------------------------------------------
161 sub get
162 0 ▶{
Subroutine get: not called CC=4 SCAR=30.0
Pod documented
163 0 my $self = shift;
164 0 my @columns = @_;
165  
166 0 ▶ if (@columns)
Branch: 2 branches
BranchTrueFalseTotal
if (@columns) { } 0 0 0
elsif (wantarray) { } 0 0 0
167 {
168 0 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
169 {
170 0 return map { $self->{data}{$_} } @columns;
171 }
172 else
173 {
174 0 return $self->{data}{$columns[0]};
175 }
176 }
177 elsif (wantarray)
178 {
179 0 return %{$self->{data}};
180 }
181 else
182 {
183 0 return $self->{data};
184 }
185 }
186  
187 # ----------------------------------------------------------------------
188 sub reset
189 0 ▶{
Subroutine reset: not called CC=2 SCAR=17.9
Pod documented
190 0 my $self = shift;
191  
192 0 $self->{data} = {};
193 0 $self->{olddata} = {};
194  
195 0 ▶ if ($self->{sth})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sth"}) 0 0 0
196 {
197 0 $self->{sth}->finish;
198 0 $self->{sth} = undef;
199 }
200  
201 0 return $self;
202 }
203  
204 # ----------------------------------------------------------------------
205 sub where
206 0 ▶{
Subroutine where: not called CC=1 SCAR=6.9
Pod documented
207 0 my $self = shift;
208  
209 0 $self->reset;
210 0 $self->{where} = shift;
211 0 $self->{values} = \@_;
212  
213 0 return $self;
214 }
215  
216 # ----------------------------------------------------------------------
217 sub values
218 0 ▶{
Subroutine values: not called CC=1 SCAR=6.9
Pod documented
219 0 my $self = shift;
220  
221 0 $self->reset;
222 0 $self->{values} = \@_;
223  
224 0 return $self;
225 }
226  
227 # ----------------------------------------------------------------------
228 sub fetch
229 0 ▶{
Subroutine fetch: not called CC=6 SCAR=37.4
Pod documented
230 0 my $self = shift;
231  
232 0 ▶ unless ($self->{sth})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sth"}) 0 0 0
233 {
234 0 my $table = $self->{table};
235 0 my $dbh = $self->{dbh};
236 0 my $sql = "select * from $table";
237 0 my $where = $self->{where};
238  
239 0 ▶ if ($where)
Branch: 1 branch
BranchTrueFalseTotal
if ($where) 0 0 0
240 {
241 0 ▶ $sql .= ' where' if ($where !~ /^\s*(where)|(order\s+by)\s/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $where =~ /^\s*(where)|(order\s+by)\s/i 0 0 0
242 0 $sql .= " $where";
243 }
244  
245 0 ▶ my @values = @{$self->{values}} if $self->{values};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"values"} 0 0 0
246 0 $self->{sth} = $dbh->prepare($sql);
247 0 $self->{sth}->execute(@values);
248 }
249  
250 0 ▶ if ($self->{data} = $self->{sth}->fetchrow_hashref)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'data'} = $self->{'sth'}->fetchrow_hashref) { } 0 0 0
251 {
252 0 %{$self->{olddata}} = %{$self->{data}};
253 0 return $self->get;
254 }
255 else
256 {
257 0 $self->reset;
258 0 return undef;
259 }
260 }
261  
262 # ----------------------------------------------------------------------
263 sub update
264 0 ▶{
Subroutine update: not called CC=12 SCAR=50.5
Pod documented
265 0 my $self = shift;
266  
267 0 my $data = $self->{data};
268 0 my $odata = $self->{olddata};
269 0 my $table = $self->{table};
270 0 my $dbh = $self->{dbh};
271 0 my $pk = $self->{pk};
272  
273 # the columns to update are either given as parameter or
274 # every not primary-key-column
275 my @updatecols = @_ ? @_
276 : grep { # seach columns
277 0 my $col = $_;
278 # return every column not in primary-key
279 0 ! grep { $col eq $_ } @$pk
280 0 ▶ } @{$self->{NAME}};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
281  
282 # check if update is needed
283 0 my @cols;
284 my @values;
285 0 foreach my $col (@updatecols)
286 {
287 0 my $d = $data->{$col};
288 0 my $o = $odata->{$col};
289 0 ▶ next unless defined $d && !defined $o
Branch: 1 branch
BranchTrueFalseTotal
unless defined $d and not defined $o or not defined $d and defined $o or defined $d and defined $o and $o ne $d 0 0 0
Condition0%0/18
defined $d and not defined $o or not defined $d and defined $o or defined $d and defined $o and $o ne $d
l!l&&r!l&&!r
000
defined $d and not defined $o or not defined $d and defined $o
l!l&&r!l&&!r
000
defined $d and not defined $o
!ll&&!rl&&r
000
not defined $d and defined $o
!ll&&!rl&&r
000
defined $d and defined $o and $o ne $d
!ll&&!rl&&r
000
defined $d and defined $o
!ll&&!rl&&r
000
MC/DC0%0/7
defined $d and not defined $o or not defined $d and defined $o or defined $d and defined $o and $o ne $d
defined $d not defined $o not defined $d defined $o defined $d defined $o $o ne $d
Truth table0%0/19
A and not(B) or not(C) and D or E and F and G
A= defined $dB= not defined $oC= not defined $dD= defined $oE= defined $dF= defined $oG= $o ne $d
ABCDEFGresult
11XXXXX1
0X11XXX1
1011XXX1
0X0X1111
0X101111
100X1111
10101111
0X0X0XX0
0X0X10X0
0X0X1100
0X100XX0
0X1010X0
0X101100
100X0XX0
100X10X0
100X1100
10100XX0
101010X0
10101100
290 || !defined $d && defined $o
291 || defined $d && defined $o && $o ne $d;
292 0 push @cols, $col;
293 0 push @values, $d;
294 }
295  
296 0 ▶ return unless @cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @cols 0 0 0
297  
298 my $sql = "update $table set "
299 0 . join(', ', map { "$_ = ?" } @cols )
300 . ' where '
301 0 . join(' and ', map { "$_ = ?" } @$pk);
302  
303 0 ▶ my @pkvalues = map { $odata->{$_} || $data->{$_} } @$pk;
Condition0%0/3
$odata->{$_} || $data->{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$odata->{$_} || $data->{$_}
$odata->{$_} $data->{$_}
Truth table0%0/3
A || B
A= $odata->{$_}B= $data->{$_}
ABresult
1X1
011
000
304  
305 0 my $sth = $dbh->prepare($sql);
306 0 my $ret = $sth->execute(@values, @pkvalues);
307 0 %{$self->{olddata}} = %{$self->{data}};
308 0 return $ret;
309 }
310  
311 # ----------------------------------------------------------------------
312 sub insert
313 0 ▶{
Subroutine insert: not called CC=2 SCAR=17.9
Pod documented
314 0 my $self = shift;
315 0 my $table = $self->{table};
316 0 my $dbh = $self->{dbh};
317  
318 0 my $sth = $self->{sth_insert};
319 0 ▶ unless ($sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
320 {
321 my $sql = "insert into $table values ("
322 0 . join(', ', ('?') x @{$self->{NAME}})
323 . ')';
324  
325 0 $sth = $self->{sth_insert} = $dbh->prepare($sql);
326 }
327  
328 0 my @values = map { $self->{data}{$_} } @{$self->{NAME}};
329  
330 0 return $sth->execute(@values);
331 }
332  
333 # ----------------------------------------------------------------------
334 sub replace
335 0 ▶{
Subroutine replace: not called CC=2 SCAR=17.9
Pod documented
336 0 my $self = shift;
337 0 ▶ return $self->update != 0 || $self->insert;
Condition0%0/3
$self->update != 0 || $self->insert
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->update != 0 || $self->insert
$self->update != 0 $self->insert
Truth table0%0/3
A || B
A= $self->update != 0B= $self->insert
ABresult
1X1
011
000
338 }
339  
340 # ----------------------------------------------------------------------
341 sub delete
342 0 ▶{
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
343 0 my $self = shift;
344  
345 0 my $data = $self->{data};
346 0 my $table = $self->{table};
347 0 my $dbh = $self->{dbh};
348 0 my $pk = $self->{pk};
349  
350 0 my $sth = $self->{sth_delete};
351 0 ▶ unless ($sth)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
352 {
353 my $sql = "delete from $table where "
354 0 . join(' and ', map { "$_ = ?" } @$pk);
355 0 $sth = $self->{sth_delete} = $dbh->prepare($sql);
356 }
357  
358 0 my @pkvalues = map { $data->{$_} } @$pk;
359  
360 0 $sth->execute(@pkvalues);
361 }
362  
363 1 1;
364  
365 __END__
↑ Summary