lib/DB/Object/Query.pm

filter: Statementstmt 51.1% 597 / 1168 Branchbran 31.3% 236 / 754 Conditioncond 26.3% 135 / 512 MC/DCmcdc 2.7% 8 / 287 Subroutinesub 58.5% 55 / 94 Podpod 92.7% 64 / 69 totaltotal 37.9% 1095 / 2884 CC 589 SCAR 59.8
CC
589
Cov
40%
__ANON__
1433.3
select
935.2
_group_order
468.8
CRAP 396.5
« blib/lib/DB/Object.pm ↑ Summary blib/lib/DB/Object/SQLite/Query.pm »
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## Database Object Interface - ~/lib/DB/Object/Query.pm
4 ## Version v0.10.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 package DB::Object::Query;
16 BEGIN
17 {
18 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶ use parent qw( DB::Object );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶ use vars qw( $VERSION $DEBUG $EXCEPTION_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶ use DB::Object::Query::Clause;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶ use DB::Object::Query::Elements;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶ use DB::Object::Query::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶ use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶ use Wanted;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶ our $DEBUG = 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
29 3 our $VERSION = 'v0.10.0';
30 };
31  
32 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 sub init
36 {
37 11 ▶ my $self = shift( @_ );
Subroutine init: called CC=28 SCAR=38.0
Pod documented
38 11 ▶ $self->{alias} = {} unless( CORE::exists( $self->{alias} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"alias"} 11 0 11
39 11 ▶ $self->{avoid} = [] unless( CORE::exists( $self->{avoid} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"avoid"} 11 0 11
40 11 ▶ $self->{binded} = [] unless( CORE::exists( $self->{binded} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"binded"} 11 0 11
41 # $self->{binded_group} = [] unless( CORE::exists( $self->{binded_group} ) );
42 # $self->{binded_limit} = [] unless( CORE::exists( $self->{binded_limit} ) );
43 # $self->{binded_order} = [] unless( CORE::exists( $self->{binded_order} ) );
44 11 ▶ $self->{binded_types} = [] unless( CORE::exists( $self->{binded_types} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"binded_types"} 11 0 11
45 11 ▶ $self->{binded_values} = [] unless( CORE::exists( $self->{binded_values} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"binded_values"} 11 0 11
46 11 ▶ $self->{binded_where} = [] unless( CORE::exists( $self->{binded_where} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"binded_where"} 11 0 11
47 11 $self->{dirty} = 0;
48 11 ▶ $self->{elements} = undef unless( CORE::exists( $self->{elements} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"elements"} 11 0 11
49 11 ▶ $self->{enhance} = 0 unless( CORE::exists( $self->{enhance} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"enhance"} 11 0 11
50 11 ▶ $self->{from_table} = [] unless( CORE::exists( $self->{from_table} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"from_table"} 11 0 11
51 11 ▶ $self->{from_unixtime} = [] unless( CORE::exists( $self->{from_unixtime} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"from_unixtime"} 11 0 11
52 11 ▶ $self->{group_by} = '' unless( CORE::exists( $self->{group_by} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"group_by"} 11 0 11
53 11 ▶ $self->{having} = '' unless( CORE::exists( $self->{having} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"having"} 1 10 11
54 11 ▶ $self->{join_fields} = '' unless( CORE::exists( $self->{join_fields} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"join_fields"} 11 0 11
55 11 ▶ $self->{left_join} = {} unless( CORE::exists( $self->{left_join} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"left_join"} 11 0 11
56 11 ▶ $self->{limit} = '' unless( CORE::exists( $self->{limit} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"limit"} 11 0 11
57 11 ▶ $self->{local} = {} unless( CORE::exists( $self->{local} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"local"} 11 0 11
58 11 ▶ $self->{order_by} = '' unless( CORE::exists( $self->{order_by} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"order_by"} 11 0 11
59 11 ▶ $self->{prepare_options}= {} unless( CORE::exists( $self->{prepare_options} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"prepare_options"} 11 0 11
60 11 ▶ $self->{query_values} = undef unless( CORE::exists( $self->{query_values} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"query_values"} 11 0 11
61 11 ▶ $self->{reverse} = '' unless( CORE::exists( $self->{reverse} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"reverse"} 11 0 11
62 11 ▶ $self->{sorted} = [] unless( CORE::exists( $self->{sorted} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"sorted"} 11 0 11
63 11 ▶ $self->{table_alias} = '' unless( CORE::exists( $self->{table_alias} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"table_alias"} 11 0 11
64 11 ▶ $self->{table_object} = '' unless( CORE::exists( $self->{table_object} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"table_object"} 11 0 11
65 11 ▶ $self->{unix_timestamp} = [] unless( CORE::exists( $self->{unix_timestamp} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"unix_timestamp"} 11 0 11
66 11 ▶ $self->{where} = '' unless( CORE::exists( $self->{where} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"where"} 11 0 11
67 11 $self->{_init_strict_use_sub} = 1;
68 11 $self->{_exception_class} = $EXCEPTION_CLASS;
69 11 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 11 11
70 11 $self->{constant} = {};
71 11 $self->{query} = '';
72 11 $self->{query_reset} = 0;
73 11 $self->{query_reset_core_keys} = [qw(
74 alias binded binded_group binded_limit binded_order binded_types binded_values
75 binded_where dirty from_unixtime group_by having limit local order_by reverse
76 sorted table_alias unix_timestamp where
77 from_table join_fields join_tables left_join prepare_options query_values constant
78 query selected_fields tie_order
79 )];
80 11 $self->{selected_fields} = '';
81 11 $self->{table_object} = '';
82 11 $self->{tie_order} = [];
83 11 ▶ unless( $self->{elements} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"elements"}) 11 0 11
84 {
85 11 $self->{elements} = $self->new_elements;
86 }
87 11 return( $self );
88 }
89  
90 2 ▶sub alias { return( shift->_set_get_hash( 'alias', @_ ) ); }
Subroutine alias: called CC=1 SCAR=0.0
Pod documented
91  
92 5 ▶sub as_string { return( shift->{query} ); }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
93  
94 sub attach
95 {
96 0 ▶ my $self = shift( @_ );
Subroutine attach: not called CC=8 SCAR=42.8
Pod documented
97 0 my $this = shift( @_ );
98 0 ▶ if( $self->_is_a( $this => 'DB::Object' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_a($this, 'DB::Object')) { } 0 0 0
elsif ($self->_is_a($this, 'DB::Object::Tables')) { } 0 0 0
99 {
100 0 my $base_class = $self->base_class;
101 0 ▶ unless( $self->_is_a( $this => $base_class ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($this, $base_class)) 0 0 0
102 {
103 0 return( $self->error( "Value provided is not a ${base_class} object." ) );
104 }
105 0 ▶ $self->database_object( $this ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->database_object($this) or return $self->pass_error 0 0 0
106 }
107 elsif( $self->_is_a( $this => 'DB::Object::Tables' ) )
108 {
109 0 my $base_class = $self->base_class;
110 0 ▶ unless( $self->_is_a( $this => "${base_class}::Tables" ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($this, "${base_class}::Tables")) 0 0 0
111 {
112 0 return( $self->error( "Value provided is not a ${base_class}::Tables object." ) );
113 }
114 0 ▶ $self->table_object( $this ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->table_object($this) or return $self->pass_error 0 0 0
115 }
116 else
117 {
118 0 return( $self->error( "No DB::Object or DB::Object::Tables object was provided." ) );
119 }
120 0 $self->debug( $this->debug );
121 0 ▶ if( my $elems = $self->elements )
Branch: 1 branch
BranchTrueFalseTotal
if (my $elems = $self->elements) 0 0 0
122 {
123 0 $elems->attach( $self );
124 }
125 0 return( $self );
126 }
127  
128 2 ▶sub avoid { return( shift->_set_get_array_as_object( 'avoid', @_ ) ); }
Subroutine avoid: called CC=1 SCAR=0.0
Pod documented
129  
130 0 ▶sub binded { return( shift->_set_get_array_as_object( 'binded', @_ ) ); }
Subroutine binded: not called CC=1 SCAR=6.9
Pod documented
131  
132 0 ▶sub binded_group { return( shift->group->values ); }
Subroutine binded_group: not called CC=1 SCAR=6.9
Pod documented
133  
134 0 ▶sub binded_limit { return( shift->limit->values ); }
Subroutine binded_limit: not called CC=1 SCAR=6.9
Pod documented
135  
136 0 ▶sub binded_order { return( shift->order->values ); }
Subroutine binded_order: not called CC=1 SCAR=6.9
Pod documented
137  
138 # NOTE: sub binded_types is not used anymore as of 2023-07-19 (v0.11.7)
139 0 ▶sub binded_types { return( shift->_set_get_array_as_object( 'binded_types', @_ ) ); }
Subroutine binded_types: not called CC=1 SCAR=6.9
Pod documented
140 # sub binded_types
141 # {
142 # my $self = shift( @_ );
143 # my $arr = $self->new_array;
144 # my $e = $self->elements;
145 # $e->keys->sort->foreach(sub
146 # {
147 # my $k = shift( @_ );
148 # $arr->push( $e->{ $k }->type );
149 # });
150 # return( $arr );
151 # }
152  
153 # sub binded_types_as_param
154 # {
155 # my $self = shift( @_ );
156 # return( $self->error( "The driver has not implemented the method binded_types_as_param." ) );
157 # }
158 sub binded_types_as_param
159 {
160 0 ▶ my $self = shift( @_ );
Subroutine binded_types_as_param: not called CC=1 SCAR=6.9
Pod documented
161 0 my $params = $self->new_array;
162 $self->elements->foreach(sub
163 {
164 0 ▶ my $elem = shift( @_ );
Subroutine __ANON__: not called CC=5 SCAR=34.0
165 0 my $type;
166 0 ▶ if( $elem && $elem->as_is )
Branch: 2 branches
BranchTrueFalseTotal
if ($elem and $elem->as_is) { } 0 0 0
elsif ($elem and defined($type = $elem->type)) { } 0 0 0
Condition0%0/3
$elem and $elem->as_is
!ll&&!rl&&r
000
MC/DC0%0/2
$elem and $elem->as_is
$elem $elem->as_is
Truth table0%0/3
A and B
A= $elemB= $elem->as_is
ABresult
0X0
100
111
167 {
168 0 return;
169 }
170 ▶ elsif( $elem && defined( $type = $elem->type ) )
Condition0%0/3
$elem and defined($type = $elem->type)
!ll&&!rl&&r
000
MC/DC0%0/2
$elem and defined($type = $elem->type)
$elem defined($type = $elem->type)
Truth table0%0/3
A and B
A= $elemB= defined($type = $elem->type)
ABresult
0X0
100
111
171 {
172 0 $params->push( $type );
173 }
174 else
175 {
176 0 $params->push( '' );
177 }
178 0 });
179 0 return( $params );
180 }
181  
182 0 ▶sub binded_values { return( shift->_set_get_array_as_object( 'binded_values', @_ ) ); }
Subroutine binded_values: not called CC=1 SCAR=6.9
Pod documented
183  
184 0 ▶sub binded_where { return( shift->_set_get_array_as_object( 'binded_where', @_ ) ); }
Subroutine binded_where: not called CC=1 SCAR=6.9
Pod documented
185  
186 sub constant
187 {
188 10 ▶ my $self = shift( @_ );
Subroutine constant: called CC=8 SCAR=37.9
Pod documented
189 10 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 10 10
190 {
191 0 my $def = shift( @_ );
192 0 ▶ return( $self->error( "I was expecting a hash reference, but got '$def' instead." ) ) if( !$self->_is_hash( $def => 'strict' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_hash($def, "strict") 0 0 0
193 0 foreach my $k (qw( pack file line ))
194 {
195 0 ▶ return( $self->error( "Parameter \"$k\" is missing in hash refernece provided." ) ) if( !$def->{ $k } );
Branch: 1 branch
BranchTrueFalseTotal
unless $def->{$k} 0 0 0
196 }
197 ## sth may or may not be there
198 0 ▶ return( $self->error( "Statement handler provided is not a DB::Object::Statement object." ) ) if( $def->{sth} && ( !$self->_is_object( $def->{sth} ) && !$def->{sth}->isa( 'DB::Object::Statement' ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"sth"} and not $self->_is_object($def->{"sth"}) || $def->{"sth"}->isa("DB::Object::Statement") 0 0 0
Condition0%0/6
$def->{"sth"} and not $self->_is_object($def->{"sth"}) || $def->{"sth"}->isa("DB::Object::Statement")
!ll&&!rl&&r
000
$self->_is_object($def->{'sth'}) || $def->{'sth'}->isa('DB::Object::Statement')
l!l&&r!l&&!r
000
MC/DC0%0/3
$def->{"sth"} and not $self->_is_object($def->{"sth"}) || $def->{"sth"}->isa("DB::Object::Statement")
$def->{"sth"} $self->_is_object($def->{'sth'}) $def->{'sth'}->isa('DB::Object::Statement')
Truth table0%0/4
A and not(B || C)
A= $def->{"sth"}B= $self->_is_object($def->{'sth'})C= $def->{'sth'}->isa('DB::Object::Statement')
ABCresult
0XX0
11X0
1010
1001
199 0 $self->{constant} = $def;
200 }
201 10 return( $self->{constant} );
202 }
203  
204 # sub database_object { return( shift->table_object->database_object ) }
205 # NOTE: we cannot simply use the database object of the table, even if it would make sense, because a query object can be instantiated by a database object (see DB::Object->_query_object_create), and when that happens, this method would set the database object of our query object.
206 18 ▶sub database_object { return( shift->_set_get_object_without_init( 'database_object', 'DB::Object', @_ ) ); }
Subroutine database_object: called CC=1 SCAR=0.0
Pod documented
207  
208 sub delete
209 {
210 1 ▶ my $self = shift( @_ );
Subroutine delete: called CC=16 SCAR=36.9
Pod documented
211 1 my $constant = $self->constant;
212 1 ▶ if( scalar( keys( %$constant ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$constant) 0 1 1
213 {
214 0 ▶ return( $constant->{sth} ) if( $constant->{sth} && $self->_is_object( $constant->{sth} ) && $constant->{sth}->isa( 'DB::Object::Statement' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $constant->{"sth"} and $self->_is_object($constant->{"sth"}) and $constant->{"sth"}->isa("DB::Object::Statement") 0 0 0
Condition0%0/6
$constant->{"sth"} and $self->_is_object($constant->{"sth"}) and $constant->{"sth"}->isa("DB::Object::Statement")
!ll&&!rl&&r
000
$constant->{"sth"} and $self->_is_object($constant->{"sth"})
!ll&&!rl&&r
000
MC/DC0%0/3
$constant->{"sth"} and $self->_is_object($constant->{"sth"}) and $constant->{"sth"}->isa("DB::Object::Statement")
$constant->{"sth"} $self->_is_object($constant->{"sth"}) $constant->{"sth"}->isa("DB::Object::Statement")
Truth table0%0/4
A and B and C
A= $constant->{"sth"}B= $self->_is_object($constant->{"sth"})C= $constant->{"sth"}->isa("DB::Object::Statement")
ABCresult
0XX0
10X0
1100
1111
215 }
216 1 ▶ my $tbl_o = $self->table_object || return( $self->error( "No table object is set." ) );
Condition50%1/2
$self->table_object || (return $self->error('No table object is set.'))
l!l
10
MC/DC0%0/1
$self->table_object || (return $self->error('No table object is set.'))
$self->table_object
Truth table50%1/2
A
A= $self->table_object
Aresult
11
00
217 1 ▶ my $table = $tbl_o->name ||
Condition50%1/2
$tbl_o->name || (return $self->error('No table to delete entries from was set.'))
l!l
10
MC/DC0%0/1
$tbl_o->name || (return $self->error('No table to delete entries from was set.'))
$tbl_o->name
Truth table50%1/2
A
A= $tbl_o->name
Aresult
11
00
218 return( $self->error( "No table to delete entries from was set." ) );
219 1 my $where = '';
220 1 ▶ $self->where( @_ ) if( @_ );
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
221 # if( !$where && $self->{ 'query_reset' } )
222 1 ▶ if( !$where )
Branch: 1 branch
BranchTrueFalseTotal
unless ($where) 1 0 1
223 {
224 1 $where = $self->where();
225 }
226 1 ▶ if( !$where )
Branch: 1 branch
BranchTrueFalseTotal
unless ($where) 0 1 1
227 {
228 0 return( $self->error( "You have provided no where clause. If you intend to delete all records from table '$table', you must do explicitly by preparing the statement yourself." ) );
229 }
230 1 my $clauses = $self->_query_components( 'delete' );
231 1 my @query = ( "DELETE FROM $table" );
232 # 'query_reset' condition to avoid catching parameters from pervious queries.
233 1 ▶ push( @query, @$clauses ) if( scalar( @$clauses ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$clauses 1 0 1
234 1 my $query = $self->{query} = CORE::join( ' ', @query );
235 1 ▶ return( $self->error( "Refusing to do a bulk delete. Enable the allow_bulk_delete database object property if you want to do so. Original query was: $query" ) ) if( !$self->where && !$self->database_object->allow_bulk_delete );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->where or $self->database_object->allow_bulk_delete 0 1 1
Condition33%1/3
$self->where or $self->database_object->allow_bulk_delete
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->where or $self->database_object->allow_bulk_delete
$self->where $self->database_object->allow_bulk_delete
Truth table33%1/3
A or B
A= $self->whereB= $self->database_object->allow_bulk_delete
ABresult
1X1
011
000
236 1 $self->_save_bind();
237 1 ▶ my $sth = $tbl_o->_cache_this( $self ) ||
Condition50%1/2
$tbl_o->_cache_this($self) || (return $self->error("Error while preparing query to delete from table '${table}':\n$query"))
l!l
10
MC/DC0%0/1
$tbl_o->_cache_this($self) || (return $self->error("Error while preparing query to delete from table '${table}':\n$query"))
$tbl_o->_cache_this($self)
Truth table50%1/2
A
A= $tbl_o->_cache_this($self)
Aresult
11
00
238 return( $self->error( "Error while preparing query to delete from table '$table':\n$query" ) );
239 # Routines such as as_string() expect an array on pupose so we do not have to commit the action
240 # but rather get the statement string. At the end, we write:
241 # $obj->delete() to really delete
242 # $obj->delete->as_string() to ONLY get the formatted statement
243 # wantarray returns undef in void context, i.e. $obj->delete()
244 1 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 1 1
245 {
246 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to delete from table '${table}':\n$query") 0 0 0
247 return( $self->error( "Error while executing query to delete from table '$table':\n$query" ) );
248 # Will be destroyed anyway and permits the end user to manipulate the object if needed
249 # $sth->finish();
250 }
251 # wantarray returns false but not undef() otherwise, i.e.
252 # $obj->delete->as_string();
253 1 return( $sth );
254 }
255  
256 7 ▶sub dirty { return( shift->_set_get_boolean( 'dirty', @_ ) ); }
Subroutine dirty: called CC=1 SCAR=0.0
Pod documented
257  
258 # sub elements { return( shift->_set_get_hash_as_mix_object( 'elements', @_ ) ); }
259 2 ▶sub elements { return( shift->_set_get_object_without_init( 'elements', 'DB::Object::Query::Elements', @_ ) ); }
Subroutine elements: called CC=1 SCAR=0.0
Pod documented
260  
261 11 ▶sub enhance { return( shift->_set_get_boolean( 'enhance', @_ ) ); }
Subroutine enhance: called CC=1 SCAR=0.0
Pod documented
262  
263 sub error
264 {
265 2 ▶ my $self = shift( @_ );
Subroutine error: called CC=2 SCAR=7.1
Pod documented
266  
267 # Query object is now in an error state and must not be re-used.
268 2 ▶ $self->{dirty} = 1 if( ref( $self ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 2 0 2
269  
270 2 return( $self->SUPER::error( @_ ) );
271 }
272  
273 # Used in conjonction with constant(), allows internally to know if the query has reached the end of the chain
274 # Such as $tbl->select->join( $tbl_object, $conditions )->join( $other_tbl_object, $other_conditions );
275 # final() enables to know the query reached the end, so that when constant is used, all the processing can be skipped
276 5 ▶sub final { return( shift->_set_get_scalar( 'final', @_ ) ); }
Subroutine final: called CC=1 SCAR=0.0
Pod documented
277  
278 sub format_from_epoch
279 {
280 0 ▶ warn( "This method \"format_from_epoch\" was not superseded.\n" );
Subroutine format_from_epoch: not called CC=1 SCAR=6.9
Pod documented
281 }
282  
283 sub format_to_epoch
284 {
285 0 ▶ warn( "This method \"format_to_epoch\" was not superseded.\n" );
Subroutine format_to_epoch: not called CC=1 SCAR=6.9
Pod documented
286 }
287  
288 # NOTE: For select or insert queries
289 sub format_statement
290 {
291 3 ▶ my $self = shift( @_ );
Subroutine format_statement: called CC=40 SCAR=53.6
Pod documented
292 3 my $opts = $self->_get_args_as_hash( @_ );
293 3 ▶ my $tbl_o = $self->table_object || return( $self->error( "No table object is set." ) );
Condition50%1/2
$self->table_object || (return $self->error('No table object is set.'))
l!l
30
MC/DC0%0/1
$self->table_object || (return $self->error('No table object is set.'))
$self->table_object
Truth table50%1/2
A
A= $self->table_object
Aresult
11
00
294 3 ▶ my $dbo = $tbl_o->database_object ||
Condition50%1/2
$tbl_o->database_object || (return $self->error(q[No database object is currently set on this table object for table '], $tbl_o->name, q[']))
l!l
30
MC/DC0%0/1
$tbl_o->database_object || (return $self->error(q[No database object is currently set on this table object for table '], $tbl_o->name, q[']))
$tbl_o->database_object
Truth table50%1/2
A
A= $tbl_o->database_object
Aresult
11
00
295 return( $self->error( "No database object is currently set on this table object for table '", $tbl_o->name, "'" ) );
296 # Should we use bind statement?
297 3 my $bind = $dbo->use_bind;
298 3 ▶ $opts->{data} = $self->{_default} if( !$opts->{data} );
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"data"} 3 0 3
299 3 ▶ $opts->{order} = $self->{_fields} if( !$opts->{order} );
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"order"} 3 0 3
300 3 ▶ $opts->{table} = $tbl_o->qualified_name if( !$opts->{table} );
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"table"} 3 0 3
301 3 local $_;
302 3 my $data = $opts->{data};
303 3 my $order = $opts->{order};
304 3 my $table = $opts->{table};
305 3 my $prefix = $tbl_o->prefix;
306 3 my $from_unix = {};
307 3 my $unixtime = {};
308 3 my $args = $self->{_args};
309 3 my $fields = '';
310 3 my $values = '';
311 3 my $base_class = $self->base_class;
312 3 $from_unix = $self->{_from_unix};
313 3 my $tmp_ref = $self->from_unixtime();
314 3 map{ $from_unix->{ $_ }++ } @$tmp_ref;
315 3 $tmp_ref = $self->unix_timestamp();
316 3 map{ $unixtime->{ $_ }++ } @$tmp_ref;
317 3 my @format_fields = ();
318 3 my @format_values = ();
319 # my $binded = $self->{binded_values} = [];
320 3 my $multi_db = $tbl_o->prefix_database;
321 3 my $db = $tbl_o->database;
322 3 my $fields_ref = $tbl_o->fields_as_array;
323 3 my $ok_list = $fields_ref->join( '|' )->scalar;
324 3 my $tables = CORE::join( '|', @{$dbo->tables} );
325 3 ▶ my $struct = $tbl_o->structure || return( $self->pass_error( $tbl_o->error ) );
Condition50%1/2
$tbl_o->structure || (return $self->pass_error($tbl_o->error))
l!l
30
MC/DC0%0/1
$tbl_o->structure || (return $self->pass_error($tbl_o->error))
$tbl_o->structure
Truth table50%1/2
A
A= $tbl_o->structure
Aresult
11
00
326 3 my $query_type = $self->{query_type};
327 3 my @sorted = ();
328 3 ▶ if( $self->query_type eq 'insert' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->query_type eq "insert" and @$args and not @$args % 2) 1 2 3
Condition66%4/6
$self->query_type eq "insert" and @$args and not @$args % 2
!ll&&!rl&&r
201
$self->query_type eq "insert" and @$args
!ll&&!rl&&r
201
MC/DC33%1/3
$self->query_type eq "insert" and @$args and not @$args % 2
$self->query_type eq "insert" @$args not @$args % 2
Truth table50%2/4
A and B and not(C)
A= $self->query_type eq "insert"B= @$argsC= not @$args % 2
ABCresult
0XX0
10X0
1100
1111
329 @$args &&
330 !( @$args % 2 ) )
331 {
332 1 ▶ for( my $i = 0; $i < @$args; $i += 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$args) 4 1 5
333 {
334 4 ▶ push( @sorted, $args->[ $i ] ) if( exists( $order->{ $args->[ $i ] } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $$order{$args->[$i]} 4 0 4
335 4 ▶ $data->{ $args->[ $i ] } = $args->[ $i + 1 ] if( !exists( $data->{ $args->[ $i ] } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$data{$args->[$i]} 4 0 4
336 }
337 }
338 3 ▶ @sorted = sort{ $order->{ $a }->pos <=> $order->{ $b }->pos } keys( %$order ) if( !@sorted );
Branch: 1 branch
BranchTrueFalseTotal
unless @sorted 2 1 3
339 # Used for insert or update so that execute can take a hash of key => value pair and we would bind the values in the right order
340 # But or that we need to know the order of the fields.
341 3 $self->{sorted} = \@sorted;
342 3 my $placeholder_re = $dbo->_placeholder_regexp;
343 3 my $elems = $self->new_elements;
344  
345 3 foreach( @sorted )
346 {
347 22 my $elem = $self->new_element;
348 22 ▶ if( exists( $data->{ $_ } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{$_}) 8 14 22
349 {
350 8 my $value = $data->{ $_ };
351 8 ▶ if( $self->_is_a( $value => "${base_class}::Statement" ) )
Branch: 10 branches
BranchTrueFalseTotal
if ($self->_is_a($value, "${base_class}::Statement")) { } 0 8 8
elsif (exists $from_unix->{$_}) { } 0 8 8
elsif (exists $unixtime->{$_}) { } 0 8 8
elsif (ref $value eq 'SCALAR') { } 0 8 8
elsif ($value =~ /($placeholder_re)/) { } 0 8 8
elsif ($struct->{$_} =~ /^\s*\bBLOB\b/i) { } 0 8 8
elsif (not $bind) { } 8 0 8
elsif ($value =~ /^\d+$/ and $struct->{$_} =~ /\bSET\(/i) { } 0 0 0
elsif ($value =~ /^\d+$/ and $struct->{$_} =~ /\bENUM\(/i and $query_type eq 'insert' || $query_type eq 'update') { } 0 0 0
elsif ($bind) { } 0 0 0
352 {
353 0 $elem->value( '(' . $value->as_string . ')' );
354 0 $elem->elements( $value->query_object->elements );
355 }
356 # This is for insert or update statement types
357 elsif( exists( $from_unix->{ $_ } ) )
358 {
359 0 ▶ if( $bind )
Branch: 1 branch
BranchTrueFalseTotal
if ($bind) { } 0 0 0
360 {
361 0 $elem->value( $value );
362 0 $elem->format( $self->format_from_epoch({ value => $value, bind => 1 }) );
363 }
364 else
365 {
366 0 $elem->format( $self->format_from_epoch({ value => $value, bind => 0 }) );
367 }
368 }
369 elsif( exists( $unixtime->{ $_ } ) )
370 {
371 0 ▶ if( $bind )
Branch: 1 branch
BranchTrueFalseTotal
if ($bind) { } 0 0 0
372 {
373 0 $elem->value( $value );
374 0 $elem->format( $self->format_to_epoch({ value => $value, bind => 1 }) );
375 }
376 else
377 {
378 0 $elem->format( $self->format_to_epoch({ value => $value, bind => 0 }) );
379 }
380 }
381 # A scalar reference implies this is used as-is
382 elsif( ref( $value ) eq 'SCALAR' )
383 {
384 0 $elem->format( $$value );
385 0 $elem->as_is(1);
386 }
387 # The placeholder may be embedded inside a function like `UPPER(?)'
388 elsif( $value =~ /($placeholder_re)/ )
389 {
390 0 $elem->placeholder( $1 );
391 0 $elem->format( $value );
392 }
393 elsif( $struct->{ $_ } =~ /^\s*\bBLOB\b/i )
394 {
395 0 $elem->placeholder( '?' );
396 0 $elem->format( '?' );
397 0 $elem->value( $value );
398 }
399 # If the value itself looks like a field name or like a SQL function
400 # or simply if bind option is inactive
401 # This stinks too much. It is way too complex to parse or guess a sql query
402 # use \( instead to pass a scalar reference
403 # elsif( $value =~ /(?:\.|\A)(?:$ok_list)\b/ ||
404 # $value =~ /[a-zA-Z_]{3,}\([^\)]*\)/ ||
405 # $value eq '?' )
406 # {
407 # push( @format_values, $value );
408 # }
409 elsif( !$bind )
410 {
411 8 $elem->format( sprintf( "%s", $dbo->quote( $value ) ) );
412 }
413 # We do this before testing for param binding because DBI puts quotes around SET number :-(
414 ▶ elsif( $value =~ /^\d+$/ && $struct->{ $_ } =~ /\bSET\(/i )
Condition0%0/3
$value =~ /^\d+$/ and $struct->{$_} =~ /\bSET\(/i
!ll&&!rl&&r
000
MC/DC0%0/2
$value =~ /^\d+$/ and $struct->{$_} =~ /\bSET\(/i
$value =~ /^\d+$/ $struct->{$_} =~ /\bSET\(/i
Truth table0%0/3
A and B
A= $value =~ /^\d+$/B= $struct->{$_} =~ /\bSET\(/i
ABresult
0X0
100
111
415 {
416 0 $elem->format( $value );
417 }
418 ▶ elsif( $value =~ /^\d+$/ &&
Condition0%0/9
$value =~ /^\d+$/ and $struct->{$_} =~ /\bENUM\(/i and $query_type eq "insert" || $query_type eq "update"
!ll&&!rl&&r
000
$value =~ /^\d+$/ and $struct->{$_} =~ /\bENUM\(/i
!ll&&!rl&&r
000
$query_type eq "insert" or $query_type eq "update"
l!l&&r!l&&!r
000
MC/DC0%0/4
$value =~ /^\d+$/ and $struct->{$_} =~ /\bENUM\(/i and $query_type eq "insert" || $query_type eq "update"
$value =~ /^\d+$/ $struct->{$_} =~ /\bENUM\(/i $query_type eq "insert" $query_type eq "update"
Truth table0%0/5
A and B and C or D
A= $value =~ /^\d+$/B= $struct->{$_} =~ /\bENUM\(/iC= $query_type eq "insert"D= $query_type eq "update"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
419 $struct->{ $_ } =~ /\bENUM\(/i &&
420 ( $query_type eq 'insert' || $query_type eq 'update' ) )
421 {
422 0 $elem->format( "'$value'" );
423 }
424 # Otherwise, bind option is enabled, we bind parameter
425 elsif( $bind )
426 {
427 0 $elem->placeholder( '?' );
428 0 $elem->format( '?' );
429 0 $elem->value( $value );
430 }
431 # In last resort, we handle the formatting ourself
432 else
433 {
434 0 $elem->format( $dbo->quote( $value ) );
435 }
436 }
437  
438 22 ▶ if( $prefix )
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix) 0 22 22
439 {
440 0 s{
441 (?<![\.\"])\b($ok_list)\b(\s*)?(?!\.)
442 }
443 {
444 0 my( $field, $spc ) = ( $1, $2 );
445 0 ▶ if( $` =~ /\s+(?:AS|FROM)\s+$/i )
Branch: 2 branches
BranchTrueFalseTotal
if ($` =~ /\s+(?:AS|FROM)\s+$/i) { } 0 0 0
elsif ($query_type eq 'select' and $prefix) { } 0 0 0
446 {
447 0 "$field$spc";
448 }
449 ▶ elsif( $query_type eq 'select' && $prefix )
Condition0%0/3
$query_type eq "select" and $prefix
!ll&&!rl&&r
000
MC/DC0%0/2
$query_type eq "select" and $prefix
$query_type eq "select" $prefix
Truth table0%0/3
A and B
A= $query_type eq "select"B= $prefix
ABresult
0X0
100
111
450 {
451 0 "$prefix.$field$spc";
452 }
453 else
454 {
455 0 "$field$spc";
456 }
457 }gex;
458 0 ▶ s/(?<!\.)($tables)(?:\.)/$db\.$1\./g if( $multi_db );
Branch: 1 branch
BranchTrueFalseTotal
if $multi_db 0 0 0
459 }
460 22 $elem->field( $_ );
461 22 $elems->push( $elem );
462 }
463 3 ▶ if( !wantarray() && scalar( @{$self->{_extra}} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not wantarray and scalar @{$self->{"_extra"};}) 2 1 3
Condition50%1/2
not wantarray and scalar @{$self->{"_extra"};}
!ll
03
MC/DC0%0/1
not wantarray and scalar @{$self->{"_extra"};}
not wantarray
Truth table50%1/2
not(A)
A= not wantarray
Aresult
00
11
464 {
465 2 foreach my $this ( @{$self->{_extra}} )
466 {
467 2 $elems->push({
468 field => $this,
469 debug => $self->debug,
470 });
471 }
472 }
473  
474 3 return( $elems );
475 }
476  
477 sub format_update
478 {
479 1 ▶ my $self = shift( @_ );
Subroutine format_update: called CC=42 SCAR=59.9
Pod documented
480 1 ▶ my $data = shift( @_ ) if( $self->_is_array( $_[0] ) || $self->_is_hash( $_[0] => 'strict' ) || @_ % 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_array($_[0]) or $self->_is_hash($_[0], "strict") or @_ % 2 0 0 0
Condition33%2/6
$self->_is_array($_[0]) or $self->_is_hash($_[0], "strict") or @_ % 2
l!l&&r!l&&!r
100
$self->_is_array($_[0]) or $self->_is_hash($_[0], "strict")
l!l&&r!l&&!r
100
MC/DC0%0/3
$self->_is_array($_[0]) or $self->_is_hash($_[0], "strict") or @_ % 2
$self->_is_array($_[0]) $self->_is_hash($_[0], "strict") @_ % 2
Truth table25%1/4
A or B or C
A= $self->_is_array($_[0])B= $self->_is_hash($_[0], "strict")C= @_ % 2
ABCresult
1XX1
01X1
0011
0000
481 1 my @arg = @_;
482 1 ▶ if( !@arg && $data )
Branch: 1 branch
BranchTrueFalseTotal
if (not @arg and $data) 1 0 1
Condition33%1/3
not @arg and $data
!ll&&!rl&&r
001
MC/DC0%0/2
not @arg and $data
not @arg $data
Truth table33%1/3
not(A) and B
A= not @argB= $data
ABresult
0X0
100
111
483 {
484 1 ▶ if( $self->_is_hash( $data => 'strict' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_hash($data, 'strict')) { } 0 1 1
elsif ($self->_is_array($data)) { } 1 0 1
485 {
486 0 @arg = %$data;
487 }
488 elsif( $self->_is_array( $data ) )
489 {
490 1 @arg = @$data;
491 }
492 }
493  
494 1 ▶ return( $self->error( "Must provide key => value pairs. I received an odd number of arguments" ) ) if( @arg && ( scalar( @arg ) % 2 ) );
Branch: 1 branch
BranchTrueFalseTotal
if @arg and scalar(@arg) % 2 0 1 1
Condition33%1/3
@arg and scalar(@arg) % 2
!ll&&!rl&&r
010
MC/DC0%0/2
@arg and scalar(@arg) % 2
@arg scalar(@arg) % 2
Truth table33%1/3
A and B
A= @argB= scalar(@arg) % 2
ABresult
0X0
100
111
495 1 my %arg = ( @arg );
496 1 ▶ my $tbl_o = $self->table_object || return( $self->error( "No table object is set." ) );
Condition50%1/2
$self->table_object || (return $self->error('No table object is set.'))
l!l
10
MC/DC0%0/1
$self->table_object || (return $self->error('No table object is set.'))
$self->table_object
Truth table50%1/2
A
A= $self->table_object
Aresult
11
00
497 1 ▶ my $dbo = $tbl_o->database_object ||
Condition50%1/2
$tbl_o->database_object || (return $self->error(q[No database object is currently set on this table object for table '], $tbl_o->name, q[']))
l!l
10
MC/DC0%0/1
$tbl_o->database_object || (return $self->error(q[No database object is currently set on this table object for table '], $tbl_o->name, q[']))
$tbl_o->database_object
Truth table50%1/2
A
A= $tbl_o->database_object
Aresult
11
00
498 return( $self->error( "No database object is currently set on this table object for table '", $tbl_o->name, "'" ) );
499 1 ▶ $arg{default} ||= $self->{_default};
Condition33%1/3
$arg{"default"} ||= $self->{"_default"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$arg{"default"} ||= $self->{"_default"}
$arg{"default"} $self->{"_default"}
Truth table33%1/3
A ||= B
A= $arg{"default"}B= $self->{"_default"}
ABresult
1X1
011
000
500 1 ▶ if( $arg{data} && !$data )
Branch: 2 branches
BranchTrueFalseTotal
if ($arg{'data'} and not $data) { } 0 1 1
elsif ($self->_is_hash($data, 'strict')) { } 0 1 1
Condition33%1/3
$arg{"data"} and not $data
!ll&&!rl&&r
100
MC/DC0%0/2
$arg{"data"} and not $data
$arg{"data"} not $data
Truth table33%1/3
A and not(B)
A= $arg{"data"}B= not $data
ABresult
0X0
100
111
501 {
502 0 my $hash = $arg{data};
503 0 my @vals = %$hash;
504 0 $data = \@vals;
505 }
506 elsif( $self->_is_hash( $data => 'strict' ) )
507 {
508 0 my @vals = %$data;
509 0 $data = \@vals;
510 }
511 1 ▶ my $info = $data || \@arg;
Condition50%1/2
$data || \@arg
l!l
10
MC/DC0%0/1
$data || \@arg
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
512 1 ▶ if( !$info || !scalar( @$info ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($info and scalar @$info) 0 1 1
Condition33%1/3
$info and scalar @$info
!ll&&!rl&&r
001
MC/DC0%0/2
$info and scalar @$info
$info scalar @$info
Truth table33%1/3
A and B
A= $infoB= scalar @$info
ABresult
0X0
100
111
513 {
514 0 return( $self->error( "No data to update was provided to format update." ) );
515 }
516 1 my $bind = $dbo->use_bind;
517 1 ▶ my $def = $arg{default} || $self->{_default};
Condition33%1/3
$arg{'default'} || $self->{'_default'}
l!l&&r!l&&!r
001
MC/DC0%0/2
$arg{'default'} || $self->{'_default'}
$arg{'default'} $self->{'_default'}
Truth table33%1/3
A || B
A= $arg{'default'}B= $self->{'_default'}
ABresult
1X1
011
000
518 1 my $fields_ref = $tbl_o->fields;
519 1 my $fields_list = CORE::join( '|', keys( %$fields_ref ) );
520 1 ▶ my $struct = $tbl_o->structure || return( $self->pass_error( $tbl_o->error ) );
Condition50%1/2
$tbl_o->structure || (return $self->pass_error($tbl_o->error))
l!l
10
MC/DC0%0/1
$tbl_o->structure || (return $self->pass_error($tbl_o->error))
$tbl_o->structure
Truth table50%1/2
A
A= $tbl_o->structure
Aresult
11
00
521 1 my $types = $tbl_o->types;
522 1 my $from_unix = $self->from_unixtime();
523 1 my $from_unixtime = { map{ $_ => 1 } @$from_unix };
524 1 my @fields = ();
525 1 my @binded = ();
526 1 my @types = ();
527 # Get the constant has definition for each table fields
528 1 my $types_const = $tbl_o->types_const;
529 1 my $placeholder_re = $dbo->_placeholder_regexp;
530 # Before we used to call getdefault supplying it our new values and the
531 # format_statement() that would take the default supplied values.
532 # Now, this works differently since we use update() method and supply directly our
533 # value to update to it.
534 # In this context, getting the default values is dangerous, since resetting the values
535 # to their default ones is not was we want, is it?
536 1 my $elems = $self->new_elements;
537 1 ▶ while( @$info )
Branch: 1 branch
BranchTrueFalseTotal
if (@$info) 1 1 2
538 {
539 1 my( $field, $value ) = splice( @$info, 0, 2 );
540 # Do not update a field that does not belong in this table
541 1 ▶ next if( !exists( $fields_ref->{ $field } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $fields_ref->{$field} 0 1 1
542 # DB::Object::Fields::Field object
543 1 my $fo = $fields_ref->{ $field };
544 1 my $elem = $self->new_element( field => $field );
545 # Make it a FROM_UNIXTIME field if this is what we need.
546 # $value = "FROM_UNIXTIME($value)" if( exists( $from_unixtime->{ $field } ) );
547 # $value = \"TO_TIMESTAMP($value)" if( exists( $from_unixtime->{ $field } ) );
548 # This is for insert or update statement types
549 1 ▶ if( exists( $from_unixtime->{ $field } ) )
Branch: 5 branches
BranchTrueFalseTotal
if (exists $from_unixtime->{$field}) { } 0 1 1
elsif (ref $value eq 'SCALAR') { } 0 1 1
elsif (not $bind) { } 1 0 1
elsif ($value =~ /^\d+$/ and $struct->{$field} =~ /\bSET\(/i) { } 0 0 0
elsif ($bind) { } 0 0 0
550 {
551 0 ▶ if( $bind )
Branch: 1 branch
BranchTrueFalseTotal
if ($bind) { } 0 0 0
552 {
553 0 $elem->value( $value );
554 0 $elem->format( "$field=" . $self->format_from_epoch({ value => $value, bind => 1 }) );
555 }
556 else
557 {
558 0 $elem->format( "$field=" . $self->format_from_epoch({ value => $value, bind => 0 }) );
559 }
560 }
561 elsif( ref( $value ) eq 'SCALAR' )
562 {
563 0 $elem->format( "$field=$$value" );
564 0 $elem->as_is(1);
565 }
566 # Maybe $bind is not enabled, but the user may have manually provided a placeholder, i.e. '?'
567 elsif( !$bind )
568 {
569 1 my $const;
570 # The placeholder could be embedded inside a function like 'UPPER(?)'
571 1 ▶ if( $value =~ /($placeholder_re)/ )
Branch: 4 branches
BranchTrueFalseTotal
if ($value =~ /($placeholder_re)/) { } 0 1 1
elsif ($self->_is_hash($value, 'strict') and $fo->type eq 'jsonb' || $fo->type eq 'json') { } 0 1 1
elsif ($dbo->placeholder->has($self->_is_scalar($value) ? $value : \$value)) { } 0 1 1
elsif ($const = $fo->datatype->constant) { } 1 0 1
572 {
573 0 $elem->placeholder( $1 );
574 0 $elem->format( "$field = $value" );
575 0 ▶ if( $const = $fo->datatype->constant )
Branch: 1 branch
BranchTrueFalseTotal
if ($const = $fo->datatype->constant) 0 0 0
576 {
577 0 $elem->type( $const );
578 }
579 }
580 ▶ elsif( $self->_is_hash( $value => 'strict' ) &&
Condition16%1/6
$self->_is_hash($value, "strict") and $fo->type eq "jsonb" || $fo->type eq "json"
!ll&&!rl&&r
100
$fo->type eq "jsonb" or $fo->type eq "json"
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->_is_hash($value, "strict") and $fo->type eq "jsonb" || $fo->type eq "json"
$self->_is_hash($value, "strict") $fo->type eq "jsonb" $fo->type eq "json"
Truth table25%1/4
A and B or C
A= $self->_is_hash($value, "strict")B= $fo->type eq "jsonb"C= $fo->type eq "json"
ABCresult
0XX0
1000
11X1
1011
581 ( $fo->type eq 'jsonb' || $fo->type eq 'json' ) )
582 {
583 0 my $this_json = $self->_encode_json( $value );
584 0 $const = $fo->datatype->constant;
585 0 $elem->format( sprintf( "$field=%s", $dbo->quote( $this_json, $const ) ) );
586 }
587 ▶ elsif( $dbo->placeholder->has( $self->_is_scalar( $value ) ? $value : \$value ) )
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_scalar($value) ? : 0 1 1
588 {
589 0 ▶ $dbo->placeholder->replace( $self->_is_scalar( $value ) ? $value : \$value );
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_scalar($value) ? : 0 0 0
590 0 $elem->placeholder( '?' );
591 0 $elem->format( "$field = ${value}" );
592 0 ▶ if( $const = $fo->datatype->constant )
Branch: 1 branch
BranchTrueFalseTotal
if ($const = $fo->datatype->constant) 0 0 0
593 {
594 0 $elem->type( $const );
595 }
596 }
597 elsif( $const = $fo->datatype->constant )
598 {
599 1 $elem->format( sprintf( "$field=%s", $dbo->quote( $value, $const ) ) );
600 }
601 else
602 {
603 0 $elem->format( sprintf( "$field=%s", $dbo->quote( $value ) ) );
604 }
605 }
606 # if this is a SET field type and value is a number, treat it as a number and not as a string
607 # We do this before testing for param binding because DBI puts quotes around SET number :-(
608 ▶ elsif( $value =~ /^\d+$/ && $struct->{ $field } =~ /\bSET\(/i )
Condition0%0/3
$value =~ /^\d+$/ and $struct->{$field} =~ /\bSET\(/i
!ll&&!rl&&r
000
MC/DC0%0/2
$value =~ /^\d+$/ and $struct->{$field} =~ /\bSET\(/i
$value =~ /^\d+$/ $struct->{$field} =~ /\bSET\(/i
Truth table0%0/3
A and B
A= $value =~ /^\d+$/B= $struct->{$field} =~ /\bSET\(/i
ABresult
0X0
100
111
609 {
610 0 $elem->format( "$field=$value" );
611 }
612 elsif( $bind )
613 {
614 0 $elem->placeholder( '?' );
615 0 $elem->format( "$field=?" );
616 0 $elem->value( $value );
617 0 my $const;
618 0 ▶ if( $const = $fo->datatype->constant )
Branch: 1 branch
BranchTrueFalseTotal
if ($const = $fo->datatype->constant) 0 0 0
619 {
620 0 $elem->type( $const );
621 }
622 }
623 else
624 {
625 0 my $const;
626 0 ▶ if( $const = $fo->datatype->constant )
Branch: 1 branch
BranchTrueFalseTotal
if ($const = $fo->datatype->constant) { } 0 0 0
627 {
628 0 $elem->format( "$field=" . $dbo->quote( $value, $const ) );
629 }
630 else
631 {
632 0 $elem->format( "$field=" . $dbo->quote( $value ) );
633 }
634 }
635 1 $elems->push( $elem );
636 }
637 1 return( $elems );
638 }
639  
640 0 ▶sub from_table { return( shift->_set_get_array_as_object( 'from_table', @_ ) ); }
Subroutine from_table: not called CC=1 SCAR=6.9
Pod documented
641  
642 7 ▶sub from_unixtime { return( shift->_set_get_array_as_object( 'from_unixtime', @_ ) ); }
Subroutine from_unixtime: called CC=1 SCAR=0.0
Pod documented
643  
644 sub getdefault
645 {
646 3 ▶ my $self = shift( @_ );
Subroutine getdefault: called CC=69 SCAR=58.7
Pod documented
647 3 my $opts = $self->_get_args_as_hash( @_ );
648 3 ▶ my $tbl_o = $self->table_object || return( $self->error( "No table object is set." ) );
Condition50%1/2
$self->table_object || (return $self->error('No table object is set.'))
l!l
30
MC/DC0%0/1
$self->table_object || (return $self->error('No table object is set.'))
$self->table_object
Truth table50%1/2
A
A= $self->table_object
Aresult
11
00
649 3 ▶ my $table = $opts->{table} || $tbl_o->name;
Condition33%1/3
$opts->{'table'} || $tbl_o->name
l!l&&r!l&&!r
300
MC/DC0%0/2
$opts->{'table'} || $tbl_o->name
$opts->{'table'} $tbl_o->name
Truth table33%1/3
A || B
A= $opts->{'table'}B= $tbl_o->name
ABresult
1X1
011
000
650 3 ▶ my $prefix = $tbl_o->query_object->table_alias ? $tbl_o->query_object->table_alias : $tbl_o->prefix;
Branch: 1 branch
BranchTrueFalseTotal
$tbl_o->query_object->table_alias ? : 0 3 3
651 3 my $arg = [];
652 3 ▶ if( $opts->{arg} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"arg"}) 1 2 3
653 {
654 1 ▶ return( $self->error( "arg parameter provided, but is not an array reference." ) ) if( !$self->_is_array( $opts->{arg} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_array($opts->{"arg"}) 0 1 1
655 1 $arg = $opts->{arg};
656 1 ▶ return( $self->error( "arg parameter provided is not a key => value pair. Its number of elements should be an even number." ) ) if( scalar( @$arg ) && ( @$arg % 2 ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$arg and @$arg % 2 0 1 1
Condition33%1/3
scalar @$arg and @$arg % 2
!ll&&!rl&&r
010
MC/DC0%0/2
scalar @$arg and @$arg % 2
scalar @$arg @$arg % 2
Truth table33%1/3
A and B
A= scalar @$argB= @$arg % 2
ABresult
0X0
100
111
657 }
658 3 my %arg = ();
659 3 my $default = {};
660 3 my $fields = [];
661 3 my $structure = {};
662 3 my $base_class = $self->base_class;
663 # Contains some extra parameters for SELECT queries only
664 # Right now a concatenation of 'last_name' and 'first_name' fields into field named 'name'
665 3 my @extra = ();
666 3 ▶ %arg = @$arg if( scalar( @$arg ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$arg 1 2 3
667 3 $opts->{table} = lc( $opts->{table} );
668 3 ▶ $opts->{time} = time() if( !defined( $opts->{time} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"time"} 3 0 3
669 3 my $time = '';
670 3 ▶ $time = $opts->{time} if( $opts->{time} =~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"time"} =~ /^\d+$/ 3 0 3
671 3 ▶ $time ||= $opts->{unixtime} || time();
Condition16%1/6
$time ||= $opts->{"unixtime"} || time
l!l&&r!l&&!r
300
$opts->{'unixtime'} || time
l!l&&r!l&&!r
000
MC/DC0%0/3
$time ||= $opts->{"unixtime"} || time
$time $opts->{'unixtime'} time
Truth table0%0/4
A ||= B || C
A= $timeB= $opts->{'unixtime'}C= time
ABCresult
1XX1
01X1
0011
0000
672 3 my $query_type = $opts->{query_type};
673 3 ▶ if( !$query_type )
Branch: 1 branch
BranchTrueFalseTotal
unless ($query_type) 3 0 3
674 {
675 3 my( $pkg, $file, $line, $sub ) = caller(1);
676 3 $sub =~ s/(.*):://;
677 3 $query_type = $sub;
678 }
679 3 my $alias = '';
680 3 ▶ if( $query_type ne 'insert' && $query_type ne 'delete' && $query_type ne 'replace' )
Branch: 1 branch
BranchTrueFalseTotal
if ($query_type ne "insert" and $query_type ne "delete" and $query_type ne "replace") 2 1 3
Condition66%4/6
$query_type ne "insert" and $query_type ne "delete" and $query_type ne "replace"
!ll&&!rl&&r
102
$query_type ne "insert" and $query_type ne "delete"
!ll&&!rl&&r
102
MC/DC33%1/3
$query_type ne "insert" and $query_type ne "delete" and $query_type ne "replace"
$query_type ne "insert" $query_type ne "delete" $query_type ne "replace"
Truth table50%2/4
A and B and C
A= $query_type ne "insert"B= $query_type ne "delete"C= $query_type ne "replace"
ABCresult
0XX0
10X0
1100
1111
681 {
682 2 $alias = $opts->{as};
683 2 ▶ $alias = $self->alias if( !$alias || !%$alias );
Branch: 1 branch
BranchTrueFalseTotal
unless $alias and %$alias 2 0 2
Condition33%1/3
$alias and %$alias
!ll&&!rl&&r
200
MC/DC0%0/2
$alias and %$alias
$alias %$alias
Truth table33%1/3
A and B
A= $aliasB= %$alias
ABresult
0X0
100
111
684 }
685 3 ▶ my $avoid = $opts->{avoid} || $self->avoid();
Condition66%2/3
$opts->{'avoid'} || $self->avoid
l!l&&r!l&&!r
120
MC/DC0%0/2
$opts->{'avoid'} || $self->avoid
$opts->{'avoid'} $self->avoid
Truth table66%2/3
A || B
A= $opts->{'avoid'}B= $self->avoid
ABresult
1X1
011
000
686 3 ▶ my $unix_time = $opts->{unix_timestamp} || $self->unix_timestamp;
Condition33%1/3
$opts->{'unix_timestamp'} || $self->unix_timestamp
l!l&&r!l&&!r
030
MC/DC0%0/2
$opts->{'unix_timestamp'} || $self->unix_timestamp
$opts->{'unix_timestamp'} $self->unix_timestamp
Truth table33%1/3
A || B
A= $opts->{'unix_timestamp'}B= $self->unix_timestamp
ABresult
1X1
011
000
687 3 ▶ my $from_unix = $opts->{from_unixtime} || $self->from_unixtime;
Condition33%1/3
$opts->{'from_unixtime'} || $self->from_unixtime
l!l&&r!l&&!r
030
MC/DC0%0/2
$opts->{'from_unixtime'} || $self->from_unixtime
$opts->{'from_unixtime'} $self->from_unixtime
Truth table33%1/3
A || B
A= $opts->{'from_unixtime'}B= $self->from_unixtime
ABresult
1X1
011
000
688  
689 3 my $enhance = $tbl_o->enhance;
690 # Need to do hard copy of hashes
691 3 ▶ $default = $tbl_o->default || return( $self->pass_error( $tbl_o->error ) );
Condition50%1/2
$tbl_o->default || (return $self->pass_error($tbl_o->error))
l!l
30
MC/DC0%0/1
$tbl_o->default || (return $self->pass_error($tbl_o->error))
$tbl_o->default
Truth table50%1/2
A
A= $tbl_o->default
Aresult
11
00
692 3 ▶ $fields = $tbl_o->fields || return( $self->pass_error( $tbl_o->error ) );
Condition50%1/2
$tbl_o->fields || (return $self->pass_error($tbl_o->error))
l!l
30
MC/DC0%0/1
$tbl_o->fields || (return $self->pass_error($tbl_o->error))
$tbl_o->fields
Truth table50%1/2
A
A= $tbl_o->fields
Aresult
11
00
693 3 ▶ $structure = $tbl_o->structure || return( $self->pass_error( $tbl_o->error ) );
Condition50%1/2
$tbl_o->structure || (return $self->pass_error($tbl_o->error))
l!l
30
MC/DC0%0/1
$tbl_o->structure || (return $self->pass_error($tbl_o->error))
$tbl_o->structure
Truth table50%1/2
A
A= $tbl_o->structure
Aresult
11
00
694  
695 3 ▶ if( !scalar( keys( %$fields ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %$fields) 0 3 3
696 {
697 0 return( $self->error( "Missing fields (", scalar( keys( %$fields ) ), " found) for table \"$table\"." ) );
698 }
699  
700 3 ▶ if( $query_type eq 'select' && $enhance )
Branch: 1 branch
BranchTrueFalseTotal
if ($query_type eq "select" and $enhance) 2 1 3
Condition66%2/3
$query_type eq "select" and $enhance
!ll&&!rl&&r
102
MC/DC50%1/2
$query_type eq "select" and $enhance
$query_type eq "select" $enhance
Truth table66%2/3
A and B
A= $query_type eq "select"B= $enhance
ABresult
0X0
100
111
701 {
702 2 my @sorted = sort{ $fields->{ $a }->pos <=> $fields->{ $b }->pos } keys( %$fields );
703 2 foreach my $field ( @sorted )
704 {
705 14 ▶ if( $structure->{ $field } =~ /^\s*(?:DATE(?:TIME)?|TIMESTAMP)\s*/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($structure->{$field} =~ /^\s*(?:DATE(?:TIME)?|TIMESTAMP)\s*/i) 4 10 14
706 {
707 4 ▶ my $f = $self->format_to_epoch({
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 4 4
708 value => ( $prefix ? "${prefix}.${field}" : $field ),
709 bind => 0,
710 quote => 0,
711 });
712 4 ▶ my $fo = $tbl_o->fo->new_field(
Condition50%1/2
$tbl_o->fo->new_field('debug', $self->debug, 'name', "$f AS ${field}_unixtime", 'pos', scalar(keys %$fields) + 1, 'query_object', $self, 'table_object', $self->table_object, 'type', 'integer') || (return $self->pass_error($tbl_o->fo->error))
l!l
40
MC/DC0%0/1
$tbl_o->fo->new_field('debug', $self->debug, 'name', "$f AS ${field}_unixtime", 'pos', scalar(keys %$fields) + 1, 'query_object', $self, 'table_object', $self->table_object, 'type', 'integer') || (return $self->pass_error($tbl_o->fo->error))
$tbl_o->fo->new_field('debug', $self->debug, 'name', "$f AS ${field}_unixtime", 'pos', scalar(keys %$fields) + 1, 'query_object', $self, 'table_object', $self->table_object, 'type', 'integer')
Truth table50%1/2
A
A= $tbl_o->fo->new_field('debug', $self->debug, 'name', "$f AS ${field}_unixtime", 'pos', scalar(keys %$fields) + 1, 'query_object', $self, 'table_object', $self->table_object, 'type', 'integer')
Aresult
11
00
713 debug => $self->debug,
714 name => "$f AS ${field}_unixtime",
715 pos => ( scalar( keys( %$fields ) ) + 1 ),
716 query_object => $self,
717 table_object => $self->table_object,
718 type => 'integer',
719 ) || return( $self->pass_error( $tbl_o->fo->error ) );
720 # $fields->{ "$f AS ${field}_unixtime" } = scalar( keys( %$fields ) ) + 1;
721 4 $fields->{ "$f AS ${field}_unixtime" } = $fo;
722 }
723 }
724 }
725  
726 3 my %to_unixtime = ();
727 3 ▶ if( $self->_is_array( $unix_time ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_array($unix_time)) { } 3 0 3
elsif ($self->_is_hash($unix_time, 'strict')) { } 0 0 0
728 {
729 3 %to_unixtime = map{ $_ => 1 } @$unix_time;
730 }
731 elsif( $self->_is_hash( $unix_time => 'strict' ) )
732 {
733 0 %to_unixtime = %$unix_time;
734 }
735  
736 3 ▶ if( %to_unixtime && scalar( keys( %to_unixtime ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (%to_unixtime and scalar keys %to_unixtime) 0 3 3
Condition50%1/2
%to_unixtime and scalar keys %to_unixtime
!ll
30
MC/DC0%0/1
%to_unixtime and scalar keys %to_unixtime
%to_unixtime
Truth table50%1/2
A
A= %to_unixtime
Aresult
00
11
737 {
738 0 foreach my $field ( keys( %to_unixtime ) )
739 {
740 0 ▶ if( exists( $fields->{ $field } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fields->{$field}) 0 0 0
741 {
742 0 ▶ my $func = $self->format_to_epoch({
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 0 0
743 value => ( $prefix ? "${prefix}.${field}" : $field ),
744 bind => 0,
745 quote => 0,
746 });
747 my $fo = $tbl_o->fo->new_field(
748 debug => $self->debug,
749 name => $func . ' AS ' . $field,
750 0 ▶ pos => $fields->{ $field }->pos,
Condition0%0/2
$tbl_o->fo->new_field('debug', $self->debug, 'name', $func . ' AS ' . $field, 'pos', $fields->{$field}->pos, 'query_object', $self, 'table_object', $self->table_object, 'type', 'integer') || (return $self->pass_error($tbl_o->fo->error))
l!l
00
MC/DC0%0/1
$tbl_o->fo->new_field('debug', $self->debug, 'name', $func . ' AS ' . $field, 'pos', $fields->{$field}->pos, 'query_object', $self, 'table_object', $self->table_object, 'type', 'integer') || (return $self->pass_error($tbl_o->fo->error))
$tbl_o->fo->new_field('debug', $self->debug, 'name', $func . ' AS ' . $field, 'pos', $fields->{$field}->pos, 'query_object', $self, 'table_object', $self->table_object, 'type', 'integer')
Truth table0%0/2
A
A= $tbl_o->fo->new_field('debug', $self->debug, 'name', $func . ' AS ' . $field, 'pos', $fields->{$field}->pos, 'query_object', $self, 'table_object', $self->table_object, 'type', 'integer')
Aresult
11
00
751 query_object => $self,
752 table_object => $self->table_object,
753 type => 'integer',
754 ) || return( $self->pass_error( $tbl_o->fo->error ) );
755 0 $fields->{ $func . ' AS ' . $field } = $fo;
756 0 delete( $fields->{ $field } );
757 }
758 }
759 }
760  
761 3 my %avoid = ();
762 3 ▶ if( $self->_is_array( $avoid ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_array($avoid)) { } 3 0 3
elsif ($self->_is_hash($avoid, 'strict')) { } 0 0 0
763 {
764 3 %avoid = map{ $_ => 1 } @$avoid;
765 }
766 elsif( $self->_is_hash( $avoid => 'strict' ) )
767 {
768 0 %avoid = %$avoid;
769 }
770  
771 3 ▶ if( %avoid && scalar( keys( %avoid ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (%avoid and scalar keys %avoid) 1 2 3
Condition100%2/2
%avoid and scalar keys %avoid
!ll
21
MC/DC100%1/1
%avoid and scalar keys %avoid
%avoid
Truth table100%2/2
A
A= %avoid
Aresult
00
11
772 {
773 1 foreach my $field ( keys( %avoid ) )
774 {
775 2 ▶ if( exists( $fields->{ $field } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fields->{$field}) 2 0 2
776 {
777 2 delete( $fields->{ $field } );
778 2 delete( $default->{ $field } );
779 }
780 }
781 }
782  
783 3 my %as = ();
784 3 ▶ if( $self->_is_hash( $alias => 'strict' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_hash($alias, "strict")) 2 1 3
785 {
786 2 %as = %$alias;
787 2 foreach my $field ( keys( %as ) )
788 {
789 0 my $f;
790 0 ▶ if( exists( $fields->{ $field } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fields->{$field}) { } 0 0 0
791 {
792 0 ▶ $f = $prefix
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 0 0
793 ? "${prefix}.${field}"
794 : $field;
795 # delete( $fields{ $field } );
796 }
797 else
798 {
799 0 $f = $field;
800 }
801 my $fo = $tbl_o->fo->new_field(
802 debug => $self->debug,
803 0 ▶ name => $f . ' AS "' . $as{ $field } . '"',
Condition0%0/2
$tbl_o->fo->new_field('debug', $self->debug, 'name', $f . ' AS "' . $as{$field} . '"', 'pos', scalar(keys %$fields) + 1, 'query_object', $self, 'table_object', $self->table_object, 'type', 'integer') || (return $self->pass_error($tbl_o->fo->error))
l!l
00
MC/DC0%0/1
$tbl_o->fo->new_field('debug', $self->debug, 'name', $f . ' AS "' . $as{$field} . '"', 'pos', scalar(keys %$fields) + 1, 'query_object', $self, 'table_object', $self->table_object, 'type', 'integer') || (return $self->pass_error($tbl_o->fo->error))
$tbl_o->fo->new_field('debug', $self->debug, 'name', $f . ' AS "' . $as{$field} . '"', 'pos', scalar(keys %$fields) + 1, 'query_object', $self, 'table_object', $self->table_object, 'type', 'integer')
Truth table0%0/2
A
A= $tbl_o->fo->new_field('debug', $self->debug, 'name', $f . ' AS "' . $as{$field} . '"', 'pos', scalar(keys %$fields) + 1, 'query_object', $self, 'table_object', $self->table_object, 'type', 'integer')
Aresult
11
00
804 pos => ( scalar( keys( %$fields ) ) + 1 ),
805 query_object => $self,
806 table_object => $self->table_object,
807 type => 'integer',
808 ) || return( $self->pass_error( $tbl_o->fo->error ) );
809 0 $fields->{ $f . ' AS "' . $as{ $field } . '"' } = $fo;
810 }
811 }
812 3 ▶ if( $query_type eq 'select' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($query_type eq "select" and $enhance and exists $fields->{"last_name"} and exists $fields->{"first_name"} and not exists $fields->{"name"}) 2 1 3
Condition66%8/12
$query_type eq "select" and $enhance and exists $fields->{"last_name"} and exists $fields->{"first_name"} and not exists $fields->{"name"}
!ll&&!rl&&r
102
$query_type eq "select" and $enhance and exists $fields->{"last_name"} and exists $fields->{"first_name"}
!ll&&!rl&&r
102
$query_type eq "select" and $enhance and exists $fields->{"last_name"}
!ll&&!rl&&r
102
$query_type eq "select" and $enhance
!ll&&!rl&&r
102
MC/DC20%1/5
$query_type eq "select" and $enhance and exists $fields->{"last_name"} and exists $fields->{"first_name"} and not exists $fields->{"name"}
$query_type eq "select" $enhance exists $fields->{"last_name"} exists $fields->{"first_name"} not exists $fields->{"name"}
Truth table33%2/6
A and B and C and D and not(E)
A= $query_type eq "select"B= $enhanceC= exists $fields->{"last_name"}D= exists $fields->{"first_name"}E= not exists $fields->{"name"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
813 $enhance &&
814 exists( $fields->{last_name} ) &&
815 exists( $fields->{first_name} ) &&
816 !exists( $fields->{name} ) )
817 {
818  
819 2 ▶ my $f = $prefix
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 2 2
820 ? "CONCAT(${prefix}.first_name, ' ', ${prefix}.last_name)"
821 : "CONCAT(first_name, ' ', last_name)";
822 2 push( @extra, "$f AS name" );
823 }
824  
825 3 ▶ if( ( exists( $default->{auth} ) && !defined( $arg{auth} ) ) ||
Branch: 1 branch
BranchTrueFalseTotal
if (exists $default->{"auth"} and not defined $arg{"auth"} or defined $arg{"auth"}) 0 3 3
Condition33%2/6
exists $default->{"auth"} and not defined $arg{"auth"} or defined $arg{"auth"}
l!l&&r!l&&!r
003
exists $default->{"auth"} and not defined $arg{"auth"}
!ll&&!rl&&r
300
MC/DC0%0/3
exists $default->{"auth"} and not defined $arg{"auth"} or defined $arg{"auth"}
exists $default->{"auth"} not defined $arg{"auth"} defined $arg{"auth"}
Truth table20%1/5
A and not(B) or C
A= exists $default->{"auth"}B= not defined $arg{"auth"}C= defined $arg{"auth"}
ABCresult
11X1
0X11
1011
0X00
1000
826 defined( $arg{auth} ) )
827 {
828 $default->{auth} = defined( $arg{auth} )
829 ? $arg{auth}
830 0 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'auth'} ? : 0 0 0
831 }
832 3 ▶ if( ( exists( $default->{status} ) && !defined( $default->{status} ) ) ||
Branch: 1 branch
BranchTrueFalseTotal
if (exists $default->{"status"} and not defined $default->{"status"} or defined $arg{"status"}) 0 3 3
Condition33%2/6
exists $default->{"status"} and not defined $default->{"status"} or defined $arg{"status"}
l!l&&r!l&&!r
003
exists $default->{"status"} and not defined $default->{"status"}
!ll&&!rl&&r
300
MC/DC0%0/3
exists $default->{"status"} and not defined $default->{"status"} or defined $arg{"status"}
exists $default->{"status"} not defined $default->{"status"} defined $arg{"status"}
Truth table20%1/5
A and not(B) or C
A= exists $default->{"status"}B= not defined $default->{"status"}C= defined $arg{"status"}
ABCresult
11X1
0X11
1011
0X00
1000
833 defined( $arg{status} ) )
834 {
835 $default->{status} = defined( $arg{status} )
836 ? $arg{status}
837 0 ▶ : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'status'} ? : 0 0 0
838 }
839  
840 3 foreach my $data ( keys( %arg ) )
841 {
842 4 ▶ if( exists( $default->{ $data } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $default->{$data}) 0 4 4
843 {
844 0 $default->{ $data } = $arg{ $data };
845 }
846 }
847 3 my %from_unixtime = ();
848 3 ▶ if( $self->_is_array( $from_unix ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_array($from_unix)) { } 3 0 3
elsif ($self->_is_hash($from_unix, 'strict')) { } 0 0 0
849 {
850 3 %from_unixtime = map{ $_ => 1 } @$from_unix;
851 }
852 elsif( $self->_is_hash( $from_unix => 'strict' ) )
853 {
854 0 %from_unixtime = %$from_unix;
855 }
856  
857  
858 3 $self->{_args} = $arg;
859 3 $self->{_default} = $default;
860 3 $self->{_fields} = $fields;
861 3 $self->{_extra} = \@extra;
862 3 $self->{_structure} = $structure;
863 3 $self->{_from_unix} = \%from_unixtime;
864 3 $self->{_to_unix} = \%to_unixtime;
865 3 $self->{query_type} = $query_type;
866 3 $self->{bind} = $tbl_o->database_object->use_bind;
867 3 return( $self );
868 }
869  
870 8 ▶sub group { return( shift->_group_order( 'group', 'group_by', @_ ) ); }
Subroutine group: called CC=1 SCAR=0.0
Pod documented
871  
872 0 ▶sub having { return( shift->error( "Having clause is not supported by this driver." ) ); }
Subroutine having: not called CC=1 SCAR=6.9
Pod documented
873  
874 # $tbl->insert( field1 => $val1, field2 => $val2 );
875 # or
876 # $tbl->insert([ field1 => $val1, field2 => $val2 ]);
877 # or
878 # $tbl->insert({ field1 => $val1, field2 => $val2 });
879 # or
880 # my $select_statement_object = $tbl2->select || die( $tbl2->error );
881 # $tbl->insert( $select_statement_object );
882 # or
883 # my $select_statement_object = $tbl2->select || die( $tbl2->error );
884 # $tbl->insert( field1 => $val1, field2 => $val2, $select_statement_object );
885 # or
886 # my $select_statement_object = $tbl2->select || die( $tbl2->error );
887 # $tbl->insert([ field1 => $val1, field2 => $val2, $select_statement_object ]);
888 sub insert
889 {
890 1 ▶ my $self = shift( @_ );
Subroutine insert: called CC=45 SCAR=58.9
Pod documented
891 # Could be an array of fields, or a SQL statement object.
892 1 ▶ my $data = shift( @_ ) if( @_ == 1 && ref( $_[ 0 ] ) );
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] 0 1 1
Condition33%1/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
100
MC/DC0%0/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
893 1 my @arg = @_;
894 1 my $constant = $self->constant;
895 1 ▶ if( scalar( keys( %$constant ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$constant) 0 1 1
896 {
897 0 ▶ return( $constant->{sth} ) if( $constant->{sth} && $self->_is_object( $constant->{sth} ) && $constant->{sth}->isa( 'DB::Object::Statement' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $constant->{"sth"} and $self->_is_object($constant->{"sth"}) and $constant->{"sth"}->isa("DB::Object::Statement") 0 0 0
Condition0%0/6
$constant->{"sth"} and $self->_is_object($constant->{"sth"}) and $constant->{"sth"}->isa("DB::Object::Statement")
!ll&&!rl&&r
000
$constant->{"sth"} and $self->_is_object($constant->{"sth"})
!ll&&!rl&&r
000
MC/DC0%0/3
$constant->{"sth"} and $self->_is_object($constant->{"sth"}) and $constant->{"sth"}->isa("DB::Object::Statement")
$constant->{"sth"} $self->_is_object($constant->{"sth"}) $constant->{"sth"}->isa("DB::Object::Statement")
Truth table0%0/4
A and B and C
A= $constant->{"sth"}B= $self->_is_object($constant->{"sth"})C= $constant->{"sth"}->isa("DB::Object::Statement")
ABCresult
0XX0
10X0
1100
1111
898 }
899 1 my %arg = ();
900 1 my $select = '';
901 1 my $base_class = $self->base_class;
902 1 ▶ my $tbl_o = $self->table_object || return( $self->error( "No table object is set." ) );
Condition50%1/2
$self->table_object || (return $self->error('No table object is set.'))
l!l
10
MC/DC0%0/1
$self->table_object || (return $self->error('No table object is set.'))
$self->table_object
Truth table50%1/2
A
A= $self->table_object
Aresult
11
00
903 1 ▶ my $table = $tbl_o->name ||
Condition50%1/2
$tbl_o->name || (return $self->error('No table was provided to insert data.'))
l!l
10
MC/DC0%0/1
$tbl_o->name || (return $self->error('No table was provided to insert data.'))
$tbl_o->name
Truth table50%1/2
A
A= $tbl_o->name
Aresult
11
00
904 return( $self->error( "No table was provided to insert data." ) );
905 # We do not decide of the value of AUTO_INCREMENT fields, so we do not use them in
906 # our INSERT statement.
907 1 ▶ my $fields_ref = $tbl_o->fields || return( $self->pass_error( $tbl_o->error ) );
Condition50%1/2
$tbl_o->fields || (return $self->pass_error($tbl_o->error))
l!l
10
MC/DC0%0/1
$tbl_o->fields || (return $self->pass_error($tbl_o->error))
$tbl_o->fields
Truth table50%1/2
A
A= $tbl_o->fields
Aresult
11
00
908 1 my @avoid = ();
909 1 my( $fields, $values, $elems );
910 1 my $el = $self->elements;
911  
912 1 ▶ if( !@arg && $data && $self->_is_hash( $data => 'strict' ) )
Branch: 4 branches
BranchTrueFalseTotal
if (not @arg and $data and $self->_is_hash($data, 'strict')) { } 0 1 1
elsif (not @arg and ref $data and $self->_is_array($data)) { } 0 1 1
elsif ($data and ref $data eq "${base_class}::Statement") { } 0 1 1
elsif (scalar @arg and $self->_is_a($arg[-1], "${base_class}::Statement")) { } 0 1 1
Condition33%2/6
not @arg and $data and $self->_is_hash($data, "strict")
!ll&&!rl&&r
100
not @arg and $data
!ll&&!rl&&r
100
MC/DC0%0/3
not @arg and $data and $self->_is_hash($data, "strict")
not @arg $data $self->_is_hash($data, "strict")
Truth table25%1/4
not(A) and B and C
A= not @argB= $dataC= $self->_is_hash($data, "strict")
ABCresult
0XX0
10X0
1100
1111
913 {
914 0 @arg = %$data;
915 }
916 # User passed an array reference
917 ▶ elsif( !@arg && ref( $data ) && $self->_is_array( $data ) )
Condition33%2/6
not @arg and ref $data and $self->_is_array($data)
!ll&&!rl&&r
100
not @arg and ref $data
!ll&&!rl&&r
100
MC/DC0%0/3
not @arg and ref $data and $self->_is_array($data)
not @arg ref $data $self->_is_array($data)
Truth table25%1/4
not(A) and B and C
A= not @argB= ref $dataC= $self->_is_array($data)
ABCresult
0XX0
10X0
1100
1111
918 {
919 0 @arg = @$data;
920 0 ▶ if( scalar( @arg ) && $self->_is_a( $arg[-1] => "${base_class}::Statement" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arg and $self->_is_a($arg[-1], "${base_class}::Statement")) 0 0 0
Condition0%0/3
scalar @arg and $self->_is_a($arg[-1], "${base_class}::Statement")
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @arg and $self->_is_a($arg[-1], "${base_class}::Statement")
scalar @arg $self->_is_a($arg[-1], "${base_class}::Statement")
Truth table0%0/3
A and B
A= scalar @argB= $self->_is_a($arg[-1], "${base_class}::Statement")
ABresult
0X0
100
111
921 {
922 0 my $sth = pop( @arg );
923 0 $select = $sth->as_string;
924 0 $el->merge( $sth->query_object->elements );
925 }
926 }
927 # insert into (field1, field2, field3) select field1, field2, field3 from some_table where some_id=12
928 ▶ elsif( $data && ref( $data ) eq "${base_class}::Statement" )
Condition33%1/3
$data and ref $data eq "${base_class}::Statement"
!ll&&!rl&&r
100
MC/DC0%0/2
$data and ref $data eq "${base_class}::Statement"
$data ref $data eq "${base_class}::Statement"
Truth table33%1/3
A and B
A= $dataB= ref $data eq "${base_class}::Statement"
ABresult
0X0
100
111
929 {
930 0 $select = $data->as_string;
931 0 $el->merge( $data->query_object->elements );
932 }
933 ▶ elsif( scalar( @arg ) && $self->_is_a( $arg[-1] => "${base_class}::Statement" ) )
Condition33%1/3
scalar @arg and $self->_is_a($arg[-1], "${base_class}::Statement")
!ll&&!rl&&r
010
MC/DC0%0/2
scalar @arg and $self->_is_a($arg[-1], "${base_class}::Statement")
scalar @arg $self->_is_a($arg[-1], "${base_class}::Statement")
Truth table33%1/3
A and B
A= scalar @argB= $self->_is_a($arg[-1], "${base_class}::Statement")
ABresult
0X0
100
111
934 {
935 0 my $sth = pop( @arg );
936 0 $select = $sth->as_string;
937 0 $el->merge( $sth->query_object->elements );
938 }
939 1 my @query = ();
940 1 ▶ if( $select )
Branch: 1 branch
BranchTrueFalseTotal
if ($select) { } 0 1 1
941 {
942 0 my $sql;
943 0 ▶ if( scalar( @arg ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arg) { } 0 0 0
944 {
945 0 ▶ for( my $i = 0; $i < scalar( @arg ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @arg) 0 0 0
946 {
947 0 ▶ if( $self->_is_a( $arg[$i] => 'DB::Object::Fields::Unknown' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($arg[$i], "DB::Object::Fields::Unknown")) 0 0 0
948 {
949 0 ▶ warn( "Unknown field object '", $arg[$i]->field, "' provided for INSERT into table '", $arg[$i], "'" ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("DB::Object") 0 0 0
950 0 splice( @arg, $i, 1 );
951 }
952 }
953 0 $sql = "INSERT INTO $table (" . join( ', ', @arg ) . ") $select";
954 }
955 else
956 {
957 0 $sql = "INSERT INTO $table $select";
958 }
959 0 @query = ( $sql );
960 }
961 else
962 {
963 1 ▶ %arg = @arg if( @arg );
Branch: 1 branch
BranchTrueFalseTotal
if @arg 1 0 1
964 1 foreach my $field ( keys( %$fields_ref ) )
965 {
966 7 my $fo = $fields_ref->{ $field };
967 # push( @avoid, $field ) if( $fields_ref->{ $field } =~ /\b(AUTO_INCREMENT|SERIAL|nextval)\b/i && !$arg{ $field } );
968 7 ▶ if( defined( $fo->default ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fo->default and $fo->default =~ /\b(AUTO_INCREMENT|SERIAL|nextval)\b/i and not $arg{$field}) 0 7 7
Condition50%3/6
defined $fo->default and $fo->default =~ /\b(AUTO_INCREMENT|SERIAL|nextval)\b/i and not $arg{$field}
!ll&&!rl&&r
700
defined $fo->default and $fo->default =~ /\b(AUTO_INCREMENT|SERIAL|nextval)\b/i
!ll&&!rl&&r
520
MC/DC0%0/3
defined $fo->default and $fo->default =~ /\b(AUTO_INCREMENT|SERIAL|nextval)\b/i and not $arg{$field}
defined $fo->default $fo->default =~ /\b(AUTO_INCREMENT|SERIAL|nextval)\b/i not $arg{$field}
Truth table50%2/4
A and B and not(C)
A= defined $fo->defaultB= $fo->default =~ /\b(AUTO_INCREMENT|SERIAL|nextval)\b/iC= not $arg{$field}
ABCresult
0XX0
10X0
1100
1111
969 $fo->default =~ /\b(AUTO_INCREMENT|SERIAL|nextval)\b/i &&
970 !$arg{ $field } )
971 {
972 0 push( @avoid, $field );
973 }
974 # It is useless to insert a blank data in a field whose default value is NULL.
975 # Especially since a test on a NULL field may be made specifically.
976 7 ▶ if( scalar( @arg ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arg and not exists $arg{$field} and $fo->is_nullable) 2 5 7
Condition83%5/6
scalar @arg and not exists $arg{$field} and $fo->is_nullable
!ll&&!rl&&r
412
scalar @arg and not exists $arg{$field}
!ll&&!rl&&r
043
MC/DC66%2/3
scalar @arg and not exists $arg{$field} and $fo->is_nullable
scalar @arg not exists $arg{$field} $fo->is_nullable
Truth table75%3/4
A and not(B) and C
A= scalar @argB= not exists $arg{$field}C= $fo->is_nullable
ABCresult
0XX0
10X0
1100
1111
977 !exists( $arg{ $field } ) &&
978 $fo->is_nullable )
979 {
980 2 push( @avoid, $field );
981 }
982 }
983 $self->getdefault(
984 1 ▶ table => $table,
Branch: 1 branch
BranchTrueFalseTotal
$self->getdefault("table", $table, "arg", \@arg, "avoid", \@avoid) or return $self->pass_error 0 1 1
985 arg => \@arg,
986 avoid => \@avoid,
987 ) || return( $self->pass_error );
988 1 ▶ $elems = $self->format_statement || return( $self->pass_error );
Condition50%1/2
$self->format_statement || (return $self->pass_error)
l!l
10
MC/DC0%0/1
$self->format_statement || (return $self->pass_error)
$self->format_statement
Truth table50%1/2
A
A= $self->format_statement
Aresult
11
00
989 1 $fields = $elems->fields->join( ', ' );
990 1 $values = $elems->formats->join( ', ' );
991 1 $el->merge( $elems );
992 1 @query = ( "INSERT INTO $table ($fields) VALUES($values)" );
993 }
994  
995 1 ▶ if( $data && $self->_is_hash( $data => 'strict' ) && $el->types->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($data and $self->_is_hash($data, "strict") and $el->types->length) 0 1 1
Condition33%2/6
$data and $self->_is_hash($data, "strict") and $el->types->length
!ll&&!rl&&r
100
$data and $self->_is_hash($data, "strict")
!ll&&!rl&&r
100
MC/DC0%0/3
$data and $self->_is_hash($data, "strict") and $el->types->length
$data $self->_is_hash($data, "strict") $el->types->length
Truth table25%1/4
A and B and C
A= $dataB= $self->_is_hash($data, "strict")C= $el->types->length
ABCresult
0XX0
10X0
1100
1111
996 {
997 0 warn( "You have passed arguments to this insert as hash reference, and you are using placeholders. Using placeholders requires fixed order of arguments which an hash reference cannot guarantee. This will potentially lead to error when executing the query. I recommend you switch to an array of arguments instead, i.e. from { field1 => value1, field2 => value2 } to ( field1 => value1, field2 => value2 ), or to use numbered placeholders like \$1, \$2, etc...\n" );
998 }
999 1 ▶ my $clauses = $self->_query_components( 'insert' ) || return( $self->pass_error );
Condition50%1/2
$self->_query_components('insert') || (return $self->pass_error)
l!l
10
MC/DC0%0/1
$self->_query_components('insert') || (return $self->pass_error)
$self->_query_components('insert')
Truth table50%1/2
A
A= $self->_query_components('insert')
Aresult
11
00
1000 1 ▶ push( @query, @$clauses ) if( scalar( @$clauses ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$clauses 0 1 1
1001 1 my $query = $self->{query} = CORE::join( ' ', @query );
1002 # Everything meaningfull lies within the object
1003 # If no bind should be done _save_bind does nothing
1004 1 $self->_save_bind();
1005 # Query string should lie within the object
1006 # _cache_this sends back an object no matter what or unde() if an error occurs
1007 1 my $sth = $tbl_o->_cache_this( $self );
1008 # STOP! No need to go further
1009 1 ▶ if( !defined( $sth ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sth) 0 1 1
1010 {
1011 0 return( $self->error( "Error '", $tbl_o->error, "' while preparing query to insert data into table '$table':\n$query" ) );
1012 }
1013 # Called in void context
1014 1 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 1 1
1015 {
1016 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to insert data to table '${table}':\n$query") 0 0 0
1017 return( $self->error( "Error while executing query to insert data to table '$table':\n$query" ) );
1018 }
1019 1 return( $sth );
1020 }
1021  
1022 0 ▶sub is_upsert { return( shift->_set_get_boolean( 'is_upsert', @_ ) ); }
Subroutine is_upsert: not called CC=1 SCAR=6.9
Pod documented
1023  
1024 0 ▶sub join_fields { return( shift->_set_get_scalar( 'join_fields', @_ ) ); }
Subroutine join_fields: not called CC=1 SCAR=6.9
Pod documented
1025  
1026 5 ▶sub join_tables { return( shift->_set_get_object_array_object( 'join_tables', 'DB::Object::Tables', @_ ) ); }
Subroutine join_tables: called CC=1 SCAR=0.0
Pod documented
1027  
1028 0 ▶sub left_join { return( shift->_set_get_hash( 'left_join', @_ ) ); }
Subroutine left_join: not called CC=1 SCAR=6.9
Pod documented
1029  
1030 sub limit
1031 {
1032 0 ▶ my $self = shift( @_ );
Subroutine limit: not called CC=11 SCAR=48.8
Pod documented
1033 0 my $limit = $self->{limit};
1034 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
1035 {
1036 # Returns a DB::Object::Query::Clause
1037 0 ▶ $limit = $self->_process_limit( @_ ) ||
Condition0%0/2
$self->_process_limit(@_) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_process_limit(@_) || (return $self->pass_error)
$self->_process_limit(@_)
Truth table0%0/2
A
A= $self->_process_limit(@_)
Aresult
11
00
1038 return( $self->pass_error );
1039 0 ▶ if( CORE::length( $limit->metadata->limit // '' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length($limit->metadata->limit // "")) 0 0 0
Condition0%0/2
$limit->metadata->limit // ""
l!l
00
MC/DC0%0/1
$limit->metadata->limit // ""
$limit->metadata->limit
Truth table0%0/2
A
A= $limit->metadata->limit
Aresult
11
00
1040 {
1041 0 ▶ $limit->generic( CORE::length( $limit->metadata->offset // '' ) ? 'LIMIT ?, ?' : 'LIMIT ?' );
Branch: 1 branch
BranchTrueFalseTotal
length($limit->metadata->offset // '') ? : 0 0 0
Condition0%0/2
$limit->metadata->offset // ""
l!l
00
MC/DC0%0/1
$limit->metadata->offset // ""
$limit->metadata->offset
Truth table0%0/2
A
A= $limit->metadata->offset
Aresult
11
00
1042 # %s works for integer, and also for numbered placeholders like $1 or ?1, or regular placeholder like ?
1043 0 ▶ $limit->value(
Branch: 1 branch
BranchTrueFalseTotal
length($limit->metadata->offset // '') ? : 0 0 0
Condition0%0/2
$limit->metadata->offset // ""
l!l
00
MC/DC0%0/1
$limit->metadata->offset // ""
$limit->metadata->offset
Truth table0%0/2
A
A= $limit->metadata->offset
Aresult
11
00
1044 CORE::length( $limit->metadata->offset // '' )
1045 ? sprintf( "LIMIT %s, %s", $limit->metadata->offset, $limit->metadata->limit )
1046 : sprintf( "LIMIT %s", $limit->metadata->limit )
1047 );
1048 }
1049 }
1050  
1051 0 ▶ if( !$limit && want( 'OBJECT' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $limit and want("OBJECT")) 0 0 0
Condition0%0/3
not $limit and want("OBJECT")
!ll&&!rl&&r
000
MC/DC0%0/2
not $limit and want("OBJECT")
not $limit want("OBJECT")
Truth table0%0/3
not(A) and B
A= not $limitB= want("OBJECT")
ABresult
0X0
100
111
1052 {
1053 0 return( $self->new_null( type => 'object' ) );
1054 }
1055 0 return( $limit );
1056 }
1057  
1058 sub local
1059 {
1060 4 ▶ my $self = shift( @_ );
Subroutine local: called CC=8 SCAR=32.6
Pod documented
1061 4 ▶ $self->{local} ||= {};
Condition50%1/2
$self->{"local"} ||= {}
l!l
40
MC/DC0%0/1
$self->{"local"} ||= {}
$self->{"local"}
Truth table50%1/2
A
A= $self->{"local"}
Aresult
11
00
1062 4 my $local = $self->{local};
1063 4 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 4 4
1064 {
1065 0 my $data = $self->_get_args_as_hash( @_ );
1066 0 my $str = '';
1067 0 ▶ if( scalar( keys( %$data ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$data) 0 0 0
1068 {
1069 0 my @keys = keys( %$data );
1070 0 @$local{ @keys } = @$data{ @keys };
1071 }
1072 }
1073 4 ▶ return( wantarray() ? () : undef() ) if( !$local || !%$local );
Branch: 2 branches
BranchTrueFalseTotal
unless $local and %$local 4 0 4
wantarray ? : 0 4 4
Condition33%1/3
$local and %$local
!ll&&!rl&&r
040
MC/DC0%0/2
$local and %$local
$local %$local
Truth table33%1/3
A and B
A= $localB= %$local
ABresult
0X0
100
111
1074 0 ▶ return( %$local ) if( wantarray() );
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
1075 0 my $str = join( ', ', map{ "\@${_} = '" . $local->{ $_ } . "'" } keys( %$local ) );
1076 # return( "SET $str" );
1077 0 return( $str );
1078 }
1079  
1080 sub new_clause
1081 {
1082 13 ▶ my $self = shift( @_ );
Subroutine new_clause: called CC=4 SCAR=14.0
Pod documented
1083 13 my $opts = $self->_get_args_as_hash( @_ );
1084 13 ▶ $opts->{debug} = $self->debug if( !exists( $opts->{debug} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"debug"} 11 2 13
1085 13 $opts->{query_object} = $self;
1086 13 ▶ my $o = DB::Object::Query::Clause->new( %$opts ) ||
Condition50%1/2
'DB::Object::Query::Clause'->new(%$opts) || (return $self->error('Unable to create a DB::Object::Query::Clause object: ', 'DB::Object::Query::Clause'->error))
l!l
130
MC/DC0%0/1
'DB::Object::Query::Clause'->new(%$opts) || (return $self->error('Unable to create a DB::Object::Query::Clause object: ', 'DB::Object::Query::Clause'->error))
'DB::Object::Query::Clause'->new(%$opts)
Truth table50%1/2
A
A= 'DB::Object::Query::Clause'->new(%$opts)
Aresult
11
00
1087 return( $self->error( "Unable to create a DB::Object::Query::Clause object: ", DB::Object::Query::Clause->error ) );
1088 # Weaken the back-reference to the query object to prevent circular reference memory leaks.
1089 13 ▶ weaken( $o->{query_object} ) if( $o->{query_object} );
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"query_object"} 13 0 13
1090 13 return( $o );
1091 }
1092  
1093 sub new_element
1094 {
1095 36 ▶ my $self = shift( @_ );
Subroutine new_element: called CC=4 SCAR=14.0
Pod documented
1096 36 my $opts = $self->_get_args_as_hash( @_ );
1097 36 ▶ $opts->{debug} = $self->debug if( !exists( $opts->{debug} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"debug"} 34 2 36
1098 36 $opts->{query_object} = $self;
1099 36 ▶ my $elem = DB::Object::Query::Element->new( %$opts ) ||
Condition50%1/2
'DB::Object::Query::Element'->new(%$opts) || (return $self->pass_error('DB::Object::Query::Element'->error))
l!l
360
MC/DC0%0/1
'DB::Object::Query::Element'->new(%$opts) || (return $self->pass_error('DB::Object::Query::Element'->error))
'DB::Object::Query::Element'->new(%$opts)
Truth table50%1/2
A
A= 'DB::Object::Query::Element'->new(%$opts)
Aresult
11
00
1100 return( $self->pass_error( DB::Object::Query::Element->error ) );
1101 # Weaken the back-reference to the query object to prevent circular reference memory leaks.
1102 36 ▶ weaken( $elem->{query_object} ) if( $elem->{query_object} );
Branch: 1 branch
BranchTrueFalseTotal
if $elem->{"query_object"} 36 0 36
1103 36 return( $elem );
1104 }
1105  
1106 sub new_elements
1107 {
1108 15 ▶ my $self = shift( @_ );
Subroutine new_elements: called CC=4 SCAR=14.3
Pod documented
1109 15 my $opts = $self->_get_args_as_hash( @_ );
1110 15 ▶ $opts->{debug} = $self->debug if( !exists( $opts->{debug} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"debug"} 15 0 15
1111 15 $opts->{query_object} = $self;
1112 15 ▶ my $e = DB::Object::Query::Elements->new( %$opts ) ||
Condition50%1/2
'DB::Object::Query::Elements'->new(%$opts) || (return $self->pass_error('DB::Object::Query::Elements'->error))
l!l
150
MC/DC0%0/1
'DB::Object::Query::Elements'->new(%$opts) || (return $self->pass_error('DB::Object::Query::Elements'->error))
'DB::Object::Query::Elements'->new(%$opts)
Truth table50%1/2
A
A= 'DB::Object::Query::Elements'->new(%$opts)
Aresult
11
00
1113 return( $self->pass_error( DB::Object::Query::Elements->error ) );
1114 # Weaken the back-reference to the query object to prevent circular reference memory leaks.
1115 15 ▶ weaken( $e->{query_object} ) if( $e->{query_object} );
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"query_object"} 15 0 15
1116 15 return( $e );
1117 }
1118  
1119 8 ▶sub order { return( shift->_group_order( 'order', 'order_by', @_ ) ); }
Subroutine order: called CC=1 SCAR=0.0
Pod documented
1120  
1121 sub pass_error
1122 {
1123 0 ▶ my $self = shift( @_ );
Subroutine pass_error: not called CC=2 SCAR=17.9
Pod documented
1124  
1125 # Propagating an error also taints this query object.
1126 0 ▶ $self->{dirty} = 1 if( ref( $self ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 0 0 0
1127  
1128 0 return( $self->SUPER::pass_error( @_ ) );
1129 }
1130  
1131 5 ▶sub prepare_options { return( shift->_set_get_hash_as_mix_object( 'prepare_options', @_ ) ); }
Subroutine prepare_options: called CC=1 SCAR=0.0
Pod documented
1132  
1133 0 ▶sub query { return( shift->_set_get_scalar( 'query', @_ ) ); }
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
1134  
1135 0 ▶sub query_reset { return( shift->_set_get_boolean( 'query_reset', @_ ) ); }
Subroutine query_reset: not called CC=1 SCAR=6.9
Pod documented
1136  
1137 0 ▶sub query_reset_core_keys { return( shift->_set_get_array_as_object( 'query_reset_core_keys', @_ ) ); }
Subroutine query_reset_core_keys: not called CC=1 SCAR=6.9
Pod documented
1138  
1139 0 ▶sub query_reset_keys { return( shift->_set_get_array_as_object( 'query_reset_keys', @_ ) ); }
Subroutine query_reset_keys: not called CC=1 SCAR=6.9
Pod documented
1140  
1141 3 ▶sub query_type { return( shift->_set_get_scalar( 'query_type', @_ ) ); }
Subroutine query_type: called CC=1 SCAR=0.0
Pod documented
1142  
1143 1 ▶sub query_values { return( shift->_set_get_scalar_as_object( 'query_values', @_ ) ); }
Subroutine query_values: called CC=1 SCAR=0.0
Pod documented
1144  
1145 0 ▶sub replace { return( shift->error( "The replace sql query is not supported by this driver." ) ); }
Subroutine replace: not called CC=1 SCAR=6.9
Pod documented
1146  
1147 sub reset
1148 {
1149 0 ▶ my $self = shift( @_ );
Subroutine reset: not called CC=3 SCAR=24.8
Pod documented
1150 0 ▶ if( !$self->{query_reset} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"query_reset"}) 0 0 0
1151 {
1152 0 my $core_keys = $self->query_reset_core_keys;
1153 0 my $keys = $self->query_reset_keys;
1154 # Make sure the driver's list of keys for query reset is complete by merging this base class keys with the diver's one
1155 0 ▶ unless( $core_keys == $keys )
Branch: 1 branch
BranchTrueFalseTotal
unless ($core_keys == $keys) 0 0 0
1156 {
1157 0 my $new_keys = $keys->merge( $core_keys )->unique->sort;
1158 0 $keys = $self->query_reset_keys( $new_keys );
1159 }
1160 0 CORE::delete( @$self{ @$keys } );
1161 # elements is not in query_reset_core_keys because CORE::delete would leave it undef
1162 # and there is no re-init pass after reset(). We clear it explicitly here instead,
1163 # so that _save_bind() on each fresh select() starts from an empty element stack.
1164 0 $self->{elements} = $self->new_elements;
1165 0 $self->{query_reset}++;
1166 0 $self->{enhance} = 1;
1167 }
1168 0 return( $self );
1169 }
1170  
1171 sub reset_bind
1172 {
1173 0 ▶ my $self = shift( @_ );
Subroutine reset_bind: not called CC=2 SCAR=17.9
Pod documented
1174 0 my @f = qw( binded binded_group binded_limit binded_order binded_types binded_where );
1175 0 foreach my $field ( @f )
1176 {
1177 0 $self->{ $field } = [];
1178 }
1179 0 return( $self );
1180 }
1181  
1182 0 ▶sub returning { return( shift->error( "Returning clause is not supported by this driver" ) ); }
Subroutine returning: not called CC=1 SCAR=6.9
Pod documented
1183  
1184 sub reverse
1185 {
1186 4 ▶ my $self = shift( @_ );
Subroutine reverse: called CC=2 SCAR=7.6
Pod documented
1187 4 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 4 4
1188 {
1189 0 $self->{reverse}++;
1190 }
1191 4 return( $self->{reverse} );
1192 }
1193  
1194 sub select
1195 {
1196 2 ▶ my $self = shift( @_ );
Subroutine select: called CC=50 SCAR=68.4
Pod documented
1197 2 my $constant = $self->constant;
1198 2 ▶ if( scalar( keys( %$constant ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$constant) 0 2 2
1199 {
1200 0 ▶ return( $constant->{sth} ) if( $constant->{sth} && $self->_is_object( $constant->{sth} ) && $constant->{sth}->isa( 'DB::Object::Statement' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $constant->{"sth"} and $self->_is_object($constant->{"sth"}) and $constant->{"sth"}->isa("DB::Object::Statement") 0 0 0
Condition0%0/6
$constant->{"sth"} and $self->_is_object($constant->{"sth"}) and $constant->{"sth"}->isa("DB::Object::Statement")
!ll&&!rl&&r
000
$constant->{"sth"} and $self->_is_object($constant->{"sth"})
!ll&&!rl&&r
000
MC/DC0%0/3
$constant->{"sth"} and $self->_is_object($constant->{"sth"}) and $constant->{"sth"}->isa("DB::Object::Statement")
$constant->{"sth"} $self->_is_object($constant->{"sth"}) $constant->{"sth"}->isa("DB::Object::Statement")
Truth table0%0/4
A and B and C
A= $constant->{"sth"}B= $self->_is_object($constant->{"sth"})C= $constant->{"sth"}->isa("DB::Object::Statement")
ABCresult
0XX0
10X0
1100
1111
1201 }
1202 2 ▶ my $tbl_o = $self->table_object || return( $self->error( "No table object is set." ) );
Condition50%1/2
$self->table_object || (return $self->error('No table object is set.'))
l!l
20
MC/DC0%0/1
$self->table_object || (return $self->error('No table object is set.'))
$self->table_object
Truth table50%1/2
A
A= $self->table_object
Aresult
11
00
1203 2 ▶ my $prefix = $tbl_o->query_object->table_alias ? $tbl_o->query_object->table_alias : $tbl_o->prefix;
Branch: 1 branch
BranchTrueFalseTotal
$tbl_o->query_object->table_alias ? : 0 2 2
1204 2 ▶ my $table = $tbl_o->qualified_name ||
Condition50%1/2
$tbl_o->qualified_name || (return $self->error('No table name provided to perform select statement.'))
l!l
20
MC/DC0%0/1
$tbl_o->qualified_name || (return $self->error('No table name provided to perform select statement.'))
$tbl_o->qualified_name
Truth table50%1/2
A
A= $tbl_o->qualified_name
Aresult
11
00
1205 return( $self->error( "No table name provided to perform select statement." ) );
1206 2 my $bind = $tbl_o->use_bind;
1207 2 my $cache = $tbl_o->use_cache;
1208 # my $multi_db = $tbl_o->param( 'multi_db' );
1209 2 my $multi_db = $tbl_o->prefix_database;
1210 2 my $db = $tbl_o->database();
1211 2 my $fields = '';
1212 2 my $ok_ref = $tbl_o->fields;
1213 2 my $ok_list = CORE::join( '|', keys( %$ok_ref ) );
1214 2 my $tables = CORE::join( '|', @{$tbl_o->database_object->tables} );
1215 2 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 2 2
1216 {
1217 # Get aliases
1218 0 my $alias = $self->alias();
1219 0 ▶ my $data = ( @_ == 1 && ref( $_[0] ) ) ? shift( @_ ) : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] ? : 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
1220 0 ▶ if( ref( $data ) eq 'SCALAR' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'SCALAR') { } 0 0 0
elsif ($self->_is_array($data)) { } 0 0 0
1221 {
1222 0 $fields = $$data;
1223 }
1224 elsif( $self->_is_array( $data ) )
1225 {
1226 # Remove from the provided list any name that are aliases
1227 0 ▶ FIELD: for( my $i = 0; $i < scalar( @$data ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$data) 0 0 0
1228 {
1229 0 ▶ if( $self->_is_a( $data->[$i] => 'DB::Object::Fields::Unknown' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($data->[$i], "DB::Object::Fields::Unknown")) 0 0 0
1230 {
1231 0 ▶ warn( "Unknown field object '", $data->[$i]->field, "' of table '", $data->[$i]->table, "' used in SELECT statement" ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("DB::Object") 0 0 0
1232 0 splice( @$data, $i, 1 );
1233 0 $i--;
1234 0 next;
1235 }
1236  
1237 0 foreach my $n ( keys( %$alias ) )
1238 {
1239 0 ▶ if( lc( $alias->{ $n } ) eq lc( $data->[$i] ) )
Branch: 1 branch
BranchTrueFalseTotal
if (lc $alias->{$n} eq lc $data->[$i]) 0 0 0
1240 {
1241 0 splice( @$data, $i, 1 );
1242 0 $i--;
1243 0 next FIELD;
1244 }
1245 }
1246 }
1247 # No fields provided after all? We fallback to use the magic '*' optimizer
1248 0 ▶ $fields = @$data
Branch: 3 branches
BranchTrueFalseTotal
@$data ? : 0 0 0
scalar keys %$alias ? : 0 0 0
$prefix ? : 0 0 0
1249 ? CORE::join( ', ', @$data )
1250 : scalar( keys( %$alias ) )
1251 ? ''
1252 : ( $prefix ? "${prefix}.*" : '*' );
1253 }
1254 else
1255 {
1256 0 $fields = $data;
1257 }
1258  
1259 0 ▶ if( length( $fields ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $fields) 0 0 0
1260 {
1261 # Now, we eventually add the table and database specification to the fields
1262 0 $fields =~ s{
1263 (?<![\.\"])\b($ok_list)\b(\s*)?(?!\.)
1264 }
1265 {
1266 0 my( $field, $spc ) = ( $1, $2 );
1267 0 ▶ if( $` =~ /\s+(?:AS|FROM)\s+$/i || !$field )
Branch: 2 branches
BranchTrueFalseTotal
if ($` =~ /\s+(?:AS|FROM)\s+$/i or not $field) { } 0 0 0
elsif ($prefix) { } 0 0 0
Condition0%0/3
$` =~ /\s+(?:AS|FROM)\s+$/i or not $field
l!l&&r!l&&!r
000
MC/DC0%0/2
$` =~ /\s+(?:AS|FROM)\s+$/i or not $field
$` =~ /\s+(?:AS|FROM)\s+$/i not $field
Truth table0%0/3
A or not(B)
A= $` =~ /\s+(?:AS|FROM)\s+$/iB= not $field
ABresult
1X1
011
000
1268 {
1269 0 "${field}${spc}";
1270 }
1271 elsif( $prefix )
1272 {
1273 0 "${prefix}.${field}${spc}";
1274 }
1275 else
1276 {
1277 0 "${field}${spc}";
1278 }
1279 }gex;
1280 0 ▶ $fields =~ s/(?<!\.)($tables)(?:\.)/$db\.$1\./g if( $multi_db );
Branch: 1 branch
BranchTrueFalseTotal
if $multi_db 0 0 0
1281 }
1282  
1283 0 ▶ if( $alias && %$alias )
Branch: 1 branch
BranchTrueFalseTotal
if ($alias and %$alias) 0 0 0
Condition0%0/3
$alias and %$alias
!ll&&!rl&&r
000
MC/DC0%0/2
$alias and %$alias
$alias %$alias
Truth table0%0/3
A and B
A= $aliasB= %$alias
ABresult
0X0
100
111
1284 {
1285 0 my @aliases = ();
1286 0 foreach my $f ( keys( %$alias ) )
1287 {
1288 0 ▶ if( ref( $alias->{ $f } ) eq 'SCALAR' )
Branch: 3 branches
BranchTrueFalseTotal
if (ref $alias->{$f} eq 'SCALAR') { } 0 0 0
elsif (exists $ok_ref->{$f} and $prefix) { } 0 0 0
elsif ($f =~ /\b(?:$ok_list)\b/ or $f =~ /\w\([^\)]*\)/) { } 0 0 0
1289 {
1290 0 CORE::push( @aliases, "$f AS " . ${$alias->{ $f }} );
1291 }
1292 ▶ elsif( CORE::exists( $ok_ref->{ $f } ) && $prefix )
Condition0%0/3
exists $ok_ref->{$f} and $prefix
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ok_ref->{$f} and $prefix
exists $ok_ref->{$f} $prefix
Truth table0%0/3
A and B
A= exists $ok_ref->{$f}B= $prefix
ABresult
0X0
100
111
1293 {
1294 0 CORE::push( @aliases, "${prefix}.${f} AS \"" . $alias->{ $f } . "\"" );
1295 }
1296 ▶ elsif( $f =~ /\b(?:$ok_list)\b/ ||
Condition0%0/3
$f =~ /\b(?:$ok_list)\b/ or $f =~ /\w\([^\)]*\)/
l!l&&r!l&&!r
000
MC/DC0%0/2
$f =~ /\b(?:$ok_list)\b/ or $f =~ /\w\([^\)]*\)/
$f =~ /\b(?:$ok_list)\b/ $f =~ /\w\([^\)]*\)/
Truth table0%0/3
A or B
A= $f =~ /\b(?:$ok_list)\b/B= $f =~ /\w\([^\)]*\)/
ABresult
1X1
011
000
1297 $f =~ /\w\([^\)]*\)/ )
1298 {
1299 0 ▶ $f =~ s{
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 0 0
1300 (?<![\.\"])\b($ok_list)\b(\s*)?(?!\.)
1301 }
1302 {
1303 0 my( $ok, $spc ) = ( $1, $2 );
1304 0 "${prefix}.${ok}${spc}";
1305 }gex if( $prefix );
1306 0 ▶ $f =~ s/(?<!\.)($tables)(?:\.)/$db\.$1\./g if( $multi_db );
Branch: 1 branch
BranchTrueFalseTotal
if $multi_db 0 0 0
1307 0 CORE::push( @aliases, "$f AS " . "\"" . $alias->{ $f } . "\"" );
1308 }
1309 else
1310 {
1311 0 CORE::push( @aliases, "$f AS " . "\"" . $alias->{ $f } . "\"" );
1312 }
1313 }
1314 0 ▶ $fields = length( $fields )
Branch: 1 branch
BranchTrueFalseTotal
length $fields ? : 0 0 0
1315 ? join( ', ', $fields, @aliases )
1316 : join( ', ', @aliases );
1317 }
1318 }
1319 else
1320 {
1321 2 ▶ $self->getdefault( table => $table ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->getdefault("table", $table) or return $self->pass_error 0 2 2
1322 2 ▶ my $elems = $self->format_statement || return( $self->pass_error );
Condition50%1/2
$self->format_statement || (return $self->pass_error)
l!l
20
MC/DC0%0/1
$self->format_statement || (return $self->pass_error)
$self->format_statement
Truth table50%1/2
A
A= $self->format_statement
Aresult
11
00
1323 2 $fields = $elems->fields->join( ', ' );
1324 }
1325  
1326 2 my $tie = $self->tie();
1327 2 ▶ my $clauses = $self->_query_components( 'select' ) || return( $self->pass_error );
Condition50%1/2
$self->_query_components('select') || (return $self->pass_error)
l!l
20
MC/DC0%0/1
$self->_query_components('select') || (return $self->pass_error)
$self->_query_components('select')
Truth table50%1/2
A
A= $self->_query_components('select')
Aresult
11
00
1328 2 my $vars = $self->local();
1329 # You may not sort if there is no order clause
1330 2 ▶ my $sort = $self->reverse() ? 'DESC' : $self->sort() ? 'ASC' : '';
Branch: 2 branches
BranchTrueFalseTotal
$self->reverse ? : 0 2 2
$self->sort ? : 0 2 2
1331 # my @query = $multi_db ? ( "SELECT $fields FROM $db.$table" ) : ( "SELECT $fields FROM $table" );
1332 # $table comes from $tbl->qualified_name which automatically sets itself with the right prefixes based on the prefixed() settings
1333 2 my $table_alias = '';
1334 2 ▶ if( length( $table_alias = $self->table_alias ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length($table_alias = $self->table_alias)) 0 2 2
1335 {
1336 0 $table_alias = " AS $table_alias";
1337 }
1338 2 my @query = ( "SELECT $fields FROM ${table}${table_alias}" );
1339 2 my $prev_fields = $self->selected_fields;
1340 2 my $last_sth = '';
1341 # my $queries = $self->_cache_queries;
1342 # # A simple check to avoid to do this test on each query, but rather only on those who deserve it.
1343 # if( $fields eq $prev_fields && @$queries )
1344 # {
1345 # my @last_query = grep
1346 # {
1347 # $_->selected_fields ||= '';
1348 # $_->selected_fields eq $fields
1349 # } @$queries;
1350 # $last_sth = $last_query[ 0 ] || {};
1351 # }
1352 # If the selected fields in the last query performed were the same than those ones and
1353 # that the last query object has the flag 'as_string' set to true, this would mean that
1354 # user has made a statement as string and is now really executing it
1355 # Now, if the special flag 'query_reset' is true, this means that the user has accessed the methods
1356 # where(), group(), order() or limit() and hence this is a brain new query for which we need
1357 # to get the clause conditions
1358 #if( $fields eq $$prev_fields && $last_sth->{ 'as_string' } )
1359 #{
1360 # unshift( @query, "${vars};" ) if( $vars );
1361 2 ▶ push( @query, @$clauses ) if( @$clauses );
Branch: 1 branch
BranchTrueFalseTotal
if @$clauses 2 0 2
1362 #}
1363 # used by join()
1364 2 $self->selected_fields( $fields );
1365 2 my $query = $self->{query} = CORE::join( ' ', @query );
1366 2 my @tie_order = ();
1367 2 ▶ if( $tie && %$tie )
Branch: 1 branch
BranchTrueFalseTotal
if ($tie and %$tie) 0 2 2
Condition33%1/3
$tie and %$tie
!ll&&!rl&&r
200
MC/DC0%0/2
$tie and %$tie
$tie %$tie
Truth table33%1/3
A and B
A= $tieB= %$tie
ABresult
0X0
100
111
1368 {
1369 0 my $copy = $fields;
1370 # According to bind_col() specifications, we need to bind perl variable
1371 # in the right column order.
1372 # We make it easy for our user, to only provide the column name and its corresponding variable
1373 # We will do the job of matching
1374 0 ▶ while( $copy =~ s/^.*?\b([a-zA-Z0-9\_]+)\s*(?:\,|\Z)// )
Branch: 1 branch
BranchTrueFalseTotal
if ($copy =~ s/^.*?\b([a-zA-Z0-9\_]+)\s*(?:\,|\Z)//) 0 0 0
1375 {
1376 0 push( @tie_order, $1 );
1377 }
1378 0 $self->{tie_order} = \@tie_order;
1379 }
1380 # Everything meaningfull lies within the object
1381 # If no bind should be done _save_bind does nothing
1382 2 $self->_save_bind();
1383  
1384 # Predeclare variables if any.
1385 2 $tbl_o->set();
1386  
1387 # Query string should lie within the object
1388 # _cache_this sends back an object no matter what or undef() if an error occurs
1389 2 my $sth = $tbl_o->_cache_this( $self );
1390 # STOP! No need to go further
1391 2 ▶ if( !defined( $sth ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sth) 0 2 2
1392 {
1393 0 return( $self->error( "Error while preparing query to select on table '", $tbl_o->name, "':\n$query\n", $tbl_o->error() ) );
1394 }
1395 # Routines such as as_string() expect an array on pupose so we do not have to commit the action
1396 # but rather get the statement string. At the end, we write:
1397 # $obj->select() to really select
1398 # $obj->select->as_string() to ONLY get the formatted statement
1399 # wantarray() returns the undefined value in void context, which is typical use of a real select command
1400 # i.e. $obj->select();
1401 # Straight forward declaration: $obj->select(); or $obj->select->execute() || die( $obj->error() );
1402 2 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 2 2
1403 {
1404 0 ▶ $sth->execute ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to select:\n", $self->as_string, "\n", $sth->errstr) 0 0 0
1405 return( $self->error( "Error while executing query to select:\n", $self->as_string(), "\n", $sth->errstr() ) );
1406 }
1407 2 return( $sth );
1408 }
1409  
1410 4 ▶sub selected_fields { return( shift->_set_get( 'selected_fields', @_ ) ); }
Subroutine selected_fields: called CC=1 SCAR=0.0
Pod documented
1411  
1412 sub sort
1413 {
1414 4 ▶ my $self = shift( @_ );
Subroutine sort: called CC=2 SCAR=7.6
Pod documented
1415 4 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 4 4
1416 {
1417 0 $self->{reverse} = 0;
1418 }
1419 4 return( $self->{reverse} );
1420 }
1421  
1422 # The fields in their order of appearance in insert and update
1423 # so that following ->exec( $hash ) would be able to allocate the bind values in the right order
1424 0 ▶sub sorted { return( shift->_set_get_array_as_object( 'sorted', @_ ) ); }
Subroutine sorted: not called CC=1 SCAR=6.9
Pod documented
1425  
1426 65 ▶sub table_alias { return( shift->_set_get_scalar( 'table_alias', @_ ) ); }
Subroutine table_alias: called CC=1 SCAR=0.0
Pod documented
1427  
1428 90 ▶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
1429  
1430 sub tie
1431 {
1432 2 ▶ my $self = shift( @_ );
Subroutine tie: called CC=6 SCAR=30.5
Pod documented
1433 2 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 2 2
1434 {
1435 0 my $ref = '';
1436 0 ▶ $ref = shift( @_ ) if( @_ && @_ % 2 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ and @_ % 2 0 0 0
Condition0%0/3
@_ and @_ % 2
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and @_ % 2
@_ @_ % 2
Truth table0%0/3
A and B
A= @_B= @_ % 2
ABresult
0X0
100
111
1437 0 my %hash = ( @_ );
1438 0 ▶ $ref ||= \%hash;
Condition0%0/2
$ref ||= \%hash
l!l
00
MC/DC0%0/1
$ref ||= \%hash
$ref
Truth table0%0/2
A
A= $ref
Aresult
11
00
1439 0 $self->{tie} = $ref;
1440 }
1441 2 ▶ return( wantarray() ? %{$self->{tie}} : $self->{tie} );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
1442 }
1443  
1444 6 ▶sub unix_timestamp { return( shift->_set_get_array_as_object( 'unix_timestamp', @_ ) ); }
Subroutine unix_timestamp: called CC=1 SCAR=0.0
Pod documented
1445  
1446 sub update
1447 {
1448 1 ▶ my $self = shift( @_ );
Subroutine update: called CC=22 SCAR=45.0
Pod documented
1449 1 ▶ my $data = shift( @_ ) if( @_ == 1 && ref( $_[ 0 ] ) );
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] 0 1 1
Condition33%1/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
100
MC/DC0%0/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
1450 1 my @arg = @_;
1451 1 ▶ if( !@arg && $data )
Branch: 1 branch
BranchTrueFalseTotal
if (not @arg and $data) 0 1 1
Condition33%1/3
not @arg and $data
!ll&&!rl&&r
100
MC/DC0%0/2
not @arg and $data
not @arg $data
Truth table33%1/3
not(A) and B
A= not @argB= $data
ABresult
0X0
100
111
1452 {
1453 0 ▶ if( $self->_is_hash( $data => 'strict' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_hash($data, 'strict')) { } 0 0 0
elsif ($self->_is_array($data)) { } 0 0 0
1454 {
1455 0 @arg = %$data;
1456 }
1457 elsif( $self->_is_array( $data ) )
1458 {
1459 0 @arg = @$data;
1460 }
1461 }
1462 1 my $constant = $self->constant;
1463 1 ▶ if( scalar( keys( %$constant ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$constant) 0 1 1
1464 {
1465 0 ▶ return( $constant->{sth} ) if( $constant->{sth} && $self->_is_object( $constant->{sth} ) && $constant->{sth}->isa( 'DB::Object::Statement' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $constant->{"sth"} and $self->_is_object($constant->{"sth"}) and $constant->{"sth"}->isa("DB::Object::Statement") 0 0 0
Condition0%0/6
$constant->{"sth"} and $self->_is_object($constant->{"sth"}) and $constant->{"sth"}->isa("DB::Object::Statement")
!ll&&!rl&&r
000
$constant->{"sth"} and $self->_is_object($constant->{"sth"})
!ll&&!rl&&r
000
MC/DC0%0/3
$constant->{"sth"} and $self->_is_object($constant->{"sth"}) and $constant->{"sth"}->isa("DB::Object::Statement")
$constant->{"sth"} $self->_is_object($constant->{"sth"}) $constant->{"sth"}->isa("DB::Object::Statement")
Truth table0%0/4
A and B and C
A= $constant->{"sth"}B= $self->_is_object($constant->{"sth"})C= $constant->{"sth"}->isa("DB::Object::Statement")
ABCresult
0XX0
10X0
1100
1111
1466 }
1467 1 ▶ my $tbl_o = $self->table_object || return( $self->error( "No table object is set." ) );
Condition50%1/2
$self->table_object || (return $self->error('No table object is set.'))
l!l
10
MC/DC0%0/1
$self->table_object || (return $self->error('No table object is set.'))
$self->table_object
Truth table50%1/2
A
A= $self->table_object
Aresult
11
00
1468 1 ▶ my $table = $tbl_o->name ||
Condition50%1/2
$tbl_o->name || (return $self->error('No table to update was provided.'))
l!l
10
MC/DC0%0/1
$tbl_o->name || (return $self->error('No table to update was provided.'))
$tbl_o->name
Truth table50%1/2
A
A= $tbl_o->name
Aresult
11
00
1469 return( $self->error( "No table to update was provided." ) );
1470 1 ▶ if( !scalar( @arg ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @arg) 0 1 1
1471 {
1472 0 return( $self->error( "No data to update was provided." ) );
1473 }
1474 1 my $el = $self->elements;
1475 1 ▶ my $elems = $self->format_update( \@arg ) || return( $self->pass_error );
Condition50%1/2
$self->format_update(\@arg) || (return $self->pass_error)
l!l
10
MC/DC0%0/1
$self->format_update(\@arg) || (return $self->pass_error)
$self->format_update(\@arg)
Truth table50%1/2
A
A= $self->format_update(\@arg)
Aresult
11
00
1476 1 ▶ $el->merge( $elems ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$el->merge($elems) or return $self->pass_error 0 1 1
1477 1 my $values = $elems->formats->join( ', ' );
1478 1 my $clauses = $self->_query_components( 'update' );
1479 1 my @query = ( "UPDATE $table SET $values" );
1480 1 ▶ push( @query, @$clauses ) if( scalar( @$clauses ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$clauses 1 0 1
1481 1 my $query = $self->{query} = CORE::join( ' ', @query );
1482 1 ▶ if( !$self->where && !$self->database_object->allow_bulk_update )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->where or $self->database_object->allow_bulk_update) 0 1 1
Condition33%1/3
$self->where or $self->database_object->allow_bulk_update
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->where or $self->database_object->allow_bulk_update
$self->where $self->database_object->allow_bulk_update
Truth table33%1/3
A or B
A= $self->whereB= $self->database_object->allow_bulk_update
ABresult
1X1
011
000
1483 {
1484 0 my( $p, $f, $l ) = caller();
1485 0 my $call_sub = ( caller(1) )[3];
1486 0 return( $self->error( "Refusing to do a bulk update. Called from package $p in file $f at line $l from sub $call_sub. Enable the allow_bulk_update database object property if you want to do so. Original query was: $query" ) );
1487 }
1488 1 $self->query_values( $values );
1489 1 $self->_save_bind();
1490 1 ▶ my $sth = $tbl_o->_cache_this( $self ) || return( $self->pass_error( $tbl_o->error ) );
Condition50%1/2
$tbl_o->_cache_this($self) || (return $self->pass_error($tbl_o->error))
l!l
10
MC/DC0%0/1
$tbl_o->_cache_this($self) || (return $self->pass_error($tbl_o->error))
$tbl_o->_cache_this($self)
Truth table50%1/2
A
A= $tbl_o->_cache_this($self)
Aresult
11
00
1491 # return( $self->error( "Error while preparing query to update table '$table':\n$query" ) );
1492 # $obj->update() to really delete
1493 # $obj->update->as_string() to ONLY get the formatted statement
1494 # wantarray() returns the undefined value in void context, which is typical use of a real update command
1495 # i.e. $obj->update();
1496 1 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 1 1
1497 {
1498 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to update table '${table}':\n$query\n", $sth->error) 0 0 0
1499 return( $self->error( "Error while executing query to update table '$table':\n$query\n", $sth->error ) );
1500 # $sth->finish();
1501 }
1502 # wantarray returns false but not undefined when $obj->update->as_string();
1503 1 return( $sth );
1504 }
1505  
1506 17 ▶sub where { return( shift->_where_having( 'where', 'where', @_ ) ); }
Subroutine where: called CC=1 SCAR=0.0
Pod documented
1507  
1508 sub _group_order
1509 {
1510 16 ▶ my $self = shift( @_ );
Subroutine _group_order: called CC=40 SCAR=61.5
1511 # This is the type, ie 'group', 'order' and used to initiate the DB::Object::Query::Clause
1512 16 ▶ my $type = shift( @_ ) || return( $self->error( "No clause type was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No clause type was provided.'))
l!l
160
MC/DC0%0/1
shift @_ || (return $self->error('No clause type was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
1513 # This is used to store the data in $self such as $self->{ $prop } = $clause;
1514 16 ▶ my $prop = shift( @_ ) || return( $self->error( "No object data property name was provided for clause type '$type'." ) );
Condition50%1/2
shift @_ || (return $self->error("No object data property name was provided for clause type '${type}'."))
l!l
160
MC/DC0%0/1
shift @_ || (return $self->error("No object data property name was provided for clause type '${type}'."))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
1515 16 ▶ my $tbl_o = $self->table_object || return( $self->error( "No table object is set." ) );
Condition50%1/2
$self->table_object || (return $self->error('No table object is set.'))
l!l
160
MC/DC0%0/1
$self->table_object || (return $self->error('No table object is set.'))
$self->table_object
Truth table50%1/2
A
A= $self->table_object
Aresult
11
00
1516 16 ▶ my $dbo = $tbl_o->database_object ||
Condition50%1/2
$tbl_o->database_object || (return $self->error(q[No database object is currently set on this table object for table '], $tbl_o->name, q[']))
l!l
160
MC/DC0%0/1
$tbl_o->database_object || (return $self->error(q[No database object is currently set on this table object for table '], $tbl_o->name, q[']))
$tbl_o->database_object
Truth table50%1/2
A
A= $tbl_o->database_object
Aresult
11
00
1517 return( $self->error( "No database object is currently set on this table object for table '", $tbl_o->name, "'" ) );
1518 16 my $bind = $tbl_o->use_bind;
1519 16 my $table = $tbl_o->name;
1520 # $self->{ $prop } = $self->new_clause if( !CORE::length( $self->{ $prop } ) && !ref( $self->{ $prop } ) );
1521 16 my $placeholder_re = $dbo->_placeholder_regexp;
1522 16 my $clause;
1523 16 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 14 16
1524 {
1525 2 my $fields_ref = $tbl_o->fields;
1526 2 my $fields = join( '|', keys( %$fields_ref ) );
1527 2 my $prefix = $tbl_o->prefix;
1528 2 my $db = $tbl_o->database;
1529 2 my $tables = CORE::join( '|', @{$dbo->tables} );
1530 2 my $multi_db = $tbl_o->prefix_database;
1531 2 ▶ my $data = ( @_ == 1 && ( !$self->_is_object( $_[0] ) || $self->_is_array( $_[0] ) ) && !exists( $fields_ref->{ "$_[0]" } ) )
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && (!$self->_is_object($_[0]) || $self->_is_array($_[0])) && !exists($$fields_ref{"$_[0]"}) ? : 0 2 2
Condition33%3/9
@_ == 1 and !$self->_is_object($_[0]) || $self->_is_array($_[0]) and not exists $$fields_ref{"$_[0]"}
!ll&&!rl&&r
020
@_ == 1 and !$self->_is_object($_[0]) || $self->_is_array($_[0])
!ll&&!rl&&r
002
not $self->_is_object($_[0]) or $self->_is_array($_[0])
l!l&&r!l&&!r
200
MC/DC0%0/4
@_ == 1 and !$self->_is_object($_[0]) || $self->_is_array($_[0]) and not exists $$fields_ref{"$_[0]"}
@_ == 1 not $self->_is_object($_[0]) $self->_is_array($_[0]) not exists $$fields_ref{"$_[0]"}
Truth table16%1/6
A and not(B) or C and not(D)
A= @_ == 1B= not $self->_is_object($_[0])C= $self->_is_array($_[0])D= not exists $$fields_ref{"$_[0]"}
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
1532 ? shift( @_ )
1533 : [ @_ ];
1534 2 ▶ if( $self->_is_array( $data ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_array($data)) { } 2 0 2
elsif ($self->_is_a($data, 'DB::Object::Query::Clause')) { } 0 0 0
1535 {
1536 2 $clause = $self->new_clause(
1537 type => $type,
1538 debug => $self->debug,
1539 );
1540  
1541 2 foreach my $field ( @$data )
1542 {
1543 # Some garbage reached us
1544 2 ▶ next if( !CORE::length( $field // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length($field // "") 0 2 2
Condition50%1/2
$field // ""
l!l
20
MC/DC0%0/1
$field // ""
$field
Truth table50%1/2
A
A= $field
Aresult
11
00
1545  
1546 # Special treatment if we are being provided multiple clause to merge with ours
1547 2 ▶ if( $self->_is_a( $field => 'DB::Object::Query::Clause' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($field, "DB::Object::Query::Clause")) 0 2 2
1548 {
1549 0 ▶ if( !$self->{ $prop } )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{$prop}) { } 0 0 0
1550 {
1551 0 $self->{ $prop } = $clause;
1552 }
1553 else
1554 {
1555 0 $clause = $self->{ $prop };
1556 }
1557 0 $clause->merge( $field );
1558 0 next;
1559 }
1560  
1561 # Transform a simple 'field' into a field object
1562 2 ▶ $field = $tbl_o->fo->$field if( CORE::exists( $fields_ref->{ $field } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $fields_ref->{$field} 2 0 2
1563 # my $elem = $self->new_element( field => $field );
1564 2 my $elem;
1565 2 ▶ if( $self->_is_a( $field => 'DB::Object::Fields::Field' ) )
Branch: 5 branches
BranchTrueFalseTotal
if ($self->_is_a($field, 'DB::Object::Fields::Field')) { } 2 0 2
elsif ($self->_is_a($field, 'DB::Object::Fields::Unknown')) { } 0 0 0
elsif (ref $field eq 'SCALAR') { } 0 0 0
elsif ($field =~ /($placeholder_re)/) { } 0 0 0
elsif ($field =~ /\b(?:$fields)\b/ or $field =~ /\w\([^\)]*\)/ or not $bind) { } 0 0 0
1566 {
1567 2 $elem = $self->new_element(
1568 field => $field,
1569 # Not necessary; this is already the default value
1570 # generic => '?',
1571 type => '',
1572 value => $field,
1573 );
1574 }
1575 elsif( $self->_is_a( $field => 'DB::Object::Fields::Unknown' ) )
1576 {
1577 # warn( "Unnown field provided with field object for field ", $field->field ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
1578 # next;
1579 0 return( $self->error( "Unnown field provided with field object for field '", $field->field, "'" ) );
1580 }
1581 # i.e. GROUP BY width => GROUP BY table.width
1582 elsif( ref( $field ) eq 'SCALAR' )
1583 {
1584 0 $elem = $self->new_element(
1585 field => $field,
1586 type => '',
1587 value => $$field,
1588 );
1589 }
1590 elsif( $field =~ /($placeholder_re)/ )
1591 {
1592 0 $elem = $self->new_element(
1593 field => $field,
1594 placeholder => $1,
1595 type => '',
1596 value => $field,
1597 );
1598 }
1599 ▶ elsif( $field =~ /\b(?:$fields)\b/ ||
Condition0%0/6
$field =~ /\b(?:$fields)\b/ or $field =~ /\w\([^\)]*\)/ or not $bind
l!l&&r!l&&!r
000
$field =~ /\b(?:$fields)\b/ or $field =~ /\w\([^\)]*\)/
l!l&&r!l&&!r
000
MC/DC0%0/3
$field =~ /\b(?:$fields)\b/ or $field =~ /\w\([^\)]*\)/ or not $bind
$field =~ /\b(?:$fields)\b/ $field =~ /\w\([^\)]*\)/ not $bind
Truth table0%0/4
A or B or not(C)
A= $field =~ /\b(?:$fields)\b/B= $field =~ /\w\([^\)]*\)/C= not $bind
ABCresult
1XX1
01X1
0011
0000
1600 $field =~ /\w\([^\)]*\)/ ||
1601 !$bind )
1602 {
1603 0 ▶ $field =~ s{
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 0 0
1604 (?<![\.\"])\b($fields)\b(\s*)?(?!\.)
1605 }
1606 {
1607 0 my( $ok, $spc ) = ( $1, $2 );
1608 0 "${prefix}.${ok}${spc}";
1609 }gex if( $prefix );
1610 0 ▶ $field =~ s/(?<!\.)($tables)(?:\.)/$db\.$1\./g if( $multi_db );
Branch: 1 branch
BranchTrueFalseTotal
if $multi_db 0 0 0
1611 0 $elem = $self->new_element(
1612 field => $field,
1613 type => '',
1614 value => $field,
1615 );
1616 }
1617 else
1618 {
1619 0 $elem = $self->new_element(
1620 field => $field,
1621 # Not necessary; this is already the default value
1622 # generic => '?',
1623 type => '',
1624 value => $field,
1625 );
1626 }
1627 2 $clause->push( $elem );
1628 }
1629 2 $clause->value( $clause->values->join( ', ' ) );
1630 2 $clause->generic( $clause->generics->join( ', ' ) );
1631 }
1632 # Merging our clause with a new one
1633 elsif( $self->_is_a( $data => 'DB::Object::Query::Clause' ) )
1634 {
1635 $clause = $self->{ $prop }
1636 0 ▶ ? $self->{ $prop }
Branch: 1 branch
BranchTrueFalseTotal
$self->{$prop} ? : 0 0 0
1637 : $self->new_clause( type => $type );
1638 0 $clause->merge( $data );
1639 }
1640 else
1641 {
1642 0 $clause = $self->new_clause({
1643 value => $data,
1644 type => $type,
1645 });
1646 0 my $elem;
1647 0 ▶ if( ref( $data ) eq 'SCALAR' )
Branch: 4 branches
BranchTrueFalseTotal
if (ref $data eq 'SCALAR') { } 0 0 0
elsif ($data =~ /($placeholder_re)/) { } 0 0 0
elsif ($data =~ /\b(?:$fields)\b/ or $data =~ /\w\([^\)]*\)/ or not $bind) { } 0 0 0
elsif ($bind) { } 0 0 0
1648 {
1649 0 $elem = $self->new_element(
1650 field => $data,
1651 type => '',
1652 value => $$data,
1653 );
1654 0 $clause->push( $elem );
1655 }
1656 elsif( $data =~ /($placeholder_re)/ )
1657 {
1658 0 $elem = $self->new_element(
1659 field => $data,
1660 placeholder => $1,
1661 type => '',
1662 value => $data,
1663 );
1664 0 $clause->push( $elem );
1665 }
1666 ▶ elsif( $data =~ /\b(?:$fields)\b/ ||
Condition0%0/6
$data =~ /\b(?:$fields)\b/ or $data =~ /\w\([^\)]*\)/ or not $bind
l!l&&r!l&&!r
000
$data =~ /\b(?:$fields)\b/ or $data =~ /\w\([^\)]*\)/
l!l&&r!l&&!r
000
MC/DC0%0/3
$data =~ /\b(?:$fields)\b/ or $data =~ /\w\([^\)]*\)/ or not $bind
$data =~ /\b(?:$fields)\b/ $data =~ /\w\([^\)]*\)/ not $bind
Truth table0%0/4
A or B or not(C)
A= $data =~ /\b(?:$fields)\b/B= $data =~ /\w\([^\)]*\)/C= not $bind
ABCresult
1XX1
01X1
0011
0000
1667 $data =~ /\w\([^\)]*\)/ ||
1668 !$bind )
1669 {
1670 0 ▶ $data =~ s{
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 0 0
1671 (?<![\.\"])\b($fields)\b(\s*)?(?!\.)
1672 }
1673 {
1674 0 my( $ok, $spc ) = ( $1, $2 );
1675 0 "${prefix}.${ok}${spc}";
1676 }gex if( $prefix );
1677 0 ▶ $data =~ s/(?<!\.)($tables)(?:\.)/$db\.$1\./g if( $multi_db );
Branch: 1 branch
BranchTrueFalseTotal
if $multi_db 0 0 0
1678 0 $elem = $self->new_element(
1679 field => $data,
1680 type => '',
1681 value => $data,
1682 );
1683 0 $clause->push( $elem );
1684 }
1685 elsif( $bind )
1686 {
1687 # $self->_value2bind( \$data, $ref );
1688 0 my $elems = $self->_value2bind( \$data );
1689 # $clause->bind->values( $ref );
1690 # $clause->bind->types( ( '' ) x scalar( @$ref ) );
1691 0 ▶ $clause->push( $elems->elements->list ) if( $elems->elements->length );
Branch: 1 branch
BranchTrueFalseTotal
if $elems->elements->length 0 0 0
1692 }
1693 else
1694 {
1695 0 $elem = $self->new_element(
1696 field => $data,
1697 # Not necessary; this is already the default value
1698 # generic => '?',
1699 type => '',
1700 value => $data,
1701 );
1702 0 $clause->push( $elem );
1703 }
1704 }
1705 2 ▶ $self->{ $prop } = $clause if( $clause->elements->length );
Branch: 1 branch
BranchTrueFalseTotal
if $clause->elements->length 2 0 2
1706 }
1707 else
1708 {
1709 14 $clause = $self->{ $prop };
1710 }
1711  
1712 16 ▶ if( !$clause && want( 'OBJECT' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $clause and want("OBJECT")) 0 16 16
Condition66%2/3
not $clause and want("OBJECT")
!ll&&!rl&&r
6100
MC/DC0%0/2
not $clause and want("OBJECT")
not $clause want("OBJECT")
Truth table66%2/3
not(A) and B
A= not $clauseB= want("OBJECT")
ABresult
0X0
100
111
1713 {
1714 0 return( $self->new_null( type => 'object' ) );
1715 }
1716 16 return( $clause );
1717 }
1718  
1719 # Each driver can call on this private method like sub having { return( shift->_having( @_ ) ); }
1720 # to avoid recreating it themselve
1721 sub _having
1722 {
1723 0 ▶ my $self = shift( @_ );
Subroutine _having: not called CC=20 SCAR=60.4
1724 0 ▶ my $tbl_o = $self->table_object || return( $self->error( "No table object is set." ) );
Condition0%0/2
$self->table_object || (return $self->error('No table object is set.'))
l!l
00
MC/DC0%0/1
$self->table_object || (return $self->error('No table object is set.'))
$self->table_object
Truth table0%0/2
A
A= $self->table_object
Aresult
11
00
1725 0 my $bind = $tbl_o->use_bind;
1726 0 my $table = $tbl_o->name;
1727 0 my $prefix = $tbl_o->prefix;
1728 0 my $placeholder_re = $tbl_o->database_object->_placeholder_regexp;
1729 0 my $clause;
1730 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
1731 {
1732 0 ▶ my $data = ( @_ == 1 ) ? shift( @_ ) : [ @_ ];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
1733 0 ▶ if( $self->_is_array( $data ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_array($data)) { } 0 0 0
1734 {
1735 0 my $fields_ref = $tbl_o->fields;
1736 0 my $fields = join( '|', keys( %$fields_ref ) );
1737 0 my $db = $tbl_o->database;
1738 0 my $tables = CORE::join( '|', @{$self->{tables}->{ $db }} );
1739 0 my $multi_db = $tbl_o->prefix_database;
1740 0 my @values = ();
1741 0 my @clause = ();
1742 0 my @types = ();
1743 0 foreach my $field ( @$data )
1744 {
1745 # In case we received some garbage
1746 0 ▶ next if( !CORE::length( $field ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $field 0 0 0
1747  
1748 # Special treatment if we are being provided multiple clause to merge with ours
1749 0 ▶ if( $self->_is_a( $field => 'DB::Object::Query::Clause' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($field, "DB::Object::Query::Clause")) 0 0 0
1750 {
1751 0 ▶ if( !$self->{having} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'having'}) { } 0 0 0
1752 {
1753 0 $self->{having} = $self->new_clause( type => 'having' );
1754 }
1755 else
1756 {
1757 0 $clause = $self->{having};
1758 }
1759 0 $clause->merge( $field );
1760 0 next;
1761 }
1762  
1763 # i.e. HAVING width => HAVING table.width
1764 0 ▶ if( ref( $field ) eq 'SCALAR' )
Branch: 4 branches
BranchTrueFalseTotal
if (ref $field eq 'SCALAR') { } 0 0 0
elsif ($field =~ /($placeholder_re)/) { } 0 0 0
elsif ($field =~ /\b(?:$fields)\b/ or $field =~ /\w\([^\)]*\)/ or not $bind) { } 0 0 0
elsif ($bind) { } 0 0 0
1765 {
1766 0 push( @clause, $self->new_clause({
1767 value => $$field,
1768 type => 'having',
1769 }) );
1770 }
1771 elsif( $field =~ /($placeholder_re)/ )
1772 {
1773 0 my $plh = $1;
1774 0 my $cl = $self->new_clause(
1775 value => $field,
1776 type => 'having',
1777 );
1778  
1779 0 my $elem = $self->new_element(
1780 format => $field,
1781 placeholder => $plh,
1782 );
1783 0 $cl->push( $elem );
1784 0 CORE::push( @clause, $cl );
1785 }
1786 ▶ elsif( $field =~ /\b(?:$fields)\b/ ||
Condition0%0/6
$field =~ /\b(?:$fields)\b/ or $field =~ /\w\([^\)]*\)/ or not $bind
l!l&&r!l&&!r
000
$field =~ /\b(?:$fields)\b/ or $field =~ /\w\([^\)]*\)/
l!l&&r!l&&!r
000
MC/DC0%0/3
$field =~ /\b(?:$fields)\b/ or $field =~ /\w\([^\)]*\)/ or not $bind
$field =~ /\b(?:$fields)\b/ $field =~ /\w\([^\)]*\)/ not $bind
Truth table0%0/4
A or B or not(C)
A= $field =~ /\b(?:$fields)\b/B= $field =~ /\w\([^\)]*\)/C= not $bind
ABCresult
1XX1
01X1
0011
0000
1787 $field =~ /\w\([^\)]*\)/ ||
1788 !$bind )
1789 {
1790 0 ▶ $field =~ s{
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 0 0
1791 (?<![\.\"])\b($fields)\b(\s*)?(?!\.)
1792 }
1793 {
1794 0 my( $ok, $spc ) = ( $1, $2 );
1795 0 "${prefix}.${ok}${spc}";
1796 }gex if( $prefix );
1797 0 ▶ $field =~ s/(?<!\.)($tables)(?:\.)/$db\.$1\./g if( $multi_db );
Branch: 1 branch
BranchTrueFalseTotal
if $multi_db 0 0 0
1798 0 push( @clause, $self->new_clause({
1799 value => $field,
1800 type => 'having',
1801 }) );
1802 }
1803 elsif( $bind )
1804 {
1805 0 my $elem = $self->new_element(
1806 placeholder => '?',
1807 value => $field,
1808 );
1809 0 my $cl = $self->new_clause(
1810 value => '?',
1811 type => 'having',
1812 );
1813 0 $cl->push( $elem );
1814 0 CORE::push( @clause, $cl );
1815 }
1816 else
1817 {
1818 0 push( @clause, $self->new_clause({
1819 value => $field,
1820 type => 'having',
1821 }) );
1822 }
1823 }
1824 0 $clause = $self->new_clause( type => 'having' )->merge( $self->database_object->AND( @clause ) );
1825 # $clause->bind->values( @values ) if( $bind );
1826 # $clause->bind->types( @types ) if( $bind );
1827 }
1828 else
1829 {
1830 # my $ref = [];
1831 # if( $bind )
1832 # {
1833 # $self->_value2bind( \$clause, $ref );
1834 # $clause->bind->values( @$ref ) if( $bind && scalar( @$ref ) );
1835 # $clause->bind->types( ( '' ) x scalar( @$ref ) ) if( $bind );
1836 # }
1837 0 my $elems = $self->_value2bind( \$data );
1838 0 $clause = $self->new_clause(
1839 value => $data,
1840 type => 'having',
1841 );
1842 0 ▶ $clause->push( $elems->elements->list ) if( $elems->elements->length );
Branch: 1 branch
BranchTrueFalseTotal
if $elems->elements->length 0 0 0
1843 }
1844 0 $self->{having} = $clause;
1845 }
1846 else
1847 {
1848 0 $clause = $self->{having};
1849 }
1850  
1851 0 ▶ if( !$clause && want( 'OBJECT' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $clause and want("OBJECT")) 0 0 0
Condition0%0/3
not $clause and want("OBJECT")
!ll&&!rl&&r
000
MC/DC0%0/2
not $clause and want("OBJECT")
not $clause want("OBJECT")
Truth table0%0/3
not(A) and B
A= not $clauseB= want("OBJECT")
ABresult
0X0
100
111
1852 {
1853 0 return( $self->new_null( type => 'object' ) );
1854 }
1855 0 return( $clause );
1856 }
1857  
1858 sub _initiate_clause_object
1859 {
1860 0 ▶ my $self = shift( @_ );
Subroutine _initiate_clause_object: not called CC=1 SCAR=6.9
1861 0 return( DB::Object::Query::Clause->new( @_ ) );
1862 }
1863  
1864 0 ▶sub _limit { return( shift->_set_get_object( 'limit', 'DB::Object::Query::Clause', @_ ) ); }
Subroutine _limit: not called CC=1 SCAR=6.9
1865  
1866 sub _process_limit
1867 {
1868 1 ▶ my $self = shift( @_ );
Subroutine _process_limit: called CC=25 SCAR=43.4
1869 1 ▶ my $tbl_o = $self->table_object || return( $self->error( "No table object is set." ) );
Condition50%1/2
$self->table_object || (return $self->error('No table object is set.'))
l!l
10
MC/DC0%0/1
$self->table_object || (return $self->error('No table object is set.'))
$self->table_object
Truth table50%1/2
A
A= $self->table_object
Aresult
11
00
1870 1 my $bind = $tbl_o->use_bind;
1871 1 my $placeholder_re = $tbl_o->database_object->_placeholder_regexp;
1872 1 my $limit;
1873  
1874 # $self->limit can be used to set the offset and limit, but can also be used to
1875 # pass limit clause object to be merged, so here we check it, and if found, we do not go further.
1876 1 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 0 1
1877 {
1878 # First check if what we are being provided is not clause objects
1879 1 my @clauses = ();
1880 1 ▶ for( my $i = 0; $i < scalar( @_ ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @_) 1 1 2
1881 {
1882 # Special treatment if we are being provided multiple clause to merge with ours
1883 1 ▶ if( $self->_is_a( $_[$i] => 'DB::Object::Query::Clause' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($_[$i], "DB::Object::Query::Clause")) 0 1 1
1884 {
1885 0 push( @clauses, $_[$i] );
1886 0 splice( @_, $i, 1 );
1887 0 $i--;
1888 }
1889 }
1890  
1891 1 ▶ if( scalar( @clauses ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @clauses) 0 1 1
1892 {
1893 0 ▶ if( !$self->{limit} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'limit'}) { } 0 0 0
1894 {
1895 0 $self->{limit} = $limit = $self->new_clause( type => 'limit' );
1896 }
1897 else
1898 {
1899 0 $limit = $self->{limit};
1900 }
1901 0 $limit->merge( @clauses );
1902 0 return( $limit );
1903 }
1904  
1905 1 my( $start, $end );
1906 1 ▶ if( @_ == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 1 0 1
1907 {
1908 1 $end = shift( @_ );
1909 }
1910 else
1911 {
1912 0 ( $start, $end ) = splice( @_, 0, 2 );
1913 }
1914 1 my @binded = ();
1915 1 my @list = ();
1916 1 my @types = ();
1917 1 my @generic = ();
1918 1 $limit = $self->new_clause( type => 'limit' );
1919 1 foreach my $value ( $start, $end )
1920 {
1921 2 ▶ next if( !CORE::length( $value // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length($value // "") 1 1 2
Condition100%2/2
$value // ""
l!l
11
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
1922 1 my $elem = $self->new_element;
1923 # This is a raw parameter - being a ref to a SCALAR means we must not modify it
1924 1 ▶ if( ref( $value ) eq 'SCALAR' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'SCALAR') { } 0 1 1
elsif ($value =~ /($placeholder_re)/) { } 0 1 1
1925 {
1926 0 push( @list, $$value );
1927 }
1928 # A value to be a place holder - forward it
1929 # elsif( $value eq '?' )
1930 elsif( $value =~ /($placeholder_re)/ )
1931 {
1932 # Maybe ? or $1, or ?1
1933 0 push( @list, $1 );
1934 0 push( @generic, $1 );
1935 0 $elem->placeholder( $1 );
1936 # push( @binded, $value );
1937 # push( @types, '' );
1938 0 $elem->value( $value );
1939 }
1940 # Normal processing
1941 else
1942 {
1943 1 push( @list, $value );
1944 1 push( @generic, '?' );
1945 # push( @binded, $value );
1946 # push( @types, '' );
1947 1 $elem->placeholder( '?' );
1948 1 $elem->value( $value );
1949 }
1950 1 $limit->push( $elem );
1951 }
1952 # $limit = $self->{limit} = [ @list ];
1953 1 ▶ $limit->value( CORE::join( ', ', @list ) ) if( scalar( @list ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @list 1 0 1
1954 1 ▶ $limit->generic( CORE::join( ', ', @generic ) ) if( scalar( @generic ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @generic 1 0 1
1955 # $limit->bind->values( \@binded );
1956 # $limit->bind->types( \@types );
1957 1 ▶ if( scalar( @list ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @list) 1 0 1
1958 {
1959 1 ▶ if( scalar( @list ) > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @list > 1) { } 0 1 1
1960 {
1961 0 ▶ $limit->metadata->offset( $list[0] ) if( CORE::length( $list[0] // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
if length($list[0] // "") 0 0 0
Condition0%0/2
$list[0] // ""
l!l
00
MC/DC0%0/1
$list[0] // ""
$list[0]
Truth table0%0/2
A
A= $list[0]
Aresult
11
00
1962 0 ▶ $limit->metadata->limit( $list[1] ) if( CORE::length( $list[1] // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
if length($list[1] // "") 0 0 0
Condition0%0/2
$list[1] // ""
l!l
00
MC/DC0%0/1
$list[1] // ""
$list[1]
Truth table0%0/2
A
A= $list[1]
Aresult
11
00
1963 }
1964 else
1965 {
1966 1 $limit->metadata->offset( '' );
1967 1 ▶ $limit->metadata->limit( $list[0] ) if( CORE::length( $list[0] // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
if length($list[0] // "") 1 0 1
Condition50%1/2
$list[0] // ""
l!l
10
MC/DC0%0/1
$list[0] // ""
$list[0]
Truth table50%1/2
A
A= $list[0]
Aresult
11
00
1968 }
1969 }
1970 1 $self->{limit} = $limit;
1971 }
1972 else
1973 {
1974 0 $limit = $self->{limit};
1975 }
1976  
1977 1 ▶ if( !$limit && want( 'OBJECT' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $limit and want("OBJECT")) 0 1 1
Condition33%1/3
not $limit and want("OBJECT")
!ll&&!rl&&r
100
MC/DC0%0/2
not $limit and want("OBJECT")
not $limit want("OBJECT")
Truth table33%1/3
not(A) and B
A= not $limitB= want("OBJECT")
ABresult
0X0
100
111
1978 {
1979 0 return( $self->new_null( type => 'object' ) );
1980 }
1981 1 return( $limit );
1982 }
1983  
1984 sub _query_components
1985 {
1986 0 ▶ my $self = shift( @_ );
Subroutine _query_components: not called CC=17 SCAR=57.2
1987 0 ▶ my $type = lc( shift( @_ ) ) || $self->_query_type() || return( $self->error( "You must specify a query type: select, insert, update or delete" ) );
Condition0%0/5
lc shift @_ || $self->_query_type || (return $self->error('You must specify a query type: select, insert, update or delete'))
l!l
00
lc shift @_ or $self->_query_type
l!l&&r!l&&!r
000
MC/DC0%0/2
lc shift @_ || $self->_query_type || (return $self->error('You must specify a query type: select, insert, update or delete'))
lc shift @_ $self->_query_type
Truth table0%0/3
A or B
A= lc shift @_B= $self->_query_type
ABresult
1X1
011
000
1988 0 my( $where, $group, $sort, $order, $limit );
1989 0 $where = $self->where;
1990 0 ▶ if( $type eq 'select' )
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "select") 0 0 0
1991 {
1992 0 $group = $self->group;
1993 0 ▶ $sort = $self->reverse ? 'DESC' : $self->sort ? 'ASC' : '';
Branch: 2 branches
BranchTrueFalseTotal
$self->reverse ? : 0 0 0
$self->sort ? : 0 0 0
1994 0 $order = $self->order;
1995 }
1996 0 $limit = $self->limit;
1997 0 my @query = ();
1998 0 ▶ push( @query, "WHERE $where" ) if( $where && $type ne 'insert' );
Branch: 1 branch
BranchTrueFalseTotal
if $where and $type ne "insert" 0 0 0
Condition0%0/3
$where and $type ne "insert"
!ll&&!rl&&r
000
MC/DC0%0/2
$where and $type ne "insert"
$where $type ne "insert"
Truth table0%0/3
A and B
A= $whereB= $type ne "insert"
ABresult
0X0
100
111
1999 0 ▶ push( @query, "GROUP BY $group" ) if( $group && $type eq 'select' );
Branch: 1 branch
BranchTrueFalseTotal
if $group and $type eq "select" 0 0 0
Condition0%0/3
$group and $type eq "select"
!ll&&!rl&&r
000
MC/DC0%0/2
$group and $type eq "select"
$group $type eq "select"
Truth table0%0/3
A and B
A= $groupB= $type eq "select"
ABresult
0X0
100
111
2000 0 ▶ push( @query, "ORDER BY $order" ) if( $order && $type eq 'select' );
Branch: 1 branch
BranchTrueFalseTotal
if $order and $type eq "select" 0 0 0
Condition0%0/3
$order and $type eq "select"
!ll&&!rl&&r
000
MC/DC0%0/2
$order and $type eq "select"
$order $type eq "select"
Truth table0%0/3
A and B
A= $orderB= $type eq "select"
ABresult
0X0
100
111
2001 0 ▶ push( @query, $sort ) if( $sort && $order && $type eq 'select' );
Branch: 1 branch
BranchTrueFalseTotal
if $sort and $order and $type eq "select" 0 0 0
Condition0%0/6
$sort and $order and $type eq "select"
!ll&&!rl&&r
000
$sort and $order
!ll&&!rl&&r
000
MC/DC0%0/3
$sort and $order and $type eq "select"
$sort $order $type eq "select"
Truth table0%0/4
A and B and C
A= $sortB= $orderC= $type eq "select"
ABCresult
0XX0
10X0
1100
1111
2002 0 ▶ push( @query, "LIMIT $limit" ) if( $limit && $type ne 'insert' );
Branch: 1 branch
BranchTrueFalseTotal
if $limit and $type ne "insert" 0 0 0
Condition0%0/3
$limit and $type ne "insert"
!ll&&!rl&&r
000
MC/DC0%0/2
$limit and $type ne "insert"
$limit $type ne "insert"
Truth table0%0/3
A and B
A= $limitB= $type ne "insert"
ABresult
0X0
100
111
2003 0 return( \@query );
2004 }
2005  
2006 sub _query_type
2007 {
2008 0 ▶ my $self = shift( @_ );
Subroutine _query_type: not called CC=3 SCAR=24.8
2009 0 ▶ if( $self->{query} && length( $self->{query} ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"query"} and length $self->{"query"}) 0 0 0
Condition0%0/3
$self->{"query"} and length $self->{"query"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"query"} and length $self->{"query"}
$self->{"query"} length $self->{"query"}
Truth table0%0/3
A and B
A= $self->{"query"}B= length $self->{"query"}
ABresult
0X0
100
111
2010 {
2011 0 return( lc( ( $self->{query} =~ /^[[:blank:]]*(ALTER|CREATE|DROP|GRANT|LISTEN|NOTIFY|INSERT|UPDATE|DELETE|SELECT|TRUNCATE)\b/i )[0] ) )
2012 }
2013 0 return;
2014 }
2015  
2016 sub _save_bind
2017 {
2018 5 ▶ my $self = shift( @_ );
Subroutine _save_bind: called CC=12 SCAR=38.7
2019 5 my $type = shift( @_ );
2020 5 ▶ if( !$type )
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 5 0 5
2021 {
2022 5 my( $pkg, $file, $line, $sub ) = caller( 1 );
2023 5 $sub =~ s/(.*):://;
2024 5 $type = $sub;
2025 }
2026 5 ▶ my $tbl_o = $self->table_object || return( $self->error( "No table object is set." ) );
Condition50%1/2
$self->table_object || (return $self->error('No table object is set.'))
l!l
50
MC/DC0%0/1
$self->table_object || (return $self->error('No table object is set.'))
$self->table_object
Truth table50%1/2
A
A= $self->table_object
Aresult
11
00
2027 5 my $bind = $tbl_o->use_bind;
2028 5 my $where = $self->where;
2029 5 my $group = $self->group;
2030 5 my $order = $self->order;
2031 5 my $limit = $self->limit;
2032 # This is used so upon execute, the saved binded parameters get sent to the DBI::execute method
2033 5 ▶ if( $bind )
Branch: 1 branch
BranchTrueFalseTotal
if ($bind) 0 5 5
2034 {
2035 # Build a fresh Elements object and assign it directly, rather than calling
2036 # $self->elements (which may return a copy) and pushing onto it. This ensures
2037 # the stored reference is always up-to-date and never accumulates across calls.
2038 0 my $elems = $self->new_elements;
2039 0 ▶ $elems->push( $where->elements ) if( $where->length && !$where->elements->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if $where->length and not $where->elements->is_empty 0 0 0
Condition0%0/3
$where->length and not $where->elements->is_empty
!ll&&!rl&&r
000
MC/DC0%0/2
$where->length and not $where->elements->is_empty
$where->length not $where->elements->is_empty
Truth table0%0/3
A and not(B)
A= $where->lengthB= not $where->elements->is_empty
ABresult
0X0
100
111
2040 0 ▶ $elems->push( $group->elements ) if( $group->length && !$group->elements->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if $group->length and not $group->elements->is_empty 0 0 0
Condition0%0/3
$group->length and not $group->elements->is_empty
!ll&&!rl&&r
000
MC/DC0%0/2
$group->length and not $group->elements->is_empty
$group->length not $group->elements->is_empty
Truth table0%0/3
A and not(B)
A= $group->lengthB= not $group->elements->is_empty
ABresult
0X0
100
111
2041 0 ▶ $elems->push( $order->elements ) if( $order->length && !$order->elements->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if $order->length and not $order->elements->is_empty 0 0 0
Condition0%0/3
$order->length and not $order->elements->is_empty
!ll&&!rl&&r
000
MC/DC0%0/2
$order->length and not $order->elements->is_empty
$order->length not $order->elements->is_empty
Truth table0%0/3
A and not(B)
A= $order->lengthB= not $order->elements->is_empty
ABresult
0X0
100
111
2042 0 ▶ $elems->push( $limit->elements ) if( $limit->length && !$limit->elements->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if $limit->length and not $limit->elements->is_empty 0 0 0
Condition0%0/3
$limit->length and not $limit->elements->is_empty
!ll&&!rl&&r
000
MC/DC0%0/2
$limit->length and not $limit->elements->is_empty
$limit->length not $limit->elements->is_empty
Truth table0%0/3
A and not(B)
A= $limit->lengthB= not $limit->elements->is_empty
ABresult
0X0
100
111
2043 0 $self->{elements} = $elems;
2044  
2045 }
2046 5 return( $self );
2047 }
2048  
2049 sub _value2bind
2050 {
2051 0 ▶ my $self = shift( @_ );
Subroutine _value2bind: not called CC=12 SCAR=50.5
2052 # If we are not suppose to bind any values, there is no point to go on.
2053 # return(1) if( !$self->{ 'bind' } );
2054 0 my $str = shift( @_ );
2055 0 my $ref = shift( @_ );
2056 0 ▶ my $tbl_o = $self->{table_object} || return( $self->error( "No table object is set." ) );
Condition0%0/2
$self->{'table_object'} || (return $self->error('No table object is set.'))
l!l
00
MC/DC0%0/1
$self->{'table_object'} || (return $self->error('No table object is set.'))
$self->{'table_object'}
Truth table0%0/2
A
A= $self->{'table_object'}
Aresult
11
00
2057 0 ▶ my $dbo = $tbl_o->database_object ||
Condition0%0/2
$tbl_o->database_object || (return $self->error(q[No database object is currently set on this table object for table '], $tbl_o->name, q[']))
l!l
00
MC/DC0%0/1
$tbl_o->database_object || (return $self->error(q[No database object is currently set on this table object for table '], $tbl_o->name, q[']))
$tbl_o->database_object
Truth table0%0/2
A
A= $tbl_o->database_object
Aresult
11
00
2058 return( $self->error( "No database object is currently set on this table object for table '", $tbl_o->name, "'" ) );
2059 0 my $table = $tbl_o->name;
2060 0 my $bind = $tbl_o->use_bind;
2061 0 my $db = $tbl_o->database;
2062 0 my $prefix = $tbl_o->prefix;
2063 0 my $fields_ref = $tbl_o->fields;
2064 0 my $fields = CORE::join( '|', keys( %$fields_ref ) );
2065 0 my $tables = CORE::join( '|', @{$dbo->tables} );
2066 0 my $multi_db = $tbl_o->param( 'multi_db' );
2067 0 my @binded = ();
2068 0 my $placeholder_re = $dbo->_placeholder_regexp;
2069 0 my $elems = $self->new_elements;
2070 0 $$str =~ s
2071 {
2072 (([\w\_]+)(?:\.))?\b([a-zA-Z\_]+)\b\s*(=|\!=|LIKE)\s*['"]([^'"]+)['"]
2073 }
2074 {
2075 do
2076 0 {
2077 0 my( $this_table, $field, $equity, $value ) = ( $2, $3, $4, $5 );
2078 # Add to the list of value to bind on execute() only if this is not already a place holder
2079 # push( @binded, $value ) if( $bind && $value ne '?' );
2080 0 ▶ $this_table ||= $table;
Condition0%0/3
$this_table ||= $table
l!l&&r!l&&!r
000
MC/DC0%0/2
$this_table ||= $table
$this_table $table
Truth table0%0/3
A ||= B
A= $this_tableB= $table
ABresult
1X1
011
000
2081 0 $this_table .= '.';
2082 # $bind ? "${this_table}${field}=?" : "${this_table}${field}='$value'";
2083 0 my $elem = $self->new_element( field => "${this_table}${field}" );
2084 0 my $result;
2085 0 ▶ if( $value !~ /[\r\n]+/ &&
Branch: 2 branches
BranchTrueFalseTotal
if (not $value =~ /[\r\n]+/ and $value =~ /\b(?:$fields)\b/ || $value =~ /\w\([^\)]*\)/ || $value =~ /$placeholder_re/) { } 0 0 0
elsif ($bind) { } 0 0 0
Condition0%0/9
not $value =~ /[\r\n]+/ and $value =~ /\b(?:$fields)\b/ || $value =~ /\w\([^\)]*\)/ || $value =~ /$placeholder_re/
!ll&&!rl&&r
000
$value =~ /\b(?:$fields)\b/ or $value =~ /\w\([^\)]*\)/ or $value =~ /$placeholder_re/
l!l&&r!l&&!r
000
$value =~ /\b(?:$fields)\b/ or $value =~ /\w\([^\)]*\)/
l!l&&r!l&&!r
000
MC/DC0%0/4
not $value =~ /[\r\n]+/ and $value =~ /\b(?:$fields)\b/ || $value =~ /\w\([^\)]*\)/ || $value =~ /$placeholder_re/
not $value =~ /[\r\n]+/ $value =~ /\b(?:$fields)\b/ $value =~ /\w\([^\)]*\)/ $value =~ /$placeholder_re/
Truth table0%0/5
not(A) and B or C or D
A= not $value =~ /[\r\n]+/B= $value =~ /\b(?:$fields)\b/C= $value =~ /\w\([^\)]*\)/D= $value =~ /$placeholder_re/
ABCDresult
0XXX0
10000
11XX1
101X1
10011
2086 ( $value =~ /\b(?:$fields)\b/ ||
2087 $value =~ /\w\([^\)]*\)/ ||
2088 $value =~ /$placeholder_re/ ) )
2089 {
2090 0 ▶ if( $value =~ /($placeholder_re)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /($placeholder_re)/) 0 0 0
2091 {
2092 0 $elem->placeholder( $1 );
2093 0 $elem->format( $value );
2094 }
2095 0 $result = "${this_table}${field} $equity $value";
2096 0 $elems->push( $elem );
2097 }
2098 elsif( $bind )
2099 {
2100 # push( @binded, $value );
2101 0 $result = "${this_table}${field} $equity ?";
2102 0 $elem->placeholder( '?' );
2103 0 $elem->value( $value );
2104 0 $elems->push( $elem );
2105 }
2106 else
2107 {
2108 0 $result = "${this_table}${field} $equity '$value'";
2109 }
2110 0 $result;
2111 };
2112 }geix;
2113 0 ▶ $$str =~ s
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 0 0
2114 {
2115 (?<![\.\"])\b($fields)\b(\s*)?(?!\.)
2116 }
2117 {
2118 0 my( $ok, $spc ) = ( $1, $2 );
2119 0 "$prefix.$ok$spc";
2120 }gex if( $prefix );
2121 0 ▶ $$str =~ s/(?<!\.)($tables)(?:\.)/$db\.$1\./g if( $multi_db );
Branch: 1 branch
BranchTrueFalseTotal
if $multi_db 0 0 0
2122 # push( @$ref, @binded ) if( @binded );
2123 # return(1);
2124 0 return( $elems );
2125 }
2126  
2127 sub _where_having
2128 {
2129 20 ▶ my $self = shift( @_ );
Subroutine _where_having: called CC=9 SCAR=31.6
2130 # This is the type, ie 'group', 'order' and used to initiate the DB::Object::Query::Clause
2131 20 ▶ my $type = shift( @_ ) || return( $self->error( "No clause type was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No clause type was provided.'))
l!l
200
MC/DC0%0/1
shift @_ || (return $self->error('No clause type was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
2132 # This is used to store the data in $self such as $self->{ $prop } = $clause;
2133 20 ▶ my $prop = shift( @_ ) || return( $self->error( "No object data property name was provided for clause type '$type'." ) );
Condition50%1/2
shift @_ || (return $self->error("No object data property name was provided for clause type '${type}'."))
l!l
200
MC/DC0%0/1
shift @_ || (return $self->error("No object data property name was provided for clause type '${type}'."))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
2134 20 ▶ my $tbl_o = $self->table_object || return( $self->error( "No table object is set." ) );
Condition50%1/2
$self->table_object || (return $self->error('No table object is set.'))
l!l
200
MC/DC0%0/1
$self->table_object || (return $self->error('No table object is set.'))
$self->table_object
Truth table50%1/2
A
A= $self->table_object
Aresult
11
00
2135 20 ▶ my $dbo = $tbl_o->database_object ||
Condition50%1/2
$tbl_o->database_object || (return $self->error(q[No database object is currently set on this table object for table '], $tbl_o->name, q[']))
l!l
200
MC/DC0%0/1
$tbl_o->database_object || (return $self->error(q[No database object is currently set on this table object for table '], $tbl_o->name, q[']))
$tbl_o->database_object
Truth table50%1/2
A
A= $tbl_o->database_object
Aresult
11
00
2136 return( $self->error( "No database object is currently set on this table object for table '", $tbl_o->name, "'" ) );
2137 20 my $base_class = $tbl_o->base_class;
2138 20 my $bind = $tbl_o->use_bind;
2139 # $self->{ $prop } = $self->new_clause if( !CORE::length( $self->{ $prop } ) || !$self->_is_object( $self->{ $prop } ) );
2140 # my $where = $self->{ $prop };
2141 20 my $where;
2142 20 my $placeholder_re = $dbo->_placeholder_regexp;
2143 20 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 5 15 20
2144 {
2145 5 my @params = @_;
2146 # This will change the belonging of the object $self to the class DB::Object::Prepare so method
2147 # such as select, insert, update, delete know there are some conditionning clause to be added
2148 5 my $table = $tbl_o->name;
2149 5 my $db = $tbl_o->database;
2150 5 my $multi_db = $tbl_o->prefix_database;
2151 5 my $prefix = $tbl_o->prefix;
2152 5 my $fields_ref = $tbl_o->fields;
2153 5 my $fields = CORE::join( '|', keys( %$fields_ref ) );
2154 5 my $fields_type = $tbl_o->types;
2155  
2156 5 my $process_where_condition;
2157 $process_where_condition = sub
2158 {
2159 # my @parameters = @_;
2160 # my $data = shift( @_ ) if( @_ % 2 && !( scalar( @_ ) == 1 && $self->_is_object( $_[0] ) ) );
2161 5 ▶ my $data;
Subroutine __ANON__: called CC=75 SCAR=72.7
2162 5 ▶ unless( scalar( @_ ) == 1 && $self->_is_object( $_[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @_ == 1 and $self->_is_object($_[0])) 5 0 5
Condition33%1/3
scalar @_ == 1 and $self->_is_object($_[0])
!ll&&!rl&&r
500
MC/DC0%0/2
scalar @_ == 1 and $self->_is_object($_[0])
scalar @_ == 1 $self->_is_object($_[0])
Truth table33%1/3
A and B
A= scalar @_ == 1B= $self->_is_object($_[0])
ABresult
0X0
100
111
2163 {
2164 5 ▶ $data = shift( @_ ) if( ( @_ % 2 ) && !$self->_is_object( $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 and not $self->_is_object($_[0]) 0 5 5
Condition33%1/3
@_ % 2 and not $self->_is_object($_[0])
!ll&&!rl&&r
500
MC/DC0%0/2
@_ % 2 and not $self->_is_object($_[0])
@_ % 2 not $self->_is_object($_[0])
Truth table33%1/3
A and not(B)
A= @_ % 2B= not $self->_is_object($_[0])
ABresult
0X0
100
111
2165 }
2166 # Implicit AND operator by default to join multiple fields
2167 5 my $agg_op = 'AND';
2168 5 my @arg = ();
2169 5 ▶ if( $self->_is_a( $_[0], 'DB::Object::Operator' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($_[0], 'DB::Object::Operator')) { } 0 5 5
2170 {
2171 0 ▶ return( $self->error( "I was expecting an operator object, but got \"", $_[0], "\" instead." ) ) if( !$_[0]->isa( 'DB::Object::Operator' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->isa("DB::Object::Operator") 0 0 0
2172 0 ▶ $agg_op = $_[0]->operator || return( $self->error( "Unknown operator for \"", $_[0], "\"." ) );
Condition0%0/2
$_[0]->operator || (return $self->error('Unknown operator for "', $_[0], '".'))
l!l
00
MC/DC0%0/1
$_[0]->operator || (return $self->error('Unknown operator for "', $_[0], '".'))
$_[0]->operator
Truth table0%0/2
A
A= $_[0]->operator
Aresult
11
00
2173 # We filter out any unknown field
2174 0 ( @arg ) = grep( !$self->_is_a( $_ => 'DB::Object::Fields::Unknown' ), $_[0]->value );
2175 }
2176 else
2177 {
2178 5 @arg = @_;
2179 }
2180 5 ▶ $data = \@arg if( @arg );
Branch: 1 branch
BranchTrueFalseTotal
if @arg 5 0 5
2181 5 my $str = '';
2182 5 my @binded = ();
2183 5 my @types = ();
2184 5 my $clause;
2185 # A simple scalar
2186 5 ▶ if( ref( $data ) eq 'SCALAR' )
Branch: 3 branches
BranchTrueFalseTotal
if (ref $data eq 'SCALAR') { } 0 5 5
elsif (ref $data) { } 5 0 5
elsif ($data) { } 0 0 0
2187 {
2188 0 $str = $$data;
2189 }
2190 elsif( ref( $data ) )
2191 {
2192 5 my @list = ();
2193 5 my( $field, $value );
2194 5 ▶ while( @arg )
Branch: 1 branch
BranchTrueFalseTotal
if (@arg) 5 5 10
2195 {
2196 5 ▶ if( $self->_is_object( $arg[0] ) && $arg[0]->isa( 'DB::Object::Operator' ) )
Branch: 8 branches
BranchTrueFalseTotal
if ($self->_is_object($arg[0]) and $arg[0]->isa('DB::Object::Operator')) { } 0 5 5
elsif ($self->_is_object($arg[0]) and $arg[0]->isa('DB::Object::Query::Clause')) { } 0 5 5
elsif ($self->_is_a($arg[0], 'DB::Object::Expression')) { } 0 5 5
elsif ($self->_is_object($arg[0]) and $arg[0]->isa('DB::Object::Fields::Overloaded')) { } 0 5 5
elsif ($self->_is_a($arg[0], 'DB::Object::Fields::Unknown')) { } 0 5 5
elsif (scalar(@arg) % 2 and not ref $arg[0]) { } 0 5 5
elsif (scalar(@arg) % 2 and ref $arg[0] eq 'SCALAR') { } 0 5 5
elsif ($self->_is_a($arg[0], 'DB::Object::Fields::Field') and scalar @arg == 1) { } 0 10 10
Condition33%1/3
$self->_is_object($arg[0]) and $arg[0]->isa("DB::Object::Operator")
!ll&&!rl&&r
500
MC/DC0%0/2
$self->_is_object($arg[0]) and $arg[0]->isa("DB::Object::Operator")
$self->_is_object($arg[0]) $arg[0]->isa("DB::Object::Operator")
Truth table33%1/3
A and B
A= $self->_is_object($arg[0])B= $arg[0]->isa("DB::Object::Operator")
ABresult
0X0
100
111
2197 {
2198 0 my $op_object = shift( @arg );
2199 0 $clause = $process_where_condition->( $op_object );
2200 0 push( @list, $clause );
2201 0 next;
2202 }
2203 # This is an already formulated clause
2204 ▶ elsif( $self->_is_object( $arg[0] ) && $arg[0]->isa( 'DB::Object::Query::Clause' ) )
Condition33%1/3
$self->_is_object($arg[0]) and $arg[0]->isa("DB::Object::Query::Clause")
!ll&&!rl&&r
500
MC/DC0%0/2
$self->_is_object($arg[0]) and $arg[0]->isa("DB::Object::Query::Clause")
$self->_is_object($arg[0]) $arg[0]->isa("DB::Object::Query::Clause")
Truth table33%1/3
A and B
A= $self->_is_object($arg[0])B= $arg[0]->isa("DB::Object::Query::Clause")
ABresult
0X0
100
111
2205 {
2206 0 push( @list, shift( @arg ) );
2207 0 next;
2208 }
2209 # An expression
2210 elsif( $self->_is_a( $arg[0] => 'DB::Object::Expression' ) )
2211 {
2212 0 push( @list, shift( @arg ) );
2213 0 next;
2214 }
2215 ▶ elsif( $self->_is_object( $arg[0] ) && $arg[0]->isa( 'DB::Object::Fields::Overloaded' ) )
Condition33%1/3
$self->_is_object($arg[0]) and $arg[0]->isa("DB::Object::Fields::Overloaded")
!ll&&!rl&&r
500
MC/DC0%0/2
$self->_is_object($arg[0]) and $arg[0]->isa("DB::Object::Fields::Overloaded")
$self->_is_object($arg[0]) $arg[0]->isa("DB::Object::Fields::Overloaded")
Truth table33%1/3
A and B
A= $self->_is_object($arg[0])B= $arg[0]->isa("DB::Object::Fields::Overloaded")
ABresult
0X0
100
111
2216 {
2217 0 my $f = shift( @arg );
2218 0 my $cl = $self->new_clause(
2219 value => $f,
2220 type => 'where',
2221 );
2222 # $cl->bind->types->push( '' ) if( $f->binded );
2223 0 ▶ if( $f->placeholder )
Branch: 1 branch
BranchTrueFalseTotal
if ($f->placeholder) 0 0 0
2224 {
2225 0 my $const = $f->field->datatype->constant;
2226 0 my $offset = $f->index;
2227 # $cl->push({
2228 # field => $f->field,
2229 # # Use the offset expressly provided (e.g. $1 (PostgreSQL) or ?1 (SQLite), or the last position in the array
2230 # ( defined( $offset ) ? ( index => $offset ) : () ),
2231 # ( $const ? ( type => $const ) : () ),
2232 # }) || return( $self->pass_error( $cl->error ) );
2233 # Even better than instantiating a new DB::Object::Element object, we just use as-is the DB::Object::Fields::Overloaded object, which inherits from DB::Object::Element :)
2234 0 $cl->push( $f );
2235  
2236 # if( $const )
2237 # {
2238 # $cl->bind->types->push( $const );
2239 # }
2240 # else
2241 # {
2242 # $cl->bind->types->push( '' );
2243 # }
2244 }
2245 0 push( @list, $cl );
2246  
2247 # If this field value assignment is followed (as a pair) by just a regular field, this is likely a typo.
2248 # Catching some typical typo errors for the benefit of the coder (from experience)
2249 0 ▶ if( scalar( @arg ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arg and $self->_is_a($arg[0], "DB::Object::Fields::Field")) 0 0 0
Condition0%0/3
scalar @arg and $self->_is_a($arg[0], "DB::Object::Fields::Field")
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @arg and $self->_is_a($arg[0], "DB::Object::Fields::Field")
scalar @arg $self->_is_a($arg[0], "DB::Object::Fields::Field")
Truth table0%0/3
A and B
A= scalar @argB= $self->_is_a($arg[0], "DB::Object::Fields::Field")
ABresult
0X0
100
111
2250 $self->_is_a( $arg[0], 'DB::Object::Fields::Field' ) )
2251 {
2252 0 warn( "Warning only: found a (proper) field value assignment ($f) followed by a field object '$arg[0]' (never mind the surrounding quotes) (", overload::StrVal( $arg[0] ), "). Did you forget to assign a value such as \$tbl->fo->$arg[0] == 'something' ?\n" );
2253 }
2254 0 next;
2255 }
2256 # Ignore it
2257 elsif( $self->_is_a( $arg[0] => 'DB::Object::Fields::Unknown' ) )
2258 {
2259 # warn( "Found an unknown field object DB::Object::Fields::Unknown '", $arg[0]->field, "' of table '", $arg[0]->table, "' in WHERE or HAVING clause: ", $arg[0]->error ) if( $self->_is_warnings_enabled( 'DB::Object' ) );
2260 # shift( @arg );
2261 # next;
2262 0 return( $self->error( "Found an unknown field object DB::Object::Fields::Unknown '", $arg[0]->field, "' of table '", $arg[0]->table, "' in WHERE or HAVING clause: ", $arg[0]->error ) );
2263 }
2264 # Case where there is a litteral query component, e.g. "LENGTH(lang) = 2" and the number of arguments is odd which means there is no second argument such as: ->where( "LENGTH(lang) = 2", $tbl->fo->user_id => "something );
2265 ▶ elsif( ( scalar( @arg ) % 2 ) && !ref( $arg[0] ) )
Condition33%1/3
scalar(@arg) % 2 and not ref $arg[0]
!ll&&!rl&&r
500
MC/DC0%0/2
scalar(@arg) % 2 and not ref $arg[0]
scalar(@arg) % 2 not ref $arg[0]
Truth table33%1/3
A and not(B)
A= scalar(@arg) % 2B= not ref $arg[0]
ABresult
0X0
100
111
2266 {
2267 0 push( @list, $self->new_clause( value => shift( @arg ), type => 'where' ) );
2268 0 next;
2269 }
2270 ▶ elsif( ( scalar( @arg ) % 2 ) && ref( $arg[0] ) eq 'SCALAR' )
Condition33%1/3
scalar(@arg) % 2 and ref $arg[0] eq "SCALAR"
!ll&&!rl&&r
500
MC/DC0%0/2
scalar(@arg) % 2 and ref $arg[0] eq "SCALAR"
scalar(@arg) % 2 ref $arg[0] eq "SCALAR"
Truth table33%1/3
A and B
A= scalar(@arg) % 2B= ref $arg[0] eq "SCALAR"
ABresult
0X0
100
111
2271 {
2272 0 my $scalar = shift( @arg );
2273 0 push( @list, $self->new_clause( value => $$scalar, type => 'where' ) );
2274 0 next;
2275 }
2276 # Catching some typical typo errors for the benefit of the coder (from experience)
2277 # The coder provided a field object without associated value and there are no other argument passed to the where clause. He/she probably forget the assignment like $tbl->fo->field == 'something'
2278 ▶ elsif( $self->_is_a( $arg[0], 'DB::Object::Fields::Field' ) && scalar( @arg ) == 1 )
Condition33%1/3
$self->_is_a($arg[0], "DB::Object::Fields::Field") and scalar @arg == 1
!ll&&!rl&&r
500
MC/DC0%0/2
$self->_is_a($arg[0], "DB::Object::Fields::Field") and scalar @arg == 1
$self->_is_a($arg[0], "DB::Object::Fields::Field") scalar @arg == 1
Truth table33%1/3
A and B
A= $self->_is_a($arg[0], "DB::Object::Fields::Field")B= scalar @arg == 1
ABresult
0X0
100
111
2279 {
2280 0 warn( "Warning only: found a field object '$arg[0]' (never mind the surrounding quotes) (", overload::StrVal( $arg[0] ), ") followed by no other argument. Did you forget to assign a value such as \$tbl->fo->$arg[0] == 'something' ?\n" );
2281 }
2282  
2283 5 my( $field, $value ) = splice( @arg, 0, 2 );
2284 # Catching some typical typo errors for the benefit of the coder (from experience)
2285 5 ▶ if( $self->_is_a( $field, 'DB::Object::Fields::Field' ) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($field, "DB::Object::Fields::Field") and $self->_is_a($value, "DB::Object::Fields::Overloaded")) 0 5 5
Condition33%1/3
$self->_is_a($field, "DB::Object::Fields::Field") and $self->_is_a($value, "DB::Object::Fields::Overloaded")
!ll&&!rl&&r
500
MC/DC0%0/2
$self->_is_a($field, "DB::Object::Fields::Field") and $self->_is_a($value, "DB::Object::Fields::Overloaded")
$self->_is_a($field, "DB::Object::Fields::Field") $self->_is_a($value, "DB::Object::Fields::Overloaded")
Truth table33%1/3
A and B
A= $self->_is_a($field, "DB::Object::Fields::Field")B= $self->_is_a($value, "DB::Object::Fields::Overloaded")
ABresult
0X0
100
111
2286 $self->_is_a( $value, 'DB::Object::Fields::Overloaded' ) )
2287 {
2288 0 warn( "Warning only: found a field object '$field' (never mind the surrounding quotes) (", overload::StrVal( $field ), ") followed by an another (proper) field value assignment ($value). Did you forget to assign a value such as \$tbl->fo->$field == 'something' ?\n" );
2289 }
2290  
2291 5 ▶ unless( $self->_is_a( $field => 'DB::Object::Fields::Field' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($field, "DB::Object::Fields::Field")) 5 0 5
2292 {
2293 5 ▶ $field =~ s/\b(?<![\.\"])($fields)\b/$prefix.$1/gs if( $prefix );
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 5 5
2294 }
2295 5 my $i_am_negative = 0;
2296 5 ▶ if( $self->_is_a( $value, 'DB::Object::NOT' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_a($value, 'DB::Object::NOT')) { } 0 5 5
elsif (not defined $value or lc $value eq 'null') { } 0 5 5
2297 {
2298 0 ( $value ) = $value->value;
2299 0 ▶ $value = $self->database_object->NULL if( !defined( $value ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
2300 # https://www.postgresql.org/docs/8.3/functions-comparison.html
2301 0 ▶ if( lc( $value ) eq 'null' )
Branch: 1 branch
BranchTrueFalseTotal
if (lc $value eq "null") 0 0 0
2302 {
2303 # If e do not first copy the value to a separate variable, we would end up with a circular reference (type REF)
2304 0 push( @list, $self->new_clause({
2305 value => "$field IS NOT NULL",
2306 type => 'where',
2307 })
2308 );
2309 0 next;
2310 }
2311 0 $i_am_negative++;
2312 }
2313 # When value is undef() or explicitly set to NULL, we need to write this as IS NULL to be sql compliant
2314 ▶ elsif( !defined( $value ) || lc( $value ) eq 'null' )
Condition33%1/3
not defined $value or lc $value eq "null"
l!l&&r!l&&!r
005
MC/DC0%0/2
not defined $value or lc $value eq "null"
not defined $value lc $value eq "null"
Truth table33%1/3
not(A) or B
A= not defined $valueB= lc $value eq "null"
ABresult
1X1
011
000
2315 {
2316 0 push( @list, $self->new_clause({
2317 value => "$field IS NULL",
2318 type => 'where',
2319 })
2320 );
2321 0 next;
2322 }
2323  
2324 5 my $f;
2325 5 ▶ if( $self->_is_a( $field => 'DB::Object::Fields::Field' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($field, 'DB::Object::Fields::Field')) { } 0 5 5
2326 {
2327 0 $f = '%s';
2328 }
2329 else
2330 {
2331 5 ▶ $f = $prefix ? "$prefix.$field" : $field;
Branch: 1 branch
BranchTrueFalseTotal
$prefix ? : 0 5 5
2332 }
2333  
2334 5 ▶ if( ref( $value ) eq 'SCALAR' )
Branch: 6 branches
BranchTrueFalseTotal
if (ref $value eq 'SCALAR') { } 0 5 5
elsif (ref $value eq 'HASH') { } 0 5 5
elsif (ref $value eq "${base_class}::Statement") { } 0 5 5
elsif (ref $value eq 'Regexp') { } 2 3 5
elsif ($value =~ /($placeholder_re)/) { } 0 3 3
elsif ($value =~ /[\s\(\)\.\'\"]+(?:$fields)[\s\(\)\.\'\"]+/ or $value =~ /\w\([^\)]*\)/) { } 0 3 3
2335 {
2336 0 ▶ push( @list, $self->new_clause({
Branch: 1 branch
BranchTrueFalseTotal
$i_am_negative ? : 0 0 0
2337 value => $i_am_negative ? "$field != $$value" : "$field = $$value",
2338 type => 'where'
2339 })
2340 );
2341 }
2342 elsif( ref( $value ) eq 'HASH' )
2343 {
2344 0 foreach my $json_key ( keys( %$value ) )
2345 {
2346 0 my $json_value = $value->{ $json_key };
2347 # Assuming default "=" operator for now
2348 0 my $clause = $self->new_clause(
2349 value => "${field}->>'${json_key}' = '${json_value}'",
2350 generic => "${field}->>'${json_key}' = ?",
2351 type => 'where',
2352 );
2353 # Bind the value if it is not a DB::Object reference
2354 # if( !$self->_is_object( $json_value ) )
2355 # {
2356 # $clause->push({ value => $json_value });
2357 # }
2358 0 $clause->push({ value => $json_value });
2359 0 push( @list, $clause );
2360 }
2361 }
2362 # If this is a sub-select - i.e.
2363 # SELECT article, dealer, price
2364 # FROM shop
2365 # WHERE price=(SELECT MAX(price) FROM shop)
2366 # By default we get the value and use it in our clause, but sub classes like DB::Object::Postgres::Query would use the statement as is to form a native sub-query
2367 elsif( ref( $value ) eq "${base_class}::Statement" )
2368 {
2369 0 my $res = $value->fetchrow();
2370 0 ▶ my $cl = $self->new_clause({
Branch: 2 branches
BranchTrueFalseTotal
$i_am_negative ? : 0 0 0
$i_am_negative ? : 0 0 0
2371 value => $i_am_negative ? "$f != '$res'" : "$f = '$res'",
2372 generic => $i_am_negative ? "$f != ?" : "$f = ?",
2373 type => 'where',
2374 });
2375 # $cl->bind->values( $res );
2376 # $cl->bind->types( '' );
2377 # $cl->fields( $field ) if( $self->_is_a( $field => 'DB::Object::Fields::Field' ) );
2378 0 $cl->push( $value->query_object->elements );
2379 0 push( @list, $cl );
2380 }
2381 elsif( ref( $value ) eq 'Regexp' )
2382 {
2383 # (?^:^want-(.*?)) => ^want-(.*?)
2384 2 ▶ if( $value =~ s/^\(\?\^\:// )
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ s/^\(\?\^\://) 2 0 2
2385 {
2386 2 $value =~ s/\)$//;
2387 }
2388 2 my $cl;
2389 2 ▶ if( $self->database_object->driver eq 'Pg' )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->database_object->driver eq 'Pg') { } 0 2 2
elsif ($self->database_object->driver eq 'SQLite' or $self->database_object->driver eq 'mysql') { } 2 0 2
2390 {
2391 0 ▶ $cl = $self->new_clause({
Branch: 2 branches
BranchTrueFalseTotal
$i_am_negative ? : 0 0 0
$i_am_negative ? : 0 0 0
2392 value => $i_am_negative ? "$f !~ '$value'" : "$f ~ '$value'",
2393 generic => $i_am_negative ? "$f !~ ?" : "$f ~ ?",
2394 type => 'where',
2395 });
2396 }
2397 ▶ elsif( $self->database_object->driver eq 'SQLite' ||
Condition33%1/3
$self->database_object->driver eq "SQLite" or $self->database_object->driver eq "mysql"
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->database_object->driver eq "SQLite" or $self->database_object->driver eq "mysql"
$self->database_object->driver eq "SQLite" $self->database_object->driver eq "mysql"
Truth table33%1/3
A or B
A= $self->database_object->driver eq "SQLite"B= $self->database_object->driver eq "mysql"
ABresult
1X1
011
000
2398 $self->database_object->driver eq 'mysql' )
2399 {
2400 2 ▶ $cl = $self->new_clause({
Branch: 2 branches
BranchTrueFalseTotal
$i_am_negative ? : 0 2 2
$i_am_negative ? : 0 2 2
2401 value => $i_am_negative ? "$f NOT REGEXP('$value')" : "$f REGEXP('$value')",
2402 generic => $i_am_negative ? "$f NOT REGEXP(?)" : "$f REGEXP(?)",
2403 type => 'where',
2404 });
2405 }
2406 # $cl->bind->values( $value );
2407 # $cl->bind->types( '' );
2408 $cl->push({
2409 2 ▶ ( $self->_is_a( $field => 'DB::Object::Fields::Field' ) ? ( field => $field ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_a($field, 'DB::Object::Fields::Field') ? : 0 2 2
2410 value => $value,
2411 });
2412 # $cl->fields( $field ) if( $self->_is_a( $field => 'DB::Object::Fields::Field' ) );
2413 2 push( @list, $cl );
2414 }
2415 elsif( $value =~ /($placeholder_re)/ )
2416 {
2417 0 my $plh = $1;
2418 0 ▶ my $cl = $self->new_clause(
Branch: 1 branch
BranchTrueFalseTotal
$i_am_negative ? : 0 0 0
2419 value => $i_am_negative ? "$f != $value" : "$f = $value",
2420 type => 'where',
2421 );
2422 0 my $el = $self->new_element(
2423 field => $field,
2424 placeholder => $plh,
2425 format => $value,
2426 );
2427 0 $cl->push( $el );
2428 0 push( @list, $cl );
2429 }
2430 ▶ elsif( $value =~ /[\s\(\)\.\'\"]+(?:$fields)[\s\(\)\.\'\"]+/ ||
Condition33%1/3
$value =~ /[\s\(\)\.\'\"]+(?:$fields)[\s\(\)\.\'\"]+/ or $value =~ /\w\([^\)]*\)/
l!l&&r!l&&!r
003
MC/DC0%0/2
$value =~ /[\s\(\)\.\'\"]+(?:$fields)[\s\(\)\.\'\"]+/ or $value =~ /\w\([^\)]*\)/
$value =~ /[\s\(\)\.\'\"]+(?:$fields)[\s\(\)\.\'\"]+/ $value =~ /\w\([^\)]*\)/
Truth table33%1/3
A or B
A= $value =~ /[\s\(\)\.\'\"]+(?:$fields)[\s\(\)\.\'\"]+/B= $value =~ /\w\([^\)]*\)/
ABresult
1X1
011
000
2431 $value =~ /\w\([^\)]*\)/ )
2432 {
2433 # Nothing fancy, as is. Even with binding option on, it will still return the clause without placeholder, because we don't know what $value is
2434 0 ▶ my $cl = $self->new_clause(
Branch: 1 branch
BranchTrueFalseTotal
$i_am_negative ? : 0 0 0
2435 value => $i_am_negative ? "$f != $value" : "$f = $value",
2436 type => 'where',
2437 );
2438 # $cl->bind->types( '' ) if( $value =~ /^$placeholder_re$/ );
2439 # $cl->fields( $field ) if( $self->_is_a( $field => 'DB::Object::Fields::Field' ) );
2440 0 ▶ if( $self->_is_a( $field => 'DB::Object::Fields::Field' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($field, "DB::Object::Fields::Field")) 0 0 0
2441 {
2442 0 my $el = $self->new_element(
2443 field => $field,
2444 );
2445 0 $cl->push( $el );
2446 }
2447 0 push( @list, $cl );
2448 }
2449 else
2450 {
2451 3 my( $cl, $const, $el );
2452 3 ▶ if( lc( $fields_type->{ $field } ) eq 'bytea' && ( $const = $self->database_object->get_sql_type( 'bytea' ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (lc $fields_type->{$field} eq 'bytea' and $const = $self->database_object->get_sql_type('bytea')) { } 0 3 3
Condition33%1/3
lc $fields_type->{$field} eq "bytea" and $const = $self->database_object->get_sql_type("bytea")
!ll&&!rl&&r
300
MC/DC0%0/2
lc $fields_type->{$field} eq "bytea" and $const = $self->database_object->get_sql_type("bytea")
lc $fields_type->{$field} eq "bytea" $const = $self->database_object->get_sql_type("bytea")
Truth table33%1/3
A and B
A= lc $fields_type->{$field} eq "bytea"B= $const = $self->database_object->get_sql_type("bytea")
ABresult
0X0
100
111
2453 {
2454 0 ▶ $cl = $self->new_clause(
Branch: 1 branch
BranchTrueFalseTotal
$i_am_negative ? : 0 0 0
2455 value => "$f" . ( $i_am_negative ? '!=' : '=' ) . $dbo->quote( $value, $const ),
2456 type => 'where',
2457 );
2458 0 $el = $self->new_element;
2459 }
2460 else
2461 {
2462 3 ▶ $cl = $self->new_clause(
Branch: 2 branches
BranchTrueFalseTotal
$i_am_negative ? : 0 3 3
$i_am_negative ? : 0 3 3
2463 value => "$f" . ( $i_am_negative ? '!=' : '=' ) . $dbo->quote( $value ),
2464 generic => $i_am_negative ? "$f != ?" : "$f = ?",
2465 type => 'where',
2466 );
2467 # $cl->bind->values( $value );
2468 3 $cl->push({
2469 value => $value,
2470 });
2471 3 $el = $self->new_element( value => $value );
2472 }
2473  
2474 3 ▶ $el->field( $field ) if( $self->_is_a( $field => 'DB::Object::Fields::Field' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_a($field, "DB::Object::Fields::Field") 0 3 3
2475 3 ▶ if( lc( $fields_type->{ $field } ) eq 'bytea' &&
Branch: 1 branch
BranchTrueFalseTotal
if (lc $fields_type->{$field} eq "bytea" and $const = $self->database_object->get_sql_type("bytea")) 0 3 3
Condition33%1/3
lc $fields_type->{$field} eq "bytea" and $const = $self->database_object->get_sql_type("bytea")
!ll&&!rl&&r
300
MC/DC0%0/2
lc $fields_type->{$field} eq "bytea" and $const = $self->database_object->get_sql_type("bytea")
lc $fields_type->{$field} eq "bytea" $const = $self->database_object->get_sql_type("bytea")
Truth table33%1/3
A and B
A= lc $fields_type->{$field} eq "bytea"B= $const = $self->database_object->get_sql_type("bytea")
ABresult
0X0
100
111
2476 ( $const = $self->database_object->get_sql_type( 'bytea' ) ) )
2477 {
2478 0 $el->type( $const );
2479 }
2480 # else
2481 # {
2482 # $cl->bind->types( '' ) if( $value =~ /^$placeholder_re$/ );
2483 # }
2484 3 ▶ $cl->push( $el ) if( defined( $el ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $el 3 0 3
2485 3 CORE::push( @list, $cl );
2486 }
2487 }
2488 # End while @arg loop
2489 5 ▶ if( scalar( @list ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @list) 5 0 5
2490 {
2491 5 $clause = $self->new_clause->merge( $dbo->$agg_op( @list ) );
2492 }
2493 }
2494 elsif( $data )
2495 {
2496 # $self->_value2bind( \$data, \@binded ) if( $bind );
2497 # $str = $data;
2498 # @types = ( '' ) x scalar( @binded );
2499 # $clause = $self->new_clause({
2500 # value => $str,
2501 # bind =>
2502 # {
2503 # values => \@binded,
2504 # types => \@types,
2505 # }
2506 # });
2507 0 my $elems = $self->_value2bind( \$data );
2508 0 $clause = $self->new_clause(
2509 value => $data,
2510 type => 'where',
2511 );
2512 0 ▶ $clause->push( $elems ) if( $elems->elements->length );
Branch: 1 branch
BranchTrueFalseTotal
if $elems->elements->length 0 0 0
2513 }
2514 5 return( $clause );
2515 5 };
2516 5 ▶ $where = $self->{ $prop } = $process_where_condition->( @params ) ||
Condition50%1/2
&$process_where_condition(@params) || (return $self->pass_error)
l!l
50
MC/DC0%0/1
&$process_where_condition(@params) || (return $self->pass_error)
&$process_where_condition(@params)
Truth table50%1/2
A
A= &$process_where_condition(@params)
Aresult
11
00
2517 return( $self->pass_error );
2518 5 return( $where );
2519 }
2520 else
2521 {
2522 15 $where = $self->{ $prop };
2523 }
2524  
2525 15 ▶ if( !$where && want( 'OBJECT' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $where and want("OBJECT")) 0 15 15
Condition66%2/3
not $where and want("OBJECT")
!ll&&!rl&&r
1230
MC/DC0%0/2
not $where and want("OBJECT")
not $where want("OBJECT")
Truth table66%2/3
not(A) and B
A= not $whereB= want("OBJECT")
ABresult
0X0
100
111
2526 {
2527 0 return( $self->new_null( type => 'object' ) );
2528 }
2529 15 return( $where );
2530 }
2531  
2532 # NOTE: For CBOR and Sereal
2533 sub FREEZE
2534 {
2535 0 ▶ my $self = CORE::shift( @_ );
Subroutine FREEZE: not called CC=8 SCAR=42.8
Pod undocumented
2536 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
2537 0 my $class = CORE::ref( $self );
2538 0 my $hash = {};
2539 0 my @keys = grep{ !/^binded_/ } @{$self->{query_reset_core_keys}};
2540 0 push( @keys, qw( elements table_object ) );
2541 0 foreach my $prop ( @keys )
2542 {
2543 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
2544 defined( $self->{ $prop } ) &&
2545 # Because we cannot reliably freeze code reference, and we do not usually need to.
2546 CORE::ref( $self->{ $prop } ) ne 'CODE' )
2547 {
2548 0 $hash->{ $prop } = $self->{ $prop };
2549 }
2550 }
2551  
2552 # Return an array reference rather than a list so this works with Sereal and CBOR
2553 # Before Sereal version 4.023, Sereal did not support multiple values returned
2554 0 ▶ if( $serialiser eq 'Sereal' )
Branch: 1 branch
BranchTrueFalseTotal
if ($serialiser eq "Sereal") 0 0 0
2555 {
2556 0 require Sereal::Encoder;
2557 0 require version;
2558
2559 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
2560 {
2561 0 CORE::return( [$class, $hash] );
2562 }
2563 }
2564 # But Storable want a list with the first element being the serialised element
2565 0 CORE::return( $class, $hash );
2566 }
2567  
2568 0 ▶sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
Pod undocumented
2569  
2570 0 ▶sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
Pod undocumented
2571  
2572 sub STORABLE_thaw_post_processing
2573 {
2574 0 ▶ my $obj = shift( @_ );
Subroutine STORABLE_thaw_post_processing: not called CC=1 SCAR=6.9
Pod undocumented
2575 0 my @keys = %$obj;
2576 0 my $class = ref( $obj );
2577 0 my $hash = {};
2578 0 @$hash{ @keys } = @$obj{ @keys };
2579 0 my $self = bless( $hash => $class );
2580 0 return( $self );
2581 }
2582  
2583 # NOTE: CBOR will call the THAW method with the stored classname as first argument, the constant string CBOR as second argument, and all values returned by FREEZE as remaining arguments.
2584 # NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
2585 sub THAW
2586 {
2587 # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue
2588 # 'CBOR' as the second value.
2589 0 ▶ my( $self, undef, @args ) = @_;
Subroutine THAW: not called CC=18 SCAR=57.1
Pod undocumented
2590 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
2591 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
2592 0 ▶ my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 0 0 0
2593 # Since this class is inherited, we also need to load from which we depend, if necessary.
2594 0 ▶ if( $class ne 'DB::Object::Query' )
Branch: 1 branch
BranchTrueFalseTotal
if ($class ne "DB::Object::Query") 0 0 0
2595 {
2596 0 require DB::Object::Query;
2597  
2598 # If inheritance is still missing (or incomplete) for any reason, repair it explicitly.
2599 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
2600  
2601 0 my $stash = \%{"${class}\::"};
2602 0 my $isa_ref;
2603 # We check if the 'ISA' symbol exists in the stash and its ARRAY slot is defined
2604 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
2605 0 defined( *{"${class}\::ISA"}{ARRAY} ) )
2606 {
2607 0 $isa_ref = *{"${class}\::ISA"}{ARRAY};
2608 }
2609 else
2610 {
2611 # # Force creation of @ISA array slot
2612 # @{"${class}::ISA"} = ();
2613 # $isa_ref = *{"${class}::ISA"}{ARRAY};
2614 # But I want it to die, so this can be dealt with, and not swept under the rug.
2615 0 die( "Unable to find the \@ISA value in $class. The package contains the following stashes:\n", $self->Module::Generic::dump( $stash ) );
2616 }
2617 0 my $has_qo = 0;
2618  
2619 0 foreach my $p ( @$isa_ref )
2620 {
2621 0 ▶ $has_qo = 1 if( CORE::defined( $p ) && $p eq 'DB::Object::Query' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $p and $p eq "DB::Object::Query" 0 0 0
Condition0%0/3
defined $p and $p eq "DB::Object::Query"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p and $p eq "DB::Object::Query"
defined $p $p eq "DB::Object::Query"
Truth table0%0/3
A and B
A= defined $pB= $p eq "DB::Object::Query"
ABresult
0X0
100
111
2622 }
2623  
2624 0 ▶ if( !$has_qo )
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_qo) 0 0 0
2625 {
2626 # Same as what we can find in modules DB::Object::(Postgres|SQLite|Mysql)::Query
2627 0 @$isa_ref = ( 'DB::Object::Query' );
2628 }
2629 }
2630 0 my $new;
2631 # Storable pattern requires to modify the object it created rather than returning a new one
2632 0 ▶ if( CORE::ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
2633 {
2634 0 foreach( CORE::keys( %$hash ) )
2635 {
2636 # No, this was a bad idea, because the instance property, such as 'where' bears the same name as the method
2637 # So, we would be calling the method 'where', but there is no table object or database object...
2638 # if( my $ref = $self->can( $_ ) )
2639 # {
2640 # $ref->( $self, CORE::delete( $hash->{ $_ } ) );
2641 # }
2642 0 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
2643 }
2644 0 $new = CORE::bless( $self => $class );
2645 }
2646 else
2647 {
2648 0 $new = CORE::bless( $hash => $class );
2649 }
2650 0 ▶ unless( $new->{elements} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($new->{"elements"}) 0 0 0
2651 {
2652 0 $new->{elements} = $new->new_elements;
2653 }
2654 0 CORE::return( $new );
2655 }
2656  
2657 3 1;
2658 # NOTE: POD
2659 __END__
2660  
2661 =encoding utf-8
2662  
2663 =head1 NAME
2664  
2665 DB::Object::Query - Query Object
2666  
2667 =head1 SYNOPSIS
2668  
2669 my $q = DB::Object::Query->new;
2670  
2671 =head1 VERSION
2672  
2673 v0.10.0
2674  
2675 =head1 DESCRIPTION
2676  
2677 This is the base class for this L<DB::Object> query formatter.
2678  
2679 =head1 METHODS
2680  
2681 =head2 alias
2682  
2683 Sets or gets an hash of column name to alias.
2684  
2685 =head2 as_string
2686  
2687 Returns the formatted query as a string.
2688  
2689 =head2 attach
2690  
2691 $q->attach( $db_object );
2692  
2693 Provided with a L<DB::Object>, or one of its inheriting classes, and this will attach that database object to this query object.
2694  
2695 It returns the current query object upon success, or upon error, it set an error an returns an empty list in list context, or C<undef> in scalar context.
2696  
2697 =head2 avoid
2698  
2699 Takes a list or array reference of column to avoid in the next query. This returns a L<Module::Generic::Array> object.
2700  
2701 =head2 binded
2702  
2703 Takes a list or array reference of values to bind in the next query in L<DB::Object::Statement/execute>. This returns a L<Module::Generic::Array> object.
2704  
2705 =head2 binded_group
2706  
2707 This returns the values to bind for the C<group> clause of the query. This returns a L<Module::Generic::Array> object.
2708  
2709 =head2 binded_limit
2710  
2711 This returns the values to bind for the C<limit> clause of the query. This returns a L<Module::Generic::Array> object.
2712  
2713 =head2 binded_order
2714  
2715 This returns the values to bind for the C<order> clause of the query. This returns a L<Module::Generic::Array> object.
2716  
2717 =head2 binded_types
2718  
2719 Takes a list or array reference of value types to bind in the next query in L<DB::Object::Statement/execute>. This returns a L<Module::Generic::Array> object.
2720  
2721 =head2 binded_types_as_param
2722  
2723 This does nothing and must be implemented by the driver package. So, see L<DB::Object::Mysql::Query/binded_types_as_param>, L<DB::Object::Postgres::Query/binded_types_as_param>, and L<DB::Object::SQLite::Query/binded_types_as_param>
2724  
2725 =head2 binded_values
2726  
2727 Takes a list or array reference of values to bind in the next query in L<DB::Object::Statement/execute>. This returns a L<Module::Generic::Array> object.
2728  
2729 =head2 binded_where
2730  
2731 This returns the values to bind for the C<where> clause of the query. This returns a L<Module::Generic::Array> object.
2732  
2733 =head2 constant
2734  
2735 If any argument is provided, this expects an hash reference of constants to value pairs.
2736  
2737 It returns the currently set constants as a hash reference.
2738  
2739 =head2 database_object
2740  
2741 Returns the current database object, which should be driver specific like L<DB::Object::Postgres>
2742  
2743 =head2 delete
2744  
2745 Takes some optional arguments used to define the C<where> clause, and this will prepare a C<DELETE> query.
2746  
2747 It will refuse to prepare the query if no C<where> clause has been defined, as this would be a very unsafe query. You would need to execute such query yourself using L<DB::Object/do>.
2748  
2749 If this method is called in void, this will execute the query.
2750  
2751 It returns the newly created statement handler as a L<DB::Object::Statement>
2752  
2753 =head2 dirty
2754  
2755 Defines whether this query object is dirty or not, and this means whether it has suffered from an error, and is in a stalled state, thus unusable. This state is checked by L<DB::Object> when setting or reetting the query object for a table object.
2756  
2757 Return the current boolean value.
2758  
2759 =head2 elements
2760  
2761 Sets or gets an L<DB::Object::Query::Elements> object. This object serves to contain all the elements used in creating SQL queries, keeping track of their order, value, dta types, placeholders used, etc.
2762  
2763 =head2 enhance
2764  
2765 Enable or disable enhancement mode.
2766  
2767 =for Pod::Coverage error
2768  
2769 =head2 final
2770  
2771 Enables to know the query reached the end, so that when constant is used, all the processing can be skipped.
2772  
2773 =head2 from_table
2774  
2775 The table used, if any, in a C<FROM> clause.
2776  
2777 =head2 format_statement
2778  
2779 Provided with an hash or hash reference of parameters and this will format the sql statement for queries of types C<select>, C<delete> and C<insert>
2780  
2781 In list context, it returns 2 strings: one comma-separated list of fields and one comma-separated list of values. In scalar context, it only returns a comma-separated string of fields.
2782  
2783 Accepted parameters are:
2784  
2785 =over 4
2786  
2787 =item C<data>
2788  
2789 =item C<order>
2790  
2791 If not provided, this will use the default column order for this table.
2792  
2793 =item C<table>
2794  
2795 The table name to use, or, if not specified, this will be set to a value set with L<DB::Object::Tables/qualified_name>
2796  
2797 =back
2798  
2799 If any values were set by L</from_unixtime> or L</unix_timestamp>, the associated columns will be formatted accordingly in the sql statement.
2800  
2801 It will go through each of the parameter passed to the original C<insert>, C<delete>, or C<select> query and if a column is one set earlier by L</from_unixtime> or L</unix_timestamp>, it will format it.
2802  
2803 If a parameter provided is a L<DB::Object::Statement> it will stringify the query and add it surrounded by parenthesis.
2804  
2805 If a parameter is actually a scalar reference, this means to us to use the underlying string as is.
2806  
2807 If a parameter is C<?>, this will be treated as a placeholder.
2808  
2809 If a parameter is a blob, it will be transformed into a parameter as a placeholder with its value saved to be bound in L<DB::Object::Statement/execute>
2810  
2811 If L<DB::Object/bind> is not enabled, then the value provided with this parameter will be added after being possibly surrounded by quotes using L<DB::Object::Tables/quote>.
2812  
2813 If the column type for this parameter is C<ENUM> and the query is of type C<INSERT> or C<UPDATE>, then the parameter value is surrounded with single quote.
2814  
2815 If L<DB::Object/bind> is enabled, then a placeholder C<?> will be added and the parameter value will be saed to be passed during L<DB::Object::Statement/execute>
2816  
2817 If nothing else matches it will add the value, possible quoted, using L<DB::Object::Tables/quote>, whose implementation can be driver specific.
2818  
2819 If column prefix is required, then the necessary prefix will be prepended to columns.
2820  
2821 In list context, this returns the formatted columns and their values, and in scalar context it will returns only the formatted columns.
2822  
2823 =head2 format_update
2824  
2825 Provided with a list of parameters either as a key-value pairs, as an hash reference or even as an array reference and this will format update query based on the following arguments provided:
2826  
2827 =over 4
2828  
2829 =item C<data>
2830  
2831 An array of key-value pairs to be used in the update query. This array can be provided as the prime argument as a reference to an array, an array, or as the C<data> element of a hash or a reference to a hash provided.
2832  
2833 Why an array if eventually we build a list of key-value pair? Because the order of the fields may be important, and if the key-value pair list is provided, B<format_update> honors the order in which the fields are provided.
2834  
2835 =back
2836  
2837 If no data is provided, this will return an error.
2838  
2839 L</format_update> will then iterate through each field-value pair, and perform some work:
2840  
2841 If the field being reviewed was provided to L</from_unixtime>, then L</format_update> will enclose it in the function suitable for the driver to convert it into a database datetime. For example, for Mysql, this would be:
2842  
2843 FROM_UNIXTIME(field_name)
2844  
2845 If the the given value is a reference to a scalar, it will be used as-is, ie. it will not be enclosed in quotes or anything. This is useful if you want to control which function to use around that field.
2846  
2847 If the value is C<?> it will be used as a placeholder and the value will be saved to be bound later in L<DB::Object::Statement/execute>. Its associated type will be added as blank, so it can be guessed later. However, if the column data type is C<bytea>, the the PostgreSQL data type C<DBD::Pg::PG_BYTEA> will be used when binding the value.
2848  
2849 If the column type is C<jsonb>, and the value is an hash reference, it will be json encoded and used instead.
2850  
2851 If the given value is another field or looks like a function having parenthesis, or if the value is a question mark, the value will be used as-is.
2852  
2853 If L<DB::Object/bind> is off, the value will be escaped and the pair field='value' created.
2854  
2855 If the field is a SET data type and the value is a number, the value will be used as-is without surrounding single quote.
2856  
2857 If L<DB::Object/bind> is enabled, a question mark will be used as the value and the original value will be saved as value to bind upon executing the query.
2858  
2859 Finally, otherwise the value is escaped and surrounded by single quotes.
2860  
2861 L</format_update> returns a regular string representing the comma-separated list of columns with their value assigment that will be used.
2862  
2863 =head2 from_unixtime
2864  
2865 Takes a list or array reference of columns that needs to be treated as unix timestamp and will be converted into a database timestamp.
2866  
2867 It returns that list as a L<Module::Generic::Array>
2868  
2869 =head2 format_from_epoch
2870  
2871 This is the driver specific implementation to convert unix timestamp to the database timestamp.
2872  
2873 This is superseded in driver specific implementation, so see L<DB::Object::Mysql::Query/format_from_epoch>, L<DB::Object::Postgres::Query/format_from_epoch> and L<DB::Object::SQLite:::Query/format_from_epoch>
2874  
2875 =head2 format_to_epoch
2876  
2877 This is the driver specific implementation to convert the database timestamp to unix timestamp.
2878  
2879 This is superseded in driver specific implementation, so see L<DB::Object::Mysql::Query/format_from_epoch>, L<DB::Object::Postgres::Query/format_from_epoch> and L<DB::Object::SQLite:::Query/format_from_epoch>
2880  
2881 =head2 getdefault
2882  
2883 Provided with an hash or hash reference of parameters and this will do some preparation work.
2884  
2885 Possible parameters are:
2886  
2887 =over 4
2888  
2889 =item C<arg>
2890  
2891 An array reference of data which should be a key-value pairs.
2892  
2893 =item C<as>
2894  
2895 An hash reference of column to alias pairs. Alternatively, if this is not provided, the value set with L</alias> will be used.
2896  
2897 =item C<avoid>
2898  
2899 An array reference of column to avoid using. Alternatively, if this is not provided, the value set with L</avoid> will be used.
2900  
2901 =item C<from_unixtime>
2902  
2903 An array reference of columns to be converted from unix timestamp to the database timestamp.
2904  
2905 =item C<query_type>
2906  
2907 The type of query, such as C<delete>, C<insert>, C<replace>, C<select>, C<update>
2908  
2909 =item C<table>
2910  
2911 The table name.
2912  
2913 =item C<time>
2914  
2915 A unix timestamp. Alternatively, C<unixtime> can be used.
2916  
2917 =item C<unix_timestamp>
2918  
2919 An array reference of columns to be converted into unix timestamp.
2920  
2921 =back
2922  
2923 Does some preparation work such as :
2924  
2925 =over 4
2926  
2927 =item 1
2928  
2929 the date/time field to use the FROM_UNIXTIME and UNIX_TIMESTAMP functions
2930  
2931 =item 2
2932  
2933 removing from the query the fields to avoid, ie the ones set with the B<avoid> method.
2934  
2935 =item 3
2936  
2937 set the fields alias based on the information provided with the B<alias> method.
2938  
2939 =item 4
2940  
2941 if a field last_name and first_name exist, it will also create an alias C<name> based on the concatenation of the 2.
2942  
2943 =item 5
2944  
2945 it will set the default values provided. This is used for UPDATE queries.
2946  
2947 =back
2948  
2949 It sets the following properties of the current object:
2950  
2951 =over 4
2952  
2953 =item C<bind>
2954  
2955 A boolean value whether the use of placeholder is enabled.
2956  
2957 =item C<query_type>
2958  
2959 The type of query, such as C<select>, C<insert>, etc...
2960  
2961 =item C<_args>
2962  
2963 The arguments provided as an array reference.
2964  
2965 =item C<_default>
2966  
2967 The default value as an hash reference.
2968  
2969 =item C<_extra>
2970  
2971 Extra parameters as an array reference.
2972  
2973 =item C<_fields>
2974  
2975 The columns as an hash reference.
2976  
2977 =item C<_from_unix>
2978  
2979 An hash reference
2980  
2981 =item C<_structure>
2982  
2983 The table structure which is an hash reference of column name to definition pairs.
2984  
2985 =item C<_to_unix>
2986  
2987 An hash reference
2988  
2989 =back
2990  
2991 It returns a new L<DB::Object::Tables> object with all the data prepared within.
2992  
2993 =head2 group
2994  
2995 Format the C<group by> portion of the query by calling L</_group_order>
2996  
2997 It returns a new L<DB::Object::Query::Clause> object.
2998  
2999 =head2 having
3000  
3001 This must be superseded by driver specific implementation of this class. Check out L<DB::Object::Mysql::Query/having>, L<DB::Object::Postgres::Query/having>, L<DB::Object::SQLite::Query/having>
3002  
3003 =head2 insert
3004  
3005 $tbl->insert( col1 => $val1, col2 => $val 2 );
3006 # or
3007 $other_tbl->where( user => 'joe' );
3008 my $sth = $other_tbl->select;
3009 $tbl->insert( $sth );
3010 # will become INSERT INTO some_table SELECT col1, col2 FROM other_table WHERE user = 'joe'
3011  
3012 Provided with an array reference or an hash reference or a statement object (L<DB::Object::Statement>) or a list of parameters and this will prepares an C<insert> query using the field-value pairs provided.
3013  
3014 If a L<DB::Object::Statement> object is provided as first argument, it will be considered as a SELECT query to be used in the INSERT query, as in: INSERT INTO my table SELECT FROM another_table
3015  
3016 Otherwise, L</insert> will build the query based on the fields provided.
3017  
3018 In void context, it will execute the query by calling of L<DB::Object::Statement/execute>.
3019  
3020 It returns the statement object.
3021  
3022 =head2 is_upsert
3023  
3024 Sets or gets the boolean value if the query is an C<upsert>, which means a C<insert> or C<update> query that uses an C<ON CONFLICT> clause. See L<DB::Object::Postgres::Query/on_conflict>
3025  
3026 =head2 join_fields
3027  
3028 Sets or gets the join fields. This is a regular string.
3029  
3030 =head2 join_tables
3031  
3032 Sets or gets the table joined. This returns a L<Module::Generic::Array>
3033  
3034 =head2 left_join
3035  
3036 Sets or gets an hash reference of column joint column pairs
3037  
3038 =head2 limit
3039  
3040 Set or get the limit for the future statement, by calling L</_process_limit>
3041  
3042 It returns a L<DB::Object::Query::Clause> representing the C<limit> clause.
3043  
3044 =head2 local
3045  
3046 Provided with a variable name and value pairs and this will set them.
3047  
3048 It returns the formated declaration as a string.
3049  
3050 =head2 new_clause
3051  
3052 This returns a new L<DB::Object::Query::Clause> object.
3053  
3054 =head2 new_element
3055  
3056 Instantiate a new L<DB::Object::Query::Element> object, passing it whatever arguments were provided and sharing wit it the value of the L</debug> flag.
3057  
3058 =head2 new_elements
3059  
3060 Instantiate a new L<DB::Object::Query::Elements> object, passing it whatever arguments were provided and sharing wit it the value of the L</debug> flag.
3061  
3062 =head2 order
3063  
3064 Provided with a list of parameter and this will format the C<order> clause by calling L</_group_order>
3065  
3066 It returns a new L<DB::Object::Query::Clause> object.
3067  
3068 =for Pod::Coverage pass_error
3069  
3070 =head2 prepare_options
3071  
3072 Sets or gets the options that will be used in L<DB::Object/_cache_this>, which is taked with preparing statement when they are not already cached.
3073  
3074 This method basically handles an hash reference of properties set by L<DB::Object::Query> and their inheriting packages. Currently only PostgreSQL makes use of this with L<DB::Object::Postgres::Query/dollar_placeholder> and L<DB::Object::Postgres::Query/server_prepare>
3075  
3076 =head2 query
3077  
3078 Sets or gets the query string. It returns whatever is set as a regular string.
3079  
3080 =head2 query_reset
3081  
3082 Reset the query object to its nominal value so it can be re-used.
3083  
3084 =head2 query_reset_core_keys
3085  
3086 Returns an L<Module::Generic::Array> object of core object properties shared with inheriting package.
3087  
3088 Those are used to know what properties to reset.
3089  
3090 =head2 query_reset_keys
3091  
3092 Returns an L<Module::Generic::Array> object of object properties shared with inheriting package.
3093  
3094 This contains driver specific properties and together with the ones provided with L</query_reset_core_keys>, they form a whole.
3095  
3096 =head2 query_type
3097  
3098 Sets or gets the query type, such as C<delete>, C<insert>, C<select>, C<update>, etc.
3099  
3100 =head2 query_values
3101  
3102 Sets or gets the query values.
3103  
3104 Returns a L<scalar object|Module::Generic::Scalar>
3105  
3106 =head2 replace
3107  
3108 This is unsupported by default and its implementation is driver specific.
3109  
3110 =head2 reset
3111  
3112 Reset the query object.
3113  
3114 What it does is remove the following object properties: alias local binded binded_group binded_limit binded_order binded_types binded_values binded_where where limit group_by order_by reverse from_unixtime unix_timestamp sorted
3115  
3116 =head2 reset_bind
3117  
3118 Reset the bind values by setting the following object property to an empty anonymous array: binded binded_group binded_limit binded_order binded_types binded_where
3119  
3120 =head2 returning
3121  
3122 This is unsupported by default and its implementation is driver specific.
3123  
3124 =head2 reverse
3125  
3126 Mark the query to use reverse order. This is used by L</order>.
3127  
3128 =head2 select
3129  
3130 Provided with a list or an array reference of columns, and this will format the C<select> statement.
3131  
3132 If the parameters provided is actually a scalar reference, it will be used as is.
3133  
3134 If the parameters provided is an array or array reference, it will be joined using comma to get the list of columns to get, or, if the array is empty, the special C<*> will be used to get all the columns.
3135  
3136 Otherwise, it will use the data parameter provided as is.
3137  
3138 If any alias have been set using L</alias>, they will be added to the list of columns to get.
3139  
3140 if a table alias was set using L</table_alias> it will be set here.
3141  
3142 If the method was called in void context, it will execute immediately the statement object prepared.
3143  
3144 It returns the statement object (DB::Object::Statement).
3145  
3146 =head2 selected_fields
3147  
3148 Sets or gets the string representing the list of columns used in previous C<select> statement.
3149  
3150 Returns a regular string.
3151  
3152 =head2 sort
3153  
3154 Set the query to use normal sorting order.
3155  
3156 =head2 sorted
3157  
3158 Sets or gets the list of sorted columns used in statements. This returns a L<Module::Generic::Array> object.
3159  
3160 =head2 table_alias
3161  
3162 Sets an optional alias for this table to be used in statement.
3163  
3164 This method should be called by L<DB::Object::Tables/as>. If you change this directly, you risk facing some discrepancies between the actual table alias and the one set here.
3165  
3166 Returns the current value.
3167  
3168 =head2 table_object
3169  
3170 Sets or gets the table object. This will return a L<DB::Object::Tables> object
3171  
3172 =head2 tie
3173  
3174 If provided a hash or a hash ref, it sets the list of fields and their corresponding perl variable to bind their values to.
3175  
3176 In list context, it returns the list of those field-variable pair, or a reference to it in scalar context.
3177  
3178 =head2 unix_timestamp
3179  
3180 Provided a list or an array reference of columns, and this sets the columns to be treated for seamless conversion from and to unix time.
3181  
3182 It returns a L<Module::Generic::Array> object.
3183  
3184 =head2 update
3185  
3186 Provided with a list, an array reference, or an hash or hash reference of key-value pairs and this will format the update statement.
3187  
3188 If no parameter is provided, this will return an error.
3189  
3190 This will call L</format_update> to format the parameter provided and use the resulting string in the C<update> statement.
3191  
3192 If any clauses have been defined such as C<where>, C<limit>, etc, they will be properly formatted and added to the statement.
3193  
3194 The resulting formatted query will be saved as the object property L</query>.
3195  
3196 The formatted update columns and values will be saved in the current object property L</query_values>
3197  
3198 If L</update> is called in void context, this will execute the query immediately.
3199  
3200 It returns the statement object (L<DB::Object::Statement>).
3201  
3202 =head2 where
3203  
3204 Build the where clause based on the field-value hash provided by calling L</_where_having>.
3205  
3206 It returns a clause object (L<DB::Object::Query::Clause>).
3207  
3208 =head2 _group_order
3209  
3210 This support method is called by L</group> and L</order> to format those clauses.
3211  
3212 Provided with an object, or a list or an array reference of parameters and this will format the relevant clause.
3213  
3214 it will go through each parameter and if the parameter provided is an L<DB::Object::Fields::Field> object, it will collect its various attribute.
3215  
3216 If the parameter is a scalar reference, it will be used as is.
3217  
3218 If the parameter looks like it contains some field, it will be prepended by an appropriate prefix of table and possible database and schema name, if necessary.
3219  
3220 Otherwise, it will use whatever value was provided as a column and use it.
3221  
3222 It returns a L<DB::Object::Query::Clause> object.
3223  
3224 =head2 _having
3225  
3226 This is called by L</where> and L</having>
3227  
3228 Provided with some data as a list or an array reference and this will format the C<where> or C<having> clause.
3229  
3230 Walking through each parameter provided, if a parameter is a scalar reference, it will be used as is.
3231  
3232 If the parameter looks like it contains some field, it will be prepended by an appropriate prefix of table and possible database and schema name, if necessary.
3233  
3234 If L<DB::Object/bind> is enabled, it will save the value used and use a placeholder.
3235  
3236 It returns a L<DB::Object::Query::Clause> object.
3237  
3238 =head2 _initiate_clause_object
3239  
3240 This instantiate a new L<DB::Object::Query::Clause> object passing it whatever parameters were provided.
3241  
3242 =head2 _limit
3243  
3244 Sets or gets the limit clause object and returns a L<DB::Object::Query::Clause> object.
3245  
3246 =head2 _process_limit
3247  
3248 Provided with some parameters and this will format the C<limit> clause of the query.
3249  
3250 If one parameter was provided, then this will only define the ending limit. The start will be set as L<perlfunc/undef>
3251  
3252 If two parameters are provided, then this will set the start offset and the limit.
3253  
3254 It check each of the start offset and end limit thus set, and if it is a scalar reference, it will be used as is. However, if the parameter is a C<?> it will be used as a placeholder.
3255  
3256 Otherwise, if L<DB::Object/bind> is enabled, this will save the parameter value as a binded value to be passed to L<DN::Object::Statement/execute>
3257  
3258 It returns the C<limit> clause object (DB::Object::Query::Clause)
3259  
3260 =head2 _query_components
3261  
3262 This returns an array reference of formatted clause, in their proper order to be added to query.
3263  
3264 This method is called by L</delete>, L</insert>, L</replace>, L</select> and L</update>
3265  
3266 This method is overriden by driver packages, so check L<DB::Object::Mysql::Query/_query_components>, L<DB::Object::Postgres::Query/_query_components> and L<DB::Object::SQLite::Query/_query_components>
3267  
3268 =head2 _query_type
3269  
3270 Based on the latest formatted query and the object property L</query>, this will return the type of query this is. This can be possibly one of the following: C<alter>, C<create>, C<drop>, C<grant>, C<listen>, C<notify>, C<insert>, C<update>, C<delete>, C<select>, C<truncate>
3271  
3272 =head2 _save_bind
3273  
3274 Provided with a query type and this will collect binded values from various clauses.
3275  
3276 It returns the current object.
3277  
3278 =head2 _value2bind
3279  
3280 If L<DB::Object/use_bind> is enabled, and this will modify the query passed to replace value with placeholders.
3281  
3282 Actually this method is not used anymore and really qui dangerous because parsing sql is quite challenging.
3283  
3284 =head2 _where_having
3285  
3286 This is used to format C<WHERE> and C<HAVING> clause.
3287  
3288 Provided with a query type and clause property name such as C<having> or C<where> and other parameters and this will format the C<where> or C<having> clause and return a new L<DB::Object::Query::Clause> object.
3289  
3290 It checks each parameter passed.
3291  
3292 if the first parameter is a L<DB::Object::Operator> object, it will take it embedded values by calling L<DB::Object::Query::Clause/value>. However, if there are any unknown fields, they will be ignored.
3293  
3294 If the parameter is a scalar reference, it will use it as is.
3295  
3296 If the parameter is a L<DB::Object::Operator> object like L<DB::Object::AND>, L<DB::Object::OR> or L<DB::Object::NOT>, it will recursively process its embedded elements.
3297  
3298 If the parameter is a L<DB::Object::Query::Clause> object, it will be added to the stack of elements.
3299  
3300 If the parameter is a L<DB::Object::Expression> object, it will be added to the stack of elements.
3301  
3302 If the parameter is a L<DB::Object::Fields::Overloaded> object, it will be added as a new L<DB::Object::Query::Clause> to the stack.
3303  
3304 If the parameter is a litteral represented as a string or a scalar reference, then it will be added to the list as-is. For example:
3305  
3306 $tbl->where(
3307 $tbl->fo->status eq 'active',
3308 "LENGTH(?) > 12"
3309 );
3310  
3311 or
3312  
3313 $tbl->where(
3314 $tbl->fo->status eq 'active',
3315 \"LENGTH(?) > 12"
3316 );
3317  
3318 However, make sure to put this expression at the end.
3319  
3320 It then checks parameters two by two, the first one being the column and the second being its value.
3321  
3322 If the value is the operator object L<DB::Object::NOT>, it adds to the stack a new clause object of type C<column IS NOT something>, such as C<column IS NOT NULL>
3323  
3324 if the value is undefined or that the value is equal to C<NULL>, then it adds to the stack a new clause object L<DB::Object::Query::Clause> of type C<column IS NULL>
3325  
3326 If the value is a scalar reference, it will be added as is in a new clause object that is added to the stack.
3327  
3328 If the value is a L<DB::Object::Statement>, L<DB::Object::Statement/fetchrow> will be called and the value fetched will be added as a new clause object to the stack.
3329  
3330 If the value is a perl Regexp object, then it will be formatted in a way suitable to the driver and added to a new clause object and onto the stack.
3331  
3332 If the value looks like some table field embedded inside some SQL function, then it will be added to a new clause object and onto the stack.
3333  
3334 See L<Postgres documentation for more information|https://www.postgresql.org/docs/9.5/functions-matching.html>
3335  
3336 See L<MySQL documentation for more information|https://dev.mysql.com/doc/refman/5.7/en/regexp.html>
3337  
3338 See L<SQLite documentation for more information|https://sqlite.org/lang_expr.html>
3339  
3340 If the column is of type C<bytea>, then a new clause object will be added to the stack with the column value being quoted properly using L<DB::Object/quote>
3341  
3342 All the clause objects in the stack will be merged into one new clause object using L<DB::Object::Query::Clause/merge>
3343  
3344 The resulting final clause object (L<DB::Object::Query::Clause>) is returned.
3345  
3346 =head1 AUTOLOAD
3347  
3348 When the C<AUTOLOAD> is called, it will check if the value of the method used corresponds to an existing database table, and if it does, it returns the value returned by calling L</table> with the table name.
3349  
3350 =head1 SEE ALSO
3351  
3352 L<DBI>
3353  
3354 =head1 AUTHOR
3355  
3356 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
3357  
3358 =head1 COPYRIGHT & LICENSE
3359  
3360 Copyright (c) 2018-2021 DEGUEST Pte. Ltd.
3361  
3362 You can use, copy, modify and redistribute this package and associated
3363 files under the same terms as Perl itself.
3364  
3365 =cut
« blib/lib/DB/Object.pm ↑ Summary blib/lib/DB/Object/SQLite/Query.pm »