lib/DB/Object/Statement.pm

filter: Statementstmt 23.4% 171 / 728 Branchbran 15.3% 70 / 456 Conditioncond 15.3% 53 / 345 MC/DCmcdc 0.5% 1 / 183 Subroutinesub 37.1% 26 / 70 Podpod 89.5% 43 / 48 totaltotal 19.8% 364 / 1830 CC 362 SCAR 73.7
CC
362
Cov
19%
join
4692.0
execute
2944.0
__ANON__
506.0
CRAP 1588.8
↑ Summary blib/lib/DB/Object/Statement.pm »
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## Database Object Interface - ~/lib/DB/Object/Statement.pm
4 ## Version v0.9.0
5 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2017/07/19
8 ## Modified 2026/03/27
9 ## All rights reserved
10 ##
11 ##
12 ## This program is free software; you can redistribute it and/or modify it
13 ## under the same terms as Perl itself.
14 ##----------------------------------------------------------------------------
15 ## This package's purpose is to automatically terminate the statement object and
16 ## separate them from the connection object (DB::Object).
17 ## Connection object last longer than statement objects
18 ##----------------------------------------------------------------------------
19 package DB::Object::Statement;
20 BEGIN
21 {
22 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶ use parent qw( DB::Object );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶ use vars qw( $VERSION $DEBUG $EXCEPTION_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶ use Class::Struct qw( struct );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶ use Wanted;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶ our $DEBUG = 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
31 2 our $VERSION = 'v0.9.0';
32 };
33  
34 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 sub init
38 {
39 0 ▶ my $self = shift( @_ );
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
40 0 $self->{_exception_class} = $EXCEPTION_CLASS;
41 0 ▶ $self->Module::Generic::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->Module::Generic::init(@_) or return $self->pass_error 0 0 0
42 0 return( $self );
43 }
44  
45 sub as_string
46 {
47 5 ▶ my $self = shift( @_ );
Subroutine as_string: called CC=2 SCAR=7.0
Pod documented
48 # my $q = $self->_query_object_current;
49 # used by select, insert, update, delete to flag that we need to reformat the query
50 5 $self->{as_string}++;
51 5 ▶ if( my $qo = $self->query_object )
Branch: 1 branch
BranchTrueFalseTotal
if (my $qo = $self->query_object) 5 0 5
52 {
53 5 $qo->final(1);
54 }
55 # return( $self->{sth}->{Statement} );
56 # Same:
57 # return( $q->as_string );
58 5 return( $self->{query} );
59 }
60  
61 sub attach
62 {
63 0 ▶ my $self = shift( @_ );
Subroutine attach: not called CC=10 SCAR=47.0
Pod documented
64 0 ▶ my $dbo = shift( @_ ) ||
Condition0%0/2
shift @_ || (return $self->error('No DB::Object object was provided.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No DB::Object object was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
65 return( $self->error( "No DB::Object object was provided." ) );
66 0 my $base_class = $self->base_class;
67 0 ▶ unless( $self->_is_a( $dbo => $base_class ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($dbo, $base_class)) 0 0 0
68 {
69 0 return( $self->error( "Value provided is not a ${base_class} object." ) );
70 }
71 0 ▶ $self->database_object( $dbo ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->database_object($dbo) or return $self->pass_error 0 0 0
72 0 ▶ if( my $tbl = $self->table_object )
Branch: 1 branch
BranchTrueFalseTotal
if (my $tbl = $self->table_object) 0 0 0
73 {
74 0 ▶ if( $self->_is_a( $tbl => "${base_class}::Tables" ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($tbl, "${base_class}::Tables")) { } 0 0 0
75 {
76 0 ▶ $self->_load_class( "${base_class}::Tables" ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("${base_class}::Tables") or return $self->pass_error 0 0 0
77 0 $tbl->attach( $dbo );
78 }
79 else
80 {
81 0 warn( "Table object found (", $self->_str_val( $tbl ), ") in this statement object is actually not a table object (${base_class}::Tables) !" );
82 0 $self->table_object( undef );
83 }
84 }
85 0 ▶ if( my $qo = $self->query_object )
Branch: 1 branch
BranchTrueFalseTotal
if (my $qo = $self->query_object) 0 0 0
86 {
87 0 ▶ unless( $self->_is_a( $qo => "${base_class}::Query" ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($qo, "${base_class}::Query")) 0 0 0
88 {
89 0 die( "Query object found (", $self->_str_val( $qo ), ") in this statement object is actually not a query object (${base_class}::Query) !" );
90 }
91 0 ▶ $self->_load_class( "${base_class}::Query" ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("${base_class}::Query") or return $self->pass_error 0 0 0
92 0 $qo->attach( $dbo );
93 }
94 0 return( $self );
95 }
96  
97 sub bind_param
98 {
99 0 ▶ my $self = shift( @_ );
Subroutine bind_param: not called CC=4 SCAR=30.0
Pod documented
100 0 my( $pack, $file, $line ) = caller();
101 0 my $sub = ( caller(1) )[3];
102 0 $self->{pack} = $pack;
103 0 $self->{file} = $file;
104 0 $self->{line} = $line;
105 0 $self->{sub} = $sub;
106 my $rc = eval
107 0 {
108 0 $self->{sth}->bind_param( @_ );
109 };
110 0 ▶ if( $@ )
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif ($rc) { } 0 0 0
111 {
112 0 my $err = $self->errstr();
113 0 $err =~ s/ at line \d+.*$//;
114 # printf( STDERR "%s in %s at line %d within sub '%s'.\n", $err, $self->{file}, $self->{line}, $self->{sub} );
115 # exit(1);
116 0 return( $self->error( $err ) );
117 }
118 elsif( $rc )
119 {
120 0 return( $rc );
121 }
122 else
123 {
124 0 ▶ my $err = $@ = $self->{sth}->errstr() || "Unknown error while binding parameters to query.";
Condition0%0/2
$self->{'sth'}->errstr || 'Unknown error while binding parameters to query.'
l!l
00
MC/DC0%0/1
$self->{'sth'}->errstr || 'Unknown error while binding parameters to query.'
$self->{'sth'}->errstr
Truth table0%0/2
A
A= $self->{'sth'}->errstr
Aresult
11
00
125 0 return( $self->error( $err ) );
126 }
127 }
128  
129 sub commit
130 {
131 0 ▶ my $self = shift( @_ );
Subroutine commit: not called CC=5 SCAR=34.0
Pod documented
132 0 ▶ if( $self->{sth} && $self->param( 'autocommit' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sth"} and $self->param("autocommit")) 0 0 0
Condition0%0/3
$self->{"sth"} and $self->param("autocommit")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sth"} and $self->param("autocommit")
$self->{"sth"} $self->param("autocommit")
Truth table0%0/3
A and B
A= $self->{"sth"}B= $self->param("autocommit")
ABresult
0X0
100
111
133 {
134 0 ▶ my $sth = $self->prepare( 'COMMIT' ) || return( $self->pass_error );
Condition0%0/2
$self->prepare('COMMIT') || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->prepare('COMMIT') || (return $self->pass_error)
$self->prepare('COMMIT')
Truth table0%0/2
A
A= $self->prepare('COMMIT')
Aresult
11
00
135 0 ▶ $sth->execute() || return( $self->error( "An error occurred while executing query: ", $sth->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("An error occurred while executing query: ", $sth->error) 0 0 0
136 0 $sth->finish();
137 }
138 0 return( $self );
139 }
140  
141 12 ▶sub database_object { return( shift->_set_get_object_without_init( 'dbo', 'DB::Object', @_ ) ); }
Subroutine database_object: called CC=1 SCAR=0.0
Pod documented
142  
143 sub distinct
144 {
145 0 ▶ my $self = shift( @_ );
Subroutine distinct: not called CC=7 SCAR=40.3
Pod documented
146 my $query = $self->{query} ||
147 0 ▶ return( $self->error( "No query to set as to be ignored." ) );
Condition0%0/2
$self->{'query'} || (return $self->error('No query to set as to be ignored.'))
l!l
00
MC/DC0%0/1
$self->{'query'} || (return $self->error('No query to set as to be ignored.'))
$self->{'query'}
Truth table0%0/2
A
A= $self->{'query'}
Aresult
11
00
148  
149 0 my $type = uc( ( $query =~ /^\s*(\S+)\s+/ )[0] );
150 # ALTER for table alteration statements (DB::Object::Tables
151 0 my @allowed = qw( SELECT );
152 0 my $allowed = CORE::join( '|', @allowed );
153 0 ▶ if( !scalar( grep{ /^$type$/i } @allowed ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar grep({/^$type$/i;} @allowed)) 0 0 0
154 {
155 0 return( $self->error( "You may not flag statement of type \U$type\E to be distinct:\n$query" ) );
156 }
157 # Incompatible. Do not bother going further
158 0 ▶ return( $self ) if( $query =~ /^\s*(?:$allowed)\s+(?:DISTINCT|DISTINCTROW|ALL)\s+/i );
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^\s*(?:$allowed)\s+(?:DISTINCT|DISTINCTROW|ALL)\s+/i 0 0 0
159  
160 0 $query =~ s/^(\s*)($allowed)(\s+)/$1$2 DISTINCT /;
161 # my $sth = $self->prepare( $query ) ||
162 # $self->{ 'query' } = $query;
163 # saving parameters to bind later on must have been done previously
164 0 ▶ my $sth = $self->_cache_this( $query ) ||
Condition0%0/2
$self->_cache_this($query) || (return $self->error("Error while preparing new ignored query:\n$query"))
l!l
00
MC/DC0%0/1
$self->_cache_this($query) || (return $self->error("Error while preparing new ignored query:\n$query"))
$self->_cache_this($query)
Truth table0%0/2
A
A= $self->_cache_this($query)
Aresult
11
00
165 return( $self->error( "Error while preparing new ignored query:\n$query" ) );
166 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
167 {
168 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing new ignored query:\n$query") 0 0 0
169 return( $self->error( "Error while executing new ignored query:\n$query" ) );
170 }
171 0 return( $sth );
172 }
173  
174 sub dump
175 {
176 0 ▶ my $self = shift( @_ );
Subroutine dump: not called CC=10 SCAR=47.0
Pod documented
177 0 my $file = shift( @_ );
178 0 ▶ if( $file )
Branch: 2 branches
BranchTrueFalseTotal
if ($file) { } 0 0 0
elsif (exists $self->{'sth'}) { } 0 0 0
179 {
180 # Used to be handled by SQL server
181 # my $query = $self->as_string();
182 # $query =~ s/(\s+FROM\s+)/ INTO OUTFILE '$file'$1/;
183 # my $sth = $self->prepare( $query ) ||
184 # return( $self->error( "Error while preparing query to dump result on select:\n$query" ) );
185 # $sth->execute() ||
186 # return( $self->error( "Error while executing query to dump result on select:\n$query" ) );
187 0 ▶ $self->_load_class( 'DateTime' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("DateTime") or return $self->pass_error 0 0 0
188 0 my $fields = $self->{_fields};
189 0 my @header = sort{ $fields->{ $a } <=> $fields->{ $b } } keys( %$fields );
190 # new_file is inherited from Module::Generic
191 0 $file = $self->new_file( $file );
192 0 ▶ my $io = $file->open( '>', { binmode => 'utf8' }) ||
Condition0%0/2
$file->open('>', {'binmode', 'utf8'}) || (return $self->error("Unable to open file '${file}' in write mode: ", $file->error))
l!l
00
MC/DC0%0/1
$file->open('>', {'binmode', 'utf8'}) || (return $self->error("Unable to open file '${file}' in write mode: ", $file->error))
$file->open('>', {'binmode', 'utf8'})
Truth table0%0/2
A
A= $file->open('>', {'binmode', 'utf8'})
Aresult
11
00
193 return( $self->error( "Unable to open file '$file' in write mode: ", $file->error ) );
194 0 my $date = DateTime->now;
195 0 my $table = $self->{table};
196 0 $io->printf( "## Generated on %s for table $table\n", $date->strftime( '%c' ) );
197 0 $io->print( "## ", CORE::join( "\t", @header ), "\n" );
198 0 my @data = ();
199 0 ▶ while( @data = $self->fetchrow() )
Branch: 1 branch
BranchTrueFalseTotal
if (@data = $self->fetchrow) 0 0 0
200 {
201 0 print( $io CORE::join( "\t", @data ), "\n" );
202 }
203 0 $io->close;
204 0 $self->finish;
205 0 return( $self );
206 }
207 elsif( exists( $self->{sth} ) )
208 {
209 # my $fields = $self->{ '_fields' };
210 0 my @fields = @{ $self->{sth}->FETCH( 'NAME' ) };
211 0 my $max = 0;
212 # foreach my $field ( keys( %$fields ) )
213 0 foreach my $field ( @fields )
214 {
215 0 ▶ $max = length( $field ) if( length( $field ) > $max );
Branch: 1 branch
BranchTrueFalseTotal
if length $field > $max 0 0 0
216 }
217 0 my $template = '';
218 ## foreach my $field ( sort{ $fields->{ $a } <=> $fields->{ $b } } keys( %$fields ) )
219 0 foreach my $field ( @fields )
220 {
221 0 $template .= "$field" . ( '.' x ( $max - length( $field ) ) ) . ": %s\n";
222 }
223 0 $template .= "\n";
224 0 my @data = ();
225 0 ▶ while( @data = $self->fetchrow() )
Branch: 1 branch
BranchTrueFalseTotal
if (@data = $self->fetchrow) 0 0 0
226 {
227 0 printf( STDERR $template, @data );
228 }
229 0 $self->finish;
230 0 return( $self );
231 }
232 else
233 {
234 0 return( $self->error( "No query to dump." ) );
235 }
236 }
237  
238 0 ▶sub exec { return( shift->execute( @_ ) ); }
Subroutine exec: not called CC=1 SCAR=6.9
Pod documented
239  
240 sub execute
241 {
242 11 ▶ my $self = shift( @_ );
Subroutine execute: called CC=112 SCAR=79.9
Pod documented
243 11 my @args = @_;
244 # Resolve caller context only when needed for error reporting or debug.
245 # Calling caller() unconditionally on every execute() is expensive.
246 11 ▶ if( $self->debug || !$self->{pack} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug or not $self->{"pack"}) 11 0 11
Condition33%1/3
$self->debug or not $self->{"pack"}
l!l&&r!l&&!r
0110
MC/DC0%0/2
$self->debug or not $self->{"pack"}
$self->debug not $self->{"pack"}
Truth table33%1/3
A or not(B)
A= $self->debugB= not $self->{"pack"}
ABresult
1X1
011
000
247 {
248 11 my( $pack, $file, $line ) = caller();
249 11 my $sub = ( caller(1) )[3];
250 # What we want is to get the point from where we were originatly called
251 11 my $class = 'DB::Object';
252 11 ▶ if( substr( $pack, 0, length( $class ) ) eq $class )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($pack, 0, length $class) eq $class) 11 0 11
253 {
254 11 ▶ for( my $i = 1; $i < 5; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 5) 24 4 28
255 {
256 24 ( $pack, $file, $line ) = caller( $i );
257 24 $sub = ( caller( $i + 1 ) )[3];
258 24 ▶ last if( substr( $pack, 0, length( $class ) ) ne $class );
Branch: 1 branch
BranchTrueFalseTotal
if substr($pack, 0, length $class) ne $class 7 17 24
259 }
260 }
261 11 $self->{pack} = $pack;
262 11 $self->{file} = $file;
263 11 $self->{line} = $line;
264 11 $self->{sub} = $sub;
265 }
266 11 $self->{executed}++;
267 11 my $q = $self->query_object;
268 11 ▶ $q->final(1) if( $q );
Branch: 1 branch
BranchTrueFalseTotal
if $q 0 11 11
269 11 my @binded = ();
270 11 my @binded_types = ();
271 # Boolean used so we do not automatically change a string litteral into an array if
272 # the number of types and values are not the same, as it would lead to undesirable results.
273 11 my $bind_mismatch = 0;
274 11 my $el;
275 # As a rule, if the first placeholder is a numbered one, then all are, because we cannot mix numbered placeholder and non-numbered ones.
276 11 ▶ my $use_numbered_placeholders = ( ( $q && $q->elements->first->is_numbered ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$q && $q->elements->first->is_numbered ? : 0 11 11
Condition33%1/3
$q and $q->elements->first->is_numbered
!ll&&!rl&&r
1100
MC/DC0%0/2
$q and $q->elements->first->is_numbered
$q $q->elements->first->is_numbered
Truth table33%1/3
A and B
A= $qB= $q->elements->first->is_numbered
ABresult
0X0
100
111
277 11 ▶ if( $q &&
Branch: 1 branch
BranchTrueFalseTotal
if ($q and $el = $q->elements and $el->types->length) 0 11 11
Condition33%2/6
$q and $el = $q->elements and $el->types->length
!ll&&!rl&&r
1100
$q and $el = $q->elements
!ll&&!rl&&r
1100
MC/DC0%0/3
$q and $el = $q->elements and $el->types->length
$q $el = $q->elements $el->types->length
Truth table25%1/4
A and B and C
A= $qB= $el = $q->elementsC= $el->types->length
ABCresult
0XX0
10X0
1100
1111
278 ( $el = $q->elements ) &&
279 $el->types->length )
280 {
281 0 my $types = $q->binded_types_as_param;
282 0 @binded_types = @$types;
283 }
284  
285 11 ▶ if( defined( $el ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $el) 0 11 11
286 {
287 }
288  
289 # Get the values to bind
290 11 ▶ if( $q && $self->{bind} )
Branch: 1 branch
BranchTrueFalseTotal
if ($q and $self->{"bind"}) 0 11 11
Condition33%1/3
$q and $self->{"bind"}
!ll&&!rl&&r
1100
MC/DC0%0/2
$q and $self->{"bind"}
$q $self->{"bind"}
Truth table33%1/3
A and B
A= $qB= $self->{"bind"}
ABresult
0X0
100
111
291 {
292 0 ▶ if( @_ &&
Branch: 2 branches
BranchTrueFalseTotal
if (@_ and @_ == 1 && $self->_is_hash($_[0], 'strict') || !(@_ % 2) && ref $_[0] ne 'HASH') { } 0 0 0
elsif (@_) { } 0 0 0
Condition0%0/12
@_ and @_ == 1 && $self->_is_hash($_[0], "strict") || !(@_ % 2) && ref $_[0] ne "HASH"
!ll&&!rl&&r
000
@_ == 1 and $self->_is_hash($_[0], "strict") or not @_ % 2 and ref $_[0] ne "HASH"
l!l&&r!l&&!r
000
@_ == 1 and $self->_is_hash($_[0], "strict")
!ll&&!rl&&r
000
not @_ % 2 and ref $_[0] ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/5
@_ and @_ == 1 && $self->_is_hash($_[0], "strict") || !(@_ % 2) && ref $_[0] ne "HASH"
@_ @_ == 1 $self->_is_hash($_[0], "strict") not @_ % 2 ref $_[0] ne "HASH"
Truth table0%0/8
A and B and C or not(D) and E
A= @_B= @_ == 1C= $self->_is_hash($_[0], "strict")D= not @_ % 2E= ref $_[0] ne "HASH"
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
293 (
294 # hash reference
295 ( @_ == 1 && $self->_is_hash( $_[0] => 'strict' ) ) ||
296 # key => value pairs
297 ( !( @_ % 2 ) && ref( $_[0] ) ne 'HASH' )
298 )
299 )
300 {
301 0 my $vals = {};
302 0 ▶ if( $self->_is_hash( $_[0] => 'strict' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_hash($_[0], 'strict')) { } 0 0 0
303 {
304 0 $vals = shift( @_ );
305 }
306 else
307 {
308 0 $vals = { @_ };
309 }
310 # This is the list of fields as they appear in the order in insert or update query
311 # Knowing their order of appearance is key so we can bind follow-on values to them
312 0 my $sorted = $q->sorted;
313 0 foreach my $f ( @$sorted )
314 {
315 0 ▶ if( !CORE::exists( $vals->{ $f } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $vals->{$f}) { } 0 0 0
316 {
317 0 push( @binded, undef() );
318 }
319 # The value may be defined or not, or may be zero length long
320 else
321 {
322 0 push( @binded, $vals->{ $f } );
323 }
324 }
325 }
326 elsif( @_ )
327 {
328 0 push( @binded, @_ );
329 }
330 else
331 {
332 # my $binded_values = $q->binded;
333 0 my $binded_values = $q->elements->values;
334 0 ▶ push( @binded, @$binded_values ) if( scalar( @$binded_values ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$binded_values 0 0 0
335 }
336 }
337  
338 11 ▶ @binded = @_ if( !@binded && @_ );
Branch: 1 branch
BranchTrueFalseTotal
if not @binded and @_ 2 9 11
Condition66%2/3
not @binded and @_
!ll&&!rl&&r
092
MC/DC50%1/2
not @binded and @_
not @binded @_
Truth table66%2/3
not(A) and B
A= not @bindedB= @_
ABresult
0X0
100
111
339 11 ▶ @binded = () if( !@binded );
Branch: 1 branch
BranchTrueFalseTotal
unless @binded 9 2 11
340 11 ▶ if( $q && $q->is_upsert )
Branch: 1 branch
BranchTrueFalseTotal
if ($q and $q->is_upsert) 0 11 11
Condition33%1/3
$q and $q->is_upsert
!ll&&!rl&&r
1100
MC/DC0%0/2
$q and $q->is_upsert
$q $q->is_upsert
Truth table33%1/3
A and B
A= $qB= $q->is_upsert
ABresult
0X0
100
111
341 {
342 0 ▶ if( !$use_numbered_placeholders &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $use_numbered_placeholders and scalar @binded_types > scalar @binded) 0 0 0
Condition0%0/3
not $use_numbered_placeholders and scalar @binded_types > scalar @binded
!ll&&!rl&&r
000
MC/DC0%0/2
not $use_numbered_placeholders and scalar @binded_types > scalar @binded
not $use_numbered_placeholders scalar @binded_types > scalar @binded
Truth table0%0/3
not(A) and B
A= not $use_numbered_placeholdersB= scalar @binded_types > scalar @binded
ABresult
0X0
100
111
343 scalar( @binded_types ) > scalar( @binded ) )
344 {
345 0 CORE::push( @binded, @binded );
346 }
347 }
348  
349 11 ▶ if( scalar( @_ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 2 9 11
350 {
351 2 my $temp = {};
352 2 my $el;
353 2 ▶ $el = $q->elements->elements if( $q && $q->elements );
Branch: 1 branch
BranchTrueFalseTotal
if $q and $q->elements 0 2 2
Condition33%1/3
$q and $q->elements
!ll&&!rl&&r
200
MC/DC0%0/2
$q and $q->elements
$q $q->elements
Truth table33%1/3
A and B
A= $qB= $q->elements
ABresult
0X0
100
111
354 2 local $" = ', ';
355 2 ▶ for( my $i = 0; $i < scalar( @args ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @args) 2 2 4
356 {
357 # { $some_value => 'varchar' }
358 2 ▶ if( ref( $_[$i] ) eq 'HASH' &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[$i] eq "HASH" and scalar keys %{$_[$i];} == 1 and "DBI"->can('SQL_' . uc([values %{$_[$i];}]->[0]))) 0 2 2
Condition33%2/6
ref $_[$i] eq "HASH" and scalar keys %{$_[$i];} == 1 and "DBI"->can('SQL_' . uc([values %{$_[$i];}]->[0]))
!ll&&!rl&&r
200
ref $_[$i] eq "HASH" and scalar keys %{$_[$i];} == 1
!ll&&!rl&&r
200
MC/DC0%0/3
ref $_[$i] eq "HASH" and scalar keys %{$_[$i];} == 1 and "DBI"->can('SQL_' . uc([values %{$_[$i];}]->[0]))
ref $_[$i] eq "HASH" scalar keys %{$_[$i];} == 1 "DBI"->can('SQL_' . uc([values %{$_[$i];}]->[0]))
Truth table25%1/4
A and B and C
A= ref $_[$i] eq "HASH"B= scalar keys %{$_[$i];} == 1C= "DBI"->can('SQL_' . uc([values %{$_[$i];}]->[0]))
ABCresult
0XX0
10X0
1100
1111
359 0 scalar( keys( %{$_[$i]} ) ) == 1 &&
360 # e.g. DBI::SQL_VARCHAR or DBI::SQL_INTEGER
361 0 DBI->can( "SQL_" . uc( [values( %{$_[$i]} )]->[0] ) ) )
362 {
363 0 my $constant = DBI->can( "SQL_" . uc( [values( %{$_[$i]} )]->[0] ) );
364 0 $temp->{$i} = { type => $constant->(), value => [keys( %{$_[$i]} )]->[0] };
365 }
366 }
367  
368 # The user has chosen to override any datatype computed and be explicit.
369 2 ▶ if( scalar( keys( %$temp ) ) == scalar( @_ ) )
Branch: 2 branches
BranchTrueFalseTotal
if (scalar keys %$temp == scalar @_) { } 0 2 2
elsif (scalar keys %$temp) { } 0 2 2
370 {
371 # @binded = @_;
372 0 @binded_types = ();
373 0 @binded = ();
374 0 foreach my $i ( sort( keys( %$temp ) ) )
375 {
376 0 push( @binded_types, $temp->{ $i }->{type} );
377 0 push( @binded, $temp->{ $i }->{value} );
378 }
379 }
380 elsif( scalar( keys( %$temp ) ) )
381 {
382 0 foreach my $i ( sort( keys( %$temp ) ) )
383 {
384 0 CORE::splice( @binded_types, $i, 1, $temp->{ $i }->{type} );
385 0 $binded[$i] = $temp->{ $i }->{value};
386 }
387 }
388 }
389  
390 11 ▶ if( $q && scalar( @binded ) != scalar( @binded_types ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($q and scalar @binded != scalar @binded_types) 0 11 11
Condition33%1/3
$q and scalar @binded != scalar @binded_types
!ll&&!rl&&r
1100
MC/DC0%0/2
$q and scalar @binded != scalar @binded_types
$q scalar @binded != scalar @binded_types
Truth table33%1/3
A and B
A= $qB= scalar @binded != scalar @binded_types
ABresult
0X0
100
111
391 {
392 # Flag we use a bit below
393 0 $bind_mismatch++;
394 0 ▶ warn( sprintf( "Warning: total %d bound values does not match the total %d bound types ('%s')! Check the code for query $self->{sth}->{Statement}\n", scalar( @binded ), scalar( @binded_types ), CORE::join( "','", map{ ref( $_ ) eq 'HASH' ? $self->Module::Generic::dump( $_ ) : $_ } @binded_types ) ) );
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 0 0 0
395 # Cancel it, because it will create problems
396 0 @binded_types = ();
397 }
398  
399 # If there are any array object of some sort provided, make sure they are transformed into a regular array so DBD::Ph can then transform it into a Postgres array.
400 11 ▶ for( my $i = 0; $i < scalar( @binded ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @binded) 2 11 13
401 {
402 2 ▶ next if( !defined( $binded[$i] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $binded[$i] 0 2 2
403 2 my $elem;
404 2 ▶ if( defined( $el ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field")) 0 2 2
Condition33%4/12
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field")
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i]
!ll&&!rl&&r
200
defined $el and not $bind_mismatch
!ll&&!rl&&r
200
MC/DC0%0/5
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field")
defined $el not $bind_mismatch $elem = $el->elements->[$i] defined $elem $self->_is_a($elem->fo, "DB::Object::Fields::Field")
Truth table16%1/6
A and not(B) and C and D and E
A= defined $elB= not $bind_mismatchC= $elem = $el->elements->[$i]D= defined $elemE= $self->_is_a($elem->fo, "DB::Object::Fields::Field")
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
405 !$bind_mismatch &&
406 ( $elem = $el->elements->[$i] ) &&
407 defined( $elem ) &&
408 $self->_is_a( $elem->fo => 'DB::Object::Fields::Field' ) )
409 {
410 }
411  
412 2 ▶ if( defined( $el ) &&
Branch: 3 branches
BranchTrueFalseTotal
if (defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, 'DB::Object::Fields::Field') and $elem->fo->is_array and not $self->_is_array($binded[$i]) and defined $binded[$i]) { } 0 2 2
elsif (defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, 'DB::Object::Fields::Field') and $elem->fo->type eq 'jsonb' || $elem->fo->type eq 'json' and $self->_is_hash($binded[$i], 'strict')) { } 0 2 2
elsif (defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, 'DB::Object::Fields::Field') and $elem->fo->type =~ /^(date|timestamp)/i || $elem->fo->datatype->alias->grep(qr/^(date|timestamp)/i)->length and defined $binded[$i] and $self->_is_a($binded[$i], 'DateTime') and not $binded[$i]->formatter) { } 0 4 4
Condition33%7/21
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field") and $elem->fo->is_array and not $self->_is_array($binded[$i]) and defined $binded[$i]
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field") and $elem->fo->is_array and not $self->_is_array($binded[$i])
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field") and $elem->fo->is_array
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field")
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i]
!ll&&!rl&&r
200
defined $el and not $bind_mismatch
!ll&&!rl&&r
200
MC/DC0%0/8
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field") and $elem->fo->is_array and not $self->_is_array($binded[$i]) and defined $binded[$i]
defined $el not $bind_mismatch $elem = $el->elements->[$i] defined $elem $self->_is_a($elem->fo, "DB::Object::Fields::Field") $elem->fo->is_array not $self->_is_array($binded[$i]) defined $binded[$i]
Truth table11%1/9
A and not(B) and C and D and E and F and not(G) and H
A= defined $elB= not $bind_mismatchC= $elem = $el->elements->[$i]D= defined $elemE= $self->_is_a($elem->fo, "DB::Object::Fields::Field")F= $elem->fo->is_arrayG= not $self->_is_array($binded[$i])H= defined $binded[$i]
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
11110XXX0
111110XX0
1111110X0
111111100
111111111
413 !$bind_mismatch &&
414 ( $elem = $el->elements->[$i] ) &&
415 defined( $elem ) &&
416 $self->_is_a( $elem->fo => 'DB::Object::Fields::Field' ) &&
417 $elem->fo->is_array &&
418 !$self->_is_array( $binded[$i] ) &&
419 defined( $binded[$i] ) )
420 {
421 0 ▶ $binded[$i] = $self->_is_number( $binded[$i] ) ? [$binded[$i]] : [$self->database_object->quote( $binded[$i] )];
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_number($binded[$i]) ? : 0 0 0
422 }
423 ▶ elsif( defined( $el ) &&
Condition28%6/21
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field") and $elem->fo->type eq "jsonb" || $elem->fo->type eq "json" and $self->_is_hash($binded[$i], "strict")
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field") and $elem->fo->type eq "jsonb" || $elem->fo->type eq "json"
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field")
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i]
!ll&&!rl&&r
200
defined $el and not $bind_mismatch
!ll&&!rl&&r
200
$elem->fo->type eq "jsonb" or $elem->fo->type eq "json"
l!l&&r!l&&!r
000
MC/DC0%0/8
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field") and $elem->fo->type eq "jsonb" || $elem->fo->type eq "json" and $self->_is_hash($binded[$i], "strict")
defined $el not $bind_mismatch $elem = $el->elements->[$i] defined $elem $self->_is_a($elem->fo, "DB::Object::Fields::Field") $elem->fo->type eq "jsonb" $elem->fo->type eq "json" $self->_is_hash($binded[$i], "strict")
Truth table10%1/10
A and not(B) and C and D and E and F or G and H
A= defined $elB= not $bind_mismatchC= $elem = $el->elements->[$i]D= defined $elemE= $self->_is_a($elem->fo, "DB::Object::Fields::Field")F= $elem->fo->type eq "jsonb"G= $elem->fo->type eq "json"H= $self->_is_hash($binded[$i], "strict")
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
11110XXX0
1111100X0
111111X00
111110100
111111X11
111110111
424 !$bind_mismatch &&
425 ( $elem = $el->elements->[$i] ) &&
426 defined( $elem ) &&
427 $self->_is_a( $elem->fo => 'DB::Object::Fields::Field' ) &&
428 ( $elem->fo->type eq 'jsonb' || $elem->fo->type eq 'json' ) &&
429 $self->_is_hash( $binded[$i] => 'strict' ) )
430 {
431 # try-catch
432 0 local $@;
433 my $json = eval
434 0 {
435 0 $self->new_json->encode( $binded[$i] );
436 };
437 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
438 {
439 0 ▶ warn( "Error trying to encode hash value ", $binded[$i], ": $@" ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("DB::Object") 0 0 0
440 }
441 else
442 {
443 0 $binded[$i] = $json;
444 }
445 }
446 # If the value provided is a DateTime object without a formatter, we transform the given value into a ISO8601 datetime string
447 ▶ elsif( defined( $el ) &&
Condition29%8/27
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field") and $elem->fo->type =~ /^(date|timestamp)/i || $elem->fo->datatype->alias->grep(qr/^(date|timestamp)/i)->length and defined $binded[$i] and $self->_is_a($binded[$i], "DateTime") and not $binded[$i]->formatter
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field") and $elem->fo->type =~ /^(date|timestamp)/i || $elem->fo->datatype->alias->grep(qr/^(date|timestamp)/i)->length and defined $binded[$i] and $self->_is_a($binded[$i], "DateTime")
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field") and $elem->fo->type =~ /^(date|timestamp)/i || $elem->fo->datatype->alias->grep(qr/^(date|timestamp)/i)->length and defined $binded[$i]
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field") and $elem->fo->type =~ /^(date|timestamp)/i || $elem->fo->datatype->alias->grep(qr/^(date|timestamp)/i)->length
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field")
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem
!ll&&!rl&&r
200
defined $el and not $bind_mismatch and $elem = $el->elements->[$i]
!ll&&!rl&&r
200
defined $el and not $bind_mismatch
!ll&&!rl&&r
200
$elem->fo->type =~ /^(date|timestamp)/i or $elem->fo->datatype->alias->grep(qr/^(date|timestamp)/i)->length
l!l&&r!l&&!r
000
MC/DC0%0/10
defined $el and not $bind_mismatch and $elem = $el->elements->[$i] and defined $elem and $self->_is_a($elem->fo, "DB::Object::Fields::Field") and $elem->fo->type =~ /^(date|timestamp)/i || $elem->fo->datatype->alias->grep(qr/^(date|timestamp)/i)->length and defined $binded[$i] and $self->_is_a($binded[$i], "DateTime") and not $binded[$i]->formatter
defined $el not $bind_mismatch $elem = $el->elements->[$i] defined $elem $self->_is_a($elem->fo, "DB::Object::Fields::Field") $elem->fo->type =~ /^(date|timestamp)/i $elem->fo->datatype->alias->grep(qr/^(date|timestamp)/i)->length defined $binded[$i] $self->_is_a($binded[$i], "DateTime") not $binded[$i]->formatter
Truth table7%1/14
A and not(B) and C and D and E and F or G and H and I and not(J)
A= defined $elB= not $bind_mismatchC= $elem = $el->elements->[$i]D= defined $elemE= $self->_is_a($elem->fo, "DB::Object::Fields::Field")F= $elem->fo->type =~ /^(date|timestamp)/iG= $elem->fo->datatype->alias->grep(qr/^(date|timestamp)/i)->lengthH= defined $binded[$i]I= $self->_is_a($binded[$i], "DateTime")J= not $binded[$i]->formatter
ABCDEFGHIJresult
0XXXXXXXXX0
10XXXXXXXX0
110XXXXXXX0
1110XXXXXX0
11110XXXXX0
1111100XXX0
111111X0XX0
11111010XX0
111111X10X0
111110110X0
111111X1100
11111011100
111111X1111
11111011111
448 !$bind_mismatch &&
449 ( $elem = $el->elements->[$i] ) &&
450 defined( $elem ) &&
451 $self->_is_a( $elem->fo => 'DB::Object::Fields::Field' ) &&
452 ( $elem->fo->type =~ /^(date|timestamp)/i || $elem->fo->datatype->alias->grep( qr/^(date|timestamp)/i )->length ) &&
453 defined( $binded[$i] ) &&
454 $self->_is_a( $binded[$i] => 'DateTime' ) &&
455 # There is no formatter
456 !$binded[$i]->formatter )
457 {
458 0 $binded[$i] = $binded[$i]->iso8601;
459 }
460  
461 # The value is an array object, but not a simple array, so we need to convert it
462 # or else the driver will not understand nor accept it.
463 2 ▶ if( $self->_is_array( $binded[$i] ) &&
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_is_array($binded[$i]) and ref $binded[$i] ne 'ARRAY') { } 0 2 2
elsif ($self->_is_object($binded[$i]) and overload::Overloaded($binded[$i]) and overload::Method($binded[$i], '""')) { } 0 2 2
elsif ($self->_is_hash($binded[$i]) and $self->_can($binded[$i], 'as_json')) { } 0 4 4
Condition33%1/3
$self->_is_array($binded[$i]) and ref $binded[$i] ne "ARRAY"
!ll&&!rl&&r
200
MC/DC0%0/2
$self->_is_array($binded[$i]) and ref $binded[$i] ne "ARRAY"
$self->_is_array($binded[$i]) ref $binded[$i] ne "ARRAY"
Truth table33%1/3
A and B
A= $self->_is_array($binded[$i])B= ref $binded[$i] ne "ARRAY"
ABresult
0X0
100
111
464 ref( $binded[$i] ) ne 'ARRAY' )
465 {
466 0 $binded[$i] = [@{$binded[$i]}];
467 }
468 ▶ elsif( $self->_is_object( $binded[$i] ) &&
Condition33%2/6
$self->_is_object($binded[$i]) and overload::Overloaded($binded[$i]) and overload::Method($binded[$i], "\"\"")
!ll&&!rl&&r
200
$self->_is_object($binded[$i]) and overload::Overloaded($binded[$i])
!ll&&!rl&&r
200
MC/DC0%0/3
$self->_is_object($binded[$i]) and overload::Overloaded($binded[$i]) and overload::Method($binded[$i], "\"\"")
$self->_is_object($binded[$i]) overload::Overloaded($binded[$i]) overload::Method($binded[$i], "\"\"")
Truth table25%1/4
A and B and C
A= $self->_is_object($binded[$i])B= overload::Overloaded($binded[$i])C= overload::Method($binded[$i], "\"\"")
ABCresult
0XX0
10X0
1100
1111
469 overload::Overloaded( $binded[$i] ) &&
470 overload::Method( $binded[$i], '""' ) )
471 {
472 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
473 0 my $v = "$binded[$i]";
474 0 ▶ $binded[$i] = defined( $v ) ? $v : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
475 }
476 # Will work well with Module::Generic::Hash
477 ▶ elsif( $self->_is_hash( $binded[$i] ) &&
Condition33%1/3
$self->_is_hash($binded[$i]) and $self->_can($binded[$i], "as_json")
!ll&&!rl&&r
200
MC/DC0%0/2
$self->_is_hash($binded[$i]) and $self->_can($binded[$i], "as_json")
$self->_is_hash($binded[$i]) $self->_can($binded[$i], "as_json")
Truth table33%1/3
A and B
A= $self->_is_hash($binded[$i])B= $self->_can($binded[$i], "as_json")
ABresult
0X0
100
111
478 $self->_can( $binded[$i], 'as_json' ) )
479 {
480 0 $binded[$i] = $binded[$i]->as_json;
481 }
482 }
483  
484 11 local $_;
485 my $rv = eval
486 11 {
487 11 ▶ local( $SIG{ALRM} ) = sub{ die( "Timeout while processing query $self->{sth}->{Statement}\n" ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
488 11 ▶ for( my $i = 0; $i < scalar( @binded ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @binded) 2 11 13
489 {
490 # Stringify the binded value if it is a stringifyable object.
491 2 ▶ if( ref( $binded[$i] ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (ref $binded[$i] and $self->_is_object($binded[$i]) and overload::Overloaded($binded[$i]) and overload::Method($binded[$i], "\"\"")) 0 2 2
Condition33%3/9
ref $binded[$i] and $self->_is_object($binded[$i]) and overload::Overloaded($binded[$i]) and overload::Method($binded[$i], "\"\"")
!ll&&!rl&&r
200
ref $binded[$i] and $self->_is_object($binded[$i]) and overload::Overloaded($binded[$i])
!ll&&!rl&&r
200
ref $binded[$i] and $self->_is_object($binded[$i])
!ll&&!rl&&r
200
MC/DC0%0/4
ref $binded[$i] and $self->_is_object($binded[$i]) and overload::Overloaded($binded[$i]) and overload::Method($binded[$i], "\"\"")
ref $binded[$i] $self->_is_object($binded[$i]) overload::Overloaded($binded[$i]) overload::Method($binded[$i], "\"\"")
Truth table20%1/5
A and B and C and D
A= ref $binded[$i]B= $self->_is_object($binded[$i])C= overload::Overloaded($binded[$i])D= overload::Method($binded[$i], "\"\"")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
492 $self->_is_object( $binded[$i] ) &&
493 overload::Overloaded( $binded[$i] ) &&
494 overload::Method( $binded[$i], '""' ) )
495 {
496 0 $binded[$i] .= '';
497 }
498  
499 2 my $data_type = $binded_types[ $i ];
500 2 ▶ if( CORE::length( $data_type ) && $self->_is_hash( $data_type ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $data_type and $self->_is_hash($data_type)) { } 0 2 2
Condition33%1/3
length $data_type and $self->_is_hash($data_type)
!ll&&!rl&&r
200
MC/DC0%0/2
length $data_type and $self->_is_hash($data_type)
length $data_type $self->_is_hash($data_type)
Truth table33%1/3
A and B
A= length $data_typeB= $self->_is_hash($data_type)
ABresult
0X0
100
111
501 {
502 0 $self->{sth}->bind_param( $i + 1, $binded[ $i ], $data_type );
503 }
504 else
505 {
506 2 $self->{sth}->bind_param( $i + 1, $binded[ $i ] );
507 }
508 }
509 11 $self->{sth}->execute();
510 };
511 11 my $error = $@;
512 11 ▶ $error ||= $self->{sth}->errstr if( !$rv );
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 11 11
Condition0%0/3
$error ||= $self->{"sth"}->errstr
l!l&&r!l&&!r
000
MC/DC0%0/2
$error ||= $self->{"sth"}->errstr
$error $self->{"sth"}->errstr
Truth table0%0/3
A ||= B
A= $errorB= $self->{"sth"}->errstr
ABresult
1X1
011
000
513 11 ▶ if( $q )
Branch: 1 branch
BranchTrueFalseTotal
if ($q) 0 11 11
514 {
515 # Only reset the table's query object if this is NOT a cached/reused joined statement.
516 # For joined statements, the query object ($q_source) is permanently attached to the
517 # statement handle and resetting the table creates a new ~500KB query object on every
518 # execute() that is never freed (the old one is orphaned). The reset serves to prepare
519 # the table for a fresh query after a one-shot select(), which is not the case here.
520 # A joined statement is identified by having join_tables populated.
521 # For a joined statement that is reused in a loop (exec() called multiple times),
522 # calling reset() on any of the tables is wasteful: it creates a new ~500KB query
523 # object for every single exec() call, and those objects are never freed because the
524 # allocator retains the arena pages. The reset was designed for one-shot select()
525 # usage, not for reused prepared statements. Skip it entirely for joined statements.
526 0 ▶ if( $q->join_tables->length == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($q->join_tables->length == 0) 0 0 0
527 {
528 0 $q->table_object->reset;
529 }
530 }
531 11 ▶ my $tie = $self->{tie} || {};
Condition50%1/2
$self->{'tie'} || {}
l!l
011
MC/DC0%0/1
$self->{'tie'} || {}
$self->{'tie'}
Truth table50%1/2
A
A= $self->{'tie'}
Aresult
11
00
532 # Maybe it is time to bind SQL result to possible provided perl variables?
533 11 ▶ if( !$error && %$tie )
Branch: 1 branch
BranchTrueFalseTotal
if (not $error and %$tie) 0 11 11
Condition33%1/3
not $error and %$tie
!ll&&!rl&&r
0110
MC/DC0%0/2
not $error and %$tie
not $error %$tie
Truth table33%1/3
not(A) and B
A= not $errorB= %$tie
ABresult
0X0
100
111
534 {
535 0 my $order = $self->{tie_order};
536 0 my $sth = $self->{sth};
537 0 ▶ for( my $i = 0; $i < @$order; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$order) 0 0 0
538 {
539 0 my $pos = $i + 1;
540 0 my $val = $order->[ $i ];
541 0 ▶ if( exists( $tie->{ $val } ) && ref( $tie->{ $val } ) eq 'SCALAR' )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tie->{$val} and ref $tie->{$val} eq "SCALAR") 0 0 0
Condition0%0/3
exists $tie->{$val} and ref $tie->{$val} eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $tie->{$val} and ref $tie->{$val} eq "SCALAR"
exists $tie->{$val} ref $tie->{$val} eq "SCALAR"
Truth table0%0/3
A and B
A= exists $tie->{$val}B= ref $tie->{$val} eq "SCALAR"
ABresult
0X0
100
111
542 {
543 0 $sth->bind_col( $pos, $tie->{ $val } );
544 }
545 }
546 }
547 11 ▶ if( $error )
Branch: 2 branches
BranchTrueFalseTotal
if ($error) { } 0 11 11
elsif ($self->{'sth'}->errstr) { } 0 11 11
548 {
549 0 $error =~ s/ at (\S+\s)?line \d+.*$//s;
550 # $err .= ":\n\"$self->{ 'query' }\"";
551 0 $error .= ":\n\"$self->{sth}->{Statement}\"";
552 0 $error = "Error while trying to execute query $self->{sth}->{Statement}: $error";
553 0 ▶ if( $self->fatal() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fatal) { } 0 0 0
554 {
555 0 die( "$error in $self->{file} at line $self->{line} within sub $self->{sub}\n" );
556 }
557 else
558 {
559 # return( $self->error( "$err in $self->{ 'file' } at line $self->{ 'line' } within sub $self->{ 'sub' }" ) );
560 0 return( $self->error( $error ) );
561 }
562 }
563 elsif( $self->{sth}->errstr() )
564 {
565 0 return( $self->error( "Error while trying to execute query $self->{sth}->{Statement}: ", $self->{sth}->errstr ) );
566 }
567 # Clear any error, since this query statement may be re-used
568 11 $self->clear_error;
569  
570 # User wants an object for chaining like:
571 # $sth->exec( 'some value' )->fetchrow;
572 11 ▶ if( want( 'OBJECT' ) )
Branch: 2 branches
BranchTrueFalseTotal
if (want('OBJECT')) { } 0 11 11
elsif ($rv) { } 11 0 11
573 {
574 0 return( $self );
575 }
576 elsif( $rv )
577 {
578 11 return( $rv );
579 }
580 # For void context too
581 else
582 {
583 0 return(1);
584 }
585 }
586  
587 sub executed
588 {
589 12 ▶ my $self = shift( @_ );
Subroutine executed: called CC=3 SCAR=12.5
Pod documented
590 # For hand made query to avoid clash when executing generic routine such as fetchall_arrayref...
591 12 ▶ return( 1 ) if( !exists( $self->{query} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"query"} 0 12 12
592 12 ▶ return( exists( $self->{executed} ) && $self->{executed} );
Condition33%1/3
exists $self->{'executed'} && $self->{'executed'}
!ll&&!rl&&r
0012
MC/DC0%0/2
exists $self->{'executed'} && $self->{'executed'}
exists $self->{'executed'} $self->{'executed'}
Truth table33%1/3
A && B
A= exists $self->{'executed'}B= $self->{'executed'}
ABresult
0X0
100
111
593 }
594  
595 sub fetchall_arrayref($@)
596 {
597 1 ▶ my $self = shift( @_ );
Subroutine fetchall_arrayref: called CC=18 SCAR=41.6
Pod documented
598 1 ▶ my $slice = shift( @_ ) || [];
Condition50%1/2
shift @_ || []
l!l
10
MC/DC0%0/1
shift @_ || []
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
599 1 my $dbo = $self->database_object;
600 1 my $sth = $self->{sth};
601 1 ▶ if( !$self->executed() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->executed) 0 1 1
602 {
603 0 ▶ $self->execute() || return;
Branch: 1 branch
BranchTrueFalseTotal
$self->execute or return 0 0 0
604 }
605 # Ensure we set the cache of field names and types. We will need them to post process the query results with _convert_datetime2object()
606 # This is necessary for MySQL whose object lose that information, while the PostgreSQL or SQLite driver keep the information.
607 1 ▶ my $need_post_processing = ( ( $dbo->auto_decode_json || $dbo->auto_convert_datetime_to_object ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$dbo->auto_decode_json || $dbo->auto_convert_datetime_to_object ? : 1 0 1
Condition33%1/3
$dbo->auto_decode_json or $dbo->auto_convert_datetime_to_object
l!l&&r!l&&!r
100
MC/DC0%0/2
$dbo->auto_decode_json or $dbo->auto_convert_datetime_to_object
$dbo->auto_decode_json $dbo->auto_convert_datetime_to_object
Truth table33%1/3
A or B
A= $dbo->auto_decode_jsonB= $dbo->auto_convert_datetime_to_object
ABresult
1X1
011
000
608 1 ▶ if( $need_post_processing )
Branch: 1 branch
BranchTrueFalseTotal
if ($need_post_processing) 1 0 1
609 {
610 1 $self->_cache_field_names;
611 1 $self->_cache_field_types;
612 }
613 # $self->_cleanup();
614 1 my $mode = ref( $slice );
615 1 my @rows;
616 my $row;
617 1 ▶ if( $mode eq 'ARRAY' )
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'ARRAY') { } 0 1 1
elsif ($mode eq 'HASH') { } 1 0 1
618 {
619 0 ▶ if( @$slice )
Branch: 1 branch
BranchTrueFalseTotal
if (@$slice) { } 0 0 0
620 {
621 0 ▶ push( @rows, [ @{ $row }[ @{ $slice } ] ] ) while( $row = $sth->fetch() );
Branch: 1 branch
BranchTrueFalseTotal
$row = $sth->fetch and do { push @rows, [@{$row;}[@{$slice;}]] } 0 0 0
622 }
623 else
624 {
625 0 ▶ push( @rows, [ @{ $row } ] ) while( $row = $sth->fetch );
Branch: 1 branch
BranchTrueFalseTotal
$row = $sth->fetch and do { push @rows, [@{$row;}] } 0 0 0
626 }
627 }
628 elsif( $mode eq 'HASH' )
629 {
630 1 my @o_keys = keys( %$slice );
631 1 ▶ if( @o_keys )
Branch: 1 branch
BranchTrueFalseTotal
if (@o_keys) { } 0 1 1
632 {
633 # my %i_names = map{ ( lc( $_ ) => $_ ) } @{ $sth->FETCH( 'NAME' ) };
634 # my @i_keys = map{ $i_names{ lc( $_ ) } } @o_keys;
635 0 my $i_keys = $self->field_names_lc;
636 0 ▶ while( $row = $sth->fetchrow_hashref() )
Branch: 1 branch
BranchTrueFalseTotal
if ($row = $sth->fetchrow_hashref) 0 0 0
637 {
638 0 my %hash;
639 0 @hash{ @o_keys } = @{ $row }{ @$i_keys };
640 0 push( @rows, \%hash );
641 }
642 }
643 else
644 {
645 1 ▶ push( @rows, $row ) while( $row = $sth->fetchrow_hashref() );
Branch: 1 branch
BranchTrueFalseTotal
$row = $sth->fetchrow_hashref and do { push @rows, $row } 2 1 3
646 }
647 }
648 else
649 {
650 0 warn( "fetchall_arrayref($mode) invalid" );
651 }
652 # return( \@rows );
653 1 ▶ return( \@rows ) if( !$need_post_processing );
Branch: 1 branch
BranchTrueFalseTotal
unless $need_post_processing 0 1 1
654 1 my $data = \@rows;
655 1 ▶ $data = $self->_convert_json2hash({ statement => $sth, data => $data }) if( $dbo->auto_decode_json );
Branch: 1 branch
BranchTrueFalseTotal
if $dbo->auto_decode_json 1 0 1
656 1 ▶ $data = $self->_convert_datetime2object({ statement => $sth, data => $data }) if( $dbo->auto_convert_datetime_to_object );
Branch: 1 branch
BranchTrueFalseTotal
if $dbo->auto_convert_datetime_to_object 0 1 1
657 1 return( $data );
658 }
659  
660 sub fetchcol($;$)
661 {
662 0 ▶ my $self = shift( @_ );
Subroutine fetchcol: not called CC=4 SCAR=30.0
Pod documented
663 # @arr = $sth->fetchcol( $col_number );
664 0 my $col_num = shift( @_ );
665 0 ▶ if( !$self->executed() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->executed) 0 0 0
666 {
667 0 ▶ $self->execute() || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->execute or return $self->pass_error 0 0 0
668 }
669 # $self->_cleanup();
670 # return( $h->fetchcol( $COL_NUM ) );
671 0 my @col;
672 # $self->dataseek( 0 );
673 my $ref;
674 0 ▶ while( $ref = $self->{sth}->fetchrow_arrayref() )
Branch: 1 branch
BranchTrueFalseTotal
if ($ref = $self->{"sth"}->fetchrow_arrayref) 0 0 0
675 {
676 0 push( @col, $ref->[ $col_num ] );
677 }
678 0 return( @col );
679 }
680  
681 sub fetchhash(@)
682 {
683 0 ▶ my $self = shift( @_ );
Subroutine fetchhash: not called CC=11 SCAR=48.8
Pod documented
684 0 my $dbo = $self->database_object;
685 0 ▶ if( !$self->executed() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->executed) 0 0 0
686 {
687 0 ▶ $self->execute() || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->execute or return $self->pass_error 0 0 0
688 }
689 # Ensure we set the cache of field names and types. We will need them to post process the query results with _convert_datetime2object()
690 # This is necessary for MySQL whose object lose that information, while the PostgreSQL or SQLite driver keep the information.
691 0 ▶ if( $dbo->auto_decode_json || $dbo->auto_convert_datetime_to_object )
Branch: 1 branch
BranchTrueFalseTotal
if ($dbo->auto_decode_json or $dbo->auto_convert_datetime_to_object) 0 0 0
Condition0%0/3
$dbo->auto_decode_json or $dbo->auto_convert_datetime_to_object
l!l&&r!l&&!r
000
MC/DC0%0/2
$dbo->auto_decode_json or $dbo->auto_convert_datetime_to_object
$dbo->auto_decode_json $dbo->auto_convert_datetime_to_object
Truth table0%0/3
A or B
A= $dbo->auto_decode_jsonB= $dbo->auto_convert_datetime_to_object
ABresult
1X1
011
000
692 {
693 0 $self->_cache_field_names;
694 0 $self->_cache_field_types;
695 }
696 # $self->_cleanup();
697 # %hash = $sth->fetchhash;
698 # return( $h->fetchhash );
699 0 ▶ my $sth = $self->{sth} || die( "Unable to get the underlying statement handler!" );
Condition0%0/2
$self->{'sth'} || die('Unable to get the underlying statement handler!')
l!l
00
MC/DC0%0/1
$self->{'sth'} || die('Unable to get the underlying statement handler!')
$self->{'sth'}
Truth table0%0/2
A
A= $self->{'sth'}
Aresult
11
00
700 0 my $ref = $sth->fetchrow_hashref();
701 0 ▶ if( $ref )
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 0 0 0
702 {
703 0 ▶ if( $dbo->auto_decode_json || $dbo->auto_convert_datetime_to_object )
Branch: 1 branch
BranchTrueFalseTotal
if ($dbo->auto_decode_json or $dbo->auto_convert_datetime_to_object) 0 0 0
Condition0%0/3
$dbo->auto_decode_json or $dbo->auto_convert_datetime_to_object
l!l&&r!l&&!r
000
MC/DC0%0/2
$dbo->auto_decode_json or $dbo->auto_convert_datetime_to_object
$dbo->auto_decode_json $dbo->auto_convert_datetime_to_object
Truth table0%0/3
A or B
A= $dbo->auto_decode_jsonB= $dbo->auto_convert_datetime_to_object
ABresult
1X1
011
000
704 {
705 0 ▶ $ref = $self->_convert_json2hash({ statement => $sth, data => $ref }) if( $dbo->auto_decode_json );
Branch: 1 branch
BranchTrueFalseTotal
if $dbo->auto_decode_json 0 0 0
706 0 ▶ $ref = $self->_convert_datetime2object({ statement => $sth, data => $ref }) if( $dbo->auto_convert_datetime_to_object );
Branch: 1 branch
BranchTrueFalseTotal
if $dbo->auto_convert_datetime_to_object 0 0 0
707 }
708 0 return( %$ref );
709 }
710 else
711 {
712 0 return( () );
713 }
714 }
715  
716 sub fetchrow(@)
717 {
718 0 ▶ my $self = shift( @_ );
Subroutine fetchrow: not called CC=5 SCAR=34.0
Pod documented
719 0 ▶ if( !$self->executed() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->executed) 0 0 0
720 {
721 0 ▶ $self->execute() || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->execute or return $self->pass_error 0 0 0
722 }
723 # $self->_cleanup();
724 # @arr = $sth->fetchrow; # Array context
725 # $firstcol = $sth->fetchrow; # Scalar context
726 # return( $h->fetchrow );
727 # my $ref = $self->fetchrow_arrayref();
728 0 my $ref = $self->{sth}->fetchrow_arrayref();
729 # my $ref = $self->{sth}->fetch();
730 0 ▶ if( $ref )
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 0 0 0
731 {
732 0 ▶ return( wantarray ? @$ref : $ref->[0] );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
733 }
734 else
735 {
736 0 return( () );
737 }
738 }
739  
740 # sub fetchrow_hashref(@) is inherited from DBI
741 sub fetchrow_hashref
742 {
743 11 ▶ my $self = shift( @_ );
Subroutine fetchrow_hashref: called CC=7 SCAR=21.8
Pod documented
744 11 my $dbo = $self->database_object;
745 11 my $deb = {};
746 11 %$deb = %$self;
747 11 my $sth = $self->{sth};
748 11 ▶ if( !$self->executed() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->executed) 0 11 11
749 {
750 0 ▶ $self->execute() || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->execute or return $self->pass_error 0 0 0
751 }
752 # Ensure we set the cache of field names and types. We will need them to post process the query results with _convert_datetime2object()
753 # This is necessary for MySQL whose object lose that information, while the PostgreSQL or SQLite driver keep the information.
754 11 ▶ if( $dbo->auto_decode_json || $dbo->auto_convert_datetime_to_object )
Branch: 1 branch
BranchTrueFalseTotal
if ($dbo->auto_decode_json or $dbo->auto_convert_datetime_to_object) 11 0 11
Condition33%1/3
$dbo->auto_decode_json or $dbo->auto_convert_datetime_to_object
l!l&&r!l&&!r
1100
MC/DC0%0/2
$dbo->auto_decode_json or $dbo->auto_convert_datetime_to_object
$dbo->auto_decode_json $dbo->auto_convert_datetime_to_object
Truth table33%1/3
A or B
A= $dbo->auto_decode_jsonB= $dbo->auto_convert_datetime_to_object
ABresult
1X1
011
000
755 {
756 11 $self->_cache_field_names;
757 11 $self->_cache_field_types;
758 }
759 11 my $ref = $sth->fetchrow_hashref;
760 11 ▶ $ref = $self->_convert_json2hash({ statement => $sth, data => $ref }) if( $dbo->auto_decode_json );
Branch: 1 branch
BranchTrueFalseTotal
if $dbo->auto_decode_json 11 0 11
761 11 ▶ $ref = $self->_convert_datetime2object({ statement => $sth, data => $ref }) if( $dbo->auto_convert_datetime_to_object );
Branch: 1 branch
BranchTrueFalseTotal
if $dbo->auto_convert_datetime_to_object 0 11 11
762 11 return( $ref );
763 }
764  
765 sub fetchrow_object
766 {
767 0 ▶ my $self = shift( @_ );
Subroutine fetchrow_object: not called CC=6 SCAR=37.4
Pod documented
768 # This should give us something like Postgres or Mysql or SQLite
769 0 my $basePack = ( ref( $self ) =~ /^DB::Object::([^\:]+)/ )[0];
770 0 ▶ if( !$self->executed() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->executed) 0 0 0
771 {
772 0 ▶ $self->execute() || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->execute or return $self->pass_error 0 0 0
773 }
774 # $self->_cleanup();
775 0 my $rows = $self->{sth}->rows;
776 0 my $ref = $self->{sth}->fetchrow_hashref();
777 0 ▶ if( $ref && scalar( keys( %$ref ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($ref and scalar keys %$ref) { } 0 0 0
Condition0%0/2
$ref and scalar keys %$ref
!ll
00
MC/DC0%0/1
$ref and scalar keys %$ref
$ref
Truth table0%0/2
A
A= $ref
Aresult
00
11
778 {
779 0 my $struct = { map{ $_ => '$' } keys( %$ref ) };
780 0 my $table = $self->table;
781 0 my $class = "DB::Object::${basePack}::Result::${table}";
782 0 ▶ if( !defined( &{ $class . '::new' } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &{$class . '::new';}) 0 0 0
783 {
784 0 struct $class => $struct;
785 }
786 0 my $obj = $class->new( %$ref );
787 0 return( $obj );
788 }
789 else
790 {
791 0 return( () );
792 }
793 }
794  
795 # NOTE: field_names -> NAME
796 # <https://metacpan.org/pod/DBI#NAME>
797 5 ▶sub field_names { return( shift->_get_statement_attribute( 'NAME' ) ); }
Subroutine field_names: called CC=1 SCAR=0.0
Pod documented
798  
799 # NOTE: field_names_lc -> NAME_lc
800 # <https://metacpan.org/pod/DBI#NAME_lc>
801 0 ▶sub field_names_lc { return( shift->_get_statement_attribute( 'NAME_lc' ) ); }
Subroutine field_names_lc: not called CC=1 SCAR=6.9
Pod documented
802  
803 # NOTE: field_names_uc -> NAME_uc
804 # <https://metacpan.org/pod/DBI#NAME_uc>
805 0 ▶sub field_names_uc { return( shift->_get_statement_attribute( 'NAME_uc' ) ); }
Subroutine field_names_uc: not called CC=1 SCAR=6.9
Pod documented
806  
807 # NOTE: field_nullables -> NULLABLE
808 # <https://metacpan.org/pod/DBI#NULLABLE>
809 0 ▶sub field_nullables { return( shift->_get_statement_attribute( 'NULLABLE' ) ); }
Subroutine field_nullables: not called CC=1 SCAR=6.9
Pod documented
810  
811 # NOTE: field_precisions -> PRECISION
812 # <https://metacpan.org/pod/DBI#PRECISION>
813 0 ▶sub field_precisions { return( shift->_get_statement_attribute( 'PRECISION' ) ); }
Subroutine field_precisions: not called CC=1 SCAR=6.9
Pod documented
814  
815 # NOTE: field_scales -> SCALE
816 # <https://metacpan.org/pod/DBI#SCALE>
817 0 ▶sub field_scales { return( shift->_get_statement_attribute( 'SCALE' ) ); }
Subroutine field_scales: not called CC=1 SCAR=6.9
Pod documented
818  
819 # NOTE: field_types -> TYPE
820 # <https://metacpan.org/pod/DBI#TYPE>
821 5 ▶sub field_types { return( shift->_get_statement_attribute( 'TYPE' ) ); }
Subroutine field_types: called CC=1 SCAR=0.0
Pod documented
822  
823 sub finish
824 {
825 4 ▶ my $self = shift( @_ );
Subroutine finish: called CC=2 SCAR=7.4
Pod documented
826 4 my $rc = $self->{sth}->finish();
827 4 ▶ if( !$rc )
Branch: 1 branch
BranchTrueFalseTotal
if (not $rc) { } 0 4 4
828 {
829 0 return( $self->error( $self->{sth}->errstr() ) );
830 }
831 else
832 {
833 4 return( $rc );
834 }
835 }
836  
837 sub ignore
838 {
839 0 ▶ my $self = shift( @_ );
Subroutine ignore: not called CC=9 SCAR=45.0
Pod documented
840 my $query = $self->{query} ||
841 0 ▶ return( $self->error( "No query to set as to be ignored." ) );
Condition0%0/2
$self->{'query'} || (return $self->error('No query to set as to be ignored.'))
l!l
00
MC/DC0%0/1
$self->{'query'} || (return $self->error('No query to set as to be ignored.'))
$self->{'query'}
Truth table0%0/2
A
A= $self->{'query'}
Aresult
11
00
842  
843 0 my $type = uc( ( $query =~ /^\s*(\S+)\s+/ )[0] );
844 # ALTER for table alteration statements (DB::Object::Tables
845 0 my @allowed = qw( INSERT UPDATE ALTER );
846 0 my $allowed = CORE::join( '|', @allowed );
847 0 ▶ if( !scalar( grep{ /^$type$/i } @allowed ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar grep({/^$type$/i;} @allowed)) 0 0 0
848 {
849 0 return( $self->error( "You may not flag statement of type \U$type\E to be ignored:\n$query" ) );
850 }
851 # Incompatible. Do not bother going further
852 0 ▶ return( $self ) if( $query =~ /^\s*(?:$allowed)\s+(?:DELAYED|LOW_PRIORITY|HIGH_PRIORITY)\s+/i );
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^\s*(?:$allowed)\s+(?:DELAYED|LOW_PRIORITY|HIGH_PRIORITY)\s+/i 0 0 0
853 0 ▶ return( $self ) if( $type eq 'ALTER' && $query !~ /^\s*$type\s+TABLE\s+/i );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ALTER" and not $query =~ /^\s*$type\s+TABLE\s+/i 0 0 0
Condition0%0/3
$type eq "ALTER" and not $query =~ /^\s*$type\s+TABLE\s+/i
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "ALTER" and not $query =~ /^\s*$type\s+TABLE\s+/i
$type eq "ALTER" not $query =~ /^\s*$type\s+TABLE\s+/i
Truth table0%0/3
A and not(B)
A= $type eq "ALTER"B= not $query =~ /^\s*$type\s+TABLE\s+/i
ABresult
0X0
100
111
854  
855 0 $query =~ s/^(\s*)($allowed)(\s+)/$1$2 IGNORE /;
856 # my $sth = $self->prepare( $query ) ||
857 # $self->{ 'query' } = $query;
858 # saving parameters to bind later on must have been done previously
859 0 ▶ my $sth = $self->_cache_this( $query ) ||
Condition0%0/2
$self->_cache_this($query) || (return $self->error("Error while preparing new ignored query:\n$query"))
l!l
00
MC/DC0%0/1
$self->_cache_this($query) || (return $self->error("Error while preparing new ignored query:\n$query"))
$self->_cache_this($query)
Truth table0%0/2
A
A= $self->_cache_this($query)
Aresult
11
00
860 return( $self->error( "Error while preparing new ignored query:\n$query" ) );
861 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
862 {
863 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing new ignored query:\n$query") 0 0 0
864 return( $self->error( "Error while executing new ignored query:\n$query" ) );
865 }
866 0 return( $sth );
867 }
868  
869 sub join
870 {
871 0 ▶ my $self = shift( @_ );
Subroutine join: not called CC=68 SCAR=84.5
Pod documented
872 0 my $data = shift( @_ );
873 0 my $on;
874 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
875 {
876 0 ▶ $on = ( scalar( @_ ) == 1 && ref( $_[0] ) ) ? shift( @_ ) : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 && ref $_[0] ? : 0 0 0
Condition0%0/3
scalar @_ == 1 and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ == 1 and ref $_[0]
scalar @_ == 1 ref $_[0]
Truth table0%0/3
A and B
A= scalar @_ == 1B= ref $_[0]
ABresult
0X0
100
111
877 }
878 0 ▶ my $q = $self->query_object || return( $self->error( "No query formatter object was set" ) );
Condition0%0/2
$self->query_object || (return $self->error('No query formatter object was set'))
l!l
00
MC/DC0%0/1
$self->query_object || (return $self->error('No query formatter object was set'))
$self->query_object
Truth table0%0/2
A
A= $self->query_object
Aresult
11
00
879 0 ▶ my $tbl_o = $q->table_object || return( $self->error( "No table object is set in query object." ) );
Condition0%0/2
$q->table_object || (return $self->error('No table object is set in query object.'))
l!l
00
MC/DC0%0/1
$q->table_object || (return $self->error('No table object is set in query object.'))
$q->table_object
Truth table0%0/2
A
A= $q->table_object
Aresult
11
00
880 0 ▶ my $query = $q->query ||
Condition0%0/2
$q->query || (return $self->error('No query prepared for join with another table.'))
l!l
00
MC/DC0%0/1
$q->query || (return $self->error('No query prepared for join with another table.'))
$q->query
Truth table0%0/2
A
A= $q->query
Aresult
11
00
881 return( $self->error( "No query prepared for join with another table." ) );
882 0 ▶ if( $query !~ /^[[:blank:]]*SELECT[[:blank:]]+/i )
Branch: 1 branch
BranchTrueFalseTotal
unless ($query =~ /^[[:blank:]]*SELECT[[:blank:]]+/i) 0 0 0
883 {
884 0 return( $self->error( "You may not perform a join on a query other than select." ) );
885 }
886 0 my $constant = $q->constant;
887 # Constant is set and query object marked as final, which means this statement has already been processed as a join and so we skip all further processing.
888 0 ▶ if( scalar( keys( %$constant ) ) && $q->final )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$constant and $q->final) 0 0 0
Condition0%0/3
scalar keys %$constant and $q->final
!ll&&!rl&&r
000
MC/DC0%0/2
scalar keys %$constant and $q->final
scalar keys %$constant $q->final
Truth table0%0/3
A and B
A= scalar keys %$constantB= $q->final
ABresult
0X0
100
111
889 {
890 0 return( $self );
891 }
892 0 my $table = $tbl_o->table;
893 0 my $db = $tbl_o->database;
894 0 my $multi_db = $tbl_o->prefix_database;
895 0 my $alias = $tbl_o->as;
896 0 my $new_fields = '';
897 0 my $new_table = '';
898 0 my $new_db = '';
899 0 my $class = ref( $self );
900 0 my $q_source = $q->clone;
901 # clone() does a shallow copy - $q_source->{elements} would share the same object
902 # as $q->{elements}. Any subsequent _save_bind() on $q would then corrupt the
903 # element list of the cached statement. We reset elements here so $q_source gets
904 # its own fresh object, populated by _query_components below.
905 0 $q_source->{elements} = $q_source->new_elements;
906 # clone() copies the hash shallowly, which means {table_object} is a strong reference
907 # in the clone even though _query_object_create() weakens it for freshly created query
908 # objects. If left strong, $q_source permanently retains the table object, which in turn
909 # retains every new query object created by _reset_query() on each execute() cycle,
910 # causing an ~8 MB per-iteration memory leak. Weaken it here to restore correct ownership.
911 0 ▶ Scalar::Util::weaken( $q_source->{table_object} ) if( $q_source->{table_object} && !Scalar::Util::isweak( $q_source->{table_object} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $q_source->{"table_object"} and not builtin::is_weak($q_source->{"table_object"}) 0 0 0
Condition0%0/3
$q_source->{"table_object"} and not builtin::is_weak($q_source->{"table_object"})
!ll&&!rl&&r
000
MC/DC0%0/2
$q_source->{"table_object"} and not builtin::is_weak($q_source->{"table_object"})
$q_source->{"table_object"} not builtin::is_weak($q_source->{"table_object"})
Truth table0%0/3
A and not(B)
A= $q_source->{"table_object"}B= not builtin::is_weak($q_source->{"table_object"})
ABresult
0X0
100
111
912 0 ▶ Scalar::Util::weaken( $q_source->{database_object} ) if( $q_source->{database_object} && !Scalar::Util::isweak( $q_source->{database_object} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $q_source->{"database_object"} and not builtin::is_weak($q_source->{"database_object"}) 0 0 0
Condition0%0/3
$q_source->{"database_object"} and not builtin::is_weak($q_source->{"database_object"})
!ll&&!rl&&r
000
MC/DC0%0/2
$q_source->{"database_object"} and not builtin::is_weak($q_source->{"database_object"})
$q_source->{"database_object"} not builtin::is_weak($q_source->{"database_object"})
Truth table0%0/3
A and not(B)
A= $q_source->{"database_object"}B= not builtin::is_weak($q_source->{"database_object"})
ABresult
0X0
100
111
913  
914 0 my $q_target;
915 # On the duplicated table object, add the current table in the join
916 0 ▶ $q_source->join_tables( $tbl_o ) if( !$q_source->join_tables->length );
Branch: 1 branch
BranchTrueFalseTotal
unless $q_source->join_tables->length 0 0 0
917 # $data is a DB::Object::Postgres::Statement object - we get all its parameter and merge them with ours
918 # if( ref( $data ) && ref( $data ) eq $class )
919 0 ▶ if( ref( $data ) && $self->_is_a( $data, $class ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data and $self->_is_a($data, $class)) { } 0 0 0
Condition0%0/3
ref $data and $self->_is_a($data, $class)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $data and $self->_is_a($data, $class)
ref $data $self->_is_a($data, $class)
Truth table0%0/3
A and B
A= ref $dataB= $self->_is_a($data, $class)
ABresult
0X0
100
111
920 {
921 0 $q_target = $data->query_object;
922 }
923 # $data is the table name
924 else
925 {
926 0 my $join_tbl;
927 0 ▶ if( $self->_is_object( $data ) && $data->isa( 'DB::Object::Tables' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_object($data) and $data->isa('DB::Object::Tables')) { } 0 0 0
elsif ($self->_is_object($data)) { } 0 0 0
Condition0%0/3
$self->_is_object($data) and $data->isa("DB::Object::Tables")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_is_object($data) and $data->isa("DB::Object::Tables")
$self->_is_object($data) $data->isa("DB::Object::Tables")
Truth table0%0/3
A and B
A= $self->_is_object($data)B= $data->isa("DB::Object::Tables")
ABresult
0X0
100
111
928 {
929 0 $join_tbl = $data;
930 }
931 elsif( $self->_is_object( $data ) )
932 {
933 0 return( $self->error( "I was expecting either a table name as a scalar or a table object, but instead got \"$data\" (", ref( $data ), ")." ) );
934 }
935 else
936 {
937 0 ▶ return( $self->error( "No such table \"$data\" exists in database \"$db\"." ) ) if( !$self->database_object->table_exists( $data ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->database_object->table_exists($data) 0 0 0
938 0 $join_tbl = $self->database_object->table( $data );
939 0 ▶ return( $self->error( "Could not get a table object from \"$data\"." ) ) if( !$join_tbl );
Branch: 1 branch
BranchTrueFalseTotal
unless $join_tbl 0 0 0
940 }
941 0 ▶ $join_tbl->prefixed( $db ne $join_tbl->database_object->database ? 3 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$db ne $join_tbl->database_object->database ? : 0 0 0
942 0 ▶ my $sth_tmp = $join_tbl->select || return( $self->pass_error( $join_tbl->error ) );
Condition0%0/2
$join_tbl->select || (return $self->pass_error($join_tbl->error))
l!l
00
MC/DC0%0/1
$join_tbl->select || (return $self->pass_error($join_tbl->error))
$join_tbl->select
Truth table0%0/2
A
A= $join_tbl->select
Aresult
11
00
943 0 ▶ $q_target = $sth_tmp->query_object ||
Condition0%0/2
$sth_tmp->query_object || (return $self->error(qq[Could not get a query object out of the dummy select query I made from table "$data".]))
l!l
00
MC/DC0%0/1
$sth_tmp->query_object || (return $self->error(qq[Could not get a query object out of the dummy select query I made from table "$data".]))
$sth_tmp->query_object
Truth table0%0/2
A
A= $sth_tmp->query_object
Aresult
11
00
944 return( $self->error( "Could not get a query object out of the dummy select query I made from table \"$data\"." ) );
945 0 $new_fields = $q_target->selected_fields;
946 # NOTE: 2021-08-22: If we reset it here, we lose the table aliasing
947 # $join_tbl->reset;
948  
949 # $join_tbl->prefixed( $db ne $join_tbl->database_object->database ? 3 : 1 ) unless( $join_tbl->prefixed );
950 0 $new_table = $join_tbl->prefix;
951 0 $join_tbl->reset;
952 # We assume this table is part of our same database
953 0 $new_db = $db;
954 # my $db_data = $self->getdefault( $new_table );
955 # $new_fields = $db_data->format_statement();
956 0 $new_fields = '';
957 }
958 # TODO: check this or remove it
959 # $q_target->table_object->prefixed( $db ne $q_target->database_object->database ? 3 : 1 );
960 0 $new_fields = $q_target->selected_fields;
961 0 $new_table = $q_target->table_object->name;
962 # $new_table = $q_target->table_object->prefix;
963 0 $new_db = $q_target->database_object->database;
964 0 $q_source->join_tables->push( $q_target->table_object );
965 0 ▶ if( $q->where && $q_target->where )
Branch: 2 branches
BranchTrueFalseTotal
if ($q->where and $q_target->where) { } 0 0 0
elsif ($q_target->where) { } 0 0 0
Condition0%0/3
$q->where and $q_target->where
!ll&&!rl&&r
000
MC/DC0%0/2
$q->where and $q_target->where
$q->where $q_target->where
Truth table0%0/3
A and B
A= $q->whereB= $q_target->where
ABresult
0X0
100
111
966 {
967 # We must NOT call $q_source->where( AND(...) ) here because _where_having would
968 # reconstruct the WHERE via process_where_condition, losing the bind elements that
969 # were built up in $q->where. Instead we build a merged Clause directly, copying
970 # the existing elements from $q->where so they are available to _save_bind below.
971 0 my $merged = $q_source->new_clause;
972 0 $merged->merge( $self->AND( $q->where, $q_target->new_clause({ value => '( ' . ( $q_target->where ) . ' )' }) ) );
973 0 $q_source->{where} = $merged;
974 }
975 elsif( $q_target->where )
976 {
977 0 $q_source->where( $q_target->where );
978 }
979 0 ▶ if( my $group_target = $q_target->group )
Branch: 1 branch
BranchTrueFalseTotal
if (my $group_target = $q_target->group) 0 0 0
980 {
981 0 ▶ $q_source->group( $group_target ) if( $group_target->value->length );
Branch: 1 branch
BranchTrueFalseTotal
if $group_target->value->length 0 0 0
982 }
983 0 ▶ if( my $order_target = $q_target->order )
Branch: 1 branch
BranchTrueFalseTotal
if (my $order_target = $q_target->order) 0 0 0
984 {
985 0 ▶ $q_source->order( $order_target ) if( $order_target->value->length );
Branch: 1 branch
BranchTrueFalseTotal
if $order_target->value->length 0 0 0
986 }
987  
988 0 ▶ if( ( !$q_source->limit || ( $q_source->limit && !$q_source->limit->length ) ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (!$q_source->limit || $q_source->limit && !$q_source->limit->length and $q_source->limit and $q_source->limit->length) 0 0 0
Condition0%0/12
!$q_source->limit || $q_source->limit && !$q_source->limit->length and $q_source->limit and $q_source->limit->length
!ll&&!rl&&r
000
!$q_source->limit || $q_source->limit && !$q_source->limit->length and $q_source->limit
!ll&&!rl&&r
000
not $q_source->limit or $q_source->limit and not $q_source->limit->length
l!l&&r!l&&!r
000
$q_source->limit and not $q_source->limit->length
!ll&&!rl&&r
000
MC/DC0%0/5
!$q_source->limit || $q_source->limit && !$q_source->limit->length and $q_source->limit and $q_source->limit->length
not $q_source->limit $q_source->limit not $q_source->limit->length $q_source->limit $q_source->limit->length
Truth table0%0/8
not(A) or B and not(C) and D and E
A= not $q_source->limitB= $q_source->limitC= not $q_source->limit->lengthD= $q_source->limitE= $q_source->limit->length
ABCDEresult
00XXX0
010XX0
1XX0X0
0110X0
1XX100
011100
1XX111
011111
989 $q_source->limit && $q_source->limit->length )
990 {
991 0 $q_source->limit( $q_target->limit );
992 }
993  
994 0 ▶ my $prev_fields = length( $q->join_fields ) ? $q->join_fields : $q->selected_fields;
Branch: 1 branch
BranchTrueFalseTotal
length $q->join_fields ? : 0 0 0
995 # Regular express to prepend previous fields by their table name if that's not the case already
996 0 ▶ $tbl_o->prefixed( $db ne $new_db ? 3 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$db ne $new_db ? : 0 0 0
997 # Prefix for previous fields list
998 0 my $prev_prefix = $tbl_o->prefix;
999 0 my $prev_fields_hash = $q->table_object->fields;
1000 0 my $prev_fields_list = CORE::join( '|', sort( keys( %$prev_fields_hash ) ) );
1001 0 my $re = qr/(?<![\.\"])\b($prev_fields_list)\b/;
1002 0 $prev_fields =~ s/(?<![\.\"])\b($prev_fields_list)\b/${prev_prefix}.$1/gs;
1003 0 ▶ my $fields = $new_fields ? CORE::join( ', ', $prev_fields, $new_fields ) : $prev_fields;
Branch: 1 branch
BranchTrueFalseTotal
$new_fields ? : 0 0 0
1004 0 $q_source->join_fields( $fields );
1005 0 ▶ $q_source->from_table->push(
Branch: 3 branches
BranchTrueFalseTotal
unless $q_source->from_table->length 0 0 0
$q_source->table_alias ? : 0 0 0
$q_source->table_object->prefixed ? : 0 0 0
1006 $q_source->table_alias
1007 ? sprintf( '%s AS %s', $q_source->table_object->name, $q_source->table_alias )
1008 : ( $q_source->table_object->prefixed ? $q_source->table_object->prefix : $q_source->table_object->name )
1009 ) if( !$q_source->from_table->length );
1010 0 my $left_join = '';
1011 0 my $condition = '';
1012 0 my $format_condition;
1013 $format_condition = sub
1014 {
1015 0 ▶ my @vals = ();
Subroutine __ANON__: not called CC=22 SCAR=62.3
1016 0 my $vals = shift( @_ );
1017 0 my $op = shift( @_ );
1018 0 my @res = ();
1019 0 my $fields_tables = {};
1020 0 ▶ while( scalar( @$vals ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$vals) 0 0 0
1021 {
1022 0 ▶ if( $self->_is_object( $vals->[0] ) && $vals->[0]->isa( 'DB::Object::Operator' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_object($vals->[0]) and $vals->[0]->isa('DB::Object::Operator')) { } 0 0 0
Condition0%0/3
$self->_is_object($vals->[0]) and $vals->[0]->isa("DB::Object::Operator")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_is_object($vals->[0]) and $vals->[0]->isa("DB::Object::Operator")
$self->_is_object($vals->[0]) $vals->[0]->isa("DB::Object::Operator")
Truth table0%0/3
A and B
A= $self->_is_object($vals->[0])B= $vals->[0]->isa("DB::Object::Operator")
ABresult
0X0
100
111
1023 {
1024 0 my $sub_obj = shift( @$vals );
1025 0 my $sub_op = $sub_obj->operator;
1026 0 my( @sub_vals ) = $sub_obj->value;
1027 0 my $this_ref = $format_condition->( \@sub_vals, $sub_op );
1028 0 ▶ CORE::push( @res, $this_ref->{clause} ) if( length( $this_ref->{clause} ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $this_ref->{"clause"} 0 0 0
1029 0 my $tmp = $this_ref->{fields_tables};
1030 0 my @those_table_names = keys( %$tmp );
1031 0 @$fields_tables{ @those_table_names } = @$tmp{ @those_table_names };
1032 }
1033 else
1034 {
1035 0 ▶ if( $self->_is_object( $vals->[0] ) && $vals->[0]->isa( 'DB::Object::Fields::Overloaded' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_object($vals->[0]) and $vals->[0]->isa("DB::Object::Fields::Overloaded")) 0 0 0
Condition0%0/3
$self->_is_object($vals->[0]) and $vals->[0]->isa("DB::Object::Fields::Overloaded")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_is_object($vals->[0]) and $vals->[0]->isa("DB::Object::Fields::Overloaded")
$self->_is_object($vals->[0]) $vals->[0]->isa("DB::Object::Fields::Overloaded")
Truth table0%0/3
A and B
A= $self->_is_object($vals->[0])B= $vals->[0]->isa("DB::Object::Fields::Overloaded")
ABresult
0X0
100
111
1036 {
1037 0 my $f1 = shift( @$vals );
1038 0 ▶ $f1->field->prefixed( $multi_db ? 3 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$multi_db ? : 0 0 0
1039 0 CORE::push( @res, "$f1" );
1040 0 ▶ $fields_tables->{ $f1->field->table }++ if( !$fields_tables->{ $f1->field->table } );
Branch: 1 branch
BranchTrueFalseTotal
unless $$fields_tables{$f1->field->table} 0 0 0
1041 0 next;
1042 }
1043  
1044 0 my( $f1, $f2 ) = ( shift( @$vals ), shift( @$vals ) );
1045 0 my $i_am_negative = 0;
1046 0 ▶ if( $self->_is_object( $f2 ) && $f2->isa( 'DB::Object::NOT' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_object($f2) and $f2->isa("DB::Object::NOT")) 0 0 0
Condition0%0/3
$self->_is_object($f2) and $f2->isa("DB::Object::NOT")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_is_object($f2) and $f2->isa("DB::Object::NOT")
$self->_is_object($f2) $f2->isa("DB::Object::NOT")
Truth table0%0/3
A and B
A= $self->_is_object($f2)B= $f2->isa("DB::Object::NOT")
ABresult
0X0
100
111
1047 {
1048 0 ( $f2 ) = $f2->value;
1049 0 $i_am_negative++;
1050 }
1051  
1052 0 my( $field1, $field2 );
1053 0 ▶ if( $self->_is_object( $f1 ) && $f1->isa( 'DB::Object::Fields::Field' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_object($f1) and $f1->isa('DB::Object::Fields::Field')) { } 0 0 0
Condition0%0/3
$self->_is_object($f1) and $f1->isa("DB::Object::Fields::Field")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_is_object($f1) and $f1->isa("DB::Object::Fields::Field")
$self->_is_object($f1) $f1->isa("DB::Object::Fields::Field")
Truth table0%0/3
A and B
A= $self->_is_object($f1)B= $f1->isa("DB::Object::Fields::Field")
ABresult
0X0
100
111
1054 {
1055 0 ▶ $f1->prefixed( $multi_db ? 3 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$multi_db ? : 0 0 0
1056 0 $field1 = $f1->name;
1057 0 ▶ $fields_tables->{ $f1->table }++ if( !$fields_tables->{ $f1->table } );
Branch: 1 branch
BranchTrueFalseTotal
unless $$fields_tables{$f1->table} 0 0 0
1058 }
1059 else
1060 {
1061 0 ▶ $field1 = $multi_db ? "$new_db.$new_table.$f1" : "$new_table.$f1";
Branch: 1 branch
BranchTrueFalseTotal
$multi_db ? : 0 0 0
1062 }
1063 0 ▶ if( $self->_is_object( $f2 ) && $f2->isa( 'DB::Object::Fields::Field' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_object($f2) and $f2->isa('DB::Object::Fields::Field')) { } 0 0 0
Condition0%0/3
$self->_is_object($f2) and $f2->isa("DB::Object::Fields::Field")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_is_object($f2) and $f2->isa("DB::Object::Fields::Field")
$self->_is_object($f2) $f2->isa("DB::Object::Fields::Field")
Truth table0%0/3
A and B
A= $self->_is_object($f2)B= $f2->isa("DB::Object::Fields::Field")
ABresult
0X0
100
111
1064 {
1065 0 ▶ $f2->prefixed( $multi_db ? 3 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$multi_db ? : 0 0 0
1066 0 $field2 = $f2->name;
1067 0 ▶ $fields_tables->{ $f2->table }++ if( !$fields_tables->{ $f2->table } );
Branch: 1 branch
BranchTrueFalseTotal
unless $$fields_tables{$f2->table} 0 0 0
1068 }
1069 else
1070 {
1071 0 ▶ $field2 = $multi_db ? "$new_db.$new_table.$f2" : "$new_table.$f2";
Branch: 1 branch
BranchTrueFalseTotal
$multi_db ? : 0 0 0
1072 }
1073 0 ▶ CORE::push( @res, $i_am_negative ? "$field1 != $field2" : "$field1 = $field2" );
Branch: 1 branch
BranchTrueFalseTotal
$i_am_negative ? : 0 0 0
1074 }
1075 }
1076 return({
1077 0 clause => CORE::join( $op, @res ),
1078 fields_tables => $fields_tables,
1079 });
1080 0 };
1081  
1082 # $on is either a $dbh->AND, or $dbh->OR
1083 0 ▶ if( defined( $on ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $on) { } 0 0 0
1084 {
1085 0 ▶ if( $self->_is_object( $on ) && $on->isa( 'DB::Object::Operator' ) )
Branch: 4 branches
BranchTrueFalseTotal
if ($self->_is_object($on) and $on->isa('DB::Object::Operator')) { } 0 0 0
elsif ($self->_is_object($on) and $on->isa('DB::Object::Fields::Overloaded')) { } 0 0 0
elsif ($self->_is_array($on)) { } 0 0 0
elsif ($self->_is_hash($on, 'strict')) { } 0 0 0
Condition0%0/3
$self->_is_object($on) and $on->isa("DB::Object::Operator")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_is_object($on) and $on->isa("DB::Object::Operator")
$self->_is_object($on) $on->isa("DB::Object::Operator")
Truth table0%0/3
A and B
A= $self->_is_object($on)B= $on->isa("DB::Object::Operator")
ABresult
0X0
100
111
1086 {
1087 0 my $op = $on->operator;
1088 0 my( @vals ) = $on->value;
1089 0 my $ret = $format_condition->( \@vals, $op );
1090 0 ▶ my $as = $q_target->table_alias ? sprintf( ' AS %s', $q_target->table_alias ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$q_target->table_alias ? : 0 0 0
1091 0 $left_join = "LEFT JOIN ${new_table}${as} ON $ret->{clause}";
1092 }
1093 ▶ elsif( $self->_is_object( $on ) && $on->isa( 'DB::Object::Fields::Overloaded' ) )
Condition0%0/3
$self->_is_object($on) and $on->isa("DB::Object::Fields::Overloaded")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_is_object($on) and $on->isa("DB::Object::Fields::Overloaded")
$self->_is_object($on) $on->isa("DB::Object::Fields::Overloaded")
Truth table0%0/3
A and B
A= $self->_is_object($on)B= $on->isa("DB::Object::Fields::Overloaded")
ABresult
0X0
100
111
1094 {
1095 0 ▶ my $as = $q_target->table_alias ? sprintf( ' AS %s', $q_target->table_alias ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$q_target->table_alias ? : 0 0 0
1096 0 $left_join = "LEFT JOIN ${new_table}${as} ON ${on}";
1097 }
1098 elsif( $self->_is_array( $on ) )
1099 {
1100 0 my $ret = $format_condition->( $on, 'AND' );
1101 0 ▶ my $as = $q_target->table_alias ? sprintf( ' AS %s', $q_target->table_alias ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$q_target->table_alias ? : 0 0 0
1102 0 $left_join = "LEFT JOIN ${new_table}${as} ON $ret->{clause}";
1103 }
1104 # There is a second parameter - if so this is the condition of the 'LEFT JOIN'
1105 elsif( $self->_is_hash( $on => 'strict' ) )
1106 {
1107 # Previous join
1108 0 my $join_ref = $q_source->left_join;
1109 0 my $def = { on => $on, table_object => $q_target->table_object, query_object => $q_target };
1110 ## Add the current one
1111 0 ▶ if( $multi_db )
Branch: 1 branch
BranchTrueFalseTotal
if ($multi_db) { } 0 0 0
1112 {
1113 0 $join_ref->{ "$new_db.$new_table" } = $def;
1114 }
1115 else
1116 {
1117 0 $join_ref->{ $new_table } = $def;
1118 }
1119 # (Re)build the LEFT JOIN ... ON ... definition
1120 0 my @join_data = ();
1121 0 foreach my $joined ( keys( %$join_ref ) )
1122 {
1123 0 my $condition = $join_ref->{ $joined }->{on};
1124 0 my $to = $join_ref->{ $joined }->{table_object};
1125 0 my $qo = $join_ref->{ $joined }->{query_object};
1126 0 my $join_table_name = $to->prefix;
1127 0 my $join_table_alias = '';
1128 0 ▶ if( length( $join_table_alias = $qo->table_alias ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length($join_table_alias = $qo->table_alias)) 0 0 0
1129 {
1130 0 $join_table_alias = " AS $join_table_alias";
1131 }
1132 0 push( @join_data, "LEFT JOIN ${join_table_name}${join_table_alias} ON " . CORE::join( ' AND ', map{ "$_=$condition->{ $_ }" } keys( %$condition ) ) );
1133 }
1134 0 $left_join = CORE::join( ' ', @join_data );
1135 }
1136 else
1137 {
1138 0 warn( "Warning: I have no clue what to do with '$on' (", overload::StrVal( $on ), ") in this join for table \"", $q->table_object->name, "\"\n" );
1139 }
1140 }
1141 # Otherwise, this is a straight JOIN
1142 else
1143 {
1144 # $q_source->from_table->push( $multi_db ? "$new_db.$new_table" : $new_table );
1145 0 ▶ $q_source->from_table->push(
Branch: 2 branches
BranchTrueFalseTotal
$q_target->table_alias ? : 0 0 0
$q_target->table_object->prefixed ? : 0 0 0
1146 $q_target->table_alias
1147 ? sprintf( '%s AS %s', $q_target->table_object->name, $q_target->table_alias )
1148 : ( $q_target->table_object->prefixed ? $q_target->table_object->prefix : $q_target->table_object->name )
1149 );
1150 }
1151 0 my $from = $q_source->from_table->join( ', ' );
1152 0 my $clause = $q_source->_query_components( 'select', { no_bind_copy => 1 } );
1153 # Copy WHERE (and other clause) elements directly into $q_source->elements so that
1154 # Statement::execute() can find the bind types regardless of the use_bind setting.
1155 # We do this explicitly rather than via _save_bind() because _save_bind() is gated
1156 # on use_bind, which may be 0 even when placeholders are present.
1157 # NOTE: $where->elements returns a Module::Generic::Array of Element objects directly
1158 # (NOT an Elements wrapper). Pass ->list to Elements::push(), not the array object itself.
1159 {
1160 0 my $elems = $q_source->new_elements;
1161 0 my $where = $q_source->where;
1162 0 my $limit = $q_source->limit;
1163 0 ▶ if( $where )
Branch: 1 branch
BranchTrueFalseTotal
if ($where) 0 0 0
1164 {
1165 0 my $where_arr = $where->elements;
1166 0 ▶ $elems->push( $where_arr->list ) if( $where_arr && $where_arr->length );
Branch: 1 branch
BranchTrueFalseTotal
if $where_arr and $where_arr->length 0 0 0
Condition0%0/3
$where_arr and $where_arr->length
!ll&&!rl&&r
000
MC/DC0%0/2
$where_arr and $where_arr->length
$where_arr $where_arr->length
Truth table0%0/3
A and B
A= $where_arrB= $where_arr->length
ABresult
0X0
100
111
1167 }
1168 0 ▶ if( $limit )
Branch: 1 branch
BranchTrueFalseTotal
if ($limit) 0 0 0
1169 {
1170 0 my $limit_arr = $limit->elements;
1171 0 ▶ $elems->push( $limit_arr->list ) if( $limit_arr && $limit_arr->length );
Branch: 1 branch
BranchTrueFalseTotal
if $limit_arr and $limit_arr->length 0 0 0
Condition0%0/3
$limit_arr and $limit_arr->length
!ll&&!rl&&r
000
MC/DC0%0/2
$limit_arr and $limit_arr->length
$limit_arr $limit_arr->length
Truth table0%0/3
A and B
A= $limit_arrB= $limit_arr->length
ABresult
0X0
100
111
1172 }
1173 0 $q_source->elements( $elems );
1174  
1175 }
1176 0 my @query = ( "SELECT ${fields} FROM ${from} ${left_join}" );
1177 0 ▶ push( @query, @$clause ) if( @$clause );
Branch: 1 branch
BranchTrueFalseTotal
if @$clause 0 0 0
1178 0 my $statement = CORE::join( ' ', @query );
1179 0 $q_source->query( $statement );
1180 0 ▶ my $sth = $tbl_o->_cache_this( $q_source ) ||
Condition0%0/2
$tbl_o->_cache_this($q_source) || (return $self->error("Error while preparing query to select:\n", $q_source->as_string, $tbl_o->error))
l!l
00
MC/DC0%0/1
$tbl_o->_cache_this($q_source) || (return $self->error("Error while preparing query to select:\n", $q_source->as_string, $tbl_o->error))
$tbl_o->_cache_this($q_source)
Truth table0%0/2
A
A= $tbl_o->_cache_this($q_source)
Aresult
11
00
1181 return( $self->error( "Error while preparing query to select:\n", $q_source->as_string(), $tbl_o->error ) );
1182 # Routines such as as_string() expect an array on pupose so we do not have to commit the action
1183 # but rather get the statement string. At the end, we write:
1184 # $obj->select() to really select
1185 # $obj->select->as_string() to ONLY get the formatted statement
1186 # wantarray() returns the undefined value in void context, which is typical use of a real select command
1187 # i.e. $obj->select();
1188 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
1189 {
1190 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to select:\n", $q_source->as_string, "\nError: ", $sth->error) 0 0 0
1191 return( $self->error( "Error while executing query to select:\n", $q_source->as_string(), "\nError: ", $sth->error() ) );
1192 }
1193 0 return( $sth );
1194 }
1195  
1196 # NOTE: number_of_fields -> NUM_OF_FIELDS
1197 # <https://metacpan.org/pod/DBI#NUM_OF_FIELDS>
1198 0 ▶sub number_of_fields { return( shift->_get_statement_attribute( 'NUM_OF_FIELDS' ) ); }
Subroutine number_of_fields: not called CC=1 SCAR=6.9
Pod documented
1199  
1200 # NOTE: number_of_params -> NUM_OF_PARAMS
1201 # <https://metacpan.org/pod/DBI#NUM_OF_PARAMS>
1202 0 ▶sub number_of_params { return( shift->_get_statement_attribute( 'NUM_OF_PARAMS' ) ); }
Subroutine number_of_params: not called CC=1 SCAR=6.9
Pod documented
1203  
1204 sub object
1205 {
1206 0 ▶ my $self = shift( @_ );
Subroutine object: not called CC=3 SCAR=24.8
Pod documented
1207 # This is intended for statement to fetched their object:
1208 # my $obj = $table->select( '*' )->object();
1209 # my $obj = $table->select( '*' )
1210 # would merly execute the statement before returning its object, but there are conditions
1211 # such like using a SELECT to create a table where we do not want the statement to be executed already
1212 0 ▶ return( $self->{sth} ) if( $self->{sth} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sth"} 0 0 0
1213 # More sensible approach will return a special Module::Generic::Null object to avoid perl complaining of 'called on undef value' if this is used in chaining
1214 0 ▶ return( Module::Generic::Null->new ) if( want( 'OBJECT' ) );
Branch: 1 branch
BranchTrueFalseTotal
if want("OBJECT") 0 0 0
1215 0 return;
1216 }
1217  
1218 sub priority
1219 {
1220 0 ▶ my $self = shift( @_ );
Subroutine priority: not called CC=12 SCAR=50.5
Pod documented
1221 0 my $prio = shift( @_ );
1222 0 my $map =
1223 {
1224 0 => 'LOW_PRIORITY',
1225 1 => 'HIGH_PRIORITY',
1226 };
1227 ## Bad argument. Do not bother
1228 0 ▶ return( $self ) if( !exists( $map->{ $prio } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $map->{$prio} 0 0 0
1229  
1230 my $query = $self->{query} ||
1231 0 ▶ return( $self->error( "No query to set priority for was provided." ) );
Condition0%0/2
$self->{'query'} || (return $self->error('No query to set priority for was provided.'))
l!l
00
MC/DC0%0/1
$self->{'query'} || (return $self->error('No query to set priority for was provided.'))
$self->{'query'}
Truth table0%0/2
A
A= $self->{'query'}
Aresult
11
00
1232 0 my $type = uc( ( $query =~ /^\s*(\S+)\s+/ )[ 0 ] );
1233 0 my @allowed = qw( DELETE INSERT REPLACE SELECT UPDATE );
1234 0 my $allowed = CORE::join( '|', @allowed );
1235 # Ignore if not allowed
1236 0 ▶ if( !scalar( grep{ /^$type$/i } @allowed ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar grep({/^$type$/i;} @allowed)) 0 0 0
1237 {
1238 0 $self->error( "You may not set priority on statement of type \U$type\E:\n$query" );
1239 0 return( $self );
1240 }
1241 # Incompatible. Do not bother going further
1242 0 ▶ return( $self ) if( $query =~ /^\s*(?:$allowed)\s+(?:DELAYED|LOW_PRIORITY|HIGH_PRIORITY)\s+/i );
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^\s*(?:$allowed)\s+(?:DELAYED|LOW_PRIORITY|HIGH_PRIORITY)\s+/i 0 0 0
1243 # SELECT with something else than HIGH_PRIORITY is incompatible, so do not bother to go further
1244 0 ▶ return( $self ) if( $prio != 1 && $type =~ /^(?:SELECT)$/i );
Branch: 1 branch
BranchTrueFalseTotal
if $prio != 1 and $type =~ /^(?:SELECT)$/i 0 0 0
Condition0%0/3
$prio != 1 and $type =~ /^(?:SELECT)$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$prio != 1 and $type =~ /^(?:SELECT)$/i
$prio != 1 $type =~ /^(?:SELECT)$/i
Truth table0%0/3
A and B
A= $prio != 1B= $type =~ /^(?:SELECT)$/i
ABresult
0X0
100
111
1245 0 ▶ return( $self ) if( $prio != 0 && $type =~ /^(?:DELETE|INSERT|REPLACE|UPDATE)$/i );
Branch: 1 branch
BranchTrueFalseTotal
if $prio != 0 and $type =~ /^(?:DELETE|INSERT|REPLACE|UPDATE)$/i 0 0 0
Condition0%0/3
$prio != 0 and $type =~ /^(?:DELETE|INSERT|REPLACE|UPDATE)$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$prio != 0 and $type =~ /^(?:DELETE|INSERT|REPLACE|UPDATE)$/i
$prio != 0 $type =~ /^(?:DELETE|INSERT|REPLACE|UPDATE)$/i
Truth table0%0/3
A and B
A= $prio != 0B= $type =~ /^(?:DELETE|INSERT|REPLACE|UPDATE)$/i
ABresult
0X0
100
111
1246  
1247 0 $query =~ s/^(\s*)($allowed)(\s+)/$1$2 $map->{ $prio } /i;
1248 # $self->{ 'query' } = $query;
1249 # my $sth = $self->prepare( $query ) ||
1250 0 ▶ my $sth = $self->_cache_this( $query ) ||
Condition0%0/2
$self->_cache_this($query) || (return $self->error("Error while preparing new low priority query:\n$query"))
l!l
00
MC/DC0%0/1
$self->_cache_this($query) || (return $self->error("Error while preparing new low priority query:\n$query"))
$self->_cache_this($query)
Truth table0%0/2
A
A= $self->_cache_this($query)
Aresult
11
00
1251 return( $self->error( "Error while preparing new low priority query:\n$query" ) );
1252 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
1253 {
1254 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing new low priority query:\n$query") 0 0 0
1255 return( $self->error( "Error while executing new low priority query:\n$query" ) );
1256 }
1257 0 return( $sth );
1258 }
1259  
1260 sub promise
1261 {
1262 0 ▶ my $self = shift( @_ );
Subroutine promise: not called CC=2 SCAR=17.9
Pod documented
1263 0 ▶ $self->_load_class( 'Promise::Me' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Promise::Me") or return $self->pass_error 0 0 0
1264 return( Promise::Me->new(sub
1265 {
1266 0 ▶ return( $self->execute( @_ ) );
Subroutine __ANON__: not called CC=1 SCAR=6.9
1267 0 }) );
1268 }
1269  
1270 0 ▶sub query { return( shift->_set_get_scalar( 'query', @_ ) ); }
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
1271  
1272 0 ▶sub query_object { return( shift->_set_get_object_without_init( 'query_object', 'DB::Object::Query', @_ ) ); }
Subroutine query_object: not called CC=1 SCAR=6.9
Pod documented
1273  
1274 0 ▶sub query_time { return( shift->_set_get_datetime( 'query_time', @_ ) ); }
Subroutine query_time: not called CC=1 SCAR=6.9
Pod documented
1275  
1276 sub rollback
1277 {
1278 0 ▶ my $self = shift( @_ );
Subroutine rollback: not called CC=5 SCAR=34.0
Pod documented
1279 0 ▶ if( $self->{sth} && $self->param( 'autocommit' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sth"} and $self->param("autocommit")) 0 0 0
Condition0%0/3
$self->{"sth"} and $self->param("autocommit")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sth"} and $self->param("autocommit")
$self->{"sth"} $self->param("autocommit")
Truth table0%0/3
A and B
A= $self->{"sth"}B= $self->param("autocommit")
ABresult
0X0
100
111
1280 {
1281 0 ▶ my $sth = $self->prepare( "ROLLBACK" ) || return( $self->error( "An error occurred while preparing query to rollback: ", $self->error ) );
Condition0%0/2
$self->prepare('ROLLBACK') || (return $self->error('An error occurred while preparing query to rollback: ', $self->error))
l!l
00
MC/DC0%0/1
$self->prepare('ROLLBACK') || (return $self->error('An error occurred while preparing query to rollback: ', $self->error))
$self->prepare('ROLLBACK')
Truth table0%0/2
A
A= $self->prepare('ROLLBACK')
Aresult
11
00
1282 0 ▶ $sth->execute() || return( $self->error( "Error occurred while executing query to rollback: ", $sth->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error occurred while executing query to rollback: ", $sth->error) 0 0 0
1283 0 $sth->finish();
1284 }
1285 0 return( $self );
1286 }
1287  
1288 sub rows(@)
1289 {
1290 0 ▶ my $self = shift( @_ );
Subroutine rows: not called CC=4 SCAR=30.0
Pod documented
1291 0 ▶ if( !$self->executed() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->executed) 0 0 0
1292 {
1293 0 ▶ $self->execute() || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->execute or return $self->pass_error 0 0 0
1294 }
1295 # $self->_cleanup();
1296 # $rv = $sth->rows;
1297 0 ▶ if( !ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $self) { } 0 0 0
1298 {
1299 0 return( $DBI::rows );
1300 }
1301 else
1302 {
1303 0 return( $self->{sth}->rows() );
1304 }
1305 }
1306  
1307 # NOTE: statement -> Statement
1308 # <https://metacpan.org/pod/DBI#Statement>
1309 0 ▶sub statement { return( shift->_get_statement_attribute( 'Statement' ) ); }
Subroutine statement: not called CC=1 SCAR=6.9
Pod documented
1310  
1311 # A DBI::sth object. This should rather be a _set_get_object helper method, but I am not 100% sure if this is really a DBI::sth
1312 0 ▶sub sth { return( shift->_set_get_scalar( 'sth', @_ ) ); }
Subroutine sth: not called CC=1 SCAR=6.9
Pod documented
1313  
1314 0 ▶sub table { return( shift->{table} ); }
Subroutine table: not called CC=1 SCAR=6.9
Pod documented
1315  
1316 5 ▶sub table_object { return( shift->_set_get_object_without_init( 'table_object', 'DB::Object::Tables', @_ ) ); }
Subroutine table_object: called CC=1 SCAR=0.0
Pod documented
1317  
1318 sub undo
1319 {
1320 0 ▶ goto( &rollback );
Subroutine undo: not called CC=1 SCAR=6.9
Pod documented
1321 }
1322  
1323 0 ▶sub wait { return( shift->error( "Method wait() is not implemented by this driver." ) ); }
Subroutine wait: not called CC=1 SCAR=6.9
Pod documented
1324  
1325 # Used to get the cached field names by _convert_datetime2object
1326 sub _cache_field_names
1327 {
1328 12 ▶ my $self = shift( @_ );
Subroutine _cache_field_names: called CC=2 SCAR=6.9
1329 12 my $names;
1330 12 ▶ if( !( $names = $self->{_cache_field_names} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($names = $self->{"_cache_field_names"}) 5 7 12
1331 {
1332 5 $names = $self->{_cache_field_names} = $self->field_names;
1333 }
1334 12 return( $names );
1335 }
1336  
1337 # Used to get the cached field names by _convert_datetime2object
1338 sub _cache_field_types
1339 {
1340 12 ▶ my $self = shift( @_ );
Subroutine _cache_field_types: called CC=2 SCAR=6.9
1341 12 my $types;
1342 12 ▶ if( !( $types = $self->{_cache_field_types} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($types = $self->{"_cache_field_types"}) 5 7 12
1343 {
1344 5 $types = $self->{_cache_field_types} = $self->field_types;
1345 }
1346 12 return( $types );
1347 }
1348  
1349 # sub _convert_datetime2object { return( shift->database_object->_convert_datetime2object( @_ ) ); }
1350 # sub _convert_datetime2object
1351 # {
1352 # my $self = shift( @_ );
1353 # my $opts = $self->_get_args_as_hash( @_ );
1354 # return( $opts->{data} );
1355 # }
1356 sub _convert_datetime2object
1357 {
1358 0 ▶ my $self = shift( @_ );
Subroutine _convert_datetime2object: not called CC=15 SCAR=54.8
1359 0 my $opts = $self->_get_args_as_hash( @_ );
1360 0 ▶ my $sth = $opts->{statement} || return( $self->error( "No statement handler was provided to convert data from json to perl." ) );
Condition0%0/2
$opts->{'statement'} || (return $self->error('No statement handler was provided to convert data from json to perl.'))
l!l
00
MC/DC0%0/1
$opts->{'statement'} || (return $self->error('No statement handler was provided to convert data from json to perl.'))
$opts->{'statement'}
Truth table0%0/2
A
A= $opts->{'statement'}
Aresult
11
00
1361 # my $data = $opts->{data} || return( $self->error( "No data was provided to convert from json to perl." ) );
1362 0 ▶ return( $opts->{data} ) if( !CORE::length( $opts->{data} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"data"} 0 0 0
1363 0 my $data = $opts->{data};
1364 # my $names = $sth->FETCH('NAME');
1365 # my $types = $sth->FETCH('pg_type');
1366 # Get the cached field names and types that we stored after executing the query, but before we finished reading the statement
1367 0 my $names = $self->_cache_field_names;
1368 0 my $types = $self->_cache_field_types;
1369 0 my $mode = ref( $data );
1370  
1371 0 ▶ for( my $i = 0; $i < scalar( @$names ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$names) 0 0 0
1372 {
1373 0 ▶ if( $types->[$i] eq DBI::SQL_DATE ||
Branch: 1 branch
BranchTrueFalseTotal
if ($types->[$i] eq &DBI::SQL_DATE() or $types->[$i] eq &DBI::SQL_TIMESTAMP() or $types->[$i] eq "date" or $types->[$i] eq "timestamp") 0 0 0
Condition0%0/9
$types->[$i] eq &DBI::SQL_DATE() or $types->[$i] eq &DBI::SQL_TIMESTAMP() or $types->[$i] eq "date" or $types->[$i] eq "timestamp"
l!l&&r!l&&!r
000
$types->[$i] eq &DBI::SQL_DATE() or $types->[$i] eq &DBI::SQL_TIMESTAMP() or $types->[$i] eq "date"
l!l&&r!l&&!r
000
$types->[$i] eq &DBI::SQL_DATE() or $types->[$i] eq &DBI::SQL_TIMESTAMP()
l!l&&r!l&&!r
000
MC/DC0%0/4
$types->[$i] eq &DBI::SQL_DATE() or $types->[$i] eq &DBI::SQL_TIMESTAMP() or $types->[$i] eq "date" or $types->[$i] eq "timestamp"
$types->[$i] eq &DBI::SQL_DATE() $types->[$i] eq &DBI::SQL_TIMESTAMP() $types->[$i] eq "date" $types->[$i] eq "timestamp"
Truth table0%0/5
A or B or C or D
A= $types->[$i] eq &DBI::SQL_DATE()B= $types->[$i] eq &DBI::SQL_TIMESTAMP()C= $types->[$i] eq "date"D= $types->[$i] eq "timestamp"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1374 $types->[$i] eq DBI::SQL_TIMESTAMP ||
1375 $types->[$i] eq 'date' ||
1376 $types->[$i] eq 'timestamp' )
1377 {
1378 0 ▶ if( $mode eq 'ARRAY' )
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'ARRAY') { } 0 0 0
elsif ($mode eq 'HASH') { } 0 0 0
1379 {
1380 0 ▶ for( my $j = 0; $j < scalar( @$data ); $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @$data) 0 0 0
1381 {
1382 0 ▶ next if( !$data->[ $j ]->{ $names->[ $i ] } );
Branch: 1 branch
BranchTrueFalseTotal
unless $data->[$j]{$names->[$i]} 0 0 0
1383 0 my $dt = $self->_convert_string2datetime( $data->[ $j ]->{ $names->[ $i ] } );
1384 0 ▶ if( !defined( $dt ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dt) 0 0 0
1385 {
1386 0 warn( $self->error );
1387 }
1388 0 $data->[ $j ]->{ $names->[ $i ] } = $dt;
1389 }
1390 }
1391 elsif( $mode eq 'HASH' )
1392 {
1393 0 ▶ next if( !$data->{ $names->[ $i ] } );
Branch: 1 branch
BranchTrueFalseTotal
unless $$data{$names->[$i]} 0 0 0
1394 0 my $dt = $self->_convert_string2datetime( $data->{ $names->[ $i ] } );
1395 0 ▶ if( !defined( $dt ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dt) 0 0 0
1396 {
1397 0 warn( $self->error );
1398 }
1399 0 $data->{ $names->[ $i ] } = $dt;
1400 }
1401 }
1402 }
1403 0 return( $data );
1404 }
1405  
1406 # sub _convert_json2hash { return( shift->database_object->_convert_json2hash( @_ ) ); }
1407 # Does nothing by default
1408 # Must be superseded by the subclasses because we use the data types like PG_JSON, PG_JSONB
1409 # and we don't have them at this top level
1410 sub _convert_json2hash
1411 {
1412 12 ▶ my $self = shift( @_ );
Subroutine _convert_json2hash: called CC=1 SCAR=0.0
1413 12 my $opts = $self->_get_args_as_hash( @_ );
1414 12 return( $opts->{data} );
1415 }
1416  
1417 sub _convert_string2datetime
1418 {
1419 0 ▶ my $self = shift( @_ );
Subroutine _convert_string2datetime: not called CC=4 SCAR=30.0
1420 0 ▶ my $str = shift( @_ ) || return;
Condition0%0/2
shift @_ || (return)
l!l
00
MC/DC0%0/1
shift @_ || (return)
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
1421 0 my $dt = $self->_parse_timestamp( $str );
1422 0 ▶ if( !defined( $dt ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $dt) { } 0 0 0
elsif (not $dt) { } 0 0 0
1423 {
1424 0 return( $self->pass_error );
1425 }
1426 # An empty string means it did not fail, but probably did not recognise the format
1427 elsif( !$dt )
1428 {
1429 0 return( $str );
1430 }
1431 0 return( $dt );
1432 }
1433  
1434 sub _get_statement_attribute
1435 {
1436 10 ▶ my $self = shift( @_ );
Subroutine _get_statement_attribute: called CC=5 SCAR=17.4
1437 10 my $attr = shift( @_ );
1438 10 ▶ if( !length( $attr // '' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (length($attr // "")) 0 10 10
Condition50%1/2
$attr // ""
l!l
100
MC/DC0%0/1
$attr // ""
$attr
Truth table50%1/2
A
A= $attr
Aresult
11
00
1439 {
1440 0 return( $self->error( "No statement attribute was provided to retrieve its value." ) );
1441 }
1442 10 ▶ my $sth = $self->{sth} || die( "Unable to get the underlying statement handler!" );
Condition50%1/2
$self->{'sth'} || die('Unable to get the underlying statement handler!')
l!l
100
MC/DC0%0/1
$self->{'sth'} || die('Unable to get the underlying statement handler!')
$self->{'sth'}
Truth table50%1/2
A
A= $self->{'sth'}
Aresult
11
00
1443 10 my $val;
1444 # try-catch
1445 10 local $@;
1446 eval
1447 10 {
1448 # Ensure we stringify
1449 10 $val = $sth->{ "$attr" };
1450 };
1451 10 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 10 10
1452 {
1453 0 return( $self->error( "Error retrieving value for '${attr}': $@" ) );
1454 }
1455 10 return( $val );
1456 }
1457  
1458 # NOTE: DESTROY
1459 DESTROY
1460 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
1461 # Do nothing but existing so it is handled by this package
1462 # print( STDERR "DESTROY'ing statement $self ($self->{ 'query' })\n" );
1463 };
1464  
1465 # NOTE: Storable hooks
1466 # We intentionally do NOT freeze DBI handles (dbh/sth) or back-references (dbo/table_object)
1467 # because they are process-local and cannot be safely restored.
1468 # NOTE: For CBOR and Sereal
1469 sub FREEZE
1470 {
1471 0 ▶ my $self = CORE::shift( @_ );
Subroutine FREEZE: not called CC=8 SCAR=42.8
Pod undocumented
1472 0 ▶ my $serialiser = CORE::shift( @_ ) // '';
Condition0%0/2
shift @_ // ""
l!l
00
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
1473 0 my $class = CORE::ref( $self );
1474  
1475 # We keep a strict allow-list to avoid accidentally freezing DBI handles or other
1476 # process-local state.
1477 # 2026-01-29: I removed 'query_object' in an effort to reduce memory consumption. Let's see...
1478 0 my @props = qw(
1479 as_string bind cache params query query_object selected_fields
1480 table tie tie_order _fields _cache_field_names _cache_field_types
1481 );
1482  
1483 0 my $hash = {};
1484 0 foreach my $prop ( @props )
1485 {
1486 0 ▶ if( CORE::exists( $self->{ $prop } ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$prop} and defined $self->{$prop} and ref $self->{$prop} ne "CODE") 0 0 0
Condition0%0/6
exists $self->{$prop} and defined $self->{$prop} and ref $self->{$prop} ne "CODE"
!ll&&!rl&&r
000
exists $self->{$prop} and defined $self->{$prop}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $self->{$prop} and defined $self->{$prop} and ref $self->{$prop} ne "CODE"
exists $self->{$prop} defined $self->{$prop} ref $self->{$prop} ne "CODE"
Truth table0%0/4
A and B and C
A= exists $self->{$prop}B= defined $self->{$prop}C= ref $self->{$prop} ne "CODE"
ABCresult
0XX0
10X0
1100
1111
1487 defined( $self->{ $prop } ) &&
1488 CORE::ref( $self->{ $prop } ) ne 'CODE' )
1489 {
1490 0 $hash->{ $prop } = $self->{ $prop };
1491 }
1492 }
1493 0 $hash->{_serial_version} = $DB::Object::SERIALISATION_VERSION;
1494  
1495 # Return an array reference rather than a list so this works with Sereal and CBOR.
1496 # Before Sereal version 4.023, Sereal did not support multiple values returned.
1497 0 ▶ if( $serialiser eq 'Sereal' )
Branch: 1 branch
BranchTrueFalseTotal
if ($serialiser eq "Sereal") 0 0 0
1498 {
1499 0 require Sereal::Encoder;
1500 0 require version;
1501  
1502 0 ▶ if( version->parse( Sereal::Encoder->VERSION ) < version->parse( '4.023' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse("Sereal::Encoder"->VERSION) < "version"->parse("4.023")) 0 0 0
1503 {
1504 0 CORE::return( [$class, $hash] );
1505 }
1506 }
1507  
1508 # But Storable wants a list with the first element being the serialised element
1509 0 CORE::return( $class, $hash );
1510 }
1511  
1512 0 ▶sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
Pod undocumented
1513  
1514 0 ▶sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
Pod undocumented
1515  
1516 sub STORABLE_thaw_post_processing
1517 {
1518 0 ▶ my $obj = shift( @_ );
Subroutine STORABLE_thaw_post_processing: not called CC=1 SCAR=6.9
Pod undocumented
1519 0 my @keys = %$obj;
1520 0 my $class = ref( $obj );
1521 0 my $hash = {};
1522 0 @$hash{ @keys } = @$obj{ @keys };
1523 0 my $self = bless( $hash => $class );
1524 0 return( $self );
1525 }
1526  
1527 sub THAW
1528 {
1529 # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue
1530 # 'CBOR' as the second value.
1531 0 ▶ my( $self, undef, @args ) = @_;
Subroutine THAW: not called CC=23 SCAR=62.2
Pod undocumented
1532 0 ▶ my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
Branch: 1 branch
BranchTrueFalseTotal
scalar @args == 1 && ref $args[0] eq 'ARRAY' ? : 0 0 0
Condition0%0/3
scalar @args == 1 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @args == 1 and ref $args[0] eq "ARRAY"
scalar @args == 1 ref $args[0] eq "ARRAY"
Truth table0%0/3
A and B
A= scalar @args == 1B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
1533 0 ▶ my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
Branch: 1 branch
BranchTrueFalseTotal
defined $ref && ref $ref eq 'ARRAY' && scalar @$ref > 1 ? : 0 0 0
Condition0%0/9
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
!ll&&!rl&&r
000
defined $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
000
ref $self or $self
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
defined $ref ref $ref eq "ARRAY" scalar @$ref > 1
MC/DC0%0/2
ref $self or $self
ref $self $self
Truth table0%0/4
A and B and C
A= defined $refB= ref $ref eq "ARRAY"C= scalar @$ref > 1
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
1534 0 ▶ my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 0 0 0
1535  
1536 0 ▶ my $sv = CORE::delete( $hash->{_serial_version} ) // 0;
Condition0%0/2
delete $hash->{"_serial_version"} // 0
l!l
00
MC/DC0%0/1
delete $hash->{"_serial_version"} // 0
delete $hash->{"_serial_version"}
Truth table0%0/2
A
A= delete $hash->{"_serial_version"}
Aresult
11
00
1537 0 ▶ if( $sv != $DB::Object::SERIALISATION_VERSION )
Branch: 1 branch
BranchTrueFalseTotal
if ($sv != $DB::Object::SERIALISATION_VERSION) 0 0 0
1538 {
1539 0 require( $EXCEPTION_CLASS );
1540 0 die( $EXCEPTION_CLASS->new({
1541 code => 409, # Conflict
1542 message => sprintf(
1543 "Serialisation version mismatch in %s: expected %d, got %d. Please purge the cache.",
1544 $class,
1545 $DB::Object::SERIALISATION_VERSION,
1546 $sv,
1547 )
1548 }) );
1549 }
1550  
1551 # Since this class is inherited, we also need to load from which we depend, if necessary.
1552 0 ▶ if( $class ne 'DB::Object::Statement' )
Branch: 1 branch
BranchTrueFalseTotal
if ($class ne "DB::Object::Statement") 0 0 0
1553 {
1554 0 require DB::Object;
1555 0 require DB::Object::Statement;
1556 0 my @supported_classes = CORE::values( %$DB::Object::DRIVER2PACK );
1557 0 push( @supported_classes, 'DB::Object' );
1558 0 my $ok_classes = CORE::join( '|', CORE::map{ CORE::quotemeta( $_ ) } @supported_classes );
1559 0 my $base_class = ( $class =~ /^($ok_classes)/ )[0];
1560 0 eval( "require $base_class;" );
1561 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1562 {
1563 0 require( $EXCEPTION_CLASS );
1564 0 die( $EXCEPTION_CLASS->new( "Failure to load $base_class: $@" ) );
1565 }
1566  
1567 # If inheritance is still missing (or incomplete) for any reason, repair it explicitly.
1568 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1569  
1570 0 my $stash = \%{"${class}\::"};
1571 0 my $isa_ref;
1572 # We check if the 'ISA' symbol exists in the stash and its ARRAY slot is defined
1573 0 ▶ if( exists( $stash->{ISA} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $stash->{'ISA'} and defined *{"${class}::ISA";}{'ARRAY'}) { } 0 0 0
Condition0%0/3
exists $stash->{"ISA"} and defined *{"${class}::ISA";}{"ARRAY"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $stash->{"ISA"} and defined *{"${class}::ISA";}{"ARRAY"}
exists $stash->{"ISA"} defined *{"${class}::ISA";}{"ARRAY"}
Truth table0%0/3
A and B
A= exists $stash->{"ISA"}B= defined *{"${class}::ISA";}{"ARRAY"}
ABresult
0X0
100
111
1574 0 defined( *{"${class}\::ISA"}{ARRAY} ) )
1575 {
1576 0 $isa_ref = *{"${class}\::ISA"}{ARRAY};
1577 }
1578 else
1579 {
1580 # # Force creation of @ISA array slot
1581 # @{"${class}::ISA"} = ();
1582 # $isa_ref = *{"${class}::ISA"}{ARRAY};
1583 # But I want it to die, so this can be dealt with, and not swept under the rug.
1584 0 die( "Unable to find the \@ISA value in $class. The package contains the following stashes:\n", $self->Module::Generic::dump( $stash ) );
1585 }
1586  
1587 0 my $has_stmt = 0;
1588 0 my $has_base = 0;
1589  
1590 0 foreach my $p ( @$isa_ref )
1591 {
1592 # Same as what we can find in modules DB::Object::(Postgres|SQLite|Mysql)::Statement
1593 0 ▶ $has_stmt = 1 if( CORE::defined( $p ) && $p eq 'DB::Object::Statement' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $p and $p eq "DB::Object::Statement" 0 0 0
Condition0%0/3
defined $p and $p eq "DB::Object::Statement"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p and $p eq "DB::Object::Statement"
defined $p $p eq "DB::Object::Statement"
Truth table0%0/3
A and B
A= defined $pB= $p eq "DB::Object::Statement"
ABresult
0X0
100
111
1594 0 ▶ $has_base = 1 if( CORE::defined( $p ) && $p eq $base_class );
Branch: 1 branch
BranchTrueFalseTotal
if defined $p and $p eq $base_class 0 0 0
Condition0%0/3
defined $p and $p eq $base_class
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p and $p eq $base_class
defined $p $p eq $base_class
Truth table0%0/3
A and B
A= defined $pB= $p eq $base_class
ABresult
0X0
100
111
1595 }
1596  
1597 0 ▶ if( !$has_stmt || !$has_base )
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_stmt and $has_base) 0 0 0
Condition0%0/3
$has_stmt and $has_base
!ll&&!rl&&r
000
MC/DC0%0/2
$has_stmt and $has_base
$has_stmt $has_base
Truth table0%0/3
A and B
A= $has_stmtB= $has_base
ABresult
0X0
100
111
1598 {
1599 0 @$isa_ref = ( 'DB::Object::Statement', $base_class );
1600 }
1601 }
1602 0 my $new;
1603 # Storable pattern requires to modify the object it created rather than returning a new one
1604 0 ▶ if( CORE::ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
1605 {
1606 0 foreach( CORE::keys( %$hash ) )
1607 {
1608 0 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
1609 }
1610 0 $new = $self;
1611 }
1612 else
1613 {
1614 0 $new = CORE::bless( $hash => $class );
1615 }
1616 0 CORE::return( $new );
1617 }
1618  
1619 2 1;
1620 # NOTE: POD
1621 __END__
1622  
1623 =encoding utf-8
1624  
1625 =head1 NAME
1626  
1627 DB::Object::Statement - Statement Object
1628  
1629 =head1 SYNOPSIS
1630  
1631 say $sth->as_string;
1632 $sth->bind_param( 2, $binded_value );
1633 $sth->bind_param( 2, $binded_value, $binded_type );
1634 $sth->commit;
1635 my $dbh = $sth->database_object;
1636 $sth->distinct;
1637 say $sth->dump;
1638 say $sth->execute;
1639 $sth->execute( $val1, $val2 ) || die( $sth->error );
1640 # explicitly specify types
1641 # Here in this mixed example, $val1 and $val3 have known types
1642 $tbl->where( $dbh->AND(
1643 $tbl->fo->name == '?',
1644 $tbl->fo->city == '?',
1645 '?' == $dbh->ANY( $tbl->fo->alias )
1646 ) );
1647 my $sth = $tbl->select || die( $tbl->error );
1648 $sth->execute( $val1, $val2, { $val3 => 'varchar' } ) || die( $sth->error );
1649 my $ref = $sth->fetchall_arrayref;
1650 my $val = $sth->fetchcol;
1651 my %hash = $sth->fetchhash;
1652 my @values = $sth->fetchrow;
1653 my $ref = $sth->fetchrow_hashref;
1654 my $obj = $sth->fetchrow_object;
1655 $sth->finish;
1656 $sth->ignore;
1657 $sth->join( $join_condition );
1658 my $qo = $sth->query_object;
1659 $sth->rollback;
1660 my $rows = $sth->rows;
1661 my $dbi_sth = $sth->sth;
1662 my $tbl = $sth->table_object;
1663  
1664 =head1 VERSION
1665  
1666 v0.9.0
1667  
1668 =head1 DESCRIPTION
1669  
1670 This is the statement object package from which other driver specific packages inherit from.
1671  
1672 =head1 METHODS
1673  
1674 =head2 as_string
1675  
1676 Returns the current statement object as a string.
1677  
1678 =head2 attach
1679  
1680 $tbl->attach( $db_object );
1681  
1682 Provided with a L<DB::Object>, or one of its inheriting classes, and this will attach that database object to this statement object.
1683  
1684 It returns the current statement object upon success, or upon error, it set an error an returns an empty list in list context, or C<undef> in scalar context.
1685  
1686 =head2 bind_param
1687  
1688 Provided with a list of arguments and they will be passed to L<DBI/bind_param>
1689  
1690 If an error occurred, an error is returned, otherwise the return value of calling C<bind_param> is returned.
1691  
1692 =head2 commit
1693  
1694 If the statement parameter I<autocommit> is true, a C<COMMIT> statement will be prepared and executed.
1695  
1696 The current object is returned.
1697  
1698 =head2 database_object
1699  
1700 Sets or gets the current database object.
1701  
1702 =head2 distinct
1703  
1704 Assuming a I<query> object property has already been set previously, this will add the C<DISTINCT> keyword to it if not already set.
1705  
1706 If L</distinct> is called in void context, the query is executed immediately.
1707  
1708 The query statement is returned.
1709  
1710 =head2 dump
1711  
1712 Provided with a file and this will print on STDOUT the columns used, separated by a tab and then will process each rows fetched with L<DBI::fetchrow> and will join the column valus with a tab before printing it out to STDOUT.
1713  
1714 It returns the current object for chaining.
1715  
1716 =head2 exec
1717  
1718 This is an alias for L</execute>
1719  
1720 =head2 execute
1721  
1722 $sth->execute || die( $sth->error );
1723 $sth->execute( $val1, $val2 ) || die( $sth->error );
1724 # explicitly specify types
1725 # Here in this mixed example, $val1 and $val3 have known types
1726 $tbl->where( $dbh->AND(
1727 $tbl->fo->name == '?',
1728 $tbl->fo->city == '?',
1729 '?' == $dbh->ANY( $tbl->fo->alias )
1730 ) );
1731 my $sth = $tbl->select || die( $tbl->error );
1732 $sth->execute( $val1, $val2, { $val3 => 'varchar' } ) || die( $sth->error );
1733  
1734 If binded values have been prepared, they are applied here before executing the query.
1735  
1736 Sometime, you need to clearly specify what the datatype are for the value provided with C<execute>, because L<DB::Object::Query> could not figure it out.
1737  
1738 Thus, if you do:
1739  
1740 $tbl->where(
1741 $tbl->fo->name == '?'
1742 );
1743  
1744 L<DB::Object::Query> knows the datatype, because you are using a field object (C<fo>), but if you were doing:
1745  
1746 $tbl->where(
1747 '?' == $dbh->ANY( $tbl->fo->alias )
1748 );
1749  
1750 In this scenario, L<DB::Object::Query> does not know what the bind value would be, although we could venture a guess by looking at the right-hand side, but this is a bit hazardous. So you are left with a placeholder, but no datatype. So you would execute like:
1751  
1752 $sth->execute({ $val => 'varchar' });
1753  
1754 If the total number of binded values does not match the total number of binded type, this will trigger a warning.
1755  
1756 L<DBI/execute> will be called with the binded values and if this method was called in an object context, the current object is returned, otherwise the returned value from L<DBI/execute> is returned.
1757  
1758 With the version C<0.5.0> of this module, this method is more able to find out the data type of the table field. To achieve this, it uses the L<field object|DB::Object::Fields::Field> set in each L<element object|DB::Object::Query::Element>. Those element objects are instantiated upon C<insert> or C<update> query.
1759  
1760 Also, if you provide a value during an C<insert> or C<update> for a field that the database expects an array, this method will automatically convert it into an array.
1761  
1762 Likewise, if the table field is of type C<json> or C<jsonb> and an hash reference value is provided, this method will encode the hash reference into a C<JSON> string.
1763  
1764 =head2 executed
1765  
1766 Returns true if this statement has already been executed, and false otherwise.
1767  
1768 =head2 fetchall_arrayref
1769  
1770 Similar to L<DBI/fetchall_arrayref>, this will execute the query and return an array reference of data.
1771  
1772 =head2 fetchcol
1773  
1774 Provided with an integer that represents a column number, starting from 0, and this will get each row of results and add the value for the column at the given offset.
1775  
1776 it returns a list of those column value fetched.
1777  
1778 =head2 fetchhash
1779  
1780 This will retrieve an hash reference for the given row and return it as a regular hash.
1781  
1782 =head2 fetchrow
1783  
1784 This will retrieve the data from database using L</fetchrow_arrayref> and return the list of data as array in list context, or the first entry of the array in scalar context.
1785  
1786 =head2 fetchrow_hashref
1787  
1788 This will retrieve the data from the database as an hash reference.
1789  
1790 It will convert any data from json to hash reference if L<DB::Object/auto_decode_json> is set to true.
1791  
1792 it will also convert any datetime data into a L<DateTime> object if L<DB::Object/auto_convert_datetime_to_object> is true.
1793  
1794 It returns the hash reference retrieved.
1795  
1796 =head2 fetchrow_object
1797  
1798 This will create dynamically a package named C<DB::Object::Postgres::Result::SomeTable> for example and load the hash reference retrieved from the database into this dynamically created packackage.
1799  
1800 It returns the object thus created.
1801  
1802 =head2 field_names
1803  
1804 my $array_ref = $sth->field_names;
1805  
1806 Returns an array reference of field names for the query.
1807 This must be called after executing the query, and before finishing retrieving all data from the database.
1808  
1809 See also L<https://metacpan.org/pod/DBI#NAME1>
1810  
1811 =head2 field_names_lc
1812  
1813 my $array_ref = $sth->field_names_lc;
1814  
1815 Same as L<field_names|/field_names>, except this makes all the column names in lower case.
1816  
1817 See also L<https://metacpan.org/pod/DBI#NAME_lc>
1818  
1819 =head2 field_names_uc
1820  
1821 my $array_ref = $sth->field_names_uc;
1822  
1823 Same as L<field_names|/field_names>, except this makes all the column names in lower case.
1824  
1825 See also L<https://metacpan.org/pod/DBI#NAME_uc>
1826  
1827 =head2 field_nullables
1828  
1829 my $array_ref = $sth->field_nullables;
1830  
1831 Returns an array reference of value for each field indicating if they can be null or not.
1832  
1833 Possible values are: C<0> (or an empty string) = no, C<1> = yes, C<2> = unknown.
1834  
1835 See also L<https://metacpan.org/pod/DBI#NULLABLE>
1836  
1837 =head2 field_precisions
1838  
1839 my $array_ref = $sth->field_precisions;
1840  
1841 Returns an array reference of integer value foe each field indicating their precision.
1842  
1843 As per DBI documentation: "For numeric columns, the value is the maximum number of digits (without considering a sign character or decimal point). Note that the "display size" for floating point types (REAL, FLOAT, DOUBLE) can be up to 7 characters greater than the precision (for the sign + decimal point + the letter E + a sign + 2 or 3 digits).
1844  
1845 For any character type column the value is the OCTET_LENGTH, in other words the number of bytes, not characters."
1846  
1847 See also L<https://metacpan.org/pod/DBI#PRECISION>
1848  
1849 =head2 field_scales
1850  
1851 my $array_ref = $sth->field_scales;
1852  
1853 Returns an array reference of integer value foe each field. "C<NULL> (C<undef>) values indicate columns where scale is not applicable."
1854  
1855 See also L<https://metacpan.org/pod/DBI#SCALE>
1856  
1857 =head2 field_types
1858  
1859 my $array_ref = $sth->field_types;
1860  
1861 Returns an array reference of integer value foe each field. "The value indicates the data type of the corresponding column."
1862  
1863 You can important those constant with C<use DBI ':sql_types'>
1864  
1865 See also L<https://metacpan.org/pod/DBI#TYPE>
1866  
1867 =head2 finish
1868  
1869 Calls L<DBI/finish> and return the returned value, or an error if an error occurred.
1870  
1871 =head2 ignore
1872  
1873 This will change the query prepared and add the keyword C<IGNORE>.
1874  
1875 If called in void context, this will execute the resulting statement handler immediately.
1876  
1877 =head2 join
1878  
1879 Provided with a target and an hash reference, or list or array reference of condition for the join and this will prepare the join statement.
1880  
1881 If the original query is not of type C<select>, this will trigger an error.
1882  
1883 The target mentioned above can be either a L<DB::Object::Statement> object, or a table object (L<DB::Object::Tables>), or even just a string representing the name of a table.
1884  
1885 $tbl->select->join( $sth );
1886 $tbl->select->join( $other_tbl );
1887 $tbl->select->join( 'table_name' );
1888  
1889 The condition mentioned above can be a L<DB::Object::Operator> (C<AND>, C<OR> or C<NOT>), in which case the actual condition will be taken from that operator embedded value.
1890  
1891 The condition can also be a L<DB::Object::Fields::Overloaded> object, which implies a table field with some operator and some value.
1892  
1893 $tbl->select->join( $other_tbl, $other_tbl->fo->id == 2 );
1894  
1895 Here C<$other_tbl->fo->id == 2> will become a L<DB::Object::Fields::Overloaded> object.
1896  
1897 The condition can also be an array reference or array object of conditions and implicitly the array entry will be joined with C<AND>:
1898  
1899 $tbl->select->join( $other_tbl, ["user = 'joe'", $other_tbl->fo->id == 2] );
1900  
1901 The condition can also be an hash reference with each key being a table name to join and each value an hash reference of condition for that particular join with each key being a column name and each value the value of the join for that column.
1902  
1903 my $tbl = $dbh->first_table;
1904 $tbl->select->join({
1905 other_table =>
1906 {
1907 id => 'first_table.id',
1908 user => 'first_table.user',
1909 },
1910 yet_another_table =>
1911 {
1912 id => 'other_table.id',
1913 },
1914 });
1915  
1916 would become something like:
1917  
1918 SELECT *
1919 FROM first_table
1920 LEFT JOIN other_table ON
1921 first_table.id = id AND
1922 first_table.user = user
1923 LEFT JOIN yet_another_table ON
1924 other_table.id = id
1925  
1926 Each condition will be formatted assuming an C<AND> expression, so this is less flexible than using operator objects and table field objects.
1927  
1928 If no condition is provided, this is taken to be a straight join.
1929  
1930 $tbl->where( $tbl->fo->id == 2 );
1931 $other_tbl->where( $other_tbl->fo->user 'john' );
1932 $tbl->select->join( $other_tbl );
1933  
1934 Would become something like:
1935  
1936 SELECT *
1937 FROM first_table, other_table
1938 WHERE id = 2 AND user = 'john'
1939  
1940 If called in void context, this will execute the resulting statement handler immediately.
1941  
1942 It returns the resulting statement handler.
1943  
1944 It returns the statement handler.
1945  
1946 =head2 number_of_fields
1947  
1948 $sth->exec || die( $sth->error );
1949 say "There are ", $sth->number_of_fields, " columns in this tuple.";
1950  
1951 Returns an integer representing the number of fields in the SQL query executed.
1952  
1953 See also L<https://metacpan.org/pod/DBI#NUM_OF_FIELDS>
1954  
1955 =head2 number_of_params
1956  
1957 $sth->exec || die( $sth->error );
1958 say "There are ", $sth->number_of_params, " placeholders for this query.";
1959  
1960 Returns the number of parameters (placeholders) in the prepared statement.
1961  
1962 See also L<https://metacpan.org/pod/DBI#NUM_OF_PARAMS>
1963  
1964 =head2 object
1965  
1966 Returns the statement object explicitly.
1967  
1968 my $sth = $tbl->select->object;
1969  
1970 which is really equivalent to:
1971  
1972 my $sth = $tbl->select;
1973  
1974 =head2 priority
1975  
1976 Provided with a priority integer that can be 0 or 1 with 0 being C<LOW_PRIORITY> and 1 being C<HIGH_PRIORITY> and this will adjust the query formatted to add the priority. This works only on Mysql drive though.
1977  
1978 If used on queries other than C<DELETE>, C<INSERT>, C<REPLACE>, C<SELECT>, C<UPDATE> an error will be returned.
1979  
1980 If called in void context, this will execute the newly create statement handler immediately.
1981  
1982 It returns the newly create statement handler.
1983  
1984 =head2 promise
1985  
1986 This the same as calling L</execute>, except that the query will be executed asynchronously and a L<Promise::Me> object will be returned, so you can do asynchronous queries like this:
1987  
1988 my $sth = $dbh->prepare( "SELECT some_slow_function(?)" ) || die( $dbh->error );
1989 my $p = $sth->promise(10)->then(sub
1990 {
1991 my $st = shift( @_ );
1992 my $ref = $st->fetchrow_hashref;
1993 my $obj = My::Module->new( %$ref );
1994 })->catch(sub
1995 {
1996 $log->warn( "Failed to execute query: ", @_ );
1997 });
1998 my( $obj ) = await( $p );
1999  
2000 =head2 query
2001  
2002 Sets or gets the previously formatted query as a regular string.
2003  
2004 =head2 query_object
2005  
2006 Sets or gets the query object used in this query.
2007  
2008 =head2 query_time
2009  
2010 Sets or gets the query time as a L<DateTime> object.
2011  
2012 =head2 rollback
2013  
2014 If there is a statement handler and the database parameter C<autocommit> is set to true, this will prepare a C<ROLLBACK> query and execute it.
2015  
2016 =head2 rows
2017  
2018 Returns the number of rows affected by the last query.
2019  
2020 =head2 statement
2021  
2022 Returns the SQL statement that was sent to the server to be prepared.
2023  
2024 This is different from L<as_string|/as_string> in that L<as_string|/as_string> returns the SQL query built by L<DB::Object>. Obviously, both should be the same, but they come from different sources.
2025  
2026 =head2 sth
2027  
2028 Sets or gets the L<DBI> statement handler.
2029  
2030 =head2 table
2031  
2032 Sets or gets the table object (L<DB::Object::Tables>) for this query.
2033  
2034 =head2 table_object
2035  
2036 Sets or get the table object (L<DB::Object::Tables>)
2037  
2038 =head2 undo
2039  
2040 This is an alias for L</rollback>
2041  
2042 =head2 wait
2043  
2044 The implementation is driver dependent, and in this case, this is implemented only in L<DB::Object::Mysql>
2045  
2046 =head2 _convert_datetime2object
2047  
2048 A convenient short to enable or disable L<DB::Object/_convert_datetime2object>
2049  
2050 =head2 _convert_json2hash
2051  
2052 A convenient short to enable or disable L<DB::Object/_convert_json2hash>
2053  
2054 =head1 SEE ALSO
2055  
2056 L<DB::Object::Query>, L<DB::Object::Mysql::Query>, L<DB::Object::Postgres::Query>, L<DB::Object::SQLite::Query>
2057  
2058 =head1 AUTHOR
2059  
2060 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
2061  
2062 =head1 COPYRIGHT & LICENSE
2063  
2064 Copyright (c) 2019-2021 DEGUEST Pte. Ltd.
2065  
2066 You can use, copy, modify and redistribute this package and associated
2067 files under the same terms as Perl itself.
2068  
2069 =cut
↑ Summary blib/lib/DB/Object/Statement.pm »