blib/lib/DBIx/Query.pm

filter: Statementstmt 71.5% 289 / 404 Branchbran 40.5% 47 / 116 Conditioncond 46.9% 23 / 49 MC/DCmcdc 30.0% 9 / 30 Subroutinesub 76.6% 72 / 94 Podpod 100% 2 / 2 totaltotal 63.6% 442 / 695 CC 81 SCAR 21.2
CC
81
Cov
63%
where
42.0
fetchrow_hashref
30.0
save
30.0
CRAP 8.4
↑ Summary
1 package DBIx::Query;
2 # ABSTRACT: Simplified abstracted chained DBI subclass
3  
4 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '1.18'; # VERSION
9  
10 3 ▶use DBI 1.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use parent 'DBI';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 {
14 3 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 *errstr = \*DBI::errstr;
16 }
17  
18 4 ▶sub _connect {
Subroutine _connect: called CC=5 SCAR=16.3
19 4 my ( $self, $dsn, $user, $pass, $attr, $connect ) = @_;
20  
21 4 ▶ $attr = ($attr) ? \%$attr : {};
Branch: 1 branch
BranchTrueFalseTotal
$attr ? : 2 2 4
22 4 ▶ $attr->{PrintError} = 0 unless ( exists $attr->{PrintError} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr->{"PrintError"} 4 0 4
23 4 ▶ $attr->{RaiseError} = 1 unless ( exists $attr->{RaiseError} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr->{"RaiseError"} 3 1 4
24  
25 4 ▶ return $self->SUPER::connect( $dsn, $user, $pass, {
Branch: 1 branch
BranchTrueFalseTotal
$DBI::connect_via eq 'Apache::DBI::connect' ? : 0 4 4
26 %$attr,
27 dbi_connect_method => ( $DBI::connect_via eq 'Apache::DBI::connect' )
28 ? 'Apache::DBI::connect' : $connect,
29 } );
30 }
31  
32 3 ▶sub connect {
Subroutine connect: called CC=1 SCAR=0.0
Pod documented
33 3 my ( $self, $dsn, $user, $pass, $attr ) = @_;
34 3 return $self->_connect( $dsn, $user, $pass, $attr, 'connect_cached' );
35 }
36  
37 1 ▶sub connect_uncached {
Subroutine connect_uncached: called CC=1 SCAR=0.0
Pod documented
38 1 my ( $self, $dsn, $user, $pass, $attr ) = @_;
39 1 return $self->_connect( $dsn, $user, $pass, $attr, 'connect' );
40 }
41  
42 #-----------------------------------------------------------------------------
43  
44 {
45 package DBIx::Query::_Common;
46 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶ use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 213 ▶ sub _param {
Subroutine _param: called CC=3 SCAR=11.0
51 213 my $self = shift;
52 213 my $name = shift;
53  
54 213 ▶ return unless ($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 213 213
55 213 ▶ $self->{'private_dq_stash'}{$name} = shift if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 115 98 213
56 213 return $self->{'private_dq_stash'}{$name};
57 }
58  
59 40 ▶ sub _try {
Subroutine _try: called CC=2 SCAR=7.6
60 40 my ( $self, $cb ) = @_;
61  
62 40 local $@;
63 40 eval { $cb->() };
64 40 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 40 40
65 0 ( my $error = $@ ) =~ s/\s*at.+?line \d+\.\s*//;
66 0 croak $error;
67 }
68 }
69 }
70  
71 #-----------------------------------------------------------------------------
72  
73 {
74 3 package DBIx::Query::db;
75 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 3 ▶ use SQL::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 3 ▶ use SQL::Abstract::Complete;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 3 ▶ use Carp 'carp';
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 3 ▶ use vars '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 3 @ISA = qw( DBI::db DBIx::Query::_Common );
83  
84 4 ▶ sub connected {
Subroutine connected: called CC=6 SCAR=18.0
85 4 my $self = shift;
86  
87 4 my $connection = {};
88 4 @{$connection}{qw( dsn user pass attr )} = @_;
89  
90 4 $self->_param( 'connection' => $connection );
91 4 $self->_param( 'sql_abstract' => SQL::Abstract::Complete->new );
92  
93 my $dialect = ( ref $connection eq 'HASH' and ref $connection->{attr} eq 'HASH' )
94 ? $connection->{attr}{dq_dialect}
95 4 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $connection eq 'HASH' && ref $connection->{'attr'} eq 'HASH' ? : 4 0 4
Condition33%1/3
ref $connection eq "HASH" and ref $connection->{"attr"} eq "HASH"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $connection eq "HASH" and ref $connection->{"attr"} eq "HASH"
ref $connection eq "HASH" ref $connection->{"attr"} eq "HASH"
Truth table33%1/3
A and B
A= ref $connection eq "HASH"B= ref $connection->{"attr"} eq "HASH"
ABresult
0X0
100
111
96  
97 4 ▶ my $sql_parser = SQL::Parser->new(
Condition100%2/2
$dialect || 'ANSI'
l!l
13
MC/DC100%1/1
$dialect || 'ANSI'
$dialect
Truth table100%2/2
A
A= $dialect
Aresult
11
00
98 ( $dialect || 'ANSI' ),
99 { 'RaiseError' => 0, 'PrintError' => 0 },
100 );
101  
102 4 ▶ unless ($dialect) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dialect) 3 1 4
103 $sql_parser->feature( 'reserved_words', $_, 0 )
104 3 for ( keys %{ $sql_parser->{opts}{reserved_words} } );
105 }
106  
107 4 $self->_param( 'sql_parser' => $sql_parser );
108 4 $self->_param( '_dq_parser_cache' => {} );
109  
110 4 return;
111 }
112  
113 7 ▶ sub connection {
Subroutine connection: called CC=9 SCAR=22.0
114 7 my $self = shift;
115  
116 return
117 2 ▶ ( @_ == 0 and wantarray ) ? @{ $self->_param('connection') }{ qw( dsn user pass attr ) } :
Branch: 2 branches
BranchTrueFalseTotal
@_ == 0 && !wantarray ? : 2 3 5
@_ > 1 && wantarray ? : 1 2 3
Condition83%5/6
@_ == 0 and not wantarray
!ll&&!rl&&r
302
@_ > 1 and wantarray
!ll&&!rl&&r
111
MC/DC50%1/2
@_ == 0 and not wantarray
@_ == 0 not wantarray
MC/DC100%2/2
@_ > 1 and wantarray
@_ > 1 wantarray
Truth table66%2/3
A and not(B)
A= @_ == 0B= not wantarray
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= @_ > 1B= wantarray
ABresult
0X0
100
111
118 ( @_ == 0 and not wantarray ) ? $self->_param('connection') :
119 1 ▶ ( @_ > 1 and wantarray ) ? @{ $self->_param('connection') }{@_} :
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 && !wantarray ? : 1 1 2
Condition66%2/3
@_ > 1 and not wantarray
!ll&&!rl&&r
101
MC/DC50%1/2
@_ > 1 and not wantarray
@_ > 1 not wantarray
Truth table66%2/3
A and not(B)
A= @_ > 1B= not wantarray
ABresult
0X0
100
111
120 1 ( @_ > 1 and not wantarray ) ? [ @{ $self->_param('connection') }{@_} ] :
121 7 ▶ @{ $self->_param('connection') }{@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 && wantarray ? : 2 5 7
Condition100%3/3
@_ == 0 and wantarray
!ll&&!rl&&r
322
MC/DC100%2/2
@_ == 0 and wantarray
@_ == 0 wantarray
Truth table100%3/3
A and B
A= @_ == 0B= wantarray
ABresult
0X0
100
111
122 }
123  
124 22 ▶ sub _sth_setup {
Subroutine _sth_setup: called CC=1 SCAR=0.0
125 22 my ( $self, $sql, $attr, $cache_type, $variables ) = @_;
126  
127 22 my $sth;
128 22 ▶ $self->_try( sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
129 22 ▶ $sth = ( defined $cache_type and $cache_type == -1 )
Branch: 1 branch
BranchTrueFalseTotal
defined $cache_type && $cache_type == -1 ? : 2 20 22
Condition66%2/3
defined $cache_type and $cache_type == -1
!ll&&!rl&&r
0202
MC/DC50%1/2
defined $cache_type and $cache_type == -1
defined $cache_type $cache_type == -1
Truth table66%2/3
A and B
A= defined $cache_typeB= $cache_type == -1
ABresult
0X0
100
111
130 ? $self->SUPER::prepare( $sql, $attr )
131 : $self->SUPER::prepare_cached( $sql, $attr, $cache_type );
132 22 } );
133  
134 22 return $sth;
135 }
136  
137 22 ▶ sub _query {
Subroutine _query: called CC=2 SCAR=6.9
138 22 my ( $self, $sql, $attr, $cache_type, $variables ) = @_;
139 22 ▶ $cache_type //= 3;
Condition100%2/2
$cache_type //= 3
l!l
220
MC/DC100%1/1
$cache_type //= 3
$cache_type
Truth table100%2/2
A
A= $cache_type
Aresult
11
00
140  
141 22 my $sth = $self->_sth_setup( $sql, $attr, $cache_type, $variables );
142  
143 22 $sql =~ s/(\r?\n|\s+)/ /g;
144 22 $sql =~ s/^\s+|\s+$//g;
145  
146 22 $sth->_param( 'sql' => $sql );
147 22 $sth->_param( 'dq' => $self );
148 22 $sth->_param( 'variables' => $variables );
149  
150 22 return $sth;
151 }
152  
153 7 ▶ sub sql {
Subroutine sql: called CC=2 SCAR=7.1
154 7 my ( $self, $sql, $attr, $cache_type, $variables ) = @_;
155 7 ▶ $self->_croak('SQL input missing in sql() call') unless ( length $sql );
Branch: 1 branch
BranchTrueFalseTotal
unless length $sql 0 7 7
156 7 return $self->_query( $sql, $attr, $cache_type, $variables );
157 }
158  
159 15 ▶ sub get {
Subroutine get: called CC=1 SCAR=0.0
160 15 my ( $self, $tables, $columns, $where, $meta, $attr, $cache_type ) = @_;
161 15 my ( $sql, @variables ) = $self->_param('sql_abstract')->select( $tables, $columns, $where, $meta );
162 15 my $sth = $self->_query( $sql, $attr, $cache_type, \@variables );
163  
164 15 $sth->_param( 'query' => {
165 'tables' => $tables,
166 'columns' => $columns,
167 'where' => $where,
168 'meta' => $meta,
169 'attr' => $attr,
170 'cache_type' => $cache_type,
171 'sql' => $sql,
172 'variables' => \@variables,
173 } );
174  
175 15 return $sth;
176 }
177  
178 1 ▶ sub sql_uncached {
Subroutine sql_uncached: called CC=1 SCAR=0.0
179 1 my ( $self, $sql, $attr, $cache_type, $variables ) = @_;
180 1 $cache_type = -1;
181 1 return $self->sql( $sql, $attr, $cache_type, $variables );
182 }
183  
184 1 ▶ sub get_uncached {
Subroutine get_uncached: called CC=1 SCAR=0.0
185 1 my ( $self, $tables, $columns, $where, $meta, $attr, $cache_type ) = @_;
186 1 $cache_type = -1;
187 1 return $self->get( $tables, $columns, $where, $meta, $attr, $cache_type );
188 }
189  
190 0 ▶ sub sql_fast {
Subroutine sql_fast: not called CC=1 SCAR=6.9
191 0 my $self = shift;
192 0 carp('sql_fast() is deprecated in favor of sql()');
193 0 return $self->sql(@_);
194 }
195  
196 0 ▶ sub get_fast {
Subroutine get_fast: not called CC=1 SCAR=6.9
197 0 my $self = shift;
198 0 carp('get_fast() is deprecated in favor of get()');
199 0 return $self->get(@_);
200 }
201  
202 1 ▶ sub add {
Subroutine add: called CC=2 SCAR=7.1
203 1 my ( $self, $table_name, $params, $attr, $cache_type ) = @_;
204 1 my ( $sql, @variables ) = $self->_param('sql_abstract')->insert( $table_name, $params );
205  
206 1 ▶ $self->_try( sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
207 1 my $sth = $self->sql( $sql, $attr, $cache_type, \@variables );
208 1 ▶ $sth->execute( @{ $sth->_param('variables') || [] } );
Condition50%1/2
$sth->_param('variables') || []
l!l
10
MC/DC0%0/1
$sth->_param('variables') || []
$sth->_param('variables')
Truth table50%1/2
A
A= $sth->_param('variables')
Aresult
11
00
209 1 } );
210  
211 1 my $pk;
212 1 eval {
213 $pk = $self->last_insert_id(
214 undef,
215 undef,
216 1 ▶ delete $attr->{'last_insert_table'} || $table_name,
Condition33%1/3
delete $attr->{'last_insert_table'} || $table_name
l!l&&r!l&&!r
010
MC/DC0%0/2
delete $attr->{'last_insert_table'} || $table_name
delete $attr->{'last_insert_table'} $table_name
Truth table33%1/3
A || B
A= delete $attr->{'last_insert_table'}B= $table_name
ABresult
1X1
011
000
217 undef,
218 $attr,
219 );
220 };
221  
222 1 $self->_param( 'table' => $table_name );
223  
224 1 return $pk;
225 }
226  
227 1 ▶ sub rm {
Subroutine rm: called CC=1 SCAR=0.0
228 1 my ( $self, $table_name, $params, $attr, $cache_type ) = @_;
229  
230 1 my ( $sql, @variables ) = $self->_param('sql_abstract')->delete( $table_name, $params );
231 1 my $sth = $self->sql( $sql, $attr, $cache_type, \@variables );
232  
233 1 $sth->run;
234 1 return $self;
235 }
236  
237 1 ▶ sub update {
Subroutine update: called CC=1 SCAR=0.0
238 1 my ( $self, $table_name, $params, $where, $attr, $cache_type ) = @_;
239  
240 1 my ( $sql, @variables ) = $self->_param('sql_abstract')->update( $table_name, $params, $where );
241 1 my $sth = $self->sql( $sql, $attr, $cache_type, \@variables );
242  
243 1 $sth->run;
244 1 return $self;
245 }
246  
247 1 ▶ sub abstract {
Subroutine abstract: called CC=1 SCAR=0.0
248 1 return $_[0]->_param('sql_abstract');
249 }
250  
251 5 ▶ sub get_run {
Subroutine get_run: called CC=1 SCAR=0.0
252 5 my $self = shift;
253 5 my $sth = $self->get(@_);
254  
255 5 ▶ $self->_try( sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
256 5 ▶ $sth->execute( @{ $sth->_param('variables') || [] } );
Condition50%1/2
$sth->_param('variables') || []
l!l
50
MC/DC0%0/1
$sth->_param('variables') || []
$sth->_param('variables')
Truth table50%1/2
A
A= $sth->_param('variables')
Aresult
11
00
257 5 } );
258  
259 5 return $sth;
260 }
261  
262 1 ▶ sub fetch_value {
Subroutine fetch_value: called CC=1 SCAR=0.0
263 1 my $self = shift;
264 1 my $sth = $self->get_run(@_);
265 1 my $value;
266  
267 1 ▶ $self->_try( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
268 1 $value = ( $sth->fetchrow_array )[0];
269 1 $sth->finish;
270 1 } );
271  
272 1 return $value;
273 }
274  
275 2 ▶ sub fetchall_arrayref {
Subroutine fetchall_arrayref: called CC=1 SCAR=0.0
276 2 my $self = shift;
277 2 my $sth = $self->get_run(@_);
278 2 my $value;
279  
280 2 ▶ $self->_try( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
281 2 $value = $sth->fetchall_arrayref;
282 2 $sth->finish;
283 2 } );
284  
285 2 return $value;
286 }
287  
288 1 ▶ sub fetchall_hashref {
Subroutine fetchall_hashref: called CC=1 SCAR=0.0
289 1 my $self = shift;
290 1 my $sth = $self->get_run(@_);
291 1 my $value;
292  
293 1 ▶ $self->_try( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
294 1 $value = $sth->fetchall_arrayref({});
295 1 $sth->finish;
296 1 } );
297  
298 1 return $value;
299 }
300  
301 1 ▶ sub fetch_column_arrayref {
Subroutine fetch_column_arrayref: called CC=1 SCAR=0.0
302 1 my $self = shift;
303 1 return [ map { $_->[0] } @{ $self->fetchall_arrayref(@_) } ];
304 }
305  
306 0 ▶ sub fetchrow_hashref {
Subroutine fetchrow_hashref: not called CC=5 SCAR=34.0
307 0 my ( $self, $sql ) = ( shift, shift );
308 0 ▶ $self->_croak('SQL input missing in sql() call') unless ( length $sql );
Branch: 1 branch
BranchTrueFalseTotal
unless length $sql 0 0 0
309  
310 0 my ( $variables, $attr, $cache_type );
311 0 ▶ if ( not defined $_[0] or ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[0] or ref $_[0] eq 'HASH') { } 0 0 0
Condition0%0/3
not defined $_[0] or ref $_[0] eq "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $_[0] or ref $_[0] eq "HASH"
not defined $_[0] ref $_[0] eq "HASH"
Truth table0%0/3
not(A) or B
A= not defined $_[0]B= ref $_[0] eq "HASH"
ABresult
1X1
011
000
312 0 ( $variables, $attr, $cache_type ) = @_;
313 }
314 else {
315 0 $variables = \@_;
316 }
317 0 ▶ $cache_type //= 3;
Condition0%0/2
$cache_type //= 3
l!l
00
MC/DC0%0/1
$cache_type //= 3
$cache_type
Truth table0%0/2
A
A= $cache_type
Aresult
11
00
318  
319 0 my $row;
320 0 ▶ $self->_try( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
321 0 my $sth = $self->_sth_setup( $sql, $attr, $cache_type, $variables );
322 0 $sth->execute(@$variables);
323 0 $row = $sth->fetchrow_hashref;
324 0 $sth->finish;
325 0 } );
326  
327 0 return $row;
328 }
329 }
330  
331 #-----------------------------------------------------------------------------
332  
333 {
334 3 package DBIx::Query::st;
335 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
336 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
337 3 ▶ use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
338  
339 3 ▶ use vars '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
340 3 @ISA = qw( DBI::st DBIx::Query::_Common );
341  
342 0 ▶ sub where {
Subroutine where: not called CC=6 SCAR=37.4
343 0 my $self = shift;
344  
345 0 ▶ croak('Unable to call where() because upstream query not originated with get()')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_param("query") 0 0 0
346 unless ( $self->_param('query') );
347  
348 0 ▶ croak('where() requires a hashref or an even number of items in a list')
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] ne "HASH" and @_ % 2 0 0 0
Condition0%0/3
ref $_[0] ne "HASH" and @_ % 2
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] ne "HASH" and @_ % 2
ref $_[0] ne "HASH" @_ % 2
Truth table0%0/3
A and B
A= ref $_[0] ne "HASH"B= @_ % 2
ABresult
0X0
100
111
349 if ( ref( $_[0] ) ne 'HASH' and @_ % 2 );
350  
351 0 my $query = $self->_param('query');
352 0 ▶ $query->{'where'} = { %{ $query->{'where'} || {} }, ( ref( $_[0] ) eq 'HASH' ) ? %{ $_[0] } : @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
Condition0%0/2
$query->{'where'} || {}
l!l
00
MC/DC0%0/1
$query->{'where'} || {}
$query->{'where'}
Truth table0%0/2
A
A= $query->{'where'}
Aresult
11
00
353  
354 0 return $self->up->get( @{$query}{ qw( tables columns where meta attr cache_type ) } );
355 }
356  
357 6 ▶ sub run {
Subroutine run: called CC=1 SCAR=0.1
358 6 my $self = shift;
359 6 my @input = @_;
360  
361 6 ▶ $self->_try( sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
362 6 ▶ $self->execute( (@input) ? @input : @{ $self->_param('variables') || [] } );
Branch: 1 branch
BranchTrueFalseTotal
@input ? : 0 6 6
Condition50%1/2
$self->_param('variables') || []
l!l
60
MC/DC0%0/1
$self->_param('variables') || []
$self->_param('variables')
Truth table50%1/2
A
A= $self->_param('variables')
Aresult
11
00
363 6 } );
364  
365 6 return DBIx::Query::_Dq::RowSet->new($self);
366 }
367  
368 3 ▶ sub sql {
Subroutine sql: called CC=2 SCAR=7.2
369 3 my ( $self, $sql ) = @_;
370 3 ▶ return ($sql) ? $self->_param('dq')->sql($sql) : $self->_param('sql');
Branch: 1 branch
BranchTrueFalseTotal
$sql ? : 0 3 3
371 }
372  
373 8 ▶ sub structure {
Subroutine structure: called CC=9 SCAR=22.2
374 8 my $self = shift;
375  
376 8 my $saved_structure = $self->_param('structure');
377 8 ▶ return $saved_structure if ($saved_structure);
Branch: 1 branch
BranchTrueFalseTotal
if $saved_structure 1 7 8
378 7 ▶ return if ( $self->_param('no_structure') );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_param("no_structure") 0 7 7
379  
380 7 my $sql = $self->_param('sql');
381 7 my $dq_parser_cache = $self->_param('dq')->_param('_dq_parser_cache');
382  
383 7 ▶ unless ( $dq_parser_cache->{$sql} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dq_parser_cache->{$sql}) 4 3 7
384 4 $self->_param('dq')->_param('sql_parser')->parse($sql);
385 4 my $parsed_structure = $self->_param('dq')->_param('sql_parser')->structure;
386 4 $dq_parser_cache->{$sql} = $parsed_structure;
387 4 ▶ carp $parsed_structure->{'errstr'} if ( $parsed_structure->{errstr} );
Branch: 1 branch
BranchTrueFalseTotal
if $parsed_structure->{"errstr"} 1 3 4
388 }
389  
390 7 $self->_param( 'wildcard_column' => 0 );
391  
392 7 ▶ if ( $dq_parser_cache->{$sql} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dq_parser_cache->{$sql}) { } 7 0 7
393 7 my $structure = $dq_parser_cache->{$sql};
394 7 my $column_index = 0;
395 7 my %aliases;
396  
397 {
398 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
399 $structure->{'column_lookup'} = {
400 map {
401 11 my $index = $column_index++;
402 11 ▶ $aliases{ $_->{'alias'} } = $index if ( $_->{'alias'} );
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"alias"} 0 11 11
403 11 ▶ $self->_param( 'wildcard_column' => 1 ) if ( $_->{'value'} eq '*' );
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"value"} eq "*" 3 8 11
404 11 $_->{'value'} => $index;
405 7 } @{ $structure->{'column_defs'} }
406 };
407 };
408  
409 $structure->{'column_invert_lookup'} = {
410 7 map { $structure->{'column_lookup'}->{$_} => $_ } keys %{ $structure->{'column_lookup'} }
411 };
412 7 foreach ( keys %aliases ) {
413 0 $structure->{'column_lookup'}{$_} = $aliases{$_};
414 0 $structure->{'column_invert_lookup'}{ $aliases{$_} } = $_;
415 }
416  
417 7 $self->_param( 'structure' => $structure );
418 7 return $structure;
419 }
420 else {
421 0 $self->_param( 'no_structure' => 1 );
422 0 return;
423 }
424 }
425  
426 1 ▶ sub table {
Subroutine table: called CC=1 SCAR=0.0
427 1 return shift->structure->{'table_names'}[0];
428 }
429  
430 2 ▶ sub _wildcard_column {
Subroutine _wildcard_column: called CC=2 SCAR=7.0
431 2 my $self = shift;
432  
433 2 my $wildcard_column = $self->_param('wildcard_column');
434 2 ▶ return $wildcard_column if ( defined $wildcard_column );
Branch: 1 branch
BranchTrueFalseTotal
if defined $wildcard_column 0 2 2
435  
436 2 $self->structure;
437 2 return $self->_param('wildcard_column');
438 }
439  
440 1 ▶ sub up {
Subroutine up: called CC=1 SCAR=0.0
441 1 return shift->_param('dq');
442 }
443 }
444  
445 #-----------------------------------------------------------------------------
446  
447 {
448 3 package DBIx::Query::_Dq::RowSet;
449 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
450 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
451 3 ▶ use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
452  
453 6 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
454 6 my ( $self, $sth ) = @_;
455 6 return bless( { 'sth' => $sth }, $self );
456 }
457  
458 2 ▶ sub next {
Subroutine next: called CC=4 SCAR=14.6
459 2 my ( $self, $skip ) = @_;
460 2 ▶ $skip ||= 0;
Condition50%1/2
$skip ||= 0
l!l
02
MC/DC0%0/1
$skip ||= 0
$skip
Truth table50%1/2
A
A= $skip
Aresult
11
00
461  
462 2 ▶ my $method = ( $self->{'sth'}->_wildcard_column ) ? 'fetchrow_hashref' : 'fetchrow_arrayref';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'sth'}->_wildcard_column ? : 0 2 2
463  
464 2 my $value;
465 2 ▶ DBIx::Query::_Common::_try( $self, sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
466 2 ▶ $self->{'sth'}->fetchrow_arrayref while ( $skip-- );
Branch: 1 branch
BranchTrueFalseTotal
$skip-- and do { $self->{"sth"}->fetchrow_arrayref } 0 2 2
467  
468 2 ▶ if ( my $row = $self->{'sth'}->$method ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->{"sth"}->$method) 2 0 2
469 2 $value = DBIx::Query::_Dq::Row->new( $row, $self );
470 }
471 2 } );
472  
473 2 ▶ return $value if ($value);
Branch: 1 branch
BranchTrueFalseTotal
if $value 2 0 2
474 }
475  
476 0 ▶ sub all {
Subroutine all: not called CC=1 SCAR=6.9
477 0 my $self = shift;
478 0 my @input = @_;
479  
480 0 my $value;
481 0 ▶ DBIx::Query::_Common::_try( $self, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
482 0 $value = $self->{'sth'}->fetchall_arrayref(@input);
483 0 $self->{'sth'}->finish;
484 0 } );
485  
486 0 return $value;
487 }
488  
489 0 ▶ sub each {
Subroutine each: not called CC=2 SCAR=17.9
490 0 my ( $self, $code ) = @_;
491 0 ▶ my $method = ( $self->{'sth'}->_wildcard_column ) ? 'fetchrow_hashref' : 'fetchrow_arrayref';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'sth'}->_wildcard_column ? : 0 0 0
492  
493 0 ▶ DBIx::Query::_Common::_try( $self, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
494 0 ▶ $code->( DBIx::Query::_Dq::Row->new( $_, $self ) ) while ( $_ = $self->{'sth'}->$method );
Branch: 1 branch
BranchTrueFalseTotal
$_ = $self->{"sth"}->$method and do { &$code("DBIx::Query::_Dq::Row"->new($_, $self)) } 0 0 0
495 0 $self->{'sth'}->finish;
496 0 } );
497  
498 0 return $self;
499 }
500  
501 0 ▶ sub value {
Subroutine value: not called CC=4 SCAR=30.0
502 0 my $self = shift;
503  
504 0 my @value;
505 0 ▶ DBIx::Query::_Common::_try( $self, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
506 0 @value = $self->{'sth'}->fetchrow_array;
507 0 $self->{'sth'}->finish;
508 0 } );
509  
510 0 my $wantarray = wantarray;
511 0 ▶ if ( not defined $wantarray ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $wantarray) { } 0 0 0
elsif (not wantarray) { } 0 0 0
512 0 croak('value() must not be called in void context');
513 }
514 elsif ( not wantarray ) {
515 0 ▶ if ( @value < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@value < 2) { } 0 0 0
516 0 return $value[0];
517 }
518 else {
519 0 croak('value() called in scalar context but multiple values fetched');
520 }
521 }
522 else {
523 0 return @value;
524 }
525 }
526  
527 0 ▶ sub first {
Subroutine first: not called CC=2 SCAR=17.9
528 0 my ( $self, $type ) = @_;
529 0 ▶ my $method = ( ref $type eq 'HASH' ) ? 'fetchrow_hashref' : 'fetchrow_arrayref';
Branch: 1 branch
BranchTrueFalseTotal
ref $type eq 'HASH' ? : 0 0 0
530  
531 0 my $value;
532 0 ▶ DBIx::Query::_Common::_try( $self, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
533 0 $value = $self->{'sth'}->$method;
534 0 $self->{'sth'}->finish;
535 0 } );
536  
537 0 return $value;
538 }
539  
540 0 ▶ sub column {
Subroutine column: not called CC=2 SCAR=17.9
541 0 my $self = shift;
542 0 my @values = map { $_->[0] } @{ ( $self->all )[0] };
543  
544 0 ▶ return (wantarray) ? @values : \@values;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
545 }
546  
547 2 ▶ sub up {
Subroutine up: called CC=1 SCAR=0.0
548 2 return shift->{'sth'};
549 }
550 }
551  
552 #-----------------------------------------------------------------------------
553  
554 {
555 3 package DBIx::Query::_Dq::Row;
556 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
557 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
558 3 ▶ use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
559  
560 2 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
561 2 my ( $self, $row, $set ) = @_;
562 2 return bless(
563 {
564 'row' => $row,
565 'set' => $set,
566 },
567 $self,
568 );
569 }
570  
571 1 ▶ sub cell {
Subroutine cell: called CC=10 SCAR=32.0
572 1 my ( $self, $index, $new_value ) = @_;
573 1 my ( $name, $structure, $value ) = ( $index, $self->up->up->structure, undef );
574  
575 1 ▶ croak('Query used earlier in chain failed to parse, so cell() cannot be called')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $structure eq "HASH" 0 1 1
576 unless ( ref($structure) eq 'HASH' );
577  
578 1 ▶ if ( ref( $self->{'row'} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'row'} eq 'ARRAY') { } 1 0 1
579 1 ▶ unless ( $index =~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($index =~ /^\d+$/) 1 0 1
580 1 $name = $index;
581 1 $index = $structure->{'column_lookup'}{$index};
582 }
583 1 ▶ return undef unless ( defined $index and $index < @{ $self->{'row'} } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index and $index < @{$self->{"row"};} 0 1 1
Condition33%1/3
defined $index and $index < @{$self->{"row"};}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $index and $index < @{$self->{"row"};}
defined $index $index < @{$self->{"row"};}
Truth table33%1/3
A and B
A= defined $indexB= $index < @{$self->{"row"};}
ABresult
0X0
100
111
584 1 $value = $self->{'row'}[$index];
585 }
586 else {
587 0 ▶ croak('cell() called with integer index but query does not support integer indexing')
Branch: 1 branch
BranchTrueFalseTotal
if $index =~ /^\d+$/ 0 0 0
588 if ( $index =~ /^\d+$/ );
589  
590 0 ▶ return undef unless ( exists $self->{'row'}{$index} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"row"}{$index} 0 0 0
591 0 $value = $self->{'row'}{$index};
592 }
593  
594 1 ▶ if ( defined $new_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_value) 0 1 1
595 0 ▶ if ( ref( $self->{'row'} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'row'} eq 'ARRAY') { } 0 0 0
596 0 $self->{'row'}[$index] = $new_value;
597 }
598 else {
599 0 $self->{'row'}{$name} = $new_value;
600 }
601 0 $value = $new_value;
602 }
603  
604 1 return DBIx::Query::_Dq::Cell->new( $name, $value, $index, $self );
605 }
606  
607 0 ▶ sub each {
Subroutine each: not called CC=3 SCAR=24.8
608 0 my ( $self, $code ) = @_;
609  
610 croak('each() called on a row object that does not have columns defined')
611 0 ▶ if ( ref( $self->{'row'} ) ne 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"row"} ne "ARRAY" 0 0 0
612  
613 0 ▶ for ( my $i = 0 ; $i < @{ $self->{'row'} } ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"row"};}) 0 0 0
614 $code->(
615 DBIx::Query::_Dq::Cell->new(
616 $self->up->up->structure->{'column_lookup'}{$i},
617 0 $self->{'row'}[$i],
618 $i, $self,
619 )
620 );
621 }
622  
623 0 return $self;
624 }
625  
626 0 ▶ sub data {
Subroutine data: not called CC=4 SCAR=30.0
627 0 my ($self) = @_;
628  
629 0 ▶ if ( ref( $self->{'row'} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'row'} eq 'ARRAY') { } 0 0 0
630 0 my $structure = $self->up->up->structure;
631 0 ▶ if ( ref($structure) eq 'HASH' and $structure->{'column_invert_lookup'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $structure eq 'HASH' and $structure->{'column_invert_lookup'}) { } 0 0 0
Condition0%0/3
ref $structure eq "HASH" and $structure->{"column_invert_lookup"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $structure eq "HASH" and $structure->{"column_invert_lookup"}
ref $structure eq "HASH" $structure->{"column_invert_lookup"}
Truth table0%0/3
A and B
A= ref $structure eq "HASH"B= $structure->{"column_invert_lookup"}
ABresult
0X0
100
111
632 return {
633 map {
634 0 $structure->{'column_invert_lookup'}->{$_} => $self->{'row'}[$_]
635 0 } ( 0 .. scalar( @{ $self->{'row'} } ) - 1 )
636 };
637 }
638 else {
639 0 croak('Unable to parse SQL, therefore data() unavailable; use row() instead');
640 }
641 }
642 else {
643 0 return $self->{'row'};
644 }
645 }
646  
647 0 ▶ sub row {
Subroutine row: not called CC=2 SCAR=17.9
648 0 my ($self) = @_;
649 croak('For this particular query, use data() instead')
650 0 ▶ unless ( ref( $self->{'row'} ) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"row"} eq "ARRAY" 0 0 0
651 0 return $self->{'row'};
652 }
653  
654 0 ▶ sub save {
Subroutine save: not called CC=5 SCAR=34.0
655 0 my ( $self, $key, $params, $cache_type ) = @_;
656  
657 0 ▶ croak('save() called without a key or set of keys') unless ($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
658  
659 0 my $data = $self->data;
660 0 ▶ if ( ref($params) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params eq "HASH") 0 0 0
661 0 $data->{$_} = $params->{$_} foreach ( keys %{$params} );
662 }
663  
664 0 my $dq = $self->up->up->up;
665  
666 my ( $sql, @variables ) = $dq->_param('sql_abstract')->update(
667 $self->up->up->table,
668 $data,
669 0 ▶ { map { $_ => delete $data->{$_} } ( ref($key) ? @{$key} : $key ) },
Branch: 1 branch
BranchTrueFalseTotal
ref $key ? : 0 0 0
670 );
671 0 my $sth = $dq->sql( $sql, undef, $cache_type, \@variables );
672  
673 0 $sth->run;
674 0 return $self;
675 }
676  
677 2 ▶ sub up {
Subroutine up: called CC=1 SCAR=0.0
678 2 return shift->{'set'};
679 }
680 }
681  
682 #-----------------------------------------------------------------------------
683  
684 {
685 3 package DBIx::Query::_Dq::Cell;
686 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
687 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
688  
689 1 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
690 1 my ( $self, $name, $value, $index, $row ) = @_;
691 1 return bless(
692 {
693 'name' => $name,
694 'value' => $value,
695 'index' => $index,
696 'row' => $row,
697 },
698 $self,
699 );
700 }
701  
702 0 ▶ sub name {
Subroutine name: not called CC=1 SCAR=6.9
703 0 return shift->{'name'};
704 }
705  
706 0 ▶ sub value {
Subroutine value: not called CC=2 SCAR=17.9
707 0 my ( $self, $new_value ) = @_;
708 0 ▶ return ( defined $new_value ) ? $self->up->cell( $self->name, $new_value ) : $self->{'value'};
Branch: 1 branch
BranchTrueFalseTotal
defined $new_value ? : 0 0 0
709 }
710  
711 0 ▶ sub index {
Subroutine index: not called CC=1 SCAR=6.9
712 0 return shift->{'index'};
713 }
714  
715 0 ▶ sub save {
Subroutine save: not called CC=1 SCAR=6.9
716 0 return shift->up->save(@_);
717 }
718  
719 1 ▶ sub up {
Subroutine up: called CC=1 SCAR=0.0
720 1 return shift->{'row'};
721 }
722 }
723  
724 3 1;
725  
726 __END__
↑ Summary