lib/DB/Object/SQLite/Query.pm

filter: Statementstmt 47.1% 84 / 178 Branchbran 34.3% 35 / 102 Conditioncond 32.7% 20 / 61 MC/DCmcdc 9.0% 3 / 33 Subroutinesub 75.0% 15 / 20 Podpod 100% 10 / 10 totaltotal 41.3% 167 / 404 CC 78 SCAR 54.8
CC
78
Cov
41%
on_conflict
718.8
_query_components
49.0
format_from_epoch
20.0
CRAP 238.8
« lib/DB/Object/SQLite/Tables.pm ↑ Summary blib/lib/DB/Object/Postgres/Tables.pm »
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## Database Object Interface - ~/lib/DB/Object/SQLite/Query.pm
4 ## Version v0.4.0
5 ## Copyright(c) 2023 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2019/06/16
8 ## Modified 2026/03/22
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::SQLite::Query;
16 BEGIN
17 {
18 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶ use parent qw( DB::Object::Query );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ use vars qw( $VERSION $DEBUG $EXCEPTION_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶ use Wanted;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶ our $DEBUG = 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
25 2 our $VERSION = 'v0.4.0';
26 };
27  
28 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 sub init
32 {
33 10 ▶ my $self = shift( @_ );
Subroutine init: called CC=2 SCAR=7.0
Pod documented
34 10 $self->{having} = '';
35 10 $self->{_init_strict_use_sub} = 1;
36 10 $self->{_exception_class} = $EXCEPTION_CLASS;
37 10 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 10 10
38 10 $self->{binded_having} = [];
39 10 $self->{query_reset_keys} = [qw( alias binded binded_values binded_where binded_limit binded_group binded_having binded_order from_unixtime group_by limit local _on_conflict on_conflict order_by reverse sorted unix_timestamp where )];
40 10 return( $self );
41 }
42  
43 0 ▶sub binded_having { return( shift->_set_get( 'binded_having', @_ ) ); }
Subroutine binded_having: not called CC=1 SCAR=6.9
Pod documented
44  
45 sub format_from_epoch
46 {
47 0 ▶ my $self = shift( @_ );
Subroutine format_from_epoch: not called CC=4 SCAR=30.0
Pod documented
48 0 my $opts = {};
49 0 ▶ $opts = shift( @_ ) if( scalar( @_ ) == 1 && $self->_is_hash( $_[0] => 'strict' ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 and $self->_is_hash($_[0], "strict") 0 0 0
Condition0%0/3
scalar @_ == 1 and $self->_is_hash($_[0], "strict")
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ == 1 and $self->_is_hash($_[0], "strict")
scalar @_ == 1 $self->_is_hash($_[0], "strict")
Truth table0%0/3
A and B
A= scalar @_ == 1B= $self->_is_hash($_[0], "strict")
ABresult
0X0
100
111
50 0 ▶ if( $opts->{bind} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'bind'}) { } 0 0 0
51 {
52 0 return( "DATETIME(?, 'unixepoch', 'localtime')" );
53 }
54 else
55 {
56 0 return( sprintf( "DATETIME(%s, 'unixepoch', 'localtime')", $opts->{value} ) );
57 }
58 }
59  
60 sub format_to_epoch
61 {
62 4 ▶ my $self = shift( @_ );
Subroutine format_to_epoch: called CC=5 SCAR=18.9
Pod documented
63 4 my $opts = {};
64 4 ▶ $opts = shift( @_ ) if( scalar( @_ ) == 1 && $self->_is_hash( $_[0] => 'strict' ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 and $self->_is_hash($_[0], "strict") 4 0 4
Condition33%1/3
scalar @_ == 1 and $self->_is_hash($_[0], "strict")
!ll&&!rl&&r
004
MC/DC0%0/2
scalar @_ == 1 and $self->_is_hash($_[0], "strict")
scalar @_ == 1 $self->_is_hash($_[0], "strict")
Truth table33%1/3
A and B
A= scalar @_ == 1B= $self->_is_hash($_[0], "strict")
ABresult
0X0
100
111
65 4 ▶ if( $opts->{bind} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'bind'}) { } 0 4 4
66 {
67 0 return( "STRFTIME('%s',?)" );
68 }
69 else
70 {
71 4 ▶ return( sprintf( "STRFTIME('%%s','%s')", $opts->{quote} ? "'" . $opts->{value} . "'" : $opts->{value} ) );
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'quote'} ? : 0 4 4
72 }
73 }
74  
75 # _having is in DB::Object::Query
76 # sub having { return( shift->_having( @_ ) ); }
77 3 ▶sub having { return( shift->_where_having( 'having', 'having', @_ ) ); }
Subroutine having: called CC=1 SCAR=0.0
Pod documented
78  
79 # <https://sqlite.org/lang_select.html#limitoffset>
80 # <https://sqlite.org/lang_delete.html>
81 # <https://sqlite.org/lang_update.html>
82 sub limit
83 {
84 13 ▶ my $self = shift( @_ );
Subroutine limit: called CC=11 SCAR=27.1
Pod documented
85 13 my $limit = $self->{limit};
86 13 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 12 13
87 {
88 # Returns a DB::Object::Query::Clause
89 1 ▶ $limit = $self->_process_limit( @_ ) ||
Condition50%1/2
$self->_process_limit(@_) || (return $self->pass_error)
l!l
10
MC/DC0%0/1
$self->_process_limit(@_) || (return $self->pass_error)
$self->_process_limit(@_)
Truth table50%1/2
A
A= $self->_process_limit(@_)
Aresult
11
00
90 return( $self->pass_error );
91  
92 1 ▶ if( CORE::length( $limit->metadata->limit // '' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length($limit->metadata->limit // "")) 1 0 1
Condition50%1/2
$limit->metadata->limit // ""
l!l
10
MC/DC0%0/1
$limit->metadata->limit // ""
$limit->metadata->limit
Truth table50%1/2
A
A= $limit->metadata->limit
Aresult
11
00
93 {
94 1 ▶ $limit->generic( CORE::length( $limit->metadata->offset // '' ) ? 'LIMIT ? OFFSET ?' : 'LIMIT ?' );
Branch: 1 branch
BranchTrueFalseTotal
length($limit->metadata->offset // '') ? : 0 1 1
Condition50%1/2
$limit->metadata->offset // ""
l!l
10
MC/DC0%0/1
$limit->metadata->offset // ""
$limit->metadata->offset
Truth table50%1/2
A
A= $limit->metadata->offset
Aresult
11
00
95 # %s works for integer, and also for numbered placeholders like $1 or ?1, or regular placeholder like ?
96 1 ▶ $limit->value(
Branch: 1 branch
BranchTrueFalseTotal
length($limit->metadata->offset // '') ? : 0 1 1
Condition50%1/2
$limit->metadata->offset // ""
l!l
10
MC/DC0%0/1
$limit->metadata->offset // ""
$limit->metadata->offset
Truth table50%1/2
A
A= $limit->metadata->offset
Aresult
11
00
97 CORE::length( $limit->metadata->offset // '' )
98 ? CORE::sprintf( 'LIMIT %s OFFSET %s', $limit->metadata->limit, $limit->metadata->offset )
99 : CORE::sprintf( 'LIMIT %s', $limit->metadata->limit )
100 );
101 }
102 }
103  
104 13 ▶ if( !$limit && want( 'OBJECT' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $limit and want("OBJECT")) 0 13 13
Condition66%2/3
not $limit and want("OBJECT")
!ll&&!rl&&r
490
MC/DC0%0/2
not $limit and want("OBJECT")
not $limit want("OBJECT")
Truth table66%2/3
not(A) and B
A= not $limitB= want("OBJECT")
ABresult
0X0
100
111
105 {
106 0 return( $self->new_null( type => 'object' ) );
107 }
108 13 return( $limit );
109 }
110  
111 # This implementation in SQLite is essentially identical to PostgreSQL
112 # INSERT INTO vocabulary(word) VALUES('jovial')
113 # ON CONFLICT(word) DO UPDATE SET count=count+1;
114 #
115 # INSERT INTO phonebook(name,phonenumber) VALUES('Alice','704-555-1212')
116 # ON CONFLICT(name) DO UPDATE SET phonenumber=excluded.phonenumber;
117 #
118 #
119 # INSERT INTO phonebook2(name,phonenumber,validDate)
120 # VALUES('Alice','704-555-1212','2018-05-08')
121 # ON CONFLICT(name) DO UPDATE SET
122 # phonenumber=excluded.phonenumber,
123 # validDate=excluded.validDate
124 # WHERE excluded.validDate>phonebook2.validDate;
125 # https://www.sqlite.org/lang_upsert.html
126 # $q->on_conflict({
127 # target => 'id',
128 # action => 'nothing',
129 # action => 'update',
130 # fields => { a => 'some value', b => 'some other' },
131 # });
132 sub on_conflict
133 {
134 5 ▶ my $self = shift( @_ );
Subroutine on_conflict: called CC=30 SCAR=65.8
Pod documented
135 5 my $opts = {};
136 5 ▶ $self->{_on_conflict} = {} if( ref( $self->{_on_conflict} ) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"_on_conflict"} ne "HASH" 4 1 5
137 5 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 5 5
138 {
139 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
140 0 my $ver = $tbl_o->database_object->version;
141 0 ▶ if( version->parse( $ver ) < version->parse( '3.24.0' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ("version"->parse($ver) < "version"->parse("3.24.0")) 0 0 0
142 {
143 0 return( $self->error( "SQLite version is $ver, but version 3.24.0 of 2018-06-04 or higher is required to use this on conflict clause." ) );
144 }
145 0 $opts = $self->_get_args_as_hash( @_ );
146 0 my $hash = {};
147 0 my @comp = ( 'ON CONFLICT' );
148 0 ▶ if( $opts->{target} )
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'target'}) { } 0 0 0
elsif ($opts->{'action'} ne 'nothing') { } 0 0 0
149 {
150 0 $hash->{target} = $opts->{target};
151 # Example: ON CONFLICT ON CONSTRAINT customers_name_key DO NOTHING;
152 0 ▶ if( $hash->{target} =~ /^(on[[:blank:]]+constraint)(.*?)$/i )
Branch: 3 branches
BranchTrueFalseTotal
if ($hash->{'target'} =~ /^(on[[:blank:]]+constraint)(.*?)$/i) { } 0 0 0
elsif (ref $hash->{'target'} eq 'SCALAR') { } 0 0 0
elsif ($self->_is_array($hash->{'target'})) { } 0 0 0
153 {
154 0 $hash->{target} = "\U$1\E$2";
155 0 push( @comp, $hash->{target} );
156 }
157 # a reference to a scalar was provided, so we set the value as is
158 elsif( ref( $hash->{target} ) eq 'SCALAR' )
159 {
160 0 push( @comp, $$hash->{target} );
161 }
162 elsif( $self->_is_array( $hash->{target} ) )
163 {
164 0 push( @comp, sprintf( '(%s)', join( ',', @{$hash->{target}} ) ) );
165 }
166 else
167 {
168 0 push( @comp, sprintf( '(%s)', $hash->{target} ) );
169 }
170 }
171 # https://www.postgresql.org/docs/10/sql-insert.html#SQL-ON-CONFLICT
172 elsif( $opts->{action} ne 'nothing' )
173 {
174 0 return( $self->error( "No target was specified for the on conflict clause." ) );
175 }
176  
177 0 ▶ if( $opts->{where} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"where"}) 0 0 0
178 {
179 0 $hash->{where} = $opts->{where};
180 0 push( @comp, 'WHERE ' . $opts->{where} );
181 }
182  
183 # action => update
184 0 ▶ if( $opts->{action} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'action'}) { } 0 0 0
185 {
186 0 ▶ if( $opts->{action} eq 'update' )
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'action'} eq 'update') { } 0 0 0
elsif ($opts->{'action'} eq 'nothing' or $opts->{'action'} eq 'ignore') { } 0 0 0
187 {
188 0 $hash->{action} = $opts->{action};
189 # return( $self->error( "No fields to update was provided for on conflict do update" ) ) if( !$opts->{fields} );
190 # No fields provided, so we take it from the initial insert and build the update list instead
191 0 ▶ if( !$opts->{fields} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"fields"}) 0 0 0
192 {
193 # The insert will have triggered a getdefault() which stores the parameters into a _args object fields
194 # my $f_ref = $self->{ '_args' };
195 # $opts->{inherited_fields} = $self->format_update( $f_ref );
196 $self->{_on_conflict_callback} = sub
197 {
198 0 ▶ my $f_ref = $self->{_args};
Subroutine __ANON__: not called CC=1 SCAR=6.9
199 0 $self->is_upsert(1);
200 0 my $inherited_fields = $self->format_update( $f_ref );
201 0 push( @comp, 'DO UPDATE SET' );
202 0 push( @comp, $inherited_fields );
203 0 $hash->{query} = join( ' ', @comp );
204 0 $self->{_on_conflict} = $hash;
205 0 $self->{on_conflict} = join( ' ', @comp );
206 # Usable only once
207 0 CORE::delete( $self->{_on_conflict_callback} );
208 0 };
209 # Return empty, not undef; undef is error
210 0 return( '' );
211 }
212 0 ▶ return( $self->error( "Fields property to update for on conflict do update clause is not a hash reference nor an array of fields." ) ) if( !$self->_is_hash( $opts->{fields} => 'strict' ) && !$self->_is_array( $opts->{fields} ) && !$self->{_on_conflict_callback} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_hash($opts->{"fields"}, "strict") or $self->_is_array($opts->{"fields"}) or $self->{"_on_conflict_callback"} 0 0 0
Condition0%0/6
$self->_is_hash($opts->{"fields"}, "strict") or $self->_is_array($opts->{"fields"}) or $self->{"_on_conflict_callback"}
l!l&&r!l&&!r
000
$self->_is_hash($opts->{"fields"}, "strict") or $self->_is_array($opts->{"fields"})
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->_is_hash($opts->{"fields"}, "strict") or $self->_is_array($opts->{"fields"}) or $self->{"_on_conflict_callback"}
$self->_is_hash($opts->{"fields"}, "strict") $self->_is_array($opts->{"fields"}) $self->{"_on_conflict_callback"}
Truth table0%0/4
A or B or C
A= $self->_is_hash($opts->{"fields"}, "strict")B= $self->_is_array($opts->{"fields"})C= $self->{"_on_conflict_callback"}
ABCresult
1XX1
01X1
0011
0000
213 0 ▶ if( $self->_is_hash( $opts->{fields} => 'strict' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_hash($opts->{'fields'}, 'strict')) { } 0 0 0
elsif ($self->_is_array($opts->{'fields'})) { } 0 0 0
214 {
215 0 ▶ return( $self->error( "Fields property to update for on conflict do update clause contains no fields!" ) ) if( !scalar( keys( %{$opts->{fields}} ) ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$$opts{"fields"};} 0 0 0
216 }
217 elsif( $self->_is_array( $opts->{fields} ) )
218 {
219 0 ▶ return( $self->error( "Fields property to update for on conflict do update clause contains no fields!" ) ) if( !scalar( @{$opts->{fields}} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$opts->{"fields"};} 0 0 0
220 }
221  
222 0 ▶ if( $self->_is_array( $opts->{fields} ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_array($opts->{"fields"})) 0 0 0
223 {
224 0 my $this = $opts->{fields};
225 0 my $new = {};
226 0 foreach my $f ( @$this )
227 {
228 0 $new->{ $f } = \( 'EXCLUDED.' . $f );
229 }
230 0 $opts->{fields} = $new;
231 }
232 # Here the user will use the special table 'excluded'
233 0 $hash->{fields} = $opts->{fields};
234  
235 0 my $q = [];
236  
237 0 foreach my $k ( sort( keys( %{$opts->{fields}} ) ) )
238 {
239 0 ▶ push( @$q, sprintf( '%s = %s', $k, ref( $opts->{fields}->{ $k } ) eq 'SCALAR' ? ${$opts->{fields}->{ $k }} : $tbl_o->database_object->quote( $opts->{fields}->{ $k } ) ) );
Branch: 1 branch
BranchTrueFalseTotal
ref $opts->{'fields'}{$k} eq 'SCALAR' ? : 0 0 0
240 }
241 0 ▶ if( scalar( @$q ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$q) { } 0 0 0
242 {
243 0 push( @comp, 'DO UPDATE SET' );
244 0 push( @comp, join( ", ", @$q ) );
245 }
246 else
247 {
248 0 return( $self->error( "A on conflict do update clause was specified, but I could not get a list of fields to update." ) );
249 }
250 }
251 ▶ elsif( $opts->{action} eq 'nothing' || $opts->{action} eq 'ignore' )
Condition0%0/3
$opts->{"action"} eq "nothing" or $opts->{"action"} eq "ignore"
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"action"} eq "nothing" or $opts->{"action"} eq "ignore"
$opts->{"action"} eq "nothing" $opts->{"action"} eq "ignore"
Truth table0%0/3
A or B
A= $opts->{"action"} eq "nothing"B= $opts->{"action"} eq "ignore"
ABresult
1X1
011
000
252 {
253 0 $hash->{action} = $opts->{action};
254 0 push( @comp, 'DO NOTHING' );
255 }
256 else
257 {
258 0 return( $self->error( "Unknown action '$opts->{action}' for on conflict clause." ) );
259 }
260 }
261 else
262 {
263 0 return( $self->error( "No action was specified for the on conflict clause." ) );
264 }
265 0 $hash->{query} = join( ' ', @comp );
266 0 $self->{_on_conflict} = $hash;
267 0 $self->{on_conflict} = $self->new_clause({ value => join( ' ', @comp ) });
268 }
269 # We are being called possibly by _query_components
270 # If we have a callback, we execute it
271 5 ▶ if( $self->{_on_conflict_callback} && !scalar( @_ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_on_conflict_callback"} and not scalar @_) 0 5 5
Condition33%1/3
$self->{"_on_conflict_callback"} and not scalar @_
!ll&&!rl&&r
500
MC/DC0%0/2
$self->{"_on_conflict_callback"} and not scalar @_
$self->{"_on_conflict_callback"} not scalar @_
Truth table33%1/3
A and not(B)
A= $self->{"_on_conflict_callback"}B= not scalar @_
ABresult
0X0
100
111
272 {
273 # This will use the insert components set up to format our on conflict clause properly
274 # The callback is needed, because the query formatting occurs after the calling of our method on_conflict()
275 0 $self->{_on_conflict_callback}->();
276 }
277 5 return( $self->{on_conflict} );
278 }
279  
280 sub reset
281 {
282 0 ▶ my $self = shift( @_ );
Subroutine reset: not called CC=2 SCAR=17.9
Pod documented
283 0 ▶ if( !$self->{query_reset} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"query_reset"}) 0 0 0
284 {
285 0 my $keys = [qw( alias binded binded_values binded_where binded_limit binded_group binded_having binded_order from_unixtime group_by limit local _on_conflict on_conflict order_by reverse sorted unix_timestamp where )];
286 0 CORE::delete( @$self{ @$keys } );
287 0 $self->{query_reset}++;
288 0 $self->{enhance} = 1;
289 }
290 0 return( $self );
291 }
292  
293 sub reset_bind
294 {
295 0 ▶ my $self = shift( @_ );
Subroutine reset_bind: not called CC=2 SCAR=17.9
Pod documented
296 0 my @f = qw( binded binded_where binded_group binded_having binded_order binded_limit );
297 0 foreach my $field ( @f )
298 {
299 0 $self->{ $field } = [];
300 }
301 0 return( $self );
302 }
303  
304 # Supported by SQLite since 3.35.0 (2021-03-12)
305 # <https://www.sqlite.org/lang_returning.html>
306 sub returning
307 {
308 5 ▶ my $self = shift( @_ );
Subroutine returning: called CC=5 SCAR=24.1
Pod documented
309 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
310 5 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 5 5
311 {
312 0 my $pg_version = $self->database_object->version;
313 0 ▶ return( $self->error( "Cannot use returning for PostgreSQL version lower than 3.35.0 (released on 2021-03-12). This server version is: $pg_version" ) ) if( $pg_version < '3.35.0' );
Branch: 1 branch
BranchTrueFalseTotal
if $pg_version < "3.35.0" 0 0 0
314 # It could be a field name or a wildcard
315 0 ▶ return( $self->error( "A reference was provided (", ref( $_[0] ), "), but I was expecting a string, which could be a field name or even a star (*) indicating all fields." ) ) if( ref( $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 0 0
316 0 $self->{returning} = $self->new_clause({ value => shift( @_ ) });
317 }
318 # return( wantarray() ? () : undef() ) if( !$self->{returning} );
319 # return( wantarray() ? ( $self->{returning} ) : "RETURNING $self->{returning}" );
320 5 return( $self->{returning} );
321 }
322  
323 # Inherited from DB::Object::Query
324 # sub update
325  
326 sub _query_components
327 {
328 5 ▶ my $self = shift( @_ );
Subroutine _query_components: called CC=25 SCAR=38.9
329 5 ▶ my $type = ( @_ > 0 && lc( shift( @_ ) ) ) || $self->_query_type() || return( $self->error( "You must specify a query type: select, insert, update or delete" ) );
Condition37%3/8
@_ > 0 && lc shift @_ || $self->_query_type || (return $self->error('You must specify a query type: select, insert, update or delete'))
l!l
50
@_ > 0 and lc shift @_ or $self->_query_type
l!l&&r!l&&!r
500
@_ > 0 and lc shift @_
!ll&&!rl&&r
005
MC/DC0%0/3
@_ > 0 && lc shift @_ || $self->_query_type || (return $self->error('You must specify a query type: select, insert, update or delete'))
@_ > 0 lc shift @_ $self->_query_type
Truth table20%1/5
A and B or C
A= @_ > 0B= lc shift @_C= $self->_query_type
ABCresult
11X1
0X11
1011
0X00
1000
330 5 my $opts = $self->_get_args_as_hash( @_ );
331 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
332 5 my( $where, $group, $having, $sort, $order, $limit, $returning, $on_conflict );
333 5 $where = $self->where();
334 5 $limit = $self->limit();
335 5 $returning = $self->returning;
336 5 $on_conflict = $self->on_conflict;
337 5 ▶ if( $type eq 'select' )
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'select') { } 2 3 5
elsif ($type eq 'update' or $type eq 'delete') { } 2 1 3
338 {
339 2 $group = $self->group();
340 2 $having = $self->having();
341 2 ▶ $sort = $self->reverse() ? 'DESC' : $self->sort() ? 'ASC' : '';
Branch: 2 branches
BranchTrueFalseTotal
$self->reverse ? : 0 2 2
$self->sort ? : 0 2 2
342 2 $order = $self->order();
343 2 $limit = $self->limit();
344 }
345 ▶ elsif( $type eq 'update' || $type eq 'delete' )
Condition100%3/3
$type eq "update" or $type eq "delete"
l!l&&r!l&&!r
111
MC/DC100%2/2
$type eq "update" or $type eq "delete"
$type eq "update" $type eq "delete"
Truth table100%3/3
A or B
A= $type eq "update"B= $type eq "delete"
ABresult
1X1
011
000
346 {
347 2 ▶ if( $tbl_o->can_update_delete_limit )
Branch: 1 branch
BranchTrueFalseTotal
if ($tbl_o->can_update_delete_limit) 0 2 2
348 {
349 0 $limit = $self->limit();
350 }
351 }
352 5 my @query = ();
353 5 ▶ push( @query, "WHERE $where" ) if( $where && $type ne 'insert' );
Branch: 1 branch
BranchTrueFalseTotal
if $where and $type ne "insert" 4 1 5
Condition66%2/3
$where and $type ne "insert"
!ll&&!rl&&r
104
MC/DC50%1/2
$where and $type ne "insert"
$where $type ne "insert"
Truth table66%2/3
A and B
A= $whereB= $type ne "insert"
ABresult
0X0
100
111
354 5 ▶ push( @query, "GROUP BY $group" ) if( $group );
Branch: 1 branch
BranchTrueFalseTotal
if $group 1 4 5
355 5 ▶ push( @query, "HAVING $having" ) if( $having );
Branch: 1 branch
BranchTrueFalseTotal
if $having 1 4 5
356 5 ▶ push( @query, "ORDER BY $order" ) if( $order );
Branch: 1 branch
BranchTrueFalseTotal
if $order 1 4 5
357 5 ▶ push( @query, $sort ) if( $sort && $order);
Branch: 1 branch
BranchTrueFalseTotal
if $sort and $order 0 5 5
Condition33%1/3
$sort and $order
!ll&&!rl&&r
500
MC/DC0%0/2
$sort and $order
$sort $order
Truth table33%1/3
A and B
A= $sortB= $order
ABresult
0X0
100
111
358 5 ▶ push( @query, $limit ) if( $limit );
Branch: 1 branch
BranchTrueFalseTotal
if $limit 1 4 5
359 5 ▶ if( $on_conflict )
Branch: 1 branch
BranchTrueFalseTotal
if ($on_conflict) 0 5 5
360 {
361 0 ▶ if( $type eq 'insert' )
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'insert') { } 0 0 0
362 {
363 0 push( @query, $on_conflict );
364 }
365 else
366 {
367 0 warn( "The SQLite ON CONFLICT clause is only supported for INSERT queries. Your query was of type \"$type\".\n" );
368 }
369 }
370 # Supported as of 3.35.0 (2021-03-12)
371 5 ▶ push( @query, "RETURNING $returning" ) if( $returning && ( $type eq 'insert' || $type eq 'update' || $type eq 'delete' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $returning and $type eq "insert" || $type eq "update" || $type eq "delete" 0 5 5
Condition11%1/9
$returning and $type eq "insert" || $type eq "update" || $type eq "delete"
!ll&&!rl&&r
500
$type eq "insert" or $type eq "update" or $type eq "delete"
l!l&&r!l&&!r
000
$type eq "insert" or $type eq "update"
l!l&&r!l&&!r
000
MC/DC0%0/4
$returning and $type eq "insert" || $type eq "update" || $type eq "delete"
$returning $type eq "insert" $type eq "update" $type eq "delete"
Truth table20%1/5
A and B or C or D
A= $returningB= $type eq "insert"C= $type eq "update"D= $type eq "delete"
ABCDresult
0XXX0
10000
11XX1
101X1
10011
372 5 return( \@query );
373 }
374  
375 2 1;
376 # NOTE: POD
377 __END__
378  
379 =encoding utf-8
380  
381 =head1 NAME
382  
383 DB::Object::SQLite::Query - SQLite Query Object
384  
385 =head1 SYNOPSIS
386  
387 my $q = DB::Object::SQLite::Query->new;
388  
389 =head1 VERSION
390  
391 v0.4.0
392  
393 =head1 DESCRIPTION
394  
395 This is a SQLite specific query object.
396  
397 =head1 METHODS
398  
399 =head2 binded_having
400  
401 Sets or gets the array object (L<Module::Generic::Array>) for the binded value in C<HAVING> clauses.
402  
403 =head2 format_from_epoch
404  
405 This takes the parameters I<bind> and I<value> and returns a formatted C<DATETIME(?, 'unixepoch', 'localtime')> expression.
406  
407 =head2 format_to_epoch
408  
409 This takes the parameters I<bind>, I<value> and I<quote> and returns a formatted expression C<STRFTIME('%s',?)> to returns the epoch value out of the given field.
410  
411 =head2 having
412  
413 Calls L<DB::Object::Query/_where_having> to build a C<having> clause.
414  
415 See L<SQLite documentation for more information|https://www.sqlite.org/lang_select.html>
416  
417 =head2 limit
418  
419 Build a new L<DB::Object::Query::Clause> clause object by calling L</_process_limit> and return it.
420  
421 See L<SQLite documentation for more information|https://sqlite.org/limits.html>
422  
423 =head2 on_conflict
424  
425 Provided with some options and this will build a C<ON CONFLICT> clause (L<DB::Object::Query::Clause>). This is only available for SQLite version 3.35.0 released on 2021-03-12 or above.
426  
427 =over 4
428  
429 =item * C<action>
430  
431 Valid value can be C<nothing> and in which case, nothing will be done by the database upon conflict.
432  
433 INSERT INTO distributors (did, dname) VALUES (7, 'Redline GmbH')
434 ON CONFLICT (did) DO NOTHING;
435  
436 or
437  
438 INSERT INTO distributors (did, dname) VALUES (9, 'Antwerp Design')
439 ON CONFLICT ON CONSTRAINT distributors_pkey DO NOTHING;
440  
441 Value can also be C<ignore> instructing the database to simply ignore conflict.
442  
443 If the value is C<update>, then this will set a callback routine to format an update statement using L<DB::Object::Query/format_update>
444  
445 If the original C<insert> or C<update> uses placeholders, then the C<DO UPDATE> will also use the same placeholders and the L<DB::Object::Statement> object will act accordingly when being provided the binded values. That is, it will double them to allocate those binded value also for the C<DO UPDATE> part of the query.
446  
447 The callback will be called by L<DB::Object::Query/insert> or L<DB::Object::Query/update>, because the L</on_conflict> relies on query columns being previously set.
448  
449 =item * C<fields>
450  
451 An array (or array object) of fields to use with I<action> set to C<update>
452  
453 $q->on_conflict({
454 target => 'name',
455 action => 'update,
456 fields => [qw( first_name last_name )],
457 });
458  
459 This will turn the C<DO UPDATE> prepending each field with the special keyword C<EXCLUDED>
460  
461 INSERT INTO distributors (did, dname)
462 VALUES (5, 'Gizmo Transglobal'), (6, 'Associated Computing, Inc')
463 ON CONFLICT (did) DO UPDATE SET dname = EXCLUDED.dname;
464  
465 =item * C<target>
466  
467 Target can be a table column.
468  
469 $q->on_conflict({
470 target => 'name',
471 action => 'ignore',
472 });
473  
474 or it can also be a constraint name:
475  
476 $q->on_conflict({
477 target => 'on constraint my_table_idx_name',
478 action => 'ignore',
479 });
480  
481 Value for I<target> can also be a scalar reference and it will be used as-is
482  
483 $q->on_conflict({
484 target => \'on constraint my_table_idx_name',
485 action => 'ignore',
486 });
487  
488 Value for I<target> can also be an array or array object (like L<Module::Generic::Array>) and the array will be joined using a comma.
489  
490 If no I<target> argument was provided, then I<action> must be set to C<nothing> or this will return an error.
491  
492 =item * C<where>
493  
494 You can also provide a C<WHERE> expression in the conflict and it will be added literally.
495  
496 $q->on_conflict({
497 target => 'did',
498 action => 'ignore',
499 where => 'is_active',
500 });
501  
502 INSERT INTO distributors (did, dname) VALUES (10, 'Conrad International')
503 ON CONFLICT (did) WHERE is_active DO NOTHING;
504  
505 =back
506  
507 See L<SQLite documentation for more information|https://www.sqlite.org/lang_upsert.html>.
508  
509 =head2 reset
510  
511 If the object property C<query_reset> is not already set, this will remove the following properties from the current query object, set L<DB::Object::Query/enhance> to true and return the query object.
512  
513 Properties removed are: alias local binded binded_values binded_where binded_limit binded_group binded_having binded_order where limit group_by order_by reverse from_unixtime unix_timestamp sorted
514  
515 =head2 reset_bind
516  
517 Reset all the following object properties to an anonymous array: binded binded_where binded_group binded_having binded_order binded_limit
518  
519 =head2 returning
520  
521 This feature is available with SQLite version 3.35.0 or above, otherwise an error is returned.
522  
523 It expects a string that is used to build the C<RETURNING> clause.
524  
525 # will instruct the database to return all the table columns
526 $q->returning( '*' );
527  
528 or
529  
530 $q->returning( 'id' );
531  
532 But don't pass a reference:
533  
534 $q->returning( [qw( id name age )] );
535  
536 It returns a new L<DB::Object::SQLite::Query::Clause> object.
537  
538 See L<SQLite documentation for more information|https://www.sqlite.org/lang_returning.html>
539  
540 =head2 _query_components
541  
542 This is called by the various query methods like L<DB::Object::Query/select>, L<DB::Object::Query/insert>, L<DB::Object::Query/update>, L<DB::Object::Query/delete>
543  
544 It will get the various query components (group, having, sort, order, limit) that have been set and add them formatted to an array that is returned.
545  
546 This version of L</_query_components> exists here to provide PostgreSQL specific implementation. See also the generic one in L<DB::Object::Query/_query_components>
547  
548 =head1 SEE ALSO
549  
550 L<perl>
551  
552 =head1 AUTHOR
553  
554 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
555  
556 =head1 COPYRIGHT & LICENSE
557  
558 Copyright (c) 2019-2021 DEGUEST Pte. Ltd.
559  
560 You can use, copy, modify and redistribute this package and associated
561 files under the same terms as Perl itself.
562  
563 =cut
« lib/DB/Object/SQLite/Tables.pm ↑ Summary blib/lib/DB/Object/Postgres/Tables.pm »