blib/lib/ORLite.pm

filter: Statementstmt 93.3% 349 / 374 Branchbran 79.7% 169 / 212 Conditioncond 64.1% 25 / 39 MC/DCmcdc 36.0% 9 / 25 Subroutinesub 94.4% 51 / 54 Podpod 50.0% 1 / 2 totaltotal 85.5% 604 / 706 CC 198 SCAR 33.8
CC
198
Cov
87%
import
121.1
iterate
4.0
iterate
4.0
CRAP 29.5
↑ Summary
1 package ORLite; # git description: v1.99-6-gde873f7
2  
3 # See POD at end of file for documentation
4  
5 32 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 29 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use File::Spec 0.80 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use File::Path 2.08 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 29 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 29 ▶use Params::Util 1.00 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 29 ▶use DBI 1.607 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 29 ▶use DBD::SQLite 1.27 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 27 our $VERSION = '2.00';
16  
17 # Support for the 'prune' option
18 27 my @PRUNE = ();
19 27 ▶END {
Subroutine END: called CC=3 SCAR=11.1
20 27 foreach ( reverse @PRUNE ) {
21 1 ▶ next unless -e $_;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $_ 0 1 1
22 1 require File::Remove;
23 1 File::Remove::remove( \1, $_ );
24 }
25 }
26  
27  
28  
29  
30  
31 #####################################################################
32 # Code Generation
33  
34 30 ▶sub import {
Subroutine import: called CC=96 SCAR=48.0
35 30 ▶ my $class = ref($_[0]) || $_[0];
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
0300
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
36  
37 # Check for debug mode
38 30 my $DEBUG = 0;
39 30 ▶ if ( defined Params::Util::_STRING($_[-1]) and $_[-1] eq '-DEBUG' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &Params::Util::_STRING($_[-1]) and $_[-1] eq "-DEBUG") 0 30 30
Condition66%2/3
defined &Params::Util::_STRING($_[-1]) and $_[-1] eq "-DEBUG"
!ll&&!rl&&r
2640
MC/DC0%0/2
defined &Params::Util::_STRING($_[-1]) and $_[-1] eq "-DEBUG"
defined &Params::Util::_STRING($_[-1]) $_[-1] eq "-DEBUG"
Truth table66%2/3
A and B
A= defined &Params::Util::_STRING($_[-1])B= $_[-1] eq "-DEBUG"
ABresult
0X0
100
111
40 0 $DEBUG = 1;
41 0 pop @_;
42 }
43  
44 # Check params and apply defaults
45 30 my %params = (
46 # Simple defaults here, complex defaults later
47 package => scalar(caller),
48 create => 0,
49 cleanup => '',
50 array => 0,
51 xsaccessor => 0,
52 shim => 0,
53 tables => 1,
54 views => 0,
55 unicode => 0,
56 );
57 30 ▶ if ( defined Params::Util::_STRING($_[1]) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined &Params::Util::_STRING($_[1])) { } 4 27 31
elsif (&Params::Util::_HASHLIKE($_[1])) { } 26 0 26
58 # Support the short form "use ORLite 'db.sqlite'"
59 4 $params{file} = $_[1];
60 } elsif ( Params::Util::_HASHLIKE($_[1]) ) {
61 26 %params = ( %params, %{$_[1]} );
62 } else {
63 0 Carp::croak("Missing, empty or invalid params HASH");
64 }
65 30 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Params::Util::_STRING($params{"file"}) and $params{"create"} || -f $params{"file"}) 0 30 30
Condition50%3/6
defined &Params::Util::_STRING($params{"file"}) and $params{"create"} || -f $params{"file"}
!ll&&!rl&&r
0030
$params{"create"} or -f $params{"file"}
l!l&&r!l&&!r
8220
MC/DC0%0/3
defined &Params::Util::_STRING($params{"file"}) and $params{"create"} || -f $params{"file"}
defined &Params::Util::_STRING($params{"file"}) $params{"create"} -f $params{"file"}
Truth table50%2/4
A and B or C
A= defined &Params::Util::_STRING($params{"file"})B= $params{"create"}C= -f $params{"file"}
ABCresult
0XX0
1000
11X1
1011
66 defined Params::Util::_STRING($params{file})
67 and (
68 $params{create}
69 or
70 -f $params{file}
71 )
72 ) {
73 0 Carp::croak("Missing or invalid file param");
74 }
75 30 ▶ unless ( defined $params{readonly} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"readonly"}) 28 2 30
76 28 ▶ $params{readonly} = $params{create} ? 0 : ! -w $params{file};
Branch: 1 branch
BranchTrueFalseTotal
$params{'create'} ? : 8 20 28
77 }
78 30 ▶ unless ( Params::Util::_CLASS($params{package}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Params::Util::_CLASS($params{"package"})) 0 30 30
79 0 Carp::croak("Missing or invalid package class");
80 }
81  
82 # Check caching params
83 30 my $cached = undef;
84 30 my $pkg = $params{package};
85 30 ▶ if ( defined $params{cache} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"cache"}) 2 28 30
86 # Caching is illogical or invalid in some situations
87 2 ▶ if ( $params{prune} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"prune"}) 0 2 2
88 0 Carp::croak("Cannot set a 'cache' directory while 'prune' enabled");
89 }
90 2 ▶ unless ( $params{user_version} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"user_version"}) 0 2 2
91 0 Carp::croak("Cannot set a 'cache' directory without 'user_version'");
92 }
93  
94 # To make the caching work, the version be defined before ORLite is called.
95 29 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
96 2 ▶ unless ( ${"$pkg\::VERSION"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (${"${pkg}::VERSION";}) 0 2 2
97 0 Carp::croak("Cannot set a 'cache' directory without a package \$VERSION");
98 }
99  
100 # Build the cache file from the super path using an inlined Class::ISA
101 2 my @queue = ( $class );
102 2 my %seen = ( $pkg => 1 );
103 2 my @parts = ( $pkg => ${"$pkg\::VERSION"} );
104 2 ▶ while ( @queue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 2 2 4
105 2 ▶ my $c = Params::Util::_STRING(shift @queue) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $c = &Params::Util::_STRING(shift @queue) or next 2 0 2
106 2 push @parts, $c => ${"$c\::VERSION"};
107 0 unshift @queue, grep { not $seen{$c}++ } @{"$c\::ISA"};
108 }
109 2 $cached = join '-', @parts, user_version => $params{user_version};
110 2 $cached =~ s/[:.-]+/-/g;
111 $cached = File::Spec->rel2abs(
112 2 File::Spec->catfile( $params{cache}, "$cached.pm" )
113 );
114 }
115  
116 # Create the parent directory if needed
117 30 my $file = File::Spec->rel2abs($params{file});
118 30 my $created = ! -f $params{file};
119 30 ▶ if ( $created ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($created) 6 24 30
120 6 my $dir = File::Basename::dirname($file);
121 6 ▶ unless ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 6 6
122 0 my @dirs = File::Path::mkpath( $dir, { verbose => 0 } );
123 0 ▶ $class->prune(@dirs) if $params{prune};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"prune"} 0 0 0
124 }
125 6 ▶ $class->prune($file) if $params{prune};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"prune"} 1 5 6
126 }
127  
128 # Connect to the database
129 30 my $dsn = "dbi:SQLite:$file";
130 my $dbh = DBI->connect( $dsn, undef, undef, {
131 PrintError => 0,
132 RaiseError => 1,
133 ReadOnly => $params{create} ? 0 : 1,
134 30 ▶ $params{unicode} ? ( sqlite_unicode => 1 ) : ( ),
Branch: 2 branches
BranchTrueFalseTotal
$params{'create'} ? : 8 22 30
$params{'unicode'} ? : 1 29 30
135 } );
136  
137 # Schema custom creation support
138 30 ▶ if ( $created and Params::Util::_CODELIKE($params{create}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($created and &Params::Util::_CODELIKE($params{"create"})) 2 28 30
Condition100%3/3
$created and &Params::Util::_CODELIKE($params{"create"})
!ll&&!rl&&r
2442
MC/DC100%2/2
$created and &Params::Util::_CODELIKE($params{"create"})
$created &Params::Util::_CODELIKE($params{"create"})
Truth table100%3/3
A and B
A= $createdB= &Params::Util::_CODELIKE($params{"create"})
ABresult
0X0
100
111
139 2 $params{create}->($dbh);
140 }
141  
142 # Check the schema version before generating
143 30 my $user_version = $dbh->selectrow_arrayref('pragma user_version')->[0];
144 29 ▶ if ( exists $params{user_version} and $user_version != $params{user_version} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"user_version"} and $user_version != $params{"user_version"}) 0 29 29
Condition66%2/3
exists $params{"user_version"} and $user_version != $params{"user_version"}
!ll&&!rl&&r
2360
MC/DC0%0/2
exists $params{"user_version"} and $user_version != $params{"user_version"}
exists $params{"user_version"} $user_version != $params{"user_version"}
Truth table66%2/3
A and B
A= exists $params{"user_version"}B= $user_version != $params{"user_version"}
ABresult
0X0
100
111
145 0 Carp::croak("Schema user_version mismatch (got $user_version, wanted $params{user_version})");
146 }
147  
148 # If caching and the cached version exists, load and shortcut.
149 # Don't try to catch exceptions, just let them blow up.
150 29 ▶ if ( $cached and -f $cached ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached and -f $cached) 1 28 29
Condition100%3/3
$cached and -f $cached
!ll&&!rl&&r
2711
MC/DC100%2/2
$cached and -f $cached
$cached -f $cached
Truth table100%3/3
A and B
A= $cachedB= -f $cached
ABresult
0X0
100
111
151 1 $dbh->disconnect;
152 1 require $cached;
153 1 return 1;
154 }
155  
156 # Prepare to generate code
157 28 my $cleanup = $params{cleanup};
158 28 ▶ my $readonly = $params{readonly} ? "\n\t\tReadOnly => 1," : '';
Branch: 1 branch
BranchTrueFalseTotal
$params{'readonly'} ? : 2 26 28
159 28 ▶ my $unicode = $params{unicode} ? "\n\t\tsqlite_unicode => 1," : '';
Branch: 1 branch
BranchTrueFalseTotal
$params{'unicode'} ? : 1 27 28
160 28 ▶ my $version = $unicode ? '5.008005' : '5.006';
Branch: 1 branch
BranchTrueFalseTotal
$unicode ? : 1 27 28
161  
162 # Generate the support package code
163 28 my $code = <<"END_PERL";
164 package $pkg;
165  
166 use $version;
167 use strict;
168 use Carp ();
169 use DBI 1.607 ();
170 use DBD::SQLite 1.27 ();
171  
172 my \$DBH = undef;
173  
174 sub orlite { '$VERSION' }
175  
176 sub sqlite { '$file' }
177  
178 sub dsn { '$dsn' }
179  
180 sub dbh {
181 \$DBH or \$_[0]->connect;
182 }
183  
184 sub connect {
185 DBI->connect( \$_[0]->dsn, undef, undef, {
186 PrintError => 0,
187 RaiseError => 1,$readonly$unicode
188 } );
189 }
190  
191 sub connected {
192 defined \$DBH;
193 }
194  
195 sub prepare {
196 shift->dbh->prepare(\@_);
197 }
198  
199 sub do {
200 shift->dbh->do(\@_);
201 }
202  
203 sub selectall_arrayref {
204 shift->dbh->selectall_arrayref(\@_);
205 }
206  
207 sub selectall_hashref {
208 shift->dbh->selectall_hashref(\@_);
209 }
210  
211 sub selectcol_arrayref {
212 shift->dbh->selectcol_arrayref(\@_);
213 }
214  
215 sub selectrow_array {
216 shift->dbh->selectrow_array(\@_);
217 }
218  
219 sub selectrow_arrayref {
220 shift->dbh->selectrow_arrayref(\@_);
221 }
222  
223 sub selectrow_hashref {
224 shift->dbh->selectrow_hashref(\@_);
225 }
226  
227 sub pragma {
228 \$_[0]->do("pragma \$_[1] = \$_[2]") if \@_ > 2;
229 \$_[0]->selectrow_arrayref("pragma \$_[1]")->[0] if defined wantarray;
230 }
231  
232 sub iterate {
233 my \$class = shift;
234 my \$call = pop;
235 my \$sth = \$class->prepare(shift);
236 \$sth->execute(\@_);
237 while ( \$_ = \$sth->fetchrow_arrayref ) {
238 \$call->() or return 1;;
239 }
240 }
241  
242 sub begin {
243 \$DBH or
244 \$DBH = \$_[0]->connect;
245 \$DBH->begin_work;
246 }
247  
248 sub rollback {
249 \$DBH or return 1;
250 \$DBH->rollback;
251 \$DBH->disconnect;
252 undef \$DBH;
253 return 1;
254 }
255  
256 sub rollback_begin {
257 if ( \$DBH ) {
258 \$DBH->rollback;
259 \$DBH->begin_work;
260 } else {
261 \$_[0]->begin;
262 }
263 return 1;
264 }
265  
266 END_PERL
267  
268 # If you are a read-write database, we even allow you
269 # to commit your transactions.
270 28 ▶ $code .= <<"END_PERL" unless $readonly;
Branch: 1 branch
BranchTrueFalseTotal
unless $readonly 26 2 28
271 sub commit {
272 \$DBH or return 1;
273 \$DBH->commit;
274 \$DBH->disconnect;
275 undef \$DBH;
276 return 1;
277 }
278  
279 sub commit_begin {
280 if ( \$DBH ) {
281 \$DBH->commit;
282 \$DBH->begin_work;
283 } else {
284 \$_[0]->begin;
285 }
286 return 1;
287 }
288  
289 END_PERL
290  
291 # Cleanup and shutdown operations
292 28 ▶ if ( $cleanup ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cleanup) { } 2 26 28
293 2 $code .= <<"END_PERL";
294 END {
295 if ( \$DBH ) {
296 \$DBH->rollback;
297 \$DBH->do('$cleanup');
298 \$DBH->disconnect;
299 undef \$DBH;
300 } else {
301 $pkg->do('$cleanup');
302 }
303 }
304  
305 END_PERL
306 } else {
307 26 $code .= <<"END_PERL";
308 END {
309 $pkg->rollback if \$DBH;
310 }
311  
312 END_PERL
313 }
314  
315 # Optionally generate the table classes
316 28 my $tables = undef;
317 28 ▶ if ( $params{tables} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"tables"}) 25 3 28
318 # Capture the raw schema table information
319 25 $tables = $dbh->selectall_arrayref(
320 'select * from sqlite_master where name not like ? and type in ( ?, ? )',
321 { Slice => {} }, 'sqlite_%', 'table', 'view',
322 );
323  
324 # Capture the raw schema information and do first-pass work
325 25 foreach my $t ( @$tables ) {
326 # Convenience pre-quoted form of the table name
327 35 $t->{qname} = $dbh->quote_identifier(undef, undef, $t->{name});
328  
329 # What will be the class for this table
330 35 $t->{class} = $t->{name};
331 35 ▶ if ( $t->{class} ne lc $t->{class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"class"} ne lc $t->{"class"}) 1 34 35
332 1 $t->{class} =~ s/([a-z])([A-Z])/${1}_${2}/g;
333 1 $t->{class} =~ s/_+/_/g;
334 }
335 35 $t->{class} = ucfirst lc $t->{class};
336 35 $t->{class} =~ s/_([a-z])/uc($1)/ge;
337 35 $t->{class} = "${pkg}::$t->{class}";
338  
339 # Load the structural column list
340 35 my $columns = $t->{columns} = $dbh->selectall_arrayref(
341 "pragma table_info('$t->{name}')",
342 { Slice => {} },
343 );
344  
345 # The list of columns we will select, which can
346 # be different to the general list.
347 35 my $select = $t->{select} = [ @$columns ];
348  
349 # Track array vs hash implementation on a per-table
350 # basis so that we can force views to always be done
351 # array-wise (to compensate for some weird SQLite
352 # column quoting differences between tables and views
353 35 $t->{array} = $params{array};
354 35 ▶ if ( $t->{type} eq 'view' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"type"} eq "view") 4 31 35
355 4 $t->{array} = 1;
356 }
357  
358 # Track usage of rowid on a per-table basis because
359 # views don't always support rowid.
360 35 $t->{rowid} = $t->{type} eq 'table';
361  
362 35 foreach my $c ( @$select ) {
363 # Convenience escaping for the column names
364 82 $c->{qname} = $dbh->quote_identifier($c->{name});
365  
366 # Affinity detection
367 82 ▶ if ( $c->{type} =~ /INT/i ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($c->{'type'} =~ /INT/i) { } 43 39 82
elsif ($c->{'type'} =~ /(?:CHAR|CLOB|TEXT)/i) { } 16 23 39
elsif ($c->{'type'} =~ /BLOB/i or not $c->{'type'}) { } 2 21 23
elsif ($c->{'type'} =~ /(?:REAL|FLOA|DOUB)/i) { } 1 20 21
368 43 $c->{affinity} = 'INTEGER';
369 } elsif ( $c->{type} =~ /(?:CHAR|CLOB|TEXT)/i ) {
370 16 $c->{affinity} = 'TEXT';
371 ▶ } elsif ( $c->{type} =~ /BLOB/i or not $c->{type} ) {
Condition100%3/3
$c->{"type"} =~ /BLOB/i or not $c->{"type"}
l!l&&r!l&&!r
1121
MC/DC100%2/2
$c->{"type"} =~ /BLOB/i or not $c->{"type"}
$c->{"type"} =~ /BLOB/i not $c->{"type"}
Truth table100%3/3
A or not(B)
A= $c->{"type"} =~ /BLOB/iB= not $c->{"type"}
ABresult
1X1
011
000
372 2 $c->{affinity} = 'BLOB';
373  
374 # Unicode currently breaks BLOB columns
375 2 ▶ if ( $unicode ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unicode) 0 2 2
376 0 die "BLOB column $t->{name}.$c->{name} is not supported in unicode database";
377 }
378 } elsif ( $c->{type} =~ /(?:REAL|FLOA|DOUB)/i ) {
379 1 $c->{affinity} = 'REAL';
380 } else {
381 20 $c->{affinity} = 'NUMERIC';
382 }
383 }
384  
385 # Analyze the primary keys structure
386 35 $t->{pk} = [ grep { $_->{pk} } @$columns ];
387 35 $t->{pkn} = scalar @{$t->{pk}};
388 35 ▶ if ( $t->{pkn} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"pkn"} == 1) 28 7 35
389 28 $t->{pk1} = $t->{pk}->[0];
390 28 ▶ if ( $t->{pk1}->{affinity} eq 'INTEGER' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"pk1"}{"affinity"} eq "INTEGER") 27 1 28
391 27 $t->{pki} = $t->{pk1};
392 }
393 }
394 35 ▶ if ( $t->{pki} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t->{'pki'}) { } 27 8 35
elsif ($t->{'rowid'}) { } 4 4 8
395 27 ▶ $t->{rowid} &&= $t->{pki};
Condition33%1/3
$t->{"rowid"} &&= $t->{"pki"}
!ll&&!rl&&r
0027
MC/DC0%0/2
$t->{"rowid"} &&= $t->{"pki"}
$t->{"rowid"} $t->{"pki"}
Truth table33%1/3
A &&= B
A= $t->{"rowid"}B= $t->{"pki"}
ABresult
0X0
100
111
396 27 ▶ if ( $t->{pki}->{name} eq $t->{name} . '_id' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"pki"}{"name"} eq $t->{"name"} . '_id') 3 24 27
397 3 $t->{id} = $t->{pki};
398 }
399  
400 } elsif ( $t->{rowid} ) {
401 # Add rowid to the query
402 $t->{rowid} = {
403 4 cid => -1,
404 name => 'rowid',
405 qname => '"rowid"',
406 type => 'integer',
407 affinity => 'INTEGER',
408 notnull => 1,
409 dflt_value => undef,
410 pk => 0,
411 };
412 4 push @$select, $t->{rowid};
413 }
414  
415 # Do we allow object creation?
416 35 $t->{create} = $t->{pkn};
417 35 ▶ $t->{create} = 1 if $t->{rowid};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"rowid"} 31 4 35
418 35 ▶ $t->{create} = 0 if $readonly;
Branch: 1 branch
BranchTrueFalseTotal
if $readonly 1 34 35
419  
420 # Generate the object keys for the columns
421 35 ▶ if ( $t->{array} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'array'}) { } 15 20 35
422 15 foreach my $i ( 0 .. $#$select ) {
423 40 $select->[$i]->{xs} = $i;
424 40 $select->[$i]->{key} = "[$i]";
425 }
426 } else {
427 20 foreach my $c ( @$select ) {
428 46 $c->{xs} = "'$c->{name}'";
429 46 $c->{key} = "{$c->{name}}";
430 }
431 }
432  
433 # Generate the main SQL fragments
434 35 $t->{sql_scols} = join ', ', map { $_->{qname} } @$select;
435 35 $t->{sql_icols} = join ', ', map { $_->{qname} } @$columns;
436 35 $t->{sql_ivals} = join ', ', ( '?' ) x scalar @$columns;
437 35 $t->{sql_select} = "select $t->{sql_scols} from $t->{qname}";
438 $t->{sql_insert} =
439 35 "insert into $t->{qname} " .
440 "( $t->{sql_icols} ) " .
441 "values ( $t->{sql_ivals} )";
442 $t->{sql_where} = join ' and ',
443 35 map { "$_->{qname} = ?" } @{$t->{pk}};
444  
445 # Generate the new Perl fragments
446 $t->{pl_new} = join "\n", map {
447 35 $t->{array}
448 82 ▶ ? "\t\t\$attr{$_->{name}},"
Branch: 1 branch
BranchTrueFalseTotal
$t->{'array'} ? : 37 45 82
449 : "\t\t$_->{name} => \$attr{$_->{name}},"
450 } @$columns;
451  
452 $t->{pl_insert} = join "\n", map {
453 35 "\t\t\$self->$_->{key},"
454 } @$columns;
455  
456 35 $t->{pl_fill} = '';
457 35 ▶ if ( $t->{pki} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t->{'pki'}) { } 27 8 35
elsif ($t->{'rowid'}) { } 4 4 8
458 $t->{pl_fill} =
459 27 "\t\$self->$t->{pki}->{key} " .
460 "= \$dbh->func('last_insert_rowid') " .
461 "unless \$self->$t->{pki}->{key};";
462 } elsif ( $t->{rowid} ) {
463 $t->{pl_fill} =
464 4 "\t\$self->$t->{rowid}->{key} " .
465 "= \$dbh->func('last_insert_rowid');";
466 }
467 }
468  
469 # Generate the foreign key metadata
470 25 my %tindex = map { $_->{name} => $_ } @$tables;
471 25 foreach my $t ( @$tables ) {
472 # Locate the foreign keys
473 35 my %fk = ();
474 35 my @fk_sql = $t->{sql} =~ /[(,]\s*(.+?REFERENCES.+?)\s*[,)]/g;
475  
476 # Extract the details
477 35 foreach ( @fk_sql ) {
478 4 ▶ unless ( /^(\w+).+?REFERENCES\s+(\w+)\s*\(\s*(\w+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^(\w+).+?REFERENCES\s+(\w+)\s*\(\s*(\w+)/) 0 4 4
479 0 die "Invalid foreign key $_";
480 }
481 4 $fk{"$1"} = [ "$2", $tindex{"$2"}, "$3" ];
482 }
483 35 foreach ( @{$t->{columns}} ) {
484 82 $_->{fk} = $fk{$_->{name}};
485 }
486  
487 # One final code fragment we need the fk for
488 $t->{pl_accessor} = join "\n",
489 78 map { "\t\t$_->{name} => $_->{xs}," }
490 35 grep { ! $_->{fk} } @{$t->{columns}};
491 }
492  
493 # Generate the per-table code
494 25 foreach my $t ( @$tables ) {
495 35 my @select = @{$t->{select}};
496 35 my @columns = @{$t->{columns}};
497 my $slice = $t->{array}
498 35 ▶ ? '{}'
Branch: 1 branch
BranchTrueFalseTotal
$t->{'array'} ? : 15 20 35
499 : '{ Slice => {} }';
500  
501 # Generate the package header
502 35 ▶ if ( $params{shim} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'shim'}) { } 1 34 35
503 # Generate a shim-wrapper class
504 1 $code .= <<"END_PERL";
505 package $t->{class};
506  
507 \@$t->{class}::ISA = '$t->{class}::Shim';
508  
509 package $t->{class}::Shim;
510  
511 END_PERL
512 } else {
513 # Plain vanilla package header
514 34 $code .= <<"END_PERL";
515 package $t->{class};
516  
517 END_PERL
518 }
519  
520 # Generate the common elements for all classes
521 35 $code .= <<"END_PERL";
522 sub base { '$pkg' }
523  
524 sub table { '$t->{name}' }
525  
526 sub table_info {
527 $pkg->selectall_arrayref(
528 "pragma table_info('$t->{name}')",
529 { Slice => {} },
530 );
531 }
532  
533 sub select {
534 my \$class = shift;
535 my \$sql = '$t->{sql_select} ';
536 \$sql .= shift if \@_;
537 my \$rows = $pkg->selectall_arrayref( \$sql, $slice, \@_ );
538 bless \$_, '$t->{class}' foreach \@\$rows;
539 wantarray ? \@\$rows : \$rows;
540 }
541  
542 sub count {
543 my \$class = shift;
544 my \$sql = 'select count(*) from $t->{qname} ';
545 \$sql .= shift if \@_;
546 $pkg->selectrow_array( \$sql, {}, \@_ );
547 }
548  
549 END_PERL
550  
551 # Handle different versions, because arrayref acts funny
552 35 ▶ if ( $t->{array} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'array'}) { } 15 20 35
553 15 $code .= <<"END_PERL";
554 sub iterate {
555 my \$class = shift;
556 my \$call = pop;
557 my \$sql = '$t->{sql_select} ';
558 \$sql .= shift if \@_;
559 my \$sth = $pkg->prepare(\$sql);
560 \$sth->execute(\@_);
561 while ( \$_ = \$sth->fetchrow_arrayref ) {
562 \$_ = bless [ \@\$_ ], '$t->{class}';
563 \$call->() or last;
564 }
565 \$sth->finish;
566 }
567  
568 END_PERL
569 } else {
570 20 $code .= <<"END_PERL";
571 sub iterate {
572 my \$class = shift;
573 my \$call = pop;
574 my \$sql = '$t->{sql_select} ';
575 \$sql .= shift if \@_;
576 my \$sth = $pkg->prepare(\$sql);
577 \$sth->execute(\@_);
578 while ( \$_ = \$sth->fetchrow_hashref ) {
579 bless \$_, '$t->{class}';
580 \$call->() or last;
581 }
582 \$sth->finish;
583 }
584  
585 END_PERL
586 }
587  
588 # Add the primary key based single object loader
589 35 ▶ if ( $t->{pkn} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"pkn"}) 30 5 35
590 30 ▶ if ( $t->{array} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'array'}) { } 10 20 30
591 10 $code .= <<"END_PERL";
592 sub load {
593 my \$class = shift;
594 my \@row = $pkg->selectrow_array(
595 '$t->{sql_select} where $t->{sql_where}',
596 undef, \@_,
597 );
598 unless ( \@row ) {
599 Carp::croak("$t->{class} row does not exist");
600 }
601 bless \\\@row, '$t->{class}';
602 }
603  
604 END_PERL
605 } else {
606 20 $code .= <<"END_PERL";
607 sub load {
608 my \$class = shift;
609 my \$row = $pkg->selectrow_hashref(
610 '$t->{sql_select} where $t->{sql_where}',
611 undef, \@_,
612 );
613 unless ( \$row ) {
614 Carp::croak("$t->{class} row does not exist");
615 }
616 bless \$row, '$t->{class}';
617 }
618  
619 END_PERL
620 }
621 }
622  
623 # Generate the elements for tables with primary keys
624 35 ▶ if ( $t->{create} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"create"}) 30 5 35
625 30 ▶ my $l = $t->{array} ? '[' : '{';
Branch: 1 branch
BranchTrueFalseTotal
$t->{'array'} ? : 11 19 30
626 30 ▶ my $r = $t->{array} ? ']' : '}';
Branch: 1 branch
BranchTrueFalseTotal
$t->{'array'} ? : 11 19 30
627 my $set = $t->{array}
628 30 ▶ ? '$self->set( $_ => $set{$_} ) foreach keys %set;'
Branch: 1 branch
BranchTrueFalseTotal
$t->{'array'} ? : 11 19 30
629 : '$self->{$_} = $set{$_} foreach keys %set;';
630 30 $code .= <<"END_PERL";
631 sub new {
632 my \$class = shift;
633 my \%attr = \@_;
634 bless $l
635 $t->{pl_new}
636 $r, \$class;
637 }
638  
639 sub create {
640 shift->new(\@_)->insert;
641 }
642  
643 sub insert {
644 my \$self = shift;
645 my \$dbh = $pkg->dbh;
646 \$dbh->do(
647 '$t->{sql_insert}',
648 {},
649 $t->{pl_insert}
650 );
651 $t->{pl_fill}
652 return \$self;
653 }
654  
655 sub update {
656 my \$self = shift;
657 my \%set = \@_;
658 my \$rows = $pkg->do(
659 'update $t->{qname} set ' .
660 join( ', ', map { "\\"\$_\\" = ?" } keys \%set ) .
661 ' where "rowid" = ?',
662 {},
663 values \%set,
664 \$self->rowid,
665 );
666 unless ( \$rows == 1 ) {
667 Carp::croak("Expected to update 1 row, actually updated \$rows");
668 }
669 $set
670 return 1;
671 }
672  
673 sub delete {
674 return $pkg->do(
675 'delete from $t->{qname} where "rowid" = ?', {},
676 shift->rowid,
677 ) if ref \$_[0];
678 Carp::croak("Static $pkg->delete has been deprecated");
679 }
680  
681 sub delete_where {
682 shift; $pkg->do('delete from $t->{qname} where ' . shift, {}, \@_);
683 }
684  
685 sub truncate {
686 $pkg->do('delete from $t->{qname}');
687 }
688  
689 END_PERL
690 }
691  
692 35 ▶ if ( $t->{create} and $t->{array} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"create"} and $t->{"array"}) 11 24 35
Condition100%3/3
$t->{"create"} and $t->{"array"}
!ll&&!rl&&r
51911
MC/DC100%2/2
$t->{"create"} and $t->{"array"}
$t->{"create"} $t->{"array"}
Truth table100%3/3
A and B
A= $t->{"create"}B= $t->{"array"}
ABresult
0X0
100
111
693 # Add an additional set method to avoid having
694 # the user have to enter manual positions.
695 11 $code .= <<"END_PERL";
696 sub set {
697 my \$self = shift;
698 my \$i = {
699 $t->{pl_accessor}
700 }->{\$_[0]};
701 Carp::croak("Bad name '\$_[0]'") unless defined \$i;
702 \$self->[\$i] = \$_[1];
703 }
704  
705 END_PERL
706 }
707  
708 # Generate the boring accessors
709 35 ▶ if ( $params{xsaccessor} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'xsaccessor'}) { } 4 31 35
710 4 ▶ my $type = $t->{create} ? 'accessors' : 'getters';
Branch: 1 branch
BranchTrueFalseTotal
$t->{'create'} ? : 4 0 4
711 my $xsclass = $t->{array}
712 4 ▶ ? 'Class::XSAccessor::Array'
Branch: 1 branch
BranchTrueFalseTotal
$t->{'array'} ? : 2 2 4
713 : 'Class::XSAccessor';
714 my $id = $t->{id}
715 4 ▶ ? "\t\t$t->{id}->{name} => $t->{id}->{xs},\n"
Branch: 1 branch
BranchTrueFalseTotal
$t->{'id'} ? : 0 4 4
716 : '';
717 my $rowid = ($t->{id} and $t->{rowid})
718 4 ▶ ? "\t\t$t->{rowid}->{name} => $t->{rowid}->{xs},\n"
Branch: 1 branch
BranchTrueFalseTotal
$t->{'id'} && $t->{'rowid'} ? : 0 4 4
Condition33%1/3
$t->{"id"} and $t->{"rowid"}
!ll&&!rl&&r
400
MC/DC0%0/2
$t->{"id"} and $t->{"rowid"}
$t->{"id"} $t->{"rowid"}
Truth table33%1/3
A and B
A= $t->{"id"}B= $t->{"rowid"}
ABresult
0X0
100
111
719 : '';
720  
721 4 $code .= <<"END_PERL";
722 use $xsclass 1.05 {
723 getters => {
724 ${rowid}${id}$t->{pl_accessor}
725 },
726 };
727  
728 END_PERL
729 } else {
730 31 ▶ if ( $t->{pki} and $t->{rowid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"pki"} and $t->{"rowid"}) 23 8 31
Condition66%2/3
$t->{"pki"} and $t->{"rowid"}
!ll&&!rl&&r
8023
MC/DC50%1/2
$t->{"pki"} and $t->{"rowid"}
$t->{"pki"} $t->{"rowid"}
Truth table66%2/3
A and B
A= $t->{"pki"}B= $t->{"rowid"}
ABresult
0X0
100
111
731 23 $code .= <<"END_PERL";
732 sub rowid {
733 \$_[0]->$t->{rowid}->{key};
734 }
735  
736 END_PERL
737 }
738  
739 31 ▶ if ( $t->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"id"}) 3 28 31
740 3 $code .= <<"END_PERL";
741 sub id {
742 \$_[0]->$t->{id}->{key};
743 }
744  
745 END_PERL
746 }
747  
748 31 $code .= join "\n\n", map { <<"END_PERL" } grep { ! $_->{fk} } @select;
749 sub $_->{name} {
750 \$_[0]->$_->{key};
751 }
752 END_PERL
753 }
754  
755 # Generate the foreign key accessors
756 35 $code .= join "\n\n", map { <<"END_PERL" } grep { $_->{fk} } @columns;
757 sub $_->{name} {
758 ($_->{fk}->[1]->{class}\->select('where \"$_->{fk}->[1]->{pk}->[0]->{name}\" = ?', \$_[0]->$_->{key}))[0];
759 }
760 END_PERL
761 }
762 }
763  
764 # We are finished with the database
765 28 $dbh->disconnect;
766  
767 # Start the post-table content again
768 28 ▶ $code .= "\npackage $pkg;\n" if $params{tables};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"tables"} 25 3 28
769  
770 # Append any custom code for the user
771 28 ▶ $code .= "\n$params{append}" if defined $params{append};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"append"} 4 24 28
772  
773 # Load the overload classes for each of the tables
774 28 ▶ if ( $tables ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tables) 25 3 28
775 $code .= join( "\n",
776 "local \$@ = undef;",
777 map {
778 25 "eval { require $_->{class} };"
779 } @$tables
780 );
781 }
782  
783 # End the class normally
784 28 $code .= "\n\n1;\n";
785  
786 # Save to the cache location if caching is enabled
787 28 ▶ if ( $cached ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached) 1 27 28
788 1 my $dir = File::Basename::dirname($cached);
789 1 ▶ unless ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 1 1
790 0 File::Path::mkpath( $dir, { verbose => 0 } );
791 }
792  
793 # Save a copy of the code to the file
794 1 local *FILE;
795 1 ▶ open( FILE, ">$cached" ) or Carp::croak("open($cached): $!");
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$cached" or Carp::croak("open($cached): $!") 1 0 1
796 1 print FILE $code;
797 1 close FILE;
798 }
799  
800 # Compile the code
801 28 local $@;
802 28 ▶ if ( $^P and $^V >= 5.008009 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($^P and $^V >= 5.008009) { } 28 0 28
elsif ($DEBUG) { } 0 0 0
Condition33%1/3
$^P and $^V >= "5.008009"
!ll&&!rl&&r
0028
MC/DC0%0/2
$^P and $^V >= "5.008009"
$^P $^V >= "5.008009"
Truth table33%1/3
A and B
A= $^PB= $^V >= "5.008009"
ABresult
0X0
100
111
803 28 local $^P = $^P | 0x800;
804 0 ▶ eval($code);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine END: called CC=2 SCAR=7.0
Subroutine base: called CC=1 SCAR=0.0
Subroutine begin: called CC=2 SCAR=7.0
Subroutine col1: called CC=1 SCAR=0.0
Subroutine col2: called CC=1 SCAR=0.0
Subroutine commit: called CC=2 SCAR=7.0
Subroutine commit_begin: called CC=2 SCAR=7.0
Subroutine connect: called CC=1 SCAR=0.0
Subroutine connected: called CC=1 SCAR=0.0
Subroutine count: called CC=2 SCAR=7.0
Subroutine create: called CC=1 SCAR=0.0
Subroutine dbh: called CC=2 SCAR=7.0
Subroutine delete: called CC=2 SCAR=7.0
Subroutine delete_where: called CC=1 SCAR=0.0
Subroutine do: called CC=1 SCAR=0.0
Subroutine dsn: called CC=1 SCAR=0.0
Subroutine insert: called CC=2 SCAR=7.0
Subroutine iterate: called CC=3 SCAR=11.1
Subroutine iterate: called CC=4 SCAR=14.0
Subroutine load: called CC=2 SCAR=7.0
Subroutine new: called CC=1 SCAR=0.0
Subroutine orlite: called CC=1 SCAR=0.0
Subroutine pragma: called CC=3 SCAR=11.1
Subroutine prepare: called CC=1 SCAR=0.0
Subroutine rollback: called CC=2 SCAR=7.0
Subroutine rollback_begin: called CC=2 SCAR=7.0
Subroutine rowid: called CC=1 SCAR=0.0
Subroutine select: called CC=4 SCAR=14.0
Subroutine selectall_arrayref: called CC=1 SCAR=0.0
Subroutine selectall_hashref: called CC=1 SCAR=0.0
Subroutine selectcol_arrayref: called CC=1 SCAR=0.0
Subroutine selectrow_array: not called CC=1 SCAR=0.0
Subroutine selectrow_arrayref: not called CC=1 SCAR=0.0
Subroutine selectrow_hashref: called CC=1 SCAR=0.0
Subroutine sqlite: not called CC=1 SCAR=0.0
Branch: 31 branches
BranchTrueFalseTotal
if $DBH 1 37 38
$DBH or $DBH = $_[0]->connect 4 2 6
$DBH or return 1 0 2 2
if ($DBH) { } 4 33 37
if @_ 18 192 210
$DBH or $_[0]->connect 13 8 21
if ref $_[0] 18 11 29
unless $self->{"col1"} 6 0 6
if ($_ = $sth->fetchrow_arrayref) 1 4 5
&$call() or return 1 6 0 6
if @_ 12 3 15
if ($_ = $sth->fetchrow_hashref) 9 3 12
&$call() or last 5 12 17
unless ($row) 6 15 21
if @_ > 2 19 2 21
if defined wantarray 8 4 12
$DBH or return 1 4 0 4
if ($DBH) { } 21 10 31
if @_ 25 6 31
wantarray ? : 2 9 11
unless ($rows == 1) 6 2 8
if @_ 0 0 0
if ref $_[0] 0 1 1
unless $self->{"col1"} 0 1 1
if @_ 1 0 1
if ($_ = $sth->fetchrow_hashref) 0 1 1
&$call() or last 1 0 1
unless ($row) 0 1 1
if @_ 1 0 1
wantarray ? : 0 0 0
unless ($rows == 1) 0 0 0
805 28 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 28 28
806 } elsif ( $DEBUG ) {
807 0 dval($code);
808 } else {
809 0 eval($code);
810 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
811 }
812  
813 28 return 1;
814 }
815  
816 4 ▶sub dval {
Subroutine dval: called CC=1 SCAR=0.0
Pod undocumented
817 # Write the code to the temp file
818 4 require File::Temp;
819 15 my ($fh, $filename) = File::Temp::tempfile();
820 19 $fh->print($_[0]);
821 33 close $fh;
822 39 require $filename;
823 15 unlink $filename;
824  
825 # Print the debugging output
826 # my @trace = map {
827 # s/\s*[{;]$//;
828 # s/^s/ s/;
829 # s/^p/\np/;
830 # "$_\n"
831 # } grep {
832 # /^(?:package|sub)\b/
833 # } split /\n/, $_[0];
834 # print STDERR @trace, "\nCode saved as $filename\n\n";
835  
836 4 return 1;
837 }
838  
839 16 ▶sub prune {
Subroutine prune: called CC=1 SCAR=0.0
Pod documented
840 5 my $class = shift;
841 5 push @PRUNE, map { File::Spec->rel2abs($_) } @_;
842 }
843  
844 27 1;
845  
846 __END__
↑ Summary