Simple.pm

filter: Statementstmt 11.6% 36 / 309 Branchbran 0.0% 0 / 184 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 9.5% 7 / 73 Podpod 100% 17 / 17 totaltotal 9.2% 60 / 646 CC 111 SCAR 36.8
CC
111
Cov
7%
text
210.0
connect
110.0
objects
90.0
CRAP 39.7
↑ Summary Simple/Result/RowObject.pm »
1 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 $DBIx::Simple::VERSION = '1.37';
7 $Carp::Internal{$_} = 1
8 2 for qw(DBIx::Simple DBIx::Simple::Result DBIx::Simple::DeadObject);
9  
10 2 my $no_raiseerror = $ENV{PERL_DBIX_SIMPLE_NO_RAISEERROR};
11  
12 2 my $quoted = qr/(?:'[^']*'|"[^"]*")*/; # 'foo''bar' simply matches the (?:) twice
13 2 my $quoted_mysql = qr/(?:(?:[^\\']*(?:\\.[^\\']*)*)'|"(?:[^\\"]*(?:\\.[^\\"]*)*)")*/;
14  
15 2 my %statements; # "$db" => { "$st" => $st, ... }
16 my %old_statements; # "$db" => [ [ $query, $st ], ... ]
17 2 my %keep_statements; # "$db" => $int
18  
19 2 my $err_message = '%s no longer usable (because of %%s)';
20 2 my $err_cause = '%s at %s line %d';
21  
22 package DBIx::Simple;
23  
24 ### private helper subs
25  
26 0 ▶sub _dummy { bless \my $dummy, 'DBIx::Simple::Dummy' }
Subroutine _dummy: not called CC=1 SCAR=6.9
27 1 ▶sub _swap {
Subroutine _swap: called CC=1 SCAR=0.0
28 1 my ($hash1, $hash2) = @_;
29 1 my $tempref = ref $hash1;
30 1 my $temphash = { %$hash1 };
31 1 %$hash1 = %$hash2;
32 1 bless $hash1, ref $hash2;
33 1 %$hash2 = %$temphash;
34 1 bless $hash2, $tempref;
35 }
36  
37 ### constructor
38  
39 0 ▶sub connect {
Subroutine connect: not called CC=10 SCAR=47.0
Pod documented
40 0 my ($class, @arguments) = @_;
41 0 my $self = { lc_columns => 1, result_class => 'DBIx::Simple::Result' };
42 0 ▶ if (defined $arguments[0] and UNIVERSAL::isa($arguments[0], 'DBI::db')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arguments[0] and UNIVERSAL::isa($arguments[0], 'DBI::db')) { } 0 0 0
Condition0%0/3
defined $arguments[0] and UNIVERSAL::isa($arguments[0], "DBI::db")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $arguments[0] and UNIVERSAL::isa($arguments[0], "DBI::db")
defined $arguments[0] UNIVERSAL::isa($arguments[0], "DBI::db")
Truth table0%0/3
A and B
A= defined $arguments[0]B= UNIVERSAL::isa($arguments[0], "DBI::db")
ABresult
0X0
100
111
43 0 $self->{dont_disconnect} = 1;
44 0 $self->{dbh} = shift @arguments;
45 0 ▶ Carp::carp("Additional arguments for $class->connect are ignored")
Branch: 1 branch
BranchTrueFalseTotal
if @arguments 0 0 0
46 if @arguments;
47 } else {
48 $arguments[3]->{PrintError} = 0
49 0 ▶ unless defined $arguments[3] and exists $arguments[3]{PrintError};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arguments[3] and exists $arguments[3]{"PrintError"} 0 0 0
Condition0%0/3
defined $arguments[3] and exists $arguments[3]{"PrintError"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $arguments[3] and exists $arguments[3]{"PrintError"}
defined $arguments[3] exists $arguments[3]{"PrintError"}
Truth table0%0/3
A and B
A= defined $arguments[3]B= exists $arguments[3]{"PrintError"}
ABresult
0X0
100
111
50 $arguments[3]->{RaiseError} = 1
51 unless $no_raiseerror
52 0 ▶ or defined $arguments[3] and exists $arguments[3]{RaiseError};
Branch: 1 branch
BranchTrueFalseTotal
unless $no_raiseerror or defined $arguments[3] and exists $arguments[3]{"RaiseError"} 0 0 0
Condition0%0/6
$no_raiseerror or defined $arguments[3] and exists $arguments[3]{"RaiseError"}
l!l&&r!l&&!r
000
defined $arguments[3] and exists $arguments[3]{"RaiseError"}
!ll&&!rl&&r
000
MC/DC0%0/3
$no_raiseerror or defined $arguments[3] and exists $arguments[3]{"RaiseError"}
$no_raiseerror defined $arguments[3] exists $arguments[3]{"RaiseError"}
Truth table0%0/4
A or B and C
A= $no_raiseerrorB= defined $arguments[3]C= exists $arguments[3]{"RaiseError"}
ABCresult
1XX1
0111
00X0
0100
53 0 $self->{dbh} = DBI->connect(@arguments);
54 }
55  
56 0 ▶ return undef unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 0 0 0
57  
58 0 $self->{dbd} = $self->{dbh}->{Driver}->{Name};
59 0 bless $self, $class;
60  
61 0 $statements{$self} = {};
62 0 $old_statements{$self} = [];
63 0 $keep_statements{$self} = 16;
64  
65 0 return $self;
66 }
67  
68 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
69 0 my ($class) = shift;
70 0 $class->connect(@_);
71 }
72  
73 ### properties
74  
75 0 ▶sub keep_statements : lvalue { $keep_statements{ $_[0] } }
Subroutine keep_statements: not called CC=1 SCAR=6.9
Pod documented
76 0 ▶sub lc_columns : lvalue { $_[0]->{lc_columns} }
Subroutine lc_columns: not called CC=1 SCAR=6.9
Pod documented
77 0 ▶sub result_class : lvalue { $_[0]->{result_class} }
Subroutine result_class: not called CC=1 SCAR=6.9
Pod documented
78  
79 0 ▶sub abstract : lvalue {
Subroutine abstract: not called CC=2 SCAR=17.9
Pod documented
80 0 require SQL::Abstract;
81 0 ▶ $_[0]->{abstract} ||= SQL::Abstract->new;
Condition0%0/3
$_[0]{"abstract"} ||= "SQL::Abstract"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"abstract"} ||= "SQL::Abstract"->new
$_[0]{"abstract"} "SQL::Abstract"->new
Truth table0%0/3
A ||= B
A= $_[0]{"abstract"}B= "SQL::Abstract"->new
ABresult
1X1
011
000
82 }
83  
84 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
85 0 my ($self) = @_;
86 0 ▶ return 'DBI error: ' . (ref $self ? $self->{dbh}->errstr : $DBI::errstr);
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
87 }
88  
89 0 ▶sub dbh { $_[0]->{dbh} }
Subroutine dbh: not called CC=1 SCAR=6.9
Pod documented
90  
91 ### private methods
92  
93 # Replace (??) with (?, ?, ?, ...)
94 0 ▶sub _replace_omniholder {
Subroutine _replace_omniholder: not called CC=5 SCAR=34.0
95 0 my ($self, $query, $binds) = @_;
96 0 ▶ return if $$query !~ /\(\?\?\)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $$query =~ /\(\?\?\)/ 0 0 0
97 0 my $omniholders = 0;
98 0 ▶ my $q = $self->{dbd} =~ /mysql/ ? $quoted_mysql : $quoted;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'dbd'} =~ /mysql/ ? : 0 0 0
99 0 $$query =~ s[($q|\(\?\?\))] {
100 $1 eq '(??)'
101 0 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '(??)' ? : 0 0 0
102 0 ▶ Carp::croak('There can be only one omniholder')
Branch: 1 branch
BranchTrueFalseTotal
if $omniholders++ 0 0 0
103 if $omniholders++;
104 0 '(' . join(', ', ('?') x @$binds) . ')'
105 }
106 : $1
107 }eg;
108 }
109  
110 # Invalidate and clean up
111 0 ▶sub _die {
Subroutine _die: not called CC=6 SCAR=37.4
112 0 my ($self, $cause) = @_;
113  
114 defined and $_->_die($cause, 0)
115 0 for values %{ $statements{$self} },
116 0 ▶ map $$_[1], @{ $old_statements{$self} };
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and $_->_die($cause, 0) 0 0 0
117 0 delete $statements{$self};
118 0 delete $old_statements{$self};
119 0 delete $keep_statements{$self};
120  
121 0 ▶ unless ($self->{dont_disconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dont_disconnect"}) 0 0 0
122 # Conditional, because destruction order is not guaranteed
123 # during global destruction.
124 0 ▶ $self->{dbh}->disconnect() if defined $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"dbh"} 0 0 0
125 }
126  
127 _swap(
128 0 ▶ $self,
Branch: 1 branch
BranchTrueFalseTotal
unless $cause =~ /DESTROY/ 0 0 0
129 bless {
130 what => 'Database object',
131 cause => $cause
132 }, 'DBIx::Simple::DeadObject'
133 ) unless $cause =~ /DESTROY/; # Let's not cause infinite loops :)
134 }
135  
136 ### public methods
137  
138 0 ▶sub query {
Subroutine query: not called CC=6 SCAR=37.4
Pod documented
139 0 my ($self, $query, @binds) = @_;
140 0 $self->{success} = 0;
141  
142 0 $self->_replace_omniholder(\$query, \@binds);
143  
144 0 my $st;
145 my $sth;
146  
147 0 my $old = $old_statements{$self};
148  
149 0 ▶ if (defined( my $i = (grep $old->[$_][0] eq $query, 0..$#$old)[0] )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $i = (grep(($old->[$_][0] eq $query), 0 .. $old->$#*))[0])) { } 0 0 0
150 0 $st = splice(@$old, $i, 1)->[1];
151 0 $sth = $st->{sth};
152 } else {
153 0 ▶ eval { $sth = $self->{dbh}->prepare($query) } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $sth = $self->{"dbh"}->prepare($query) } } or do { if ($@) { $@ =~ s/ at \S+ line \d+\.\n\z//; Carp::croak($@); } ; $self->{"reason"} = "Prepare failed ($DBI::errstr)"; return _dummy() } 0 0 0
154 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
155 0 $@ =~ s/ at \S+ line \d+\.\n\z//;
156 0 Carp::croak($@);
157 }
158 0 $self->{reason} = "Prepare failed ($DBI::errstr)";
159 0 return _dummy;
160 };
161  
162 # $self is quoted on purpose, to pass along the stringified version,
163 # and avoid increasing reference count.
164 0 $st = bless {
165 db => "$self",
166 sth => $sth,
167 query => $query
168 }, 'DBIx::Simple::Statement';
169 0 $statements{$self}{$st} = $st;
170 }
171  
172 0 ▶ eval { $sth->execute(@binds) } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $sth->execute(@binds) } } or do { if ($@) { $@ =~ s/ at \S+ line \d+\.\n\z//; Carp::croak($@); } ; $self->{"reason"} = "Execute failed ($DBI::errstr)"; return _dummy() } 0 0 0
173 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
174 0 $@ =~ s/ at \S+ line \d+\.\n\z//;
175 0 Carp::croak($@);
176 }
177  
178 0 $self->{reason} = "Execute failed ($DBI::errstr)";
179 0 return _dummy;
180 };
181  
182 0 $self->{success} = 1;
183  
184 0 return bless { st => $st, lc_columns => $self->{lc_columns} }, $self->{result_class};
185 }
186  
187 0 ▶sub begin_work { $_[0]->{dbh}->begin_work }
Subroutine begin_work: not called CC=1 SCAR=6.9
Pod documented
188 0 ▶sub begin { $_[0]->begin_work }
Subroutine begin: not called CC=1 SCAR=6.9
Pod documented
189 0 ▶sub commit { $_[0]->{dbh}->commit }
Subroutine commit: not called CC=1 SCAR=6.9
Pod documented
190 0 ▶sub rollback { $_[0]->{dbh}->rollback }
Subroutine rollback: not called CC=1 SCAR=6.9
Pod documented
191 0 ▶sub func { shift->{dbh}->func(@_) }
Subroutine func: not called CC=1 SCAR=6.9
Pod documented
192  
193 0 ▶sub last_insert_id {
Subroutine last_insert_id: not called CC=3 SCAR=24.8
Pod documented
194 0 my ($self) = @_;
195  
196 0 ▶ ($self->{dbi_version} ||= DBI->VERSION) >= 1.38 or Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
($self->{"dbi_version"} ||= "DBI"->VERSION) >= "1.38" or Carp::croak('DBI v1.38 required for last_insert_id' . qq[--this is only $self->{"dbi_version"}, stopped]) 0 0 0
Condition0%0/3
$self->{"dbi_version"} ||= "DBI"->VERSION
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"dbi_version"} ||= "DBI"->VERSION
$self->{"dbi_version"} "DBI"->VERSION
Truth table0%0/3
A ||= B
A= $self->{"dbi_version"}B= "DBI"->VERSION
ABresult
1X1
011
000
197 "DBI v1.38 required for last_insert_id" .
198 "--this is only $self->{dbi_version}, stopped"
199 );
200  
201 0 return shift->{dbh}->last_insert_id(@_);
202 }
203  
204 0 ▶sub disconnect {
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
205 0 my ($self) = @_;
206 0 $self->_die(sprintf($err_cause, "$self->disconnect", (caller)[1, 2]));
207 0 return 1;
208 }
209  
210 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
211 0 my ($self) = @_;
212 0 $self->_die(sprintf($err_cause, "$self->DESTROY", (caller)[1, 2]));
213 }
214  
215 ### public methods wrapping SQL::Abstract
216  
217 2 for my $method (qw/select insert update delete/) {
218 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
219 0 ▶ *$method = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
220 0 my $self = shift;
221 0 return $self->query($self->abstract->$method(@_));
222 }
223 8 }
224  
225 ### public method wrapping SQL::Interp
226  
227 0 ▶sub iquery {
Subroutine iquery: not called CC=1 SCAR=6.9
Pod documented
228 0 require SQL::Interp;
229 0 my $self = shift;
230 0 return $self->query( SQL::Interp::sql_interp(@_) );
231 }
232  
233 package DBIx::Simple::Dummy;
234  
235 use overload
236 0 ▶ '""' => sub { shift },
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine __ANON__: not called CC=1 SCAR=6.9
237 0 ▶ bool => sub { 0 };
Subroutine __ANON__: not called CC=1 SCAR=0.2
238  
239 0 ▶sub new { bless \my $dummy, shift }
Subroutine new: not called CC=1 SCAR=6.9
240 0 ▶sub AUTOLOAD { return }
Subroutine AUTOLOAD: not called CC=1 SCAR=6.9
241  
242 package DBIx::Simple::DeadObject;
243  
244 0 ▶sub _die {
Subroutine _die: not called CC=1 SCAR=6.9
245 0 my ($self) = @_;
246 Carp::croak(
247 sprintf(
248 "(This should NEVER happen!) " .
249 sprintf($err_message, $self->{what}),
250 $self->{cause}
251 )
252 0 );
253 }
254  
255 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=1 SCAR=6.9
256 0 my ($self) = @_;
257 Carp::croak(
258 sprintf(
259 sprintf($err_message, $self->{what}),
260 $self->{cause}
261 )
262 0 );
263 }
264 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
265  
266 package DBIx::Simple::Statement;
267  
268 0 ▶sub _die {
Subroutine _die: not called CC=5 SCAR=34.0
269 0 my ($self, $cause, $save) = @_;
270  
271 0 ▶ $self->{sth}->finish() if defined $self->{sth};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"sth"} 0 0 0
272 0 $self->{dead} = 1;
273  
274 0 my $stringy_db = "$self->{db}";
275 0 my $stringy_self = "$self";
276  
277 0 my $foo = bless {
278 what => 'Statement object',
279 cause => $cause
280 }, 'DBIx::Simple::DeadObject';
281  
282 0 DBIx::Simple::_swap($self, $foo);
283  
284 0 my $old = $old_statements{ $foo->{db} };
285 0 my $keep = $keep_statements{ $foo->{db} };
286  
287 0 ▶ if ($save and $keep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save and $keep) 0 0 0
Condition0%0/3
$save and $keep
!ll&&!rl&&r
000
MC/DC0%0/2
$save and $keep
$save $keep
Truth table0%0/3
A and B
A= $saveB= $keep
ABresult
0X0
100
111
288 0 $foo->{dead} = 0;
289 0 ▶ shift @$old until @$old + 1 <= $keep;
Branch: 1 branch
BranchTrueFalseTotal
@$old + 1 <= $keep or do { shift @$old } 0 0 0
290 0 push @$old, [ $foo->{query}, $foo ];
291 }
292  
293 0 delete $statements{ $stringy_db }{ $stringy_self };
294 }
295  
296 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
297 # This better only happen during global destruction...
298 0 ▶ return if $_[0]->{dead};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"dead"} 0 0 0
299 0 $_[0]->_die('Ehm', 0);
300 }
301  
302 package DBIx::Simple::Result;
303  
304 0 ▶sub _die {
Subroutine _die: not called CC=2 SCAR=17.9
305 0 my ($self, $cause) = @_;
306 0 ▶ if ($cause) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cause) { } 0 0 0
307 0 $self->{st}->_die($cause, 1);
308 0 DBIx::Simple::_swap(
309 $self,
310 bless {
311 what => 'Result object',
312 cause => $cause,
313 }, 'DBIx::Simple::DeadObject'
314 );
315 } else {
316 0 $cause = $self->{st}->{cause};
317 0 DBIx::Simple::_swap(
318 $self,
319 bless {
320 what => 'Result object',
321 cause => $cause
322 }, 'DBIx::Simple::DeadObject'
323 );
324 Carp::croak(
325 sprintf(
326 0 sprintf($err_message, $self->{what}),
327 $cause
328 )
329 );
330 }
331 }
332  
333 0 ▶sub func { shift->{st}->{sth}->func(@_) }
Subroutine func: not called CC=1 SCAR=6.9
334 0 ▶sub attr { my $dummy = $_[0]->{st}->{sth}->{$_[1]} }
Subroutine attr: not called CC=1 SCAR=6.9
335  
336 0 ▶sub columns {
Subroutine columns: not called CC=4 SCAR=30.0
337 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
338 0 ▶ my $c = $_[0]->{st}->{sth}->{ $_[0]->{lc_columns} ? 'NAME_lc' : 'NAME' };
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'lc_columns'} ? : 0 0 0
339 0 ▶ return wantarray ? @$c : $c;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
340 }
341  
342 0 ▶sub bind {
Subroutine bind: not called CC=2 SCAR=17.9
343 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
344 0 $_[0]->{st}->{sth}->bind_columns(\@_[1..$#_]);
345 }
346  
347  
348 ### Single
349  
350 0 ▶sub fetch {
Subroutine fetch: not called CC=2 SCAR=17.9
351 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
352 0 return $_[0]->{st}->{sth}->fetch;
353 }
354  
355 0 ▶sub into {
Subroutine into: not called CC=3 SCAR=24.8
356 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
357 0 my $sth = $_[0]->{st}->{sth};
358 0 ▶ $sth->bind_columns(\@_[1..$#_]) if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
359 0 return $sth->fetch;
360 }
361  
362 0 ▶sub list {
Subroutine list: not called CC=3 SCAR=24.8
363 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
364 0 ▶ return $_[0]->{st}->{sth}->fetchrow_array if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
365 0 return($_[0]->{st}->{sth}->fetchrow_array)[-1];
366 }
367  
368 0 ▶sub array {
Subroutine array: not called CC=3 SCAR=24.8
369 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
370 0 ▶ my $row = $_[0]->{st}->{sth}->fetchrow_arrayref or return;
Branch: 1 branch
BranchTrueFalseTotal
my $row = $_[0]{"st"}{"sth"}->fetchrow_arrayref or return 0 0 0
371 0 return [ @$row ];
372 }
373  
374 0 ▶sub hash {
Subroutine hash: not called CC=3 SCAR=24.8
375 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
376 return $_[0]->{st}->{sth}->fetchrow_hashref(
377 0 ▶ $_[0]->{lc_columns} ? 'NAME_lc' : 'NAME'
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'lc_columns'} ? : 0 0 0
378 );
379 }
380  
381 0 ▶sub kv_list {
Subroutine kv_list: not called CC=5 SCAR=34.0
382 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
383 0 my @keys = $_[0]->columns;
384 0 ▶ my $values = $_[0]->array or return;
Branch: 1 branch
BranchTrueFalseTotal
my $values = $_[0]->array or return 0 0 0
385 0 ▶ Carp::croak("Different numbers of column names and values")
Branch: 1 branch
BranchTrueFalseTotal
if @keys != @$values 0 0 0
386 if @keys != @$values;
387 0 ▶ return map { $keys[$_], $values->[$_] } 0 .. $#keys if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
388 0 return [ map { $keys[$_], $values->[$_] } 0 .. $#keys ];
389 }
390  
391 0 ▶sub kv_array {
Subroutine kv_array: not called CC=2 SCAR=17.9
392 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
393 0 scalar shift->kv_list(@_);
394 }
395  
396 0 ▶sub object {
Subroutine object: not called CC=7 SCAR=40.3
397 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
398 0 my $self = shift;
399 0 ▶ my $class = shift || ':RowObject';
Condition0%0/2
shift() || ':RowObject'
l!l
00
MC/DC0%0/1
shift() || ':RowObject'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
400 0 $class =~ s/^:/DBIx::Simple::Result::/;
401 0 ▶ if (not $class->can('new_from_dbix_simple') || $class->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("new_from_dbix_simple") or $class->can("new")) 0 0 0
Condition0%0/3
$class->can("new_from_dbix_simple") or $class->can("new")
l!l&&r!l&&!r
000
MC/DC0%0/2
$class->can("new_from_dbix_simple") or $class->can("new")
$class->can("new_from_dbix_simple") $class->can("new")
Truth table0%0/3
A or B
A= $class->can("new_from_dbix_simple")B= $class->can("new")
ABresult
1X1
011
000
402 0 (my $filename = "$class.pm") =~ s[::][/]g;
403 0 require $filename;
404 }
405 0 ▶ if ($class->can('new_from_dbix_simple')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("new_from_dbix_simple")) 0 0 0
406 0 return scalar $class->new_from_dbix_simple($self, @_);
407 }
408 0 ▶ if ($class->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("new")) 0 0 0
409 0 return $class->new( $self->kv_list );
410 }
411 Carp::croak(
412 0 qq(Can't locate object method "new_from_dbix_simple" or "new" ) .
413 qq(via package "$class".)
414 );
415 }
416  
417 ### Slurp
418  
419 0 ▶sub flat {
Subroutine flat: not called CC=3 SCAR=24.8
420 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
421 0 ▶ return map @$_, $_[0]->arrays if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
422 0 return [ map @$_, $_[0]->arrays ];
423 }
424  
425 0 ▶sub arrays {
Subroutine arrays: not called CC=3 SCAR=24.8
426 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
427 0 ▶ return @{ $_[0]->{st}->{sth}->fetchall_arrayref } if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
428 0 return $_[0]->{st}->{sth}->fetchall_arrayref;
429 }
430  
431 0 ▶sub hashes {
Subroutine hashes: not called CC=4 SCAR=30.0
432 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
433 0 my @return;
434 my $dummy;
435 0 ▶ push @return, $dummy while $dummy = $_[0]->hash;
Branch: 1 branch
BranchTrueFalseTotal
$dummy = $_[0]->hash and do { push @return, $dummy } 0 0 0
436 0 ▶ return wantarray ? @return : \@return;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
437 }
438  
439 0 ▶sub kv_flat {
Subroutine kv_flat: not called CC=3 SCAR=24.8
440 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
441 0 ▶ return map @$_, $_[0]->kv_arrays if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
442 0 return [ map @$_, $_[0]->kv_arrays ];
443 }
444  
445 0 ▶sub kv_arrays {
Subroutine kv_arrays: not called CC=4 SCAR=30.0
446 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
447 0 my @return;
448 my $dummy;
449 0 ▶ push @return, $dummy while $dummy = $_[0]->kv_array;
Branch: 1 branch
BranchTrueFalseTotal
$dummy = $_[0]->kv_array and do { push @return, $dummy } 0 0 0
450 0 ▶ return wantarray ? @return : \@return;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
451 }
452  
453 0 ▶sub objects {
Subroutine objects: not called CC=9 SCAR=45.0
454 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
455 0 my $self = shift;
456 0 ▶ my $class = shift || ':RowObject';
Condition0%0/2
shift() || ':RowObject'
l!l
00
MC/DC0%0/1
shift() || ':RowObject'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
457 0 $class =~ s/^:/DBIx::Simple::Result::/;
458 0 ▶ if (not $class->can('new_from_dbix_simple') || $class->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("new_from_dbix_simple") or $class->can("new")) 0 0 0
Condition0%0/3
$class->can("new_from_dbix_simple") or $class->can("new")
l!l&&r!l&&!r
000
MC/DC0%0/2
$class->can("new_from_dbix_simple") or $class->can("new")
$class->can("new_from_dbix_simple") $class->can("new")
Truth table0%0/3
A or B
A= $class->can("new_from_dbix_simple")B= $class->can("new")
ABresult
1X1
011
000
459 0 (my $package = "$class.pm") =~ s[::][/]g;
460 0 require $package;
461 }
462 0 ▶ if ($class->can('new_from_dbix_simple')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("new_from_dbix_simple")) 0 0 0
463 0 ▶ return $class->new_from_dbix_simple($self, @_) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
464 0 return [ $class->new_from_dbix_simple($self, @_) ];
465 }
466 0 ▶ if ($class->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("new")) 0 0 0
467 0 ▶ return map { $class->new( @$_ ) } $self->kv_arrays if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
468 0 return [ map { $class->new( @$_ ) } $self->kv_arrays ];
469 }
470 Carp::croak(
471 0 qq(Can't locate object method "new_from_dbix_simple" or "new" ) .
472 qq(via package "$class" (perhaps you forgot to load "$class"?))
473 );
474 }
475  
476 0 ▶sub _map {
Subroutine _map: not called CC=2 SCAR=17.9
477 0 my ($keys, $values) = @_;
478 0 my %return;
479 0 @return{@$keys} = @$values;
480 0 ▶ return wantarray ? %return : \%return;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
481 }
482  
483 0 ▶sub _group {
Subroutine _group: not called CC=3 SCAR=24.8
484 0 my ($keys, $values) = @_;
485 0 my %return;
486 0 ▶ push @{ $return{shift @$keys} }, shift @$values while @$values;
Branch: 1 branch
BranchTrueFalseTotal
@$values and do { push @{$return{shift @$keys};}, shift @$values } 0 0 0
487 0 ▶ return wantarray ? %return : \%return;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
488 }
489  
490 0 ▶sub _keys_and_hashes {
Subroutine _keys_and_hashes: not called CC=4 SCAR=30.0
491 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
492 0 my ($self, $keyname) = @_;
493 0 ▶ Carp::croak('Key column name not optional') if not defined $keyname;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keyname 0 0 0
494 0 my @rows = $self->hashes;
495 0 my @keys;
496 0 push @keys, delete $_->{$keyname} for @rows;
497 0 return \@keys, \@rows;
498 }
499  
500 0 ▶sub _keys_and_arrays {
Subroutine _keys_and_arrays: not called CC=3 SCAR=24.8
501 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
502 0 my ($self, $keyindex) = @_;
503 0 $keyindex += 0;
504 0 my @rows = $self->arrays;
505 0 my @keys;
506 0 push @keys, splice @$_, $keyindex, 1 for @rows;
507 0 return \@keys, \@rows;
508 }
509  
510 0 ▶sub group_hashes { return _group shift->_keys_and_hashes(@_) }
Subroutine group_hashes: not called CC=1 SCAR=6.9
511 0 ▶sub map_hashes { return _map shift->_keys_and_hashes(@_) }
Subroutine map_hashes: not called CC=1 SCAR=6.9
512 0 ▶sub group_arrays { return _group shift->_keys_and_arrays(@_) }
Subroutine group_arrays: not called CC=1 SCAR=6.9
513 0 ▶sub map_arrays { return _map shift->_keys_and_arrays(@_) }
Subroutine map_arrays: not called CC=1 SCAR=6.9
514  
515 0 ▶sub map {
Subroutine map: not called CC=3 SCAR=24.8
516 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
517 0 ▶ return map @$_, @{ $_[0]->{st}->{sth}->fetchall_arrayref } if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
518 0 return { map @$_, @{ $_[0]->{st}->{sth}->fetchall_arrayref } };
519 }
520  
521 0 ▶sub group {
Subroutine group: not called CC=4 SCAR=30.0
522 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
523 0 my %return;
524 0 ▶ while (my $row = $_[0]->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $_[0]->fetch) 0 0 0
525 0 push @{ $return{ $row->[0] } }, $row->[1];
526 }
527 0 ▶ return wantarray ? %return : \%return;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
528 }
529  
530 0 ▶sub rows {
Subroutine rows: not called CC=2 SCAR=17.9
531 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
532 0 $_[0]->{st}->{sth}->rows;
533 }
534  
535 0 ▶sub xto {
Subroutine xto: not called CC=3 SCAR=24.8
536 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
537 0 require DBIx::XHTML_Table;
538 0 my $self = shift;
539 0 ▶ my $attr = ref $_[0] ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
540  
541 # Old DBD::SQLite (.29) spits out garbage if done *after* fetching.
542 0 my $columns = $self->{st}->{sth}->{NAME};
543  
544 0 return DBIx::XHTML_Table->new(
545 scalar $self->arrays,
546 $columns,
547 $attr
548 );
549 }
550  
551 0 ▶sub html {
Subroutine html: not called CC=3 SCAR=24.8
552 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
553 0 my $self = shift;
554 0 ▶ my $attr = ref $_[0] ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
555 0 return $self->xto($attr)->output($attr);
556 }
557  
558 0 ▶sub text {
Subroutine text: not called CC=14 SCAR=53.5
559 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
560 0 my ($self, $type) = @_;
561 my $text_table = defined $type && length $type
562 ? 0
563 0 ▶ : eval { require Text::Table; $type = 'table'; 1 };
Branch: 1 branch
BranchTrueFalseTotal
defined $type && length $type ? : 0 0 0
Condition0%0/3
defined $type and length $type
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and length $type
defined $type length $type
Truth table0%0/3
A and B
A= defined $typeB= length $type
ABresult
0X0
100
111
564 0 ▶ $type ||= 'neat';
Condition0%0/2
$type ||= "neat"
l!l
00
MC/DC0%0/1
$type ||= "neat"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
565 0 ▶ if ($type eq 'box' or $type eq 'table') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "box" or $type eq "table") 0 0 0
Condition0%0/3
$type eq "box" or $type eq "table"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "box" or $type eq "table"
$type eq "box" $type eq "table"
Truth table0%0/3
A or B
A= $type eq "box"B= $type eq "table"
ABresult
1X1
011
000
566 0 my $box = $type eq 'box';
567 0 ▶ $text_table or require Text::Table;
Branch: 1 branch
BranchTrueFalseTotal
$text_table or require Text::Table 0 0 0
568 my @columns = map +{ title => $_, align_title => 'center' },
569 0 @{ $self->{st}->{sth}->{NAME} };
570 0 my $c = 0;
571 0 splice @columns, $_ + $c++, 0, \' | ' for 1 .. $#columns;
572 0 ▶ my $table = Text::Table->new(
Branch: 2 branches
BranchTrueFalseTotal
$box ? : 0 0 0
$box ? : 0 0 0
573 ($box ? \'| ' : ()),
574 @columns,
575 ($box ? \' |' : ())
576 );
577 0 $table->load($self->arrays);
578 0 my $rule = $table->rule(qw/- +/);
579 0 ▶ return join '',
Branch: 2 branches
BranchTrueFalseTotal
$box ? : 0 0 0
$box ? : 0 0 0
580 ($box ? $rule : ()),
581 $table->title, $rule, $table->body,
582 ($box ? $rule : ());
583 }
584 0 ▶ Carp::carp("Unknown type '$type'; using 'neat'") if $type ne 'neat';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "neat" 0 0 0
585 0 return join '', map DBI::neat_list($_) . "\n", $self->arrays;
586 }
587  
588 0 ▶sub finish {
Subroutine finish: not called CC=2 SCAR=17.9
589 0 ▶ $_[0]->_die if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
590 0 my ($self) = @_;
591 0 $self->_die(
592 sprintf($err_cause, "$self->finish", (caller)[1, 2])
593 );
594 }
595  
596 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
597 0 ▶ return if ref $_[0]->{st} eq 'DBIx::Simple::DeadObject';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"st"} eq "DBIx::Simple::DeadObject" 0 0 0
598 0 my ($self) = @_;
599 0 $self->_die(
600 sprintf($err_cause, "$self->DESTROY", (caller)[1, 2])
601 );
602 }
603  
604 2 1;
605  
606 __END__
↑ Summary Simple/Result/RowObject.pm »