Perlish.pm

filter: Statementstmt 58.9% 211 / 358 Branchbran 55.9% 85 / 152 Conditioncond 54.6% 53 / 97 MC/DCmcdc 40.0% 22 / 55 Subroutinesub 37.2% 16 / 43 Podpod 77.7% 14 / 18 totaltotal 55.4% 401 / 723 SCAR 72.0
CC
126
Cov
57%
fetch
182.0
insert
90.0
gen_sql_select
72.0
CRAP 1345.1
« Perlish/Parse.pm ↑ Summary
1 package DBIx::Perlish;
2  
3 25 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 25 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 25 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS $SQL @BIND_VALUES);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 require Exporter;
10 25 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 ▶use Keyword::Pluggable;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 25 $VERSION = '1.07';
14 25 @EXPORT = qw(sql);
15 25 @EXPORT_OK = qw(union intersect except subselect);
16 25 %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
17  
18 25 ▶use DBIx::Perlish::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 ▶sub union (&;$) {}
Subroutine union: not called CC=1 SCAR=0.0
Pod documented
21 ▶sub intersect (&;$) {}
Subroutine intersect: not called CC=1 SCAR=0.0
Pod documented
22 ▶sub except (&;$) {}
Subroutine except: not called CC=1 SCAR=0.0
Pod documented
23 ▶sub subselect (&) {}
Subroutine subselect: not called CC=1 SCAR=0.0
Pod documented
24  
25 25 my $default_object;
26 25 my $non_object_quirks = {};
27  
28 sub optree_version
29 {
30 3 ▶ return 1 if $^V lt 5.22.0;
Subroutine optree_version: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $^V lt 5.22.0 0 3 3
31 3 return 2;
32 }
33  
34 sub lexify
35 {
36 29 ▶ my ( $text, $insert ) = @_;
Subroutine lexify: called CC=2 SCAR=7.1
Pod undocumented
37 29 ▶ $insert .= 'sub ' if $$text =~ /^\s*\{/;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /^\s*\{/u 29 0 29
38 29 substr($$text, 0, 0, $insert);
39 }
40  
41 sub import
42 {
43 25 ▶ my $pkg = caller;
Subroutine import: called CC=13 SCAR=29.0
44 25 local @EXPORT_OK = @EXPORT_OK;
45 25 local %EXPORT_TAGS = %EXPORT_TAGS;
46 25 ▶ if ($pkg && $pkg->can("except")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg and $pkg->can("except")) 0 25 25
Condition33%1/3
$pkg and $pkg->can("except")
!ll&&!rl&&r
0250
MC/DC0%0/2
$pkg and $pkg->can("except")
$pkg $pkg->can("except")
Truth table33%1/3
A and B
A= $pkgB= $pkg->can("except")
ABresult
0X0
100
111
47 # XXX maybe check prototype here
48 0 pop @EXPORT_OK;
49 0 %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
50 }
51 25 my @shift;
52 25 ▶ @shift = (shift()) if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 5 20 25
53 25 my %p = @_;
54 25 ▶ if ($p{prefix} && $p{prefix} =~ /^[a-zA-Z_]\w*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"prefix"} and $p{"prefix"} =~ /^[a-zA-Z_]\w*$/u) 2 23 25
Condition66%2/3
$p{"prefix"} and $p{"prefix"} =~ /^[a-zA-Z_]\w*$/u
!ll&&!rl&&r
2302
MC/DC50%1/2
$p{"prefix"} and $p{"prefix"} =~ /^[a-zA-Z_]\w*$/u
$p{"prefix"} $p{"prefix"} =~ /^[a-zA-Z_]\w*$/u
Truth table66%2/3
A and B
A= $p{"prefix"}B= $p{"prefix"} =~ /^[a-zA-Z_]\w*$/u
ABresult
0X0
100
111
55 25 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶ if ( $p{dbh} && ref $p{dbh} && (ref $p{dbh} eq "SCALAR" || ref $p{dbh} eq "REF")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"dbh"} and ref $p{"dbh"} and ref $p{"dbh"} eq "SCALAR" || ref $p{"dbh"} eq "REF") 2 0 2
Condition44%4/9
$p{"dbh"} and ref $p{"dbh"} and ref $p{"dbh"} eq "SCALAR" || ref $p{"dbh"} eq "REF"
!ll&&!rl&&r
002
$p{"dbh"} and ref $p{"dbh"}
!ll&&!rl&&r
002
ref $p{"dbh"} eq "SCALAR" or ref $p{"dbh"} eq "REF"
l!l&&r!l&&!r
110
MC/DC0%0/4
$p{"dbh"} and ref $p{"dbh"} and ref $p{"dbh"} eq "SCALAR" || ref $p{"dbh"} eq "REF"
$p{"dbh"} ref $p{"dbh"} ref $p{"dbh"} eq "SCALAR" ref $p{"dbh"} eq "REF"
Truth table20%1/5
A and B and C or D
A= $p{"dbh"}B= ref $p{"dbh"}C= ref $p{"dbh"} eq "SCALAR"D= ref $p{"dbh"} eq "REF"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
57 2 my $dbhref = $p{dbh};
58 2 *{$pkg."::$p{prefix}_fetch"} =
59 2 *{$pkg."::$p{prefix}_select"} =
60 2 ▶ sub (&) { my $o = DBIx::Perlish->new(dbh => $$dbhref); $o->fetch(@_) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
61 2 *{$pkg."::$p{prefix}_update"} =
62 2 ▶ sub (&) { my $o = DBIx::Perlish->new(dbh => $$dbhref); $o->update(@_) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
63 2 *{$pkg."::$p{prefix}_delete"} =
64 2 ▶ sub (&) { my $o = DBIx::Perlish->new(dbh => $$dbhref); $o->delete(@_) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
65 2 *{$pkg."::$p{prefix}_insert"} =
66 2 ▶ sub { my $o = DBIx::Perlish->new(dbh => $$dbhref); $o->insert(@_) };
Subroutine __ANON__: not called CC=1 SCAR=2.6
67 2 return;
68 }
69 }
70  
71 23 ▶ my $prefix = delete($p{prefix}) // 'db';
Condition50%1/2
delete $p{"prefix"} // "db"
l!l
023
MC/DC0%0/1
delete $p{"prefix"} // "db"
delete $p{"prefix"}
Truth table50%1/2
A
A= delete $p{"prefix"}
Aresult
11
00
72 23 ▶ my $dbh = delete($p{dbh}) // '$dbh';
Condition100%2/2
delete $p{"dbh"} // "\$dbh"
l!l
221
MC/DC100%1/1
delete $p{"dbh"} // "\$dbh"
delete $p{"dbh"}
Truth table100%2/2
A
A= delete $p{"dbh"}
Aresult
11
00
73 23 my $iprefix = '__' . $dbh . '_execute_perlish';
74 23 $iprefix =~ s/\W//g;
75  
76 23 for (
77 [fetch => " $dbh, q(fetch), "],
78 [select => " $dbh, q(fetch), "],
79 [update => " $dbh, q(update), "],
80 [delete => " $dbh, q(delete), "],
81 ) {
82 92 my ($name, $code) = @$_;
83 Keyword::Pluggable::define
84 keyword => $prefix . '_' . $name,
85 29 ▶ code => sub { lexify( $_[0], $iprefix.$code ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
86 92 expression => 1,
87 package => $pkg
88 ;
89 }
90 Keyword::Pluggable::define
91 23 keyword => $prefix . '_insert',
92 code => $iprefix . "_insert $dbh, ",
93 expression => 1,
94 package => $pkg
95 ;
96  
97 {
98 25 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
99 23 *{$pkg."::${iprefix}"} = sub ($$&) {
100 0 ▶ my ( $dbh, $method, $sub ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 my $o = DBIx::Perlish->new(dbh => $dbh);
102 0 $o->$method($sub);
103 23 };
104 23 *{$pkg."::${iprefix}_insert"} = sub {
105 0 ▶ my $o = DBIx::Perlish->new(dbh => shift);
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 0 $o->insert(@_)
107 23 };
108 }
109 23 DBIx::Perlish->export_to_level(1, @shift, %p);
110 }
111  
112 0 ▶sub init { warn "DBIx::Perlish::init is deprecated" }
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
113  
114 sub new
115 {
116 2 ▶ my ($class, %p) = @_;
Subroutine new: called CC=5 SCAR=18.4
Pod documented
117 2 ▶ unless (UNIVERSAL::isa($p{dbh}, "DBI::db")) { # XXX maybe relax for other things?
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($p{"dbh"}, "DBI::db")) 1 1 2
118 1 die "Invalid database handle supplied in the \"dbh\" parameter.\n";
119 }
120 1 my $me = bless { dbh => $p{dbh}, quirks => {} }, $class;
121 1 ▶ if ($p{quirks} && ref $p{quirks} eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"quirks"} and ref $p{"quirks"} eq "ARRAY") 0 1 1
Condition33%1/3
$p{"quirks"} and ref $p{"quirks"} eq "ARRAY"
!ll&&!rl&&r
100
MC/DC0%0/2
$p{"quirks"} and ref $p{"quirks"} eq "ARRAY"
$p{"quirks"} ref $p{"quirks"} eq "ARRAY"
Truth table33%1/3
A and B
A= $p{"quirks"}B= ref $p{"quirks"} eq "ARRAY"
ABresult
0X0
100
111
122 0 for my $q (@{$p{quirks}}) {
123 0 $me->quirk(@$q);
124 }
125 }
126 1 return $me;
127 }
128  
129 sub quirk
130 {
131 0 ▶ my $flavor = shift;
Subroutine quirk: not called CC=5 SCAR=34.0
Pod documented
132 0 my $quirks = $non_object_quirks;
133 0 ▶ if (ref $flavor) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $flavor) 0 0 0
134 0 $quirks = $flavor->{quirks};
135 0 $flavor = shift;
136 }
137 0 $flavor = lc $flavor;
138 0 ▶ if ($flavor eq "oracle") {
Branch: 1 branch
BranchTrueFalseTotal
if ($flavor eq 'oracle') { } 0 0 0
139 0 my $qtype = shift;
140 0 ▶ if ($qtype eq "table_func_cast") {
Branch: 1 branch
BranchTrueFalseTotal
if ($qtype eq 'table_func_cast') { } 0 0 0
141 0 my ($func, $cast) = @_;
142 0 ▶ die "table_func_cast requires a function name and a type name" unless $cast;
Branch: 1 branch
BranchTrueFalseTotal
unless $cast 0 0 0
143 0 $quirks->{oracle_table_func_cast}{$func} = $cast;
144 } else {
145 0 die "unknown quirk $qtype for $flavor";
146 }
147 } else {
148 0 die "there are currently no quirks for $flavor";
149 }
150 }
151  
152 sub _get_flavor
153 {
154 0 ▶ my ($real_dbh) = @_;
Subroutine _get_flavor: not called CC=2 SCAR=17.9
155 0 ▶ my $dbh = tied(%$real_dbh) || $real_dbh;
Condition0%0/3
tied %$real_dbh || $real_dbh
l!l&&r!l&&!r
000
MC/DC0%0/2
tied %$real_dbh || $real_dbh
tied %$real_dbh $real_dbh
Truth table0%0/3
A || B
A= tied %$real_dbhB= $real_dbh
ABresult
1X1
011
000
156 0 return lc $dbh->{Driver}{Name};
157 }
158  
159 sub gen_sql_select
160 {
161 0 ▶ my ($moi, $sub) = @_;
Subroutine gen_sql_select: not called CC=8 SCAR=42.8
Pod undocumented
162 0 ▶ my $me = ref $moi ? $moi : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $moi ? : 0 0 0
163  
164 0 my $dbh = $me->{dbh};
165 0 my @kf;
166 0 my $flavor = _get_flavor($dbh);
167 0 ▶ my $kf_convert = sub { return $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
168 0 ▶ if ($flavor eq "pg" && $dbh->{FetchHashKeyName}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flavor eq "pg" and $dbh->{"FetchHashKeyName"}) 0 0 0
Condition0%0/3
$flavor eq "pg" and $dbh->{"FetchHashKeyName"}
!ll&&!rl&&r
000
MC/DC0%0/2
$flavor eq "pg" and $dbh->{"FetchHashKeyName"}
$flavor eq "pg" $dbh->{"FetchHashKeyName"}
Truth table0%0/3
A and B
A= $flavor eq "pg"B= $dbh->{"FetchHashKeyName"}
ABresult
0X0
100
111
169 0 ▶ if ($dbh->{FetchHashKeyName} eq "NAME_uc") {
Branch: 2 branches
BranchTrueFalseTotal
if ($dbh->{'FetchHashKeyName'} eq 'NAME_uc') { } 0 0 0
elsif ($dbh->{'FetchHashKeyName'} eq 'NAME_lc') { } 0 0 0
170 0 ▶ $kf_convert = sub { return uc $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 } elsif ($dbh->{FetchHashKeyName} eq "NAME_lc") {
172 0 ▶ $kf_convert = sub { return lc $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 }
174 }
175 my ($sql, $bind_values, $nret, %flags) = gen_sql($sub, "select",
176 flavor => $flavor,
177 dbh => $dbh,
178 0 ▶ quirks => $me->{quirks} || $non_object_quirks,
Condition0%0/3
$me->{'quirks'} || $non_object_quirks
l!l&&r!l&&!r
000
MC/DC0%0/2
$me->{'quirks'} || $non_object_quirks
$me->{'quirks'} $non_object_quirks
Truth table0%0/3
A || B
A= $me->{'quirks'}B= $non_object_quirks
ABresult
1X1
011
000
179 key_fields => \@kf,
180 kf_convert => $kf_convert,
181 );
182 0 ▶ $flags{key_fields} = \@kf if @kf;
Branch: 1 branch
BranchTrueFalseTotal
if @kf 0 0 0
183 0 return $sql, $bind_values, $nret, %flags;
184 }
185  
186 sub query
187 {
188 0 ▶ my ($moi, $sub) = @_;
Subroutine query: not called CC=2 SCAR=17.9
Pod documented
189 0 ▶ my $me = ref $moi ? $moi : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $moi ? : 0 0 0
190 0 my ( $sql ) = $moi->gen_sql_select($sub);
191 0 return $sql;
192 }
193  
194 sub fetch
195 {
196 0 ▶ my ($moi, $sub) = @_;
Subroutine fetch: not called CC=13 SCAR=52.0
Pod documented
197 0 ▶ my $me = ref $moi ? $moi : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $moi ? : 0 0 0
198  
199 0 my $nret;
200 0 my $dbh = $me->{dbh};
201 0 my %flags;
202  
203 0 ($me->{sql}, $me->{bind_values}, $nret, %flags) = $me->gen_sql_select($sub);
204 0 $SQL = $me->{sql}; @BIND_VALUES = @{$me->{bind_values}};
205  
206 0 ▶ if ($flags{key_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags{'key_fields'}) { } 0 0 0
207 0 ▶ my @kf = @{ $flags{key_fields} // [] };
Condition0%0/2
$flags{"key_fields"} // []
l!l
00
MC/DC0%0/1
$flags{"key_fields"} // []
$flags{"key_fields"}
Truth table0%0/2
A
A= $flags{"key_fields"}
Aresult
11
00
208 0 ▶ my $kf = @kf == 1 ? $kf[0] : [@kf];
Branch: 1 branch
BranchTrueFalseTotal
@kf == 1 ? : 0 0 0
209 0 ▶ my $r = $dbh->selectall_hashref($me->{sql}, $kf, {}, @{$me->{bind_values}}) || {};
Condition0%0/2
$dbh->selectall_hashref($me->{'sql'}, $kf, {}, @{$$me{'bind_values'};}) || {}
l!l
00
MC/DC0%0/1
$dbh->selectall_hashref($me->{'sql'}, $kf, {}, @{$$me{'bind_values'};}) || {}
$dbh->selectall_hashref($me->{'sql'}, $kf, {}, @{$$me{'bind_values'};})
Truth table0%0/2
A
A= $dbh->selectall_hashref($me->{'sql'}, $kf, {}, @{$$me{'bind_values'};})
Aresult
11
00
210 0 my $postprocess;
211 0 ▶ if ($nret - @kf == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nret - @kf == 1) { } 0 0 0
212 # Only one field returned apart from the key field,
213 # change hash reference to simple values.
214 $postprocess = sub {
215 0 ▶ my ($h, $level) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
216 0 ▶ if ($level <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level <= 1) { } 0 0 0
217 0 delete @$_{@kf} for values %$h;
218 0 $_ = (values %$_)[0] for values %$h;
219 } else {
220 0 for my $nh (values %$h) {
221 0 $postprocess->($nh, $level-1);
222 }
223 }
224 0 };
225 } else {
226 $postprocess = sub {
227 0 ▶ my ($h, $level) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
228 0 ▶ if ($level <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level <= 1) { } 0 0 0
229 0 delete @$_{@kf} for values %$h;
230 } else {
231 0 for my $nh (values %$h) {
232 0 $postprocess->($nh, $level-1);
233 }
234 }
235 0 };
236 }
237 0 $postprocess->($r, scalar @kf);
238 0 ▶ return wantarray ? %$r : $r;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
239 } else {
240 0 ▶ if ($nret > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nret > 1) { } 0 0 0
241 0 ▶ my $r = $dbh->selectall_arrayref($me->{sql}, {Slice=>{}}, @{$me->{bind_values}}) || [];
Condition0%0/2
$dbh->selectall_arrayref($me->{'sql'}, {'Slice', {}}, @{$$me{'bind_values'};}) || []
l!l
00
MC/DC0%0/1
$dbh->selectall_arrayref($me->{'sql'}, {'Slice', {}}, @{$$me{'bind_values'};}) || []
$dbh->selectall_arrayref($me->{'sql'}, {'Slice', {}}, @{$$me{'bind_values'};})
Truth table0%0/2
A
A= $dbh->selectall_arrayref($me->{'sql'}, {'Slice', {}}, @{$$me{'bind_values'};})
Aresult
11
00
242 0 ▶ return wantarray ? @$r : $r->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
243 } else {
244 0 ▶ my $r = $dbh->selectcol_arrayref($me->{sql}, {}, @{$me->{bind_values}}) || [];
Condition0%0/2
$dbh->selectcol_arrayref($me->{'sql'}, {}, @{$$me{'bind_values'};}) || []
l!l
00
MC/DC0%0/1
$dbh->selectcol_arrayref($me->{'sql'}, {}, @{$$me{'bind_values'};}) || []
$dbh->selectcol_arrayref($me->{'sql'}, {}, @{$$me{'bind_values'};})
Truth table0%0/2
A
A= $dbh->selectcol_arrayref($me->{'sql'}, {}, @{$$me{'bind_values'};})
Aresult
11
00
245 0 ▶ return wantarray ? @$r : $r->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
246 }
247 }
248 }
249  
250 # XXX refactor update/delete into a single implemention if possible?
251 sub update
252 {
253 0 ▶ my ($moi, $sub) = @_;
Subroutine update: not called CC=3 SCAR=24.8
Pod documented
254 0 ▶ my $me = ref $moi ? $moi : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $moi ? : 0 0 0
255  
256 0 my $dbh = $me->{dbh};
257 ($me->{sql}, $me->{bind_values}) = gen_sql($sub, "update",
258 flavor => _get_flavor($dbh),
259 dbh => $dbh,
260 0 ▶ quirks => $me->{quirks} || $non_object_quirks,
Condition0%0/3
$me->{'quirks'} || $non_object_quirks
l!l&&r!l&&!r
000
MC/DC0%0/2
$me->{'quirks'} || $non_object_quirks
$me->{'quirks'} $non_object_quirks
Truth table0%0/3
A || B
A= $me->{'quirks'}B= $non_object_quirks
ABresult
1X1
011
000
261 );
262 0 $SQL = $me->{sql}; @BIND_VALUES = @{$me->{bind_values}};
263 0 $dbh->do($me->{sql}, {}, @{$me->{bind_values}});
264 }
265  
266 sub delete
267 {
268 0 ▶ my ($moi, $sub) = @_;
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
269 0 ▶ my $me = ref $moi ? $moi : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $moi ? : 0 0 0
270  
271 0 my $dbh = $me->{dbh};
272 ($me->{sql}, $me->{bind_values}) = gen_sql($sub, "delete",
273 flavor => _get_flavor($dbh),
274 dbh => $dbh,
275 0 ▶ quirks => $me->{quirks} || $non_object_quirks,
Condition0%0/3
$me->{'quirks'} || $non_object_quirks
l!l&&r!l&&!r
000
MC/DC0%0/2
$me->{'quirks'} || $non_object_quirks
$me->{'quirks'} $non_object_quirks
Truth table0%0/3
A || B
A= $me->{'quirks'}B= $non_object_quirks
ABresult
1X1
011
000
276 );
277 0 $SQL = $me->{sql}; @BIND_VALUES = @{$me->{bind_values}};
278 0 $dbh->do($me->{sql}, {}, @{$me->{bind_values}});
279 }
280  
281 sub insert
282 {
283 0 ▶ my ($moi, $table, @rows) = @_;
Subroutine insert: not called CC=9 SCAR=45.0
Pod documented
284 0 ▶ my $me = ref $moi ? $moi : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $moi ? : 0 0 0
285  
286 0 my $dbh = $me->{dbh};
287 0 my %sth;
288 0 for my $row (@rows) {
289 0 my @keys = sort keys %$row;
290 0 my $sql = "insert into $table (";
291 0 $sql .= join ",", @keys;
292 0 $sql .= ") values (";
293 0 my (@v, @b);
294 0 my $skip_prepare;
295 0 for my $v (@$row{@keys}) {
296 0 ▶ if (ref $v eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'CODE') { } 0 0 0
297 0 push @v, scalar $v->();
298 0 $skip_prepare = 1;
299 } else {
300 0 push @v, "?";
301 0 push @b, $v;
302 }
303 }
304 0 $sql .= join ",", @v;
305 0 $sql .= ")";
306 0 ▶ if ($skip_prepare) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_prepare) { } 0 0 0
307 0 ▶ return undef unless defined $dbh->do($sql, {}, @b);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dbh->do($sql, {}, @b) 0 0 0
308 } else {
309 0 my $k = join ";", @keys;
310 0 ▶ $sth{$k} ||= $dbh->prepare($sql);
Condition0%0/3
$sth{$k} ||= $dbh->prepare($sql)
l!l&&r!l&&!r
000
MC/DC0%0/2
$sth{$k} ||= $dbh->prepare($sql)
$sth{$k} $dbh->prepare($sql)
Truth table0%0/3
A ||= B
A= $sth{$k}B= $dbh->prepare($sql)
ABresult
1X1
011
000
311 0 ▶ return undef unless defined $sth{$k}->execute(@b);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sth{$k}->execute(@b) 0 0 0
312 }
313 }
314 0 return scalar @rows;
315 }
316  
317 sub sql ($) {
318 0 ▶ my $self = shift;
Subroutine sql: not called CC=3 SCAR=24.8
Pod documented
319 0 ▶ if (ref $self && $self->isa("DBIx::Perlish")) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and $self->isa('DBIx::Perlish')) { } 0 0 0
Condition0%0/3
ref $self and $self->isa("DBIx::Perlish")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self and $self->isa("DBIx::Perlish")
ref $self $self->isa("DBIx::Perlish")
Truth table0%0/3
A and B
A= ref $selfB= $self->isa("DBIx::Perlish")
ABresult
0X0
100
111
320 0 $self->{sql};
321 } else {
322 0 ▶ sub { $self }
Subroutine __ANON__: not called CC=1 SCAR=6.9
323 0 }
324 }
325 0 ▶sub bind_values { $_[0]->{bind_values} ? @{$_[0]->{bind_values}} : () }
Subroutine bind_values: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'bind_values'} ? : 0 0 0
326  
327 sub gen_sql
328 {
329 345 ▶ my ($sub, $operation, %args) = @_;
Subroutine gen_sql: called CC=61 SCAR=41.1
Pod undocumented
330  
331 345 ▶ $args{quirks} = $non_object_quirks unless $args{quirks};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"quirks"} 326 19 345
332 345 ▶ $args{inline} //= 1;
Condition100%2/2
$args{"inline"} //= 1
l!l
19326
MC/DC100%1/1
$args{"inline"} //= 1
$args{"inline"}
Truth table100%2/2
A
A= $args{"inline"}
Aresult
11
00
333  
334 345 my $S = DBIx::Perlish::Parse::init(%args, operation => $operation);
335 345 DBIx::Perlish::Parse::parse_sub($S, $sub);
336 288 my $sql = "";
337 288 my $next_bit = "";
338 288 my $nret = 9999;
339 288 my $no_aliases;
340 my $dangerous;
341 288 my %flags;
342 288 ▶ if ($operation eq "select") {
Branch: 3 branches
BranchTrueFalseTotal
if ($operation eq 'select') { } 260 28 288
elsif ($operation eq 'delete') { } 2 26 28
elsif ($operation eq 'update') { } 26 0 26
343 260 my $nkf = 0;
344 260 ▶ if ($S->{key_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($S->{"key_fields"}) 7 253 260
345 7 $nkf = @{$S->{key_fields}};
346 7 ▶ push @{$args{key_fields}}, @{$S->{key_fields}} if $args{key_fields};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"key_fields"} 5 2 7
347 }
348 260 $sql = "select ";
349 260 ▶ $sql .= "distinct " if $S->{distinct};
Branch: 1 branch
BranchTrueFalseTotal
if $S->{"distinct"} 2 258 260
350 260 ▶ if ($S->{returns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($S->{'returns'}) { } 80 180 260
351 80 $sql .= join ", ", @{$S->{returns}};
352 80 $nret = @{$S->{returns}};
353 80 for my $ret (@{$S->{returns}}) {
354 118 ▶ $nret = 9999 if $ret =~ /\*/;
Branch: 1 branch
BranchTrueFalseTotal
if $ret =~ /\*/u 15 103 118
355 }
356 $flags{returns_dont_care} = 1 if
357 80 1 == @{$S->{returns}} &&
358 $S->{returns}->[0] =~ /^(.*)\.\*/ &&
359 80 ▶ $S->{returns_dont_care}->{$1}
Branch: 1 branch
BranchTrueFalseTotal
if 1 == @{$S->{"returns"};} and $S->{"returns"}[0] =~ /^(.*)\.\*/u and $S->{"returns_dont_care"}{$1} 2 78 80
Condition100%6/6
1 == @{$S->{"returns"};} and $S->{"returns"}[0] =~ /^(.*)\.\*/u and $S->{"returns_dont_care"}{$1}
!ll&&!rl&&r
7622
1 == @{$S->{"returns"};} and $S->{"returns"}[0] =~ /^(.*)\.\*/u
!ll&&!rl&&r
26504
MC/DC100%3/3
1 == @{$S->{"returns"};} and $S->{"returns"}[0] =~ /^(.*)\.\*/u and $S->{"returns_dont_care"}{$1}
1 == @{$S->{"returns"};} $S->{"returns"}[0] =~ /^(.*)\.\*/u $S->{"returns_dont_care"}{$1}
Truth table100%4/4
A and B and C
A= 1 == @{$S->{"returns"};}B= $S->{"returns"}[0] =~ /^(.*)\.\*/uC= $S->{"returns_dont_care"}{$1}
ABCresult
0XX0
10X0
1100
1111
360 ;
361 } else {
362 180 $sql .= "*";
363 }
364 260 $next_bit = " from ";
365 260 ▶ die "all returns are key fields, this is nonsensical\n" if $nkf == $nret;
Branch: 1 branch
BranchTrueFalseTotal
if $nkf == $nret 2 258 260
366 } elsif ($operation eq "delete") {
367 2 $no_aliases = 1;
368 2 $dangerous = 1;
369 2 $next_bit = "delete from ";
370 } elsif ($operation eq "update") {
371 26 $no_aliases = 1;
372 26 $dangerous = 1;
373 26 $next_bit = "update ";
374 } else {
375 0 die "unsupported operation: $operation\n";
376 }
377 286 my %tabs;
378 286 for my $var (keys %{$S->{vars}}) {
379 193 ▶ $tabs{$S->{var_alias}->{$var}} =
Branch: 1 branch
BranchTrueFalseTotal
$no_aliases ? : 5 188 193
380 $no_aliases ?
381 "$S->{vars}->{$var}" :
382 "$S->{vars}->{$var} $S->{var_alias}->{$var}";
383 }
384 286 for my $tab (keys %{$S->{tabs}}) {
385 130 ▶ $tabs{$S->{tab_alias}->{$tab}} =
Branch: 1 branch
BranchTrueFalseTotal
$no_aliases ? : 21 109 130
386 $no_aliases ?
387 "$tab" :
388 "$tab $S->{tab_alias}->{$tab}";
389 }
390 286 ▶ unless (keys %tabs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %tabs) 12 274 286
391 12 ▶ if ($operation eq "select" && $S->{returns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operation eq 'select' and $S->{'returns'}) { } 9 3 12
Condition100%3/3
$operation eq "select" and $S->{"returns"}
!ll&&!rl&&r
219
MC/DC100%2/2
$operation eq "select" and $S->{"returns"}
$operation eq "select" $S->{"returns"}
Truth table100%3/3
A and B
A= $operation eq "select"B= $S->{"returns"}
ABresult
0X0
100
111
392 9 ▶ if ($args{flavor} && $args{flavor} eq "oracle") {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'flavor'} and $args{'flavor'} eq 'oracle') { } 3 6 9
Condition66%2/3
$args{"flavor"} and $args{"flavor"} eq "oracle"
!ll&&!rl&&r
063
MC/DC50%1/2
$args{"flavor"} and $args{"flavor"} eq "oracle"
$args{"flavor"} $args{"flavor"} eq "oracle"
Truth table66%2/3
A and B
A= $args{"flavor"}B= $args{"flavor"} eq "oracle"
ABresult
0X0
100
111
393 3 $tabs{dual} = "dual";
394 } else {
395 6 $next_bit = " ";
396 }
397 } else {
398 3 die "no tables specified in $operation\n";
399 }
400 }
401 283 $sql .= $next_bit;
402 283 my %seentab;
403 283 my $joins = "";
404 283 for my $j ( @{$S->{joins}} ) {
405 25 my ($join, $tab1, $tab2, $condition) = @$j;
406 25 ▶ $condition = ( defined $condition) ? " on $condition" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $condition ? : 22 3 25
407 die "not sure what to do with repeated tables ($tabs{$tab1} and $tabs{$tab2}) in a join\n"
408 25 ▶ if $seentab{$tab1} && $seentab{$tab2};
Branch: 1 branch
BranchTrueFalseTotal
if $seentab{$tab1} and $seentab{$tab2} 1 24 25
Condition100%3/3
$seentab{$tab1} and $seentab{$tab2}
!ll&&!rl&&r
2131
MC/DC100%2/2
$seentab{$tab1} and $seentab{$tab2}
$seentab{$tab1} $seentab{$tab2}
Truth table100%3/3
A and B
A= $seentab{$tab1}B= $seentab{$tab2}
ABresult
0X0
100
111
409 24 ▶ if ($seentab{$tab2}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seentab{$tab2}) 2 22 24
410 2 ($tab1, $tab2) = ($tab2, $tab1);
411 2 ▶ if ($join eq "left outer") {
Branch: 2 branches
BranchTrueFalseTotal
if ($join eq 'left outer') { } 1 1 2
elsif ($join eq 'right outer') { } 0 1 1
412 1 $join = "right outer";
413 } elsif ($join eq "right outer") {
414 0 $join = "left outer";
415 }
416 }
417 24 ▶ if ($seentab{$tab1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seentab{$tab1}) { } 5 19 24
418 5 ▶ $joins .= " " if $joins;
Branch: 1 branch
BranchTrueFalseTotal
if $joins 5 0 5
419 5 $joins .= "$join join $tabs{$tab2}$condition";
420 } else {
421 19 ▶ $joins .= ", " if $joins;
Branch: 1 branch
BranchTrueFalseTotal
if $joins 1 18 19
422 19 $joins .= "$tabs{$tab1} $join join $tabs{$tab2}$condition";
423 }
424 24 $seentab{$tab1}++;
425 24 $seentab{$tab2}++;
426 }
427 282 ▶ my @joins = $joins ? ($joins) : ();
Branch: 1 branch
BranchTrueFalseTotal
$joins ? : 17 265 282
428 282 $sql .= join ", ", @joins, map { $tabs{$_} } grep { !$seentab{$_} } sort keys %tabs;
429  
430 282 my @sets = grep { $_ ne "" } @{$S->{sets}};
431 282 my @where = grep { $_ ne "" } @{$S->{where}};
432 282 my @having = grep { $_ ne "" } @{$S->{having}};
433 282 my @group_by = grep { $_ ne "" } @{$S->{group_by}};
434 282 my @order_by = grep { $_ ne "" } @{$S->{order_by}};
435  
436 282 ▶ if ($S->{autogroup_needed} && !$S->{no_autogroup} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($S->{"autogroup_needed"} and not $S->{"no_autogroup"} and not @group_by and @{$S->{"autogroup_by"};}) 2 280 282
Condition100%9/9
$S->{"autogroup_needed"} and not $S->{"no_autogroup"} and not @group_by and @{$S->{"autogroup_by"};}
!ll&&!rl&&r
27912
$S->{"autogroup_needed"} and not $S->{"no_autogroup"} and not @group_by
!ll&&!rl&&r
27813
$S->{"autogroup_needed"} and not $S->{"no_autogroup"}
!ll&&!rl&&r
27714
MC/DC100%4/4
$S->{"autogroup_needed"} and not $S->{"no_autogroup"} and not @group_by and @{$S->{"autogroup_by"};}
$S->{"autogroup_needed"} not $S->{"no_autogroup"} not @group_by @{$S->{"autogroup_by"};}
Truth table100%5/5
A and not(B) and not(C) and D
A= $S->{"autogroup_needed"}B= not $S->{"no_autogroup"}C= not @group_byD= @{$S->{"autogroup_by"};}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
437 3 !@group_by && @{$S->{autogroup_by}})
438 {
439 2 @group_by = grep { $_ ne "" } @{$S->{autogroup_by}};
440 }
441 282 ▶ die "nothing to update\n" if $operation eq "update" && !@sets;
Branch: 1 branch
BranchTrueFalseTotal
if $operation eq "update" and not @sets 1 281 282
Condition100%3/3
$operation eq "update" and not @sets
!ll&&!rl&&r
257241
MC/DC100%2/2
$operation eq "update" and not @sets
$operation eq "update" not @sets
Truth table100%3/3
A and not(B)
A= $operation eq "update"B= not @sets
ABresult
0X0
100
111
442  
443 281 ▶ $sql .= " set " . join ", ", @sets if @sets;
Branch: 1 branch
BranchTrueFalseTotal
if @sets 24 257 281
444 281 ▶ $sql .= " where " . join " and ", @where if @where;
Branch: 1 branch
BranchTrueFalseTotal
if @where 164 117 281
445 281 ▶ $sql .= " group by " . join ", ", @group_by if @group_by;
Branch: 1 branch
BranchTrueFalseTotal
if @group_by 4 277 281
446 281 ▶ $sql .= " having " . join " and ", @having if @having;
Branch: 1 branch
BranchTrueFalseTotal
if @having 1 280 281
447 281 ▶ $sql .= " order by " . join ", ", @order_by if @order_by;
Branch: 1 branch
BranchTrueFalseTotal
if @order_by 8 273 281
448  
449 281 ▶ if ($dangerous && !@where && !$S->{seen_exec}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dangerous and not @where and not $S->{"seen_exec"}) 2 279 281
Condition100%6/6
$dangerous and not @where and not $S->{"seen_exec"}
!ll&&!rl&&r
262172
$dangerous and not @where
!ll&&!rl&&r
256619
MC/DC100%3/3
$dangerous and not @where and not $S->{"seen_exec"}
$dangerous not @where not $S->{"seen_exec"}
Truth table100%4/4
A and not(B) and not(C)
A= $dangerousB= not @whereC= not $S->{"seen_exec"}
ABCresult
0XX0
10X0
1100
1111
450 2 die "unfiltered $operation is dangerous: use exec if you want it\n";
451 }
452  
453 279 ▶ my $use_rownum = $args{flavor} && $args{flavor} eq "oracle";
Condition66%2/3
$args{'flavor'} && $args{'flavor'} eq 'oracle'
!ll&&!rl&&r
026712
MC/DC50%1/2
$args{'flavor'} && $args{'flavor'} eq 'oracle'
$args{'flavor'} $args{'flavor'} eq 'oracle'
Truth table66%2/3
A && B
A= $args{'flavor'}B= $args{'flavor'} eq 'oracle'
ABresult
0X0
100
111
454  
455 279 ▶ unless ($use_rownum) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($use_rownum) 267 12 279
456 267 ▶ if ($S->{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($S->{"limit"}) 6 261 267
457 6 $sql .= " limit $S->{limit}";
458 }
459 267 ▶ if ($S->{offset}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($S->{"offset"}) 4 263 267
460 4 $sql .= " offset $S->{offset}";
461 }
462 }
463 279 my $v = $S->{set_values};
464 279 push @$v, @{$S->{ret_values}};
465 279 push @$v, @{$S->{join_values}};
466 279 push @$v, @{$S->{values}};
467  
468 279 for my $add (@{$S->{additions}}) {
469 8 $sql .= " $add->{type} $add->{sql}";
470 8 push @$v, @{$add->{vals}};
471 }
472 279 $sql =~ s/\s+$//;
473  
474 279 ▶ if ( $use_rownum && ( $S->{limit} || $S->{offset} )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_rownum and $S->{"limit"} || $S->{"offset"}) 2 277 279
Condition66%4/6
$use_rownum and $S->{"limit"} || $S->{"offset"}
!ll&&!rl&&r
267120
$S->{"limit"} or $S->{"offset"}
l!l&&r!l&&!r
2010
MC/DC0%0/3
$use_rownum and $S->{"limit"} || $S->{"offset"}
$use_rownum $S->{"limit"} $S->{"offset"}
Truth table50%2/4
A and B or C
A= $use_rownumB= $S->{"limit"}C= $S->{"offset"}
ABCresult
0XX0
1000
11X1
1011
475 2 my @p;
476 2 ▶ push @p, "ROWNUM > " . $S->{offset} if $S->{offset};
Branch: 1 branch
BranchTrueFalseTotal
if $S->{"offset"} 1 1 2
477 2 ▶ push @p, "ROWNUM <= " . ($S->{limit} + ($S->{offset} // 0)) if $S->{limit};
Branch: 1 branch
BranchTrueFalseTotal
if $S->{"limit"} 2 0 2
Condition100%2/2
$S->{"offset"} // 0
l!l
11
MC/DC100%1/1
$S->{"offset"} // 0
$S->{"offset"}
Truth table100%2/2
A
A= $S->{"offset"}
Aresult
11
00
478 2 $sql = "select * from ($sql) where " . join(' and ', @p);
479 }
480  
481 279 return ($sql, $v, $nret, %flags);
482 }
483  
484  
485 25 1;
486 __END__
« Perlish/Parse.pm ↑ Summary