blib/lib/DBIx/ThinSQL.pm

filter: Statementstmt 75.5% 361 / 478 Branchbran 68.7% 121 / 176 Conditioncond 34.4% 10 / 29 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 61.0% 58 / 95 Podpod 24.0% 6 / 25 totaltotal 67.9% 557 / 820 CC 113 SCAR 24.6
CC
113
Cov
72%
sql_bv
34.7
new
24.9
vals
20.0
CRAP 11.7
↑ Summary blib/lib/DBIx/ThinSQL/Deploy.pm »
1 package DBIx::ThinSQL;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use Exporter::Tidy
6 3 ▶ other => [qw/ bv qv qi sq func OR AND /],
Subroutine BEGIN: called CC=1 SCAR=0.0
7 sql => [
8 qw/
9 case
10 cast
11 coalesce
12 concat
13 count
14 exists
15 hex
16 length
17 lower
18 ltrim
19 max
20 min
21 replace
22 rtrim
23 substr
24 sum
25 upper
26 /
27 3 ];
28  
29 3 our @ISA = 'DBI';
30 3 our $VERSION = '0.0.49';
31  
32 0 ▶sub ejoin {
Subroutine ejoin: not called CC=2 SCAR=17.9
Pod undocumented
33 0 my $joiner = shift;
34 0 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
35  
36 0 my @tokens = map { $_, $joiner } @_;
37 0 pop @tokens;
38  
39 0 return @tokens;
40 }
41  
42 0 ▶sub func {
Subroutine func: not called CC=1 SCAR=6.9
Pod documented
43 0 my $func = uc shift;
44 0 my $joiner = shift;
45  
46 0 return DBIx::ThinSQL::expr->new( $func, '(',
47 DBIx::ThinSQL::ejoin( $joiner, @_ ), ')' );
48 }
49  
50 7 ▶sub bv { DBIx::ThinSQL::expr->new( DBIx::ThinSQL::bind_value->new(@_) ) }
Subroutine bv: called CC=1 SCAR=0.0
Pod documented
51  
52 3 ▶sub qv { DBIx::ThinSQL::expr->new( DBIx::ThinSQL::quote_value->new(@_) ) }
Subroutine qv: called CC=1 SCAR=0.0
Pod documented
53  
54 0 ▶sub qi { DBIx::ThinSQL::expr->new( DBIx::ThinSQL::quote_identifier->new(@_) ) }
Subroutine qi: not called CC=1 SCAR=6.9
Pod undocumented
55  
56 0 ▶sub OR { ' OR ' }
Subroutine OR: not called CC=1 SCAR=6.9
Pod documented
57  
58 0 ▶sub AND { ' AND ' }
Subroutine AND: not called CC=1 SCAR=6.9
Pod documented
59  
60 0 ▶sub cast { func( 'cast', ' ', @_ ) }
Subroutine cast: not called CC=1 SCAR=6.9
Pod undocumented
61  
62 0 ▶sub case { DBIx::ThinSQL::case->new(@_) }
Subroutine case: not called CC=1 SCAR=6.9
Pod undocumented
63  
64 0 ▶sub coalesce { func( 'coalesce', ', ', @_ ) }
Subroutine coalesce: not called CC=1 SCAR=6.9
Pod undocumented
65  
66 0 ▶sub concat { DBIx::ThinSQL::expr->new( DBIx::ThinSQL::ejoin( ' || ', @_ ) ) }
Subroutine concat: not called CC=1 SCAR=6.9
Pod undocumented
67  
68 0 ▶sub count { func( 'count', ', ', @_ ) }
Subroutine count: not called CC=1 SCAR=6.9
Pod undocumented
69  
70 0 ▶sub exists { func( 'exists', ', ', @_ ) }
Subroutine exists: not called CC=1 SCAR=6.9
Pod undocumented
71  
72 0 ▶sub hex { func( 'hex', ', ', @_ ) }
Subroutine hex: not called CC=1 SCAR=6.9
Pod undocumented
73  
74 0 ▶sub length { func( 'length', ', ', @_ ) }
Subroutine length: not called CC=1 SCAR=6.9
Pod undocumented
75  
76 0 ▶sub lower { func( 'lower', ', ', @_ ) }
Subroutine lower: not called CC=1 SCAR=6.9
Pod undocumented
77  
78 0 ▶sub ltrim { func( 'ltrim', ', ', @_ ) }
Subroutine ltrim: not called CC=1 SCAR=6.9
Pod undocumented
79  
80 0 ▶sub max { func( 'max', ', ', @_ ) }
Subroutine max: not called CC=1 SCAR=6.9
Pod undocumented
81  
82 0 ▶sub min { func( 'min', ', ', @_ ) }
Subroutine min: not called CC=1 SCAR=6.9
Pod undocumented
83  
84 0 ▶sub replace { func( 'replace', ', ', @_ ) }
Subroutine replace: not called CC=1 SCAR=6.9
Pod undocumented
85  
86 0 ▶sub rtrim { func( 'rtrim', ', ', @_ ) }
Subroutine rtrim: not called CC=1 SCAR=6.9
Pod undocumented
87  
88 0 ▶sub substr { func( 'substr', ', ', @_ ) }
Subroutine substr: not called CC=1 SCAR=6.9
Pod undocumented
89  
90 1 ▶sub sq { DBIx::ThinSQL::query->new(@_) }
Subroutine sq: called CC=1 SCAR=0.0
Pod documented
91  
92 0 ▶sub sum { func( 'sum', '', @_ ) }
Subroutine sum: not called CC=1 SCAR=6.9
Pod undocumented
93  
94 0 ▶sub upper { func( 'upper', ', ', @_ ) }
Subroutine upper: not called CC=1 SCAR=6.9
Pod undocumented
95  
96 package DBIx::ThinSQL::db;
97 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
100 3 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
101 3 ▶use DBIx::ThinSQL::Driver;
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 3 our @ISA = qw(DBI::db);
104 3 our @CARP_NOT;
105  
106 35 ▶sub share_dir {
Subroutine share_dir: called CC=1 SCAR=0.0
107 35 require File::ShareDir;
108 35 require Path::Tiny;
109 35 return Path::Tiny::path( File::ShareDir::dist_dir('DBIx-ThinSQL') );
110 }
111  
112 2 ▶sub throw_error {
Subroutine throw_error: called CC=1 SCAR=0.0
113 2 my $self = shift;
114 2 Carp::croak(@_);
115 }
116  
117 237 ▶sub sql_bv {
Subroutine sql_bv: called CC=29 SCAR=35.5
118 237 my $self = shift;
119 237 my $sql = shift;
120 237 my $bv = shift;
121 237 my $val = shift;
122 237 my $prefix = shift;
123  
124 237 ▶ $prefix = '' unless length($prefix);
Branch: 1 branch
BranchTrueFalseTotal
unless length $prefix 34 203 237
125  
126 237 my $ref = ref $val;
127 237 my $prefix2 = $prefix . ' ';
128  
129 # When we call ourself we already have a ref
130  
131 237 ▶ if ( $ref eq '' ) {
Branch: 10 branches
BranchTrueFalseTotal
if ($ref eq '') { } 113 124 237
elsif ($ref eq 'DBIx::ThinSQL::query') { } 36 88 124
elsif ($ref eq 'DBIx::ThinSQL::list') { } 22 66 88
elsif ($ref eq 'DBIx::ThinSQL::table') { } 4 62 66
elsif ($ref eq 'DBIx::ThinSQL::values') { } 10 52 62
elsif ($ref eq 'DBIx::ThinSQL::case') { } 0 52 52
elsif ($ref eq 'DBIx::ThinSQL::expr') { } 19 33 52
elsif ($ref eq 'DBIx::ThinSQL::bind_value') { } 29 4 33
elsif ($ref eq 'DBIx::ThinSQL::quote_value') { } 3 1 4
elsif ($ref eq 'DBIx::ThinSQL::quote_identifier') { } 1 0 1
132 113 ▶ $$sql .= defined $val ? $val : 'NULL';
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 113 0 113
133 }
134 elsif ( $ref eq 'DBIx::ThinSQL::query' ) {
135 36 ▶ my $bracket = length($prefix) ? '(' : '';
Branch: 1 branch
BranchTrueFalseTotal
length $prefix ? : 2 34 36
136 36 foreach my $pair ( $val->tokens ) {
137 94 ▶ $$sql .= "\n" if $pair->[0] =~ /UNION/;
Branch: 1 branch
BranchTrueFalseTotal
if $pair->[0] =~ /UNION/ 0 94 94
138 94 ▶ my $join_on = length( $pair->[0] )
Branch: 1 branch
BranchTrueFalseTotal
length $pair->[0] && $pair->[0] =~ /(JOIN)|(ON)/ ? : 0 94 94
Condition33%1/3
length $pair->[0] and $pair->[0] =~ /(JOIN)|(ON)/
!ll&&!rl&&r
0940
MC/DC0%0/2
length $pair->[0] and $pair->[0] =~ /(JOIN)|(ON)/
length $pair->[0] $pair->[0] =~ /(JOIN)|(ON)/
Truth table33%1/3
A and B
A= length $pair->[0]B= $pair->[0] =~ /(JOIN)|(ON)/
ABresult
0X0
100
111
139 && ( $pair->[0] =~ m/(JOIN)|(ON)/ ) ? ' ' : '';
140 94 ▶ $$sql .=
Branch: 1 branch
BranchTrueFalseTotal
if length $pair->[0] 94 0 94
Condition66%2/3
$bracket || $prefix . $join_on
l!l&&r!l&&!r
2092
MC/DC50%1/2
$bracket || $prefix . $join_on
$bracket $prefix . $join_on
Truth table66%2/3
A || B
A= $bracketB= $prefix . $join_on
ABresult
1X1
011
000
141 ( $bracket || $prefix . $join_on ) . $pair->[0] . "\n" . $prefix2
142 if length( $pair->[0] );
143 94 $self->sql_bv( $sql, $bv, $pair->[1], $prefix2 );
144 94 ▶ $$sql .= "\n" if length( $pair->[0] ) or length( $pair->[1] );
Branch: 1 branch
BranchTrueFalseTotal
if length $pair->[0] or length $pair->[1] 94 0 94
Condition33%1/3
length $pair->[0] or length $pair->[1]
l!l&&r!l&&!r
9400
MC/DC0%0/2
length $pair->[0] or length $pair->[1]
length $pair->[0] length $pair->[1]
Truth table33%1/3
A or B
A= length $pair->[0]B= length $pair->[1]
ABresult
1X1
011
000
145 94 $bracket = '';
146 }
147 36 ▶ $$sql .= $prefix . ")" if length($prefix);
Branch: 1 branch
BranchTrueFalseTotal
if length $prefix 2 34 36
148 }
149 elsif ( $ref eq 'DBIx::ThinSQL::list' ) {
150 22 my @tokens = $val->tokens;
151 22 my $last = pop @tokens;
152 22 my $i = 0;
153 22 foreach my $token (@tokens) {
154 20 ▶ $$sql .= $prefix if $i++;
Branch: 1 branch
BranchTrueFalseTotal
if $i++ 0 20 20
155 20 $self->sql_bv( $sql, $bv, $token, $prefix );
156 20 $$sql .= ",\n";
157 }
158 22 $$sql .= $prefix;
159 22 $self->sql_bv( $sql, $bv, $last, $prefix );
160 }
161 elsif ( $ref eq 'DBIx::ThinSQL::table' ) {
162 4 my @tokens = $val->tokens;
163 4 my $table = shift @tokens;
164 4 $$sql .= $table . "(\n";
165  
166 4 my $last = pop @tokens;
167 4 foreach my $token (@tokens) {
168 4 $$sql .= $prefix2;
169 4 $self->sql_bv( $sql, $bv, $token, $prefix2 );
170 4 $$sql .= ",\n";
171 }
172 4 $$sql .= $prefix2;
173 4 $self->sql_bv( $sql, $bv, $last, $prefix2 );
174 4 $$sql .= "\n" . $prefix . ")";
175 }
176 elsif ( $ref eq 'DBIx::ThinSQL::values' ) {
177 10 my @tokens = $val->tokens;
178 10 $$sql .= "(\n";
179  
180 10 my $last = pop @tokens;
181 10 foreach my $token (@tokens) {
182 10 $$sql .= $prefix2;
183 10 $self->sql_bv( $sql, $bv, $token, $prefix2 );
184 10 $$sql .= ",\n";
185 }
186 10 $$sql .= $prefix2;
187 10 $self->sql_bv( $sql, $bv, $last, $prefix2 );
188 10 $$sql .= "\n" . $prefix . ")";
189 }
190 elsif ( $ref eq 'DBIx::ThinSQL::case' ) {
191 0 $$sql .= "CASE\n";
192 0 my @tokens = $val->tokens;
193 0 foreach my $pair (@$val) {
194 0 $$sql .= $prefix2 . $pair->[0] . "\n" . $prefix2 . ' ';
195 0 $self->sql_bv( $sql, $bv, $pair->[1], $prefix2 );
196 0 $$sql .= "\n";
197 }
198 0 $$sql .= $prefix . "END";
199 }
200 elsif ( $ref eq 'DBIx::ThinSQL::expr' ) {
201 19 foreach my $token ( $val->tokens ) {
202 39 $self->sql_bv( $sql, $bv, $token, $prefix );
203 }
204 }
205 elsif ( $ref eq 'DBIx::ThinSQL::bind_value' ) {
206 29 $$sql .= '?';
207 29 push( @{$bv}, $val );
208 }
209 elsif ( $ref eq 'DBIx::ThinSQL::quote_value' ) {
210 3 $$sql .= $self->quote( $val->for_quote );
211 }
212 elsif ( $ref eq 'DBIx::ThinSQL::quote_identifier' ) {
213 1 $$sql .= $self->quote_identifier( $val->val );
214 }
215 else {
216 0 Carp::cluck "sql_bv doesn't know $ref";
217 }
218 }
219  
220 34 ▶sub query {
Subroutine query: called CC=1 SCAR=0.0
221 34 my $self = shift;
222 34 my ( $sql, @bv ) = ('');
223 34 $self->sql_bv( \$sql, \@bv, DBIx::ThinSQL::query->new(@_) );
224 34 return $sql . ";\n", @bv;
225 }
226  
227 34 ▶sub xprepare {
Subroutine xprepare: called CC=6 SCAR=19.0
228 34 my $self = shift;
229 34 ▶ Carp::croak('xprepare requires arguments!') unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 34 34
230  
231 34 my ( $sql, @bv ) = $self->query(@_);
232  
233 # TODO these locals have no effect?
234 34 local $self->{RaiseError} = 1;
235 34 local $self->{PrintError} = 0;
236 34 local $self->{ShowErrorStatement} = 1;
237  
238 34 my $prepare_ok;
239 my $sth;
240 my $prepare =
241 34 ▶ exists $self->{'_dbix_thinsql_prepare_cached'}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_dbix_thinsql_prepare_cached'} ? : 0 34 34
242 ? 'prepare_cached'
243 : 'prepare';
244  
245 34 eval {
246 34 $sth = $self->$prepare($sql);
247 34 $prepare_ok = 1;
248  
249 34 my $i = 1;
250 34 foreach my $bv (@bv) {
251 29 $sth->bind_param( $i++, $bv->for_bind_param );
252 }
253 };
254  
255 34 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 34 34
256 0 ▶ $log->debug($sql) unless $prepare_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $prepare_ok 0 0 0
257 0 $self->throw_error($@);
258 }
259  
260 34 return $sth;
261 }
262  
263 0 ▶sub xprepare_cached {
Subroutine xprepare_cached: not called CC=1 SCAR=6.9
264 0 my $self = shift;
265 0 local $self->{'_dbix_thinsql_prepare_cached'} = 1;
266 0 return $self->xprepare(@_);
267 }
268  
269 13 ▶sub xdo {
Subroutine xdo: called CC=1 SCAR=0.0
270 13 my $self = shift;
271 13 my $sth = $self->xprepare(@_);
272 13 return $sth->execute;
273 }
274  
275 0 ▶sub log_debug {
Subroutine log_debug: not called CC=2 SCAR=17.9
276 0 my $self = shift;
277 0 my $sql = (shift) . "\n";
278  
279 0 my $sth = $self->prepare( $sql . ';' );
280 0 $sth->execute(@_);
281  
282 0 my $out = join( ', ', @{ $sth->{NAME} } ) . "\n";
283 0 $out .= ' ' . ( '-' x length $out ) . "\n";
284 0 $out .= ' ' . DBI::neat_list($_) . "\n" for @{ $sth->fetchall_arrayref };
285 0 $log->debug($out);
286 }
287  
288 0 ▶sub log_warn {
Subroutine log_warn: not called CC=2 SCAR=17.9
289 0 my $self = shift;
290 0 my $sql = (shift) . "\n";
291  
292 0 my $sth = $self->prepare( $sql . ';' );
293 0 $sth->execute(@_);
294  
295 0 my $out = join( ', ', @{ $sth->{NAME} } ) . "\n";
296 0 $out .= ' ' . ( '-' x length $out ) . "\n";
297 0 $out .= ' ' . DBI::neat_list($_) . "\n" for @{ $sth->fetchall_arrayref };
298 0 warn $out;
299 }
300  
301 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=6.9
302 0 my $self = shift;
303 0 my $sth = $self->prepare(shift);
304 0 $sth->execute(@_);
305 0 $sth->dump_results;
306 }
307  
308 0 ▶sub xdump {
Subroutine xdump: not called CC=2 SCAR=17.9
309 0 my $self = shift;
310 0 my $sth = $self->xprepare(@_);
311 0 $sth->execute;
312 0 my $header = $sth->{NAME};
313 0 my $data = $sth->fetchall_arrayref;
314 0 unshift @$data, $header;
315 0 ▶ if ( eval { require Text::Table::Tiny } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Text::Table::Tiny } }) { } 0 0 0
316 0 print Text::Table::Tiny::generate_table(
317 header_row => 1,
318 rows => $data
319 ),
320 "\n";
321 }
322 else {
323 0 $sth->dump_results;
324 }
325 }
326  
327 3 ▶sub xval {
Subroutine xval: called CC=2 SCAR=7.1
328 3 my $self = shift;
329  
330 3 my $sth = $self->xprepare(@_);
331 3 $sth->execute;
332 3 my $ref = $sth->arrayref;
333 3 $sth->finish;
334  
335 3 ▶ return $ref->[0] if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 3 0 3
336 0 return;
337 }
338  
339 0 ▶sub xvals {
Subroutine xvals: not called CC=1 SCAR=6.9
340 0 my $self = shift;
341 0 my $sth = $self->xprepare(@_);
342 0 $sth->execute;
343 0 return $sth->vals;
344 }
345  
346 2 ▶sub xlist {
Subroutine xlist: called CC=2 SCAR=6.9
347 2 my $self = shift;
348  
349 2 my $sth = $self->xprepare(@_);
350 2 $sth->execute;
351 2 my $ref = $sth->arrayref;
352 2 $sth->finish;
353  
354 2 ▶ return @$ref if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 1 1 2
355 1 return;
356 }
357  
358 2 ▶sub xarrayref {
Subroutine xarrayref: called CC=2 SCAR=6.9
359 2 my $self = shift;
360  
361 2 my $sth = $self->xprepare(@_);
362 2 $sth->execute;
363 2 my $ref = $sth->arrayref;
364 2 $sth->finish;
365  
366 2 ▶ return $ref if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 1 1 2
367 1 return;
368 }
369  
370 8 ▶sub xarrayrefs {
Subroutine xarrayrefs: called CC=1 SCAR=0.0
371 8 my $self = shift;
372  
373 8 my $sth = $self->xprepare(@_);
374 8 $sth->execute;
375  
376 8 return $sth->arrayrefs;
377 }
378  
379 2 ▶sub xhashref {
Subroutine xhashref: called CC=2 SCAR=6.9
380 2 my $self = shift;
381  
382 2 my $sth = $self->xprepare(@_);
383 2 $sth->execute;
384 2 my $ref = $sth->hashref;
385 2 $sth->finish;
386  
387 2 ▶ return $ref if $ref;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 1 1 2
388 1 return;
389 }
390  
391 4 ▶sub xhashrefs {
Subroutine xhashrefs: called CC=1 SCAR=0.0
392 4 my $self = shift;
393  
394 4 my $sth = $self->xprepare(@_);
395 4 $sth->execute;
396 4 return $sth->hashrefs;
397 }
398  
399 # Can't use 'local' to managed txn count here because $self is a tied hashref?
400 # Also can't use ||=.
401 6 ▶sub txn {
Subroutine txn: called CC=17 SCAR=28.6
402 6 my $self = shift;
403 6 my $subref = shift;
404 6 my $wantarray = wantarray;
405 6 my $txn = $self->{private_DBIx_ThinSQL_txn}++;
406 6 my $driver = $self->{private_DBIx_ThinSQL_driver};
407  
408 6 ▶ $driver ||= $self->{private_DBIx_ThinSQL_driver} = do {
Condition66%2/3
$driver ||= ($self->{"private_DBIx_ThinSQL_driver"} = do { my $class = 'DBIx::ThinSQL::Driver::' . $self->{"Driver"}{"Name"}; (my $path = $class) =~ s[::][/]g; $path .= '.pm'; eval { do { require $path; $class->new } } or "DBIx::ThinSQL::Driver"->new })
l!l&&r!l&&!r
510
MC/DC0%0/2
$driver ||= ($self->{"private_DBIx_ThinSQL_driver"} = do { my $class = 'DBIx::ThinSQL::Driver::' . $self->{"Driver"}{"Name"}; (my $path = $class) =~ s[::][/]g; $path .= '.pm'; eval { do { require $path; $class->new } } or "DBIx::ThinSQL::Driver"->new })
$driver ($self->{"private_DBIx_ThinSQL_driver"} = do { my $class = 'DBIx::ThinSQL::Driver::' . $self->{"Driver"}{"Name"}; (my $path = $class) =~ s[::][/]g; $path .= '.pm'; eval { do { require $path; $class->new } } or "DBIx::ThinSQL::Driver"->new })
Truth table66%2/3
A ||= B
A= $driverB= ($self->{"private_DBIx_ThinSQL_driver"} = do { my $class = 'DBIx::ThinSQL::Driver::' . $self->{"Driver"}{"Name"}; (my $path = $class) =~ s[::][/]g; $path .= '.pm'; eval { do { require $path; $class->new } } or "DBIx::ThinSQL::Driver"->new })
ABresult
1X1
011
000
409 1 my $class = 'DBIx::ThinSQL::Driver::' . $self->{Driver}->{Name};
410 1 ( my $path = $class ) =~ s{::}{/}g;
411 1 $path .= '.pm';
412  
413 1 ▶ eval { require $path; $class->new } || DBIx::ThinSQL::Driver->new;
Condition33%1/3
eval { do { require $path; $class->new } } || 'DBIx::ThinSQL::Driver'->new
l!l&&r!l&&!r
100
MC/DC0%0/2
eval { do { require $path; $class->new } } || 'DBIx::ThinSQL::Driver'->new
eval { do { require $path; $class->new } } 'DBIx::ThinSQL::Driver'->new
Truth table33%1/3
A || B
A= eval { do { require $path; $class->new } }B= 'DBIx::ThinSQL::Driver'->new
ABresult
1X1
011
000
414 };
415  
416 6 my $current;
417 6 ▶ if ( !$txn ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($txn) 4 2 6
418 $current = {
419 RaiseError => $self->{RaiseError},
420 ShowErrorStatement => $self->{ShowErrorStatement},
421 4 };
422  
423 }
424  
425 6 ▶ $self->{RaiseError} = 1 unless exists $self->{HandleError};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"HandleError"} 6 0 6
426 6 $self->{ShowErrorStatement} = 1;
427  
428 6 my @result;
429 my $result;
430  
431 6 ▶ if ( !$txn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $txn) { } 4 2 6
432 4 $self->begin_work;
433 }
434 else {
435 2 $driver->savepoint( $self, 'txn' . $txn );
436 }
437  
438 6 eval {
439  
440 6 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 1 5 6
441 1 @result = $subref->();
442 }
443 else {
444 5 $result = $subref->();
445 }
446  
447 4 ▶ if ( !$txn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $txn) { } 3 1 4
448  
449 # We check again for the AutoCommit state in case the
450 # $subref did something like its own ->rollback(). This
451 # really just prevents a warning from being printed.
452 3 ▶ $self->commit unless $self->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"AutoCommit"} 3 0 3
453 }
454 else {
455 $driver->release( $self, 'txn' . $txn )
456 1 ▶ unless $self->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"AutoCommit"} 1 0 1
457 }
458  
459 };
460 6 my $error = $@;
461  
462 6 $self->{private_DBIx_ThinSQL_txn} = $txn;
463 6 ▶ if ( !$txn ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($txn) 4 2 6
464 4 $self->{RaiseError} = $current->{RaiseError};
465 4 $self->{ShowErrorStatement} = $current->{ShowErrorStatement};
466 }
467  
468 6 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 2 4 6
469  
470 2 eval {
471 2 ▶ if ( !$txn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $txn) { } 1 1 2
472  
473 # If the transaction failed at COMMIT, then we can no
474 # longer roll back. Maybe put this around the eval for
475 # the RELEASE case as well??
476 1 ▶ $self->rollback unless $self->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"AutoCommit"} 1 0 1
477 }
478 else {
479 $driver->rollback_to( $self, 'txn' . $txn )
480 1 ▶ unless $self->{AutoCommit};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"AutoCommit"} 1 0 1
481 }
482 };
483  
484 2 ▶ $self->throw_error(
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
485 $error . "\nAdditionally, an error occured during
486 rollback:\n$@"
487 ) if $@;
488  
489 2 $self->throw_error($error);
490 }
491  
492 4 ▶ return $wantarray ? @result : $result;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 1 3 4
493 }
494  
495 package DBIx::ThinSQL::st;
496 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
497 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
498  
499 3 our @ISA = qw(DBI::st);
500  
501 0 ▶sub val {
Subroutine val: not called CC=2 SCAR=17.9
502 0 my $self = shift;
503 0 ▶ my $ref = $self->fetchrow_arrayref || return;
Condition0%0/2
$self->fetchrow_arrayref || (return)
l!l
00
MC/DC0%0/1
$self->fetchrow_arrayref || (return)
$self->fetchrow_arrayref
Truth table0%0/2
A
A= $self->fetchrow_arrayref
Aresult
11
00
504 0 return $ref->[0];
505 }
506  
507 0 ▶sub vals {
Subroutine vals: not called CC=4 SCAR=30.0
508 0 my $self = shift;
509 0 ▶ my $all = $self->fetchall_arrayref || return;
Condition0%0/2
$self->fetchall_arrayref || (return)
l!l
00
MC/DC0%0/1
$self->fetchall_arrayref || (return)
$self->fetchall_arrayref
Truth table0%0/2
A
A= $self->fetchall_arrayref
Aresult
11
00
510 0 ▶ return unless @$all;
Branch: 1 branch
BranchTrueFalseTotal
unless @$all 0 0 0
511 0 ▶ return map { $_->[0] } @$all if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
512 0 return [ map { $_->[0] } @$all ];
513 }
514  
515 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
516 0 my $self = shift;
517 0 ▶ my $ref = $self->fetchrow_arrayref || return;
Condition0%0/2
$self->fetchrow_arrayref || (return)
l!l
00
MC/DC0%0/1
$self->fetchrow_arrayref || (return)
$self->fetchrow_arrayref
Truth table0%0/2
A
A= $self->fetchrow_arrayref
Aresult
11
00
518 0 return @$ref;
519 }
520  
521 7 ▶sub arrayref {
Subroutine arrayref: called CC=2 SCAR=7.1
522 7 my $self = shift;
523 7 ▶ return unless $self->{Active};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Active"} 0 7 7
524 7 return $self->fetchrow_arrayref;
525 }
526  
527 8 ▶sub arrayrefs {
Subroutine arrayrefs: called CC=5 SCAR=16.2
528 8 my $self = shift;
529 8 ▶ return unless $self->{Active};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Active"} 0 8 8
530  
531 8 ▶ my $all = $self->fetchall_arrayref || return;
Condition50%1/2
$self->fetchall_arrayref || (return)
l!l
80
MC/DC0%0/1
$self->fetchall_arrayref || (return)
$self->fetchall_arrayref
Truth table50%1/2
A
A= $self->fetchall_arrayref
Aresult
11
00
532 8 ▶ return unless @$all;
Branch: 1 branch
BranchTrueFalseTotal
unless @$all 2 6 8
533 6 ▶ return @$all if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 2 4 6
534 4 return $all;
535 }
536  
537 2 ▶sub hashref {
Subroutine hashref: called CC=2 SCAR=7.1
538 2 my $self = shift;
539 2 ▶ return unless $self->{Active};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Active"} 0 2 2
540  
541 2 return $self->fetchrow_hashref('NAME_lc');
542 }
543  
544 4 ▶sub hashrefs {
Subroutine hashrefs: called CC=4 SCAR=13.9
545 4 my $self = shift;
546 4 ▶ return unless $self->{Active};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Active"} 0 4 4
547  
548 4 my @all;
549 4 ▶ while ( my $ref = $self->fetchrow_hashref('NAME_lc') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $self->fetchrow_hashref("NAME_lc")) 4 4 8
550 4 push( @all, $ref );
551 }
552  
553 4 ▶ return @all if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 2 2 4
554 2 return \@all;
555 }
556  
557 package DBIx::ThinSQL::bind_value;
558 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
559 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
560 3 our @ISA = ('DBIx::ThinSQL::expr');
561  
562 31 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
563 31 my $class = shift;
564 31 ▶ return $_[0] if ref( $_[0] ) =~ m/DBIx::ThinSQL/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($_[0]) =~ /DBIx::ThinSQL/ 2 29 31
565 0 ▶ return ${ $_[0] } if ref $_[0] eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "SCALAR" 0 29 29
566 29 return bless [@_], $class;
567 }
568  
569 0 ▶sub val {
Subroutine val: not called CC=1 SCAR=6.9
570 0 return $_[0]->[0];
571 }
572  
573 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
574 0 return $_[0]->[1];
575 }
576  
577 29 ▶sub for_bind_param {
Subroutine for_bind_param: called CC=2 SCAR=7.1
578 29 my $self = shift;
579  
580 # value, type
581 29 ▶ return @$self if defined $self->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[1] 0 29 29
582  
583 # value
584 29 return $self->[0];
585 }
586  
587 package DBIx::ThinSQL::quote_value;
588 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
589 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
590 3 our @ISA = ('DBIx::ThinSQL::expr');
591  
592 3 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
593 3 my $class = shift;
594 3 ▶ return $_[0] if ref( $_[0] ) =~ m/DBIx::ThinSQL/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($_[0]) =~ /DBIx::ThinSQL/ 0 3 3
595 3 ▶ return $$_[0] if ref $_[0] eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "SCALAR" 0 3 3
596 3 return bless [@_], $class;
597 }
598  
599 0 ▶sub val {
Subroutine val: not called CC=1 SCAR=6.9
600 0 return $_[0]->[0];
601 }
602  
603 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
604 0 return $_[0]->[1];
605 }
606  
607 3 ▶sub for_quote {
Subroutine for_quote: called CC=2 SCAR=7.1
608 3 my $self = shift;
609  
610 # value, type
611 3 ▶ return @$self if defined $self->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[1] 0 3 3
612  
613 # value
614 3 return $self->[0];
615 }
616  
617 package DBIx::ThinSQL::quote_identifier;
618 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
619 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
620  
621 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
622 1 my $class = shift;
623 1 my $id = shift;
624 1 return bless \$id, $class;
625 }
626  
627 1 ▶sub val {
Subroutine val: called CC=1 SCAR=0.0
628 1 my $self = shift;
629 1 return $$self;
630 }
631  
632 package DBIx::ThinSQL::expr;
633 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
634 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
635  
636 19 ▶sub new {
Subroutine new: called CC=18 SCAR=32.1
637 19 my $class = shift;
638 19 my @tokens;
639 19 foreach my $token (@_) {
640 31 ▶ if ( ref $token eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $token eq 'ARRAY') { } 0 31 31
elsif (ref $token eq 'HASH') { } 2 29 31
641 0 push( @tokens, DBIx::ThinSQL::query->new(@$token) );
642 }
643 elsif ( ref $token eq 'HASH' ) {
644 2 my @cols = sort keys %$token;
645 2 my $narg;
646  
647 2 foreach my $col (@cols) {
648 2 ▶ if ( ref $token->{$col} eq 'SCALAR' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $token->{$col} eq 'SCALAR') { } 0 2 2
elsif (ref $token->{$col} eq 'ARRAY') { } 1 1 2
elsif (defined $token->{$col}) { } 1 0 1
649 0 $narg->{$col} = ${ $token->{$col} };
650 }
651 elsif ( ref $token->{$col} eq 'ARRAY' ) {
652 $narg->{$col} =
653 2 [ map { DBIx::ThinSQL::bind_value->new($_) }
654 1 @{ $token->{$col} } ];
655 }
656 elsif ( defined $token->{$col} ) {
657 $narg->{$col} =
658 1 DBIx::ThinSQL::bind_value->new( $token->{$col} );
659 }
660 }
661  
662 2 foreach my $col (@cols) {
663 2 my $val = $narg->{$col};
664  
665 2 my $like = $col =~ s/\s+like$/ LIKE /i;
666 2 my $not_like = $col =~ s/\s+(!|not)\s*like$/ NOT LIKE /i;
667 2 my $not = $col =~ s/\s*!$//;
668 2 my $gtlt = $col =~ s/(\s+[><]=?)$/$1 /;
669  
670 2 push( @tokens, $col );
671 2 ▶ if ( !defined $val ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val) { } 0 2 2
elsif (ref $val eq 'ARRAY') { } 1 1 2
672 0 ▶ push( @tokens, ' IS ', $not ? 'NOT NULL' : 'NULL' );
Branch: 1 branch
BranchTrueFalseTotal
$not ? : 0 0 0
673 }
674 elsif ( ref $val eq 'ARRAY' ) {
675 1 ▶ push( @tokens, ' NOT' ) if $not;
Branch: 1 branch
BranchTrueFalseTotal
if $not 1 0 1
676 1 push( @tokens, ' IN (', map { $_, ',' } @$val );
677 1 ▶ pop(@tokens) if @$val;
Branch: 1 branch
BranchTrueFalseTotal
if @$val 1 0 1
678 1 push( @tokens, ')' );
679 }
680 else {
681 1 ▶ push( @tokens, $not ? ' != ' : ' = ' )
Branch: 2 branches
BranchTrueFalseTotal
unless $like or $not_like or $gtlt 1 0 1
$not ? : 0 1 1
Condition33%2/6
$like or $not_like or $gtlt
l!l&&r!l&&!r
001
$like or $not_like
l!l&&r!l&&!r
001
MC/DC0%0/3
$like or $not_like or $gtlt
$like $not_like $gtlt
Truth table25%1/4
A or B or C
A= $likeB= $not_likeC= $gtlt
ABCresult
1XX1
01X1
0011
0000
682 unless $like
683 or $not_like
684 or $gtlt;
685  
686 1 push( @tokens, $val );
687 }
688 2 push( @tokens, ' AND ' );
689 }
690 2 pop @tokens;
691 }
692 else {
693 29 push( @tokens, $token );
694 }
695 }
696 19 return bless \@tokens, $class;
697 }
698  
699 0 ▶sub as {
Subroutine as: not called CC=1 SCAR=6.9
700 0 my $self = shift;
701 0 my $value = shift;
702  
703 0 return DBIx::ThinSQL::expr->new( $self, ' AS ',
704 DBIx::ThinSQL::quote_identifier->new($value) );
705 }
706  
707 19 ▶sub tokens {
Subroutine tokens: called CC=1 SCAR=0.0
708 19 my $self = shift;
709 19 return @$self;
710 }
711  
712 package DBIx::ThinSQL::case;
713 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
714 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
715 3 our @ISA = ('DBIx::ThinSQL::expr');
716  
717 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
718 0 my $class = shift;
719 0 my @tokens;
720  
721 0 ▶ while ( my ( $key, $val ) = splice( @_, 0, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
722 0 ( $key = uc($key) ) =~ s/_/ /g;
723 0 push( @tokens, [ $key, DBIx::ThinSQL::expr->new($val) ] );
724 }
725  
726 0 return bless \@tokens, $class;
727 }
728  
729 package DBIx::ThinSQL::list;
730 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
731 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
732  
733 36 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
734 36 my $class = shift;
735 36 my @tokens;
736 36 foreach my $token (@_) {
737 74 ▶ if ( ref $token eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $token eq 'ARRAY') { } 0 74 74
738 0 push( @tokens, DBIx::ThinSQL::query->new(@$token) );
739 }
740 else {
741 74 push( @tokens, $token );
742 }
743 }
744 36 return bless \@tokens, $class;
745 }
746  
747 36 ▶sub tokens {
Subroutine tokens: called CC=1 SCAR=0.0
748 36 my $self = shift;
749 36 return @$self;
750 }
751  
752 package DBIx::ThinSQL::table;
753 3 our @ISA = ('DBIx::ThinSQL::list');
754  
755 package DBIx::ThinSQL::values;
756 3 our @ISA = ('DBIx::ThinSQL::list');
757  
758 package DBIx::ThinSQL::query;
759 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
760 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
761  
762 36 ▶sub new {
Subroutine new: called CC=16 SCAR=28.2
763 36 my $class = shift;
764 36 my @query;
765  
766 36 eval {
767 36 ▶ while ( my ( $word, $arg ) = splice( @_, 0, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 94 36 130
768 94 ( $word = uc($word) ) =~ s/_/ /g;
769 94 my $ref = ref $arg;
770  
771 94 ▶ if ( $ref =~ m/^DBIx::ThinSQL/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref =~ /^DBIx::ThinSQL/) { } 1 93 94
elsif ($ref eq 'ARRAY') { } 31 62 93
elsif ($ref eq 'HASH') { } 7 55 62
772 1 push( @query, [ $word, $arg ] );
773 }
774 elsif ( $ref eq 'ARRAY' ) {
775 31 ▶ if ( $word =~ m/((SELECT)|(ORDER)|(GROUP))/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($word =~ /((SELECT)|(ORDER)|(GROUP))/) { } 20 11 31
elsif ($word =~ /INSERT/) { } 0 11 11
elsif ($word =~ /(^AS$)|(FROM)|(JOIN)/) { } 1 10 11
elsif ($word eq 'VALUES') { } 5 5 10
776 20 push( @query, [ $word, DBIx::ThinSQL::list->new(@$arg) ] );
777 }
778 elsif ( $word =~ m/INSERT/ ) {
779 0 push( @query, [ $word, DBIx::ThinSQL::table->new(@$arg) ] );
780 }
781 elsif ( $word =~ m/(^AS$)|(FROM)|(JOIN)/ ) {
782 1 push( @query, [ $word, DBIx::ThinSQL::query->new(@$arg) ] );
783 }
784 elsif ( $word eq 'VALUES' ) {
785 push(
786 @query,
787 [
788 $word,
789 ref $arg->[0] eq 'ARRAY'
790 ? DBIx::ThinSQL::list->new(
791 map {
792 DBIx::ThinSQL::values->new(
793 map {
794 2 DBIx::ThinSQL::bind_value->new($_)
795 } @$_
796 )
797 } @$arg
798 )
799 : DBIx::ThinSQL::values->new(
800 5 ▶ map { DBIx::ThinSQL::bind_value->new($_) }
Branch: 1 branch
BranchTrueFalseTotal
ref $arg->[0] eq 'ARRAY' ? : 1 4 5
801 @$arg
802 )
803 ]
804 );
805 }
806 else {
807 5 push( @query, [ $word, DBIx::ThinSQL::expr->new(@$arg) ] );
808 }
809 }
810 elsif ( $ref eq 'HASH' ) {
811  
812 7 ▶ if ( $word =~ m/^((WHERE)|(ON))/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($word =~ /^((WHERE)|(ON))/) { } 2 5 7
elsif ($word eq 'SET') { } 1 4 5
elsif ($word eq 'VALUES') { } 4 0 4
813 2 push( @query, [ $word, DBIx::ThinSQL::expr->new($arg) ] );
814 }
815 elsif ( $word eq 'SET' ) {
816 1 my @cols = sort keys %$arg;
817 push(
818 @query,
819 [
820 $word,
821 DBIx::ThinSQL::list->new(
822 map {
823 1 DBIx::ThinSQL::expr->new(
824  
825 # quote_identifier?
826 $_, ' = ',
827 ref $arg->{$_} eq 'SCALAR'
828 0 ? ${ $arg->{$_} }
829 : DBIx::ThinSQL::bind_value->new(
830 1 ▶ $arg->{$_}
Branch: 1 branch
BranchTrueFalseTotal
ref $arg->{$_} eq 'SCALAR' ? : 0 1 1
831 )
832 )
833 } @cols
834 )
835 ]
836 );
837 }
838 elsif ( $word eq 'VALUES' ) {
839 4 my @cols = sort keys %$arg;
840  
841 # map quote_identifier?
842 4 $query[-1]->[1] =
843 DBIx::ThinSQL::table->new( $query[-1]->[1], @cols );
844  
845 push(
846 @query,
847 [
848 $word,
849 DBIx::ThinSQL::values->new(
850 map {
851 4 ref $arg->{$_} eq 'SCALAR'
852 0 ? ${ $arg->{$_} }
853 : DBIx::ThinSQL::bind_value->new(
854 8 ▶ $arg->{$_} )
Branch: 1 branch
BranchTrueFalseTotal
ref $arg->{$_} eq 'SCALAR' ? : 0 8 8
855 } @cols
856 )
857 ]
858 );
859 }
860 else {
861 0 warn "cannot handle $word => HASH";
862 }
863 }
864 else {
865 55 push( @query, [ $word, $arg ] );
866 }
867 }
868 };
869  
870 36 ▶ Carp::croak("Bad Query: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 36 36
871 36 return bless \@query, $class;
872 }
873  
874 1 ▶sub as {
Subroutine as: called CC=1 SCAR=0.0
875 1 my $self = shift;
876 1 my $value = shift;
877  
878 1 return DBIx::ThinSQL::expr->new( '(', $self, ') AS ',
879 DBIx::ThinSQL::quote_identifier->new($value) );
880 }
881  
882 36 ▶sub tokens {
Subroutine tokens: called CC=1 SCAR=0.0
883 36 my $self = shift;
884 36 return @$self;
885 }
886  
887 3 1;
888  
889 __END__
↑ Summary blib/lib/DBIx/ThinSQL/Deploy.pm »