lib/DB/Object/SQLite.pm

filter: Statementstmt 71.7% 398 / 555 Branchbran 31.9% 55 / 172 Conditioncond 32.3% 43 / 133 MC/DCmcdc 1.3% 1 / 74 Subroutinesub 76.0% 70 / 92 Podpod 100% 30 / 30 totaltotal 56.5% 597 / 1056 CC 139 SCAR 37.2
CC
139
Cov
58%
replace
342.0
register_function
90.0
attribute
72.0
CRAP 41.1
« blib/lib/DB/Object/Fields.pm ↑ Summary lib/DB/Object/SQLite/Statement.pm »
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## Database Object Interface - ~/lib/DB/Object/SQLite.pm
4 ## Version v1.3.1
5 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2017/07/19
8 ## Modified 2026/03/26
9 ## All rights reserved
10 ##
11 ##
12 ## This program is free software; you can redistribute it and/or modify it
13 ## under the same terms as Perl itself.
14 ##----------------------------------------------------------------------------
15 # This is the subclassable module for driver specific ones.
16 package DB::Object::SQLite;
17 BEGIN
18 {
19 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 use vars qw(
22 $VERSION $CACHE_SIZE $CONNECT_VIA $ERROR $DEBUG
23 $USE_BIND $USE_CACHE $MOD_PERL
24 $PLACEHOLDER_REGEXP $DATATYPES_DICT $EXCEPTION_CLASS
25 2 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶ use DBI qw( :sql_types );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶ eval { require DBD::SQLite; };
Subroutine BEGIN: called CC=3 SCAR=11.0
28 2 ▶ die( $@ ) if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
29 2 ▶ use parent qw( DB::Object );
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶ use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶ use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶ use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶ use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶ use Module::Generic::File qw( sys_tmpdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
35 # <https://metacpan.org/pod/DBD::SQLite::Constants>
36 # <https://www.sqlite.org/datatype3.html>
37 # <https://metacpan.org/pod/DBD::SQLite::Constants#datatypes-(fundamental_datatypes)>
38 # NULL, INTEGER, REAL, TEXT, BLOB
39 # Check DBD::SQLite::Constants
40 2 our $DATATYPES_DICT =
41 {
42 blob => {
43 constant => '',
44 name => 'SQLITE_BLOB',
45 re => qr/^BLOB/,
46 type => 'blob'
47 },
48 bool => {
49 alias => [qw( boolean date datetime decimal numeric )],
50 constant => '',
51 name => 'SQLITE_NULL',
52 re => qr/^(NUMERIC|DECIMAL\(\d+,\d+\)|BOOLEAN|DATETIME|DATE)/,
53 type => 'bool'
54 },
55 float => {
56 alias => [qw( double real ), 'double precision', ],
57 constant => '',
58 name => 'SQLITE_FLOAT',
59 re => qr/^(REAL|DOUBLE|DOUBLE\s+PRECISION|FLOAT)/,
60 type => 'float'
61 },
62 integer => {
63 alias => [qw( int tinyint smallint mediumint bigint int2 int8 ), 'unsigned big int' ],
64 constant => '',
65 name => 'SQLITE_INTEGER',
66 re => qr/^(INT|INTEGER|TINYINT|SMALLINT|MEDIUMINT|BIGINT|UNSIGNED\s+BIG\s+INT|INT2|INT8)/,
67 type => 'integer',
68 },
69 # Data type text is only available from version 1.71 onward
70 };
71 2 ▶ if( $DBD::SQLite::VERSION >= 1.071 )
Branch: 1 branch
BranchTrueFalseTotal
if ($DBD::SQLite::VERSION >= "1.071") 2 0 2
72 {
73 $DATATYPES_DICT->{text} =
74 {
75 2 alias => [qw( character clob nchar nvarchar varchar ), 'native character', 'varying character' ],
76 constant => '',
77 name => 'SQLITE_TEXT',
78 re => qr/^(CHARACTER\(\d+\)|VARCHAR\(\d+\)|VARYING\s+CHARACTER\(\d+\)|NCHAR\(\d+\)|NATIVE\s+CHARACTER\(\d+\)|NVARCHAR\(\d+\)|TEXT|CLOB)/,
79 type => 'text'
80 };
81 }
82 2 our $PLACEHOLDER_REGEXP = qr/(?:(?<![?\w])\?(?<index>[1-9]\d*)(?!\w))/;
83 2 our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS;
84 2 our $VERSION = 'v1.3.1';
85 };
86  
87 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 # require DB::Object::SQLite::Statement;
90 # require DB::Object::SQLite::Tables;
91 2 our $DEBUG = 0;
92 2 our $CACHE_SIZE = 10;
93 2 our $USE_BIND = 0;
94 2 our $USE_CACHE = 0;
95 2 our $MOD_PERL = 0;
96 2 ▶if( $INC{ 'Apache/DBI.pm' } &&
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Apache/DBI.pm"} and substr($ENV{"GATEWAY_INTERFACE"} || "", 0, 8) eq "CGI-Perl") 0 2 2
Condition20%1/5
$INC{"Apache/DBI.pm"} and substr($ENV{"GATEWAY_INTERFACE"} || "", 0, 8) eq "CGI-Perl"
!ll&&!rl&&r
200
$ENV{'GATEWAY_INTERFACE'} || ''
l!l
00
MC/DC0%0/2
$INC{"Apache/DBI.pm"} and substr($ENV{"GATEWAY_INTERFACE"} || "", 0, 8) eq "CGI-Perl"
$INC{"Apache/DBI.pm"} substr($ENV{"GATEWAY_INTERFACE"} || "", 0, 8) eq "CGI-Perl"
MC/DC0%0/1
$ENV{'GATEWAY_INTERFACE'} || ''
$ENV{'GATEWAY_INTERFACE'}
Truth table33%1/3
A and B
A= $INC{"Apache/DBI.pm"}B= substr($ENV{"GATEWAY_INTERFACE"} || "", 0, 8) eq "CGI-Perl"
ABresult
0X0
100
111
Truth table0%0/2
A
A= $ENV{'GATEWAY_INTERFACE'}
Aresult
11
00
97 substr( $ENV{GATEWAY_INTERFACE}|| '', 0, 8 ) eq 'CGI-Perl' )
98 {
99 0 $CONNECT_VIA = "Apache::DBI::connect";
100 0 $MOD_PERL++;
101 }
102  
103 # Actually the one in DB::Object is used, because DBD::SQLite has no datatype constants of its own
104 # our $DATATYPES = {};
105  
106 2 our $PRIVATE_FUNCTIONS =
107 {
108 ceiling =>[1, \&_ceiling],
109 concat =>[-1, \&_concat],
110 curdate =>[0, \&_curdate],
111 curtime =>[0, \&_curtime],
112 dayname =>[1, \&_dayname],
113 dayofmonth =>[1, \&_dayofmonth],
114 dayofweek =>[1, \&_dayofweek],
115 dayofyear =>[1, \&_dayofyear],
116 distance_miles =>[4, \&_distance_miles],
117 # from_days =>[-1, \&_from_days],
118 from_unixtime =>[1, \&_from_unixtime],
119 hour =>[1, \&_hour],
120 lcase =>[1, \&_lcase],
121 left =>[2, \&_left],
122 locate =>[2, \&_locate],
123 log10 =>[1, \&_log10],
124 minute =>[1, \&_minute],
125 month =>[1, \&_month],
126 monthname =>[1, \&_monthname],
127 number_format =>[4, \&_number_format],
128 power =>[2, \&_power],
129 quarter =>[1, \&_quarter],
130 rand =>[0, \&_rand],
131 regexp =>[2, \&_regexp],
132 replace =>[3, \&_replace],
133 right =>[2, \&_right],
134 second =>[1, \&_second],
135 space =>[1, \&_space],
136 sprintf =>[-1, \&_sprintf],
137 to_days =>[1, \&_to_days],
138 # truncate =>[-1, \&_truncate],
139 ucase =>[1, \&_ucase],
140 unix_timestamp =>[1, \&_unix_timestamp],
141 week =>[1, \&_week],
142 weekday =>[1, \&_weekday],
143 year =>[1, \&_year],
144 };
145 # See compile_options method
146 # This is very useful to know which features can be used
147 2 our $COMPILE_OPTIONS = [];
148  
149 2 foreach my $type ( keys( %$DATATYPES_DICT ) )
150 {
151 10 ▶ if( CORE::exists( $DATATYPES_DICT->{ $type }->{alias} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"} and ref $DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"} eq "ARRAY" and scalar @{$DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"};}) 8 2 10
Condition80%4/5
exists $DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"} and ref $DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"} eq "ARRAY" and scalar @{$DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"};}
!ll
28
exists $DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"} and ref $DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"} eq "ARRAY"
!ll&&!rl&&r
208
MC/DC50%1/2
exists $DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"} and ref $DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"} eq "ARRAY" and scalar @{$DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"};}
exists $DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"} ref $DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"} eq "ARRAY"
Truth table66%2/3
A and B
A= exists $DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"}B= ref $DB::Object::SQLite::DATATYPES_DICT->{$type}{"alias"} eq "ARRAY"
ABresult
0X0
100
111
152 ref( $DATATYPES_DICT->{ $type }->{alias} ) eq 'ARRAY' &&
153 8 scalar( @{$DATATYPES_DICT->{ $type }->{alias}} ) )
154 {
155 8 foreach my $alias ( @{$DATATYPES_DICT->{ $type }->{alias}} )
156 {
157 46 ▶ next if( CORE::exists( $DATATYPES_DICT->{ $alias } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $DB::Object::SQLite::DATATYPES_DICT->{$alias} 0 46 46
158 46 $DATATYPES_DICT->{ $alias } = $DATATYPES_DICT->{ $type };
159 }
160 }
161 }
162  
163 2 foreach my $type ( keys( %$DATATYPES_DICT ) )
164 {
165 56 my $c = $DATATYPES_DICT->{ $type }->{name};
166 56 my $code = \&{"DBD::SQLite::Constants::${c}"};
167 my $val = eval
168 56 {
169 56 $code->();
170 };
171 56 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 56 56
172 {
173 0 warn( "Datatype \"DBD::SQLite::Constants::${c}\" is not defined in DBD::SQLite version $DBD::SQLite::VERSION: $@" );
174 0 delete( $DATATYPES_DICT->{ $type } );
175 }
176 else
177 {
178 56 $DATATYPES_DICT->{ $type }->{constant} = $val;
179 }
180 }
181  
182 sub init
183 {
184 3 ▶ my $self = shift( @_ );
Subroutine init: called CC=2 SCAR=7.0
Pod documented
185 3 $self->{_exception_class} = $EXCEPTION_CLASS;
186 3 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 3 3
187 3 $self->{driver} = 'SQLite';
188 3 $self->{_func} = {};
189 3 return( $self );
190 }
191  
192 # Get/set alias
193 # sub alias
194  
195 # sub as_string
196  
197 # sub avoid
198  
199 sub attribute($;$@)
200 {
201 0 ▶ my $self = shift( @_ );
Subroutine attribute: not called CC=8 SCAR=42.8
Pod documented
202 # $h->{AttributeName} = ...; # set/write
203 # ... = $h->{AttributeName}; # get/read
204 # 1 means that the attribute may be modified
205 # 0 mneas that the attribute may only be read
206 0 ▶ my $name = shift( @_ ) if( @_ == 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 0 0
207 0 my %arg = ( @_ );
208 0 my %attr =
209 (
210 ActiveKids => 0,
211 AutoCommit => 1,
212 AutoInactiveDestroy => 1,
213 CachedKids => 0,
214 ChildHandles => 0,
215 ChopBlanks => 1,
216 CursorName => 0,
217 Driver => 0,
218 ErrCount => 1,
219 Executed => 0,
220 FetchHashKeyName => 1,
221 HandleError => 1,
222 HandleSetErr => 1,
223 InactiveDestroy => 1,
224 Kids => 0,
225 NAME => 0,
226 NULLABLE => 0,
227 NUM_OF_FIELDS => 0,
228 NUM_OF_PARAMS => 0,
229 # Current database name
230 Name => 0,
231 PRECISION => 0,
232 PrintError => 1,
233 PrintWarn => 1,
234 Profile => 1,
235 RaiseError => 1,
236 RowCacheSize => 0,
237 RowsInCache => 0,
238 SCALE => 0,
239 ShowErrorStatement => 1,
240 Statement => 0,
241 TYPE => 0,
242 Taint => 1,
243 TaintIn => 1,
244 TaintOut => 1,
245 TraceLevel => 1,
246 Type => 1,
247 Username => 0,
248 Warn => 1,
249 # Not used
250 # LongReadLen => 1,
251 # LongTruncOk => 1,
252 # CompatMode => 1,
253 # If you set this to true, "do" method will process multiple statements at one go.
254 # This may be handy, but with performance penalty. See above for details.
255 sqlite_allow_multiple_statements => 1,
256 # If you set this to true, DBD::SQLite tries to see if the bind values are number or
257 # not, and does not quote if they are numbers.
258 sqlite_see_if_its_a_number => 1,
259 sqlite_unicode => 1,
260 # Returns an unprepared part of the statement you pass to "prepare". Typically this
261 # contains nothing but white spaces after a semicolon.
262 sqlite_unprepared_statements => 0,
263 # If you set this to true, DBD::SQLite tries to issue a "begin immediate transaction"
264 # (instead of "begin transaction") when necessary.
265 sqlite_use_immediate_transaction => 1,
266 sqlite_version => 0,
267 );
268 # Only those attribute exist
269 # Using an a non existing attribute produce an exception, so we better avoid
270 0 ▶ if( $name )
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 0 0 0
271 {
272 0 ▶ return( $self->{dbh}->{ $name } ) if( exists( $attr{ $name } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr{$name} 0 0 0
273 }
274 else
275 {
276 0 my $value;
277 0 ▶ while( ( $name, $value ) = each( %arg ) )
Branch: 1 branch
BranchTrueFalseTotal
if (($name, $value) = each %arg) 0 0 0
278 {
279 # We intend to modifiy the value of an attribute
280 # we are allowed to modify this value if it is true
281 0 ▶ if( exists( $attr{ $name } ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attr{$name} and defined $value and $attr{$name}) 0 0 0
Condition0%0/6
exists $attr{$name} and defined $value and $attr{$name}
!ll&&!rl&&r
000
exists $attr{$name} and defined $value
!ll&&!rl&&r
000
MC/DC0%0/3
exists $attr{$name} and defined $value and $attr{$name}
exists $attr{$name} defined $value $attr{$name}
Truth table0%0/4
A and B and C
A= exists $attr{$name}B= defined $valueC= $attr{$name}
ABCresult
0XX0
10X0
1100
1111
282 defined( $value ) &&
283 $attr{ $name } )
284 {
285 0 $self->{dbh}->{ $name } = $value;
286 }
287 }
288 }
289 }
290  
291 # sub available_drivers(@)
292  
293 sub begin_work($;$@)
294 {
295 1 ▶ my $self = shift( @_ );
Subroutine begin_work: called CC=1 SCAR=0.0
Pod documented
296 1 $self->{transaction} = 1;
297 1 return( $self->{dbh}->begin_work( @_ ) );
298 }
299  
300 # This method is common to DB::Object and DB::Object::Statement
301 # sub bind
302  
303 # sub cache
304  
305 2 ▶sub can_update_delete_limit { return( shift->has_compile_option( 'ENABLE_UPDATE_DELETE_LIMIT' ) ); }
Subroutine can_update_delete_limit: called CC=1 SCAR=0.0
Pod documented
306  
307 # sub check_driver(@;$@)
308  
309 sub commit($;$@)
310 {
311 1 ▶ my $self = shift( @_ );
Subroutine commit: called CC=1 SCAR=0.0
Pod documented
312 1 $self->{transaction} = 0;
313 1 return( $self->{dbh}->commit( @_ ) );
314 }
315  
316 sub compile_options
317 {
318 2 ▶ my $self = shift( @_ );
Subroutine compile_options: called CC=9 SCAR=36.6
Pod documented
319 2 ▶ return( [ @$COMPILE_OPTIONS ] ) if( scalar( @$COMPILE_OPTIONS ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$DB::Object::SQLite::COMPILE_OPTIONS 0 2 2
320 2 my $system_tmpdir = sys_tmpdir();
321 2 my $compile_options_cache_file = $system_tmpdir->join( 'sql_sqlite_compile_options.cfg' );
322 2 my @options = ();
323 # if( -e( $compile_options_cache_file ) && !-z( $compile_options_cache_file ) )
324 2 ▶ if( $compile_options_cache_file->exists && !$compile_options_cache_file->is_empty )
Branch: 1 branch
BranchTrueFalseTotal
if ($compile_options_cache_file->exists and not $compile_options_cache_file->is_empty) 0 2 2
Condition33%1/3
$compile_options_cache_file->exists and not $compile_options_cache_file->is_empty
!ll&&!rl&&r
200
MC/DC0%0/2
$compile_options_cache_file->exists and not $compile_options_cache_file->is_empty
$compile_options_cache_file->exists not $compile_options_cache_file->is_empty
Truth table33%1/3
A and not(B)
A= $compile_options_cache_file->existsB= not $compile_options_cache_file->is_empty
ABresult
0X0
100
111
325 {
326 # my $fh = IO::File->new( "<$compile_options_cache_file" ) || return( $self->error( "Unable to read the sqlite compile options cache file \"$compile_options_cache_file\": $!" ) );
327 0 ▶ my $fh = $compile_options_cache_file->open( '<' ) || return( $self->error( "Unable to read the sqlite compile options cache file \"$compile_options_cache_file\": ", $compile_options_cache_file->error ) );
Condition0%0/2
$compile_options_cache_file->open('<') || (return $self->error(qq[Unable to read the sqlite compile options cache file "$compile_options_cache_file": ], $compile_options_cache_file->error))
l!l
00
MC/DC0%0/1
$compile_options_cache_file->open('<') || (return $self->error(qq[Unable to read the sqlite compile options cache file "$compile_options_cache_file": ], $compile_options_cache_file->error))
$compile_options_cache_file->open('<')
Truth table0%0/2
A
A= $compile_options_cache_file->open('<')
Aresult
11
00
328 0 my @all = $fh->getlines;
329 # Remove any comments
330 0 @options = grep( !/^#/, @all );
331 0 $fh->close;
332 0 ▶ if( scalar( @options ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @options) 0 0 0
333 {
334 0 $COMPILE_OPTIONS = \@options;
335 # Return a copy only to be safe
336 0 return( [ @options ] );
337 }
338 }
339 # If the cache file does not yet exists or there is no options, we do the query
340 2 ▶ my $dbh = $self->{dbh} || return( $self->error( "No active database handler available. You can only call this method once a database connection has been made." ) );
Condition50%1/2
$self->{'dbh'} || (return $self->error('No active database handler available. You can only call this method once a database connection has been made.'))
l!l
02
MC/DC0%0/1
$self->{'dbh'} || (return $self->error('No active database handler available. You can only call this method once a database connection has been made.'))
$self->{'dbh'}
Truth table50%1/2
A
A= $self->{'dbh'}
Aresult
11
00
341 0 my $all = $self->do( "PRAGMA compile_options" )->fetchall_arrayref;
342 0 @options = map( $_->[0], @$all );
343 # my $fh = IO::File->new( ">$compile_options_cache_file" ) || return( $self->error( "Unable to write to sqlite compile options cache file \"$compile_options_cache_file\": $!" ) );
344 0 ▶ my $fh = $compile_options_cache_file->open( '>' ) || return( $self->error( "Unable to write to sqlite compile options cache file \"$compile_options_cache_file\": ", $compile_options_cache_file->error ) );
Condition0%0/2
$compile_options_cache_file->open('>') || (return $self->error(qq[Unable to write to sqlite compile options cache file "$compile_options_cache_file": ], $compile_options_cache_file->error))
l!l
00
MC/DC0%0/1
$compile_options_cache_file->open('>') || (return $self->error(qq[Unable to write to sqlite compile options cache file "$compile_options_cache_file": ], $compile_options_cache_file->error))
$compile_options_cache_file->open('>')
Truth table0%0/2
A
A= $compile_options_cache_file->open('>')
Aresult
11
00
345 0 $fh->autoflush(1);
346 0 ▶ $fh->print( join( "\n", @options ), "\n" ) || return( $self->error( "Unable to write to the sqlite compile options cache file \"$compile_options_cache_file\": $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->print(join("\n", @options), "\n") or return $self->error(qq[Unable to write to the sqlite compile options cache file "$compile_options_cache_file": $!]) 0 0 0
347 0 $fh->close;
348 0 $COMPILE_OPTIONS = \@options;
349 0 return( \@options );
350 }
351  
352 # connect() is inherited by DB::Object
353  
354 # NOTE: sub constant_to_datatype is inherited
355  
356 # sub copy
357  
358 # sub create_table($;%)
359  
360 # See DB::Object
361 # sub data_sources($;\%)
362  
363 # sub data_type
364  
365 # sub database
366  
367 sub database_file
368 {
369 0 ▶ return( shift->{database_file} );
Subroutine database_file: not called CC=1 SCAR=6.9
Pod documented
370 }
371  
372 sub databases
373 {
374 1 ▶ my $self = shift( @_ );
Subroutine databases: called CC=4 SCAR=14.6
Pod documented
375 # return( $self->error( "Not connected to PostgreSQL server yet. Issue $dbh->connect first." ) ) if( !$self->{dbh} );
376 1 my $dbh;
377 # If there is no connection yet, then create one using the postgres login.
378 # There should not be a live user and database just to check what databases there are.
379 1 ▶ if( !$self->{dbh} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'dbh'}) { } 1 0 1
380 {
381 # try-catch
382 1 local $@;
383 $dbh = eval
384 1 {
385 1 $self->connect( @_ );
386 };
387 1 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
388 {
389 0 return( $self->error( "Error trying to connect to the SQLite database file: $@" ) );
390 }
391 1 ▶ $dbh or return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$dbh or return $self->pass_error 0 1 1
392 }
393 else
394 {
395 0 $dbh = $self;
396 }
397 1 my $temp = $dbh->do( 'PRAGMA database_list' )->fetchall_arrayref( {} );
398 1 my @dbases = map( $_->{name}, @$temp );
399 1 return( @dbases );
400 }
401  
402 # NOTE: sub datatype_dict is inherited
403  
404 # NOTE: sub datatype_to_constant is inherited
405  
406 # NOTE: sub datatypes is in inherited
407  
408 sub func
409 {
410 0 ▶ my $self = shift( @_ );
Subroutine func: not called CC=1 SCAR=6.9
Pod documented
411 0 my $table = shift( @_ );
412 # e.g. table_attributes to get the detail information on table columns
413 0 my $func_name = shift( @_ );
414 # Returns:
415 # NAME attribute name
416 # TYPE attribute type
417 # SIZE attribute size (-1 for variable size)
418 # NULLABLE flag nullable
419 # DEFAULT default value
420 # CONSTRAINT constraint
421 # PRIMARY_KEY flag is_primary_key
422 # REMARKS attribute description
423 0 return( $self->{dbh}->func( $table, $func_name ) );
424 }
425  
426 # <https://metacpan.org/pod/DBD::SQLite::Constants#datatypes-(fundamental_datatypes)>
427 # <https://www.sqlite.org/datatype3.html>
428 # In SQLite, there are only 4 types:
429 # SQLITE_INTEGER
430 # SQLITE_FLOAT
431 # SQLITE_BLOB
432 # SQLITE_NULL
433 sub get_sql_type
434 {
435 0 ▶ my $self = shift( @_ );
Subroutine get_sql_type: not called CC=6 SCAR=37.4
Pod documented
436 0 ▶ my $type = shift( @_ ) || return( $self->error( "No sql type was provided to get its constant." ) );
Condition0%0/2
shift @_ || (return $self->error('No sql type was provided to get its constant.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No sql type was provided to get its constant.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
437 0 $type = lc( $type );
438 0 ▶ if( CORE::exists( $DATATYPES_DICT->{ $type } ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $DB::Object::SQLite::DATATYPES_DICT->{$type} and $type ne $DB::Object::SQLite::DATATYPES_DICT->{$type}{"type"}) 0 0 0
Condition0%0/3
exists $DB::Object::SQLite::DATATYPES_DICT->{$type} and $type ne $DB::Object::SQLite::DATATYPES_DICT->{$type}{"type"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $DB::Object::SQLite::DATATYPES_DICT->{$type} and $type ne $DB::Object::SQLite::DATATYPES_DICT->{$type}{"type"}
exists $DB::Object::SQLite::DATATYPES_DICT->{$type} $type ne $DB::Object::SQLite::DATATYPES_DICT->{$type}{"type"}
Truth table0%0/3
A and B
A= exists $DB::Object::SQLite::DATATYPES_DICT->{$type}B= $type ne $DB::Object::SQLite::DATATYPES_DICT->{$type}{"type"}
ABresult
0X0
100
111
439 $type ne $DATATYPES_DICT->{ $type }->{type} )
440 {
441 0 $type = $DATATYPES_DICT->{ $type }->{type};
442 }
443 0 my $const;
444 0 ▶ if( substr( $type, 0, 7 ) eq 'sqlite_' )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($type, 0, 7) eq 'sqlite_') { } 0 0 0
445 {
446 0 $const = $self->{dbh}->can( "DBD::SQLite::\U${type}\E" );
447 }
448 else
449 {
450 0 $const = $self->{dbh}->can( "DBD::SQLite::SQLITE_\U${type}\E" );
451 }
452 0 ▶ return( '' ) if( !defined( $const ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $const 0 0 0
453 0 return( $const->() );
454 }
455  
456 sub having
457 {
458 1 ▶ my $self = shift( @_ );
Subroutine having: called CC=1 SCAR=0.0
Pod documented
459 1 my $q = $self->_reset_query;
460 1 return( $q->having( @_ ) );
461 }
462  
463 # https://www.sqlite.org/compile.html
464 sub has_compile_option
465 {
466 2 ▶ my $self = shift( @_ );
Subroutine has_compile_option: called CC=3 SCAR=11.2
Pod documented
467 2 ▶ my $opt = shift( @_ ) || return( $self->error( "No compile option was provided to check" ) );
Condition50%1/2
shift @_ || (return $self->error('No compile option was provided to check'))
l!l
20
MC/DC0%0/1
shift @_ || (return $self->error('No compile option was provided to check'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
468 2 $opt = uc( $opt );
469 2 my $all = $self->compile_options;
470 2 my @found = grep( /^$opt/, @$all );
471 2 ▶ return( $found[0] ) if( scalar( @found ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @found 0 2 2
472 2 return( '' );
473 }
474  
475 sub last_insert_id
476 {
477 0 ▶ my $self = shift( @_ );
Subroutine last_insert_id: not called CC=2 SCAR=17.9
Pod documented
478 0 ▶ my $table = shift( @_ ) || $self->{table};
Condition0%0/3
shift @_ || $self->{'table'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @_ || $self->{'table'}
shift @_ $self->{'table'}
Truth table0%0/3
A || B
A= shift @_B= $self->{'table'}
ABresult
1X1
011
000
479 0 return( $self->{dbh}->last_insert_id( undef, undef, $table, undef ) );
480 }
481  
482 0 ▶sub lock { return( shift->error( "Table lock is unsupported in SQLite." ) ); }
Subroutine lock: not called CC=1 SCAR=6.9
Pod documented
483  
484 sub pragma
485 {
486 0 ▶ my $self = shfit( @_ );
Subroutine pragma: not called CC=1 SCAR=6.9
Pod documented
487 0 my $key2val =
488 {
489 foreign_keys => [ qw( ON OFF ) ],
490 journal_mode => [ qw( DELETE TRUNCATE ) ],
491 legacy_file_format => [ qw( ON OFF ) ],
492 reverse_unordered_selects => [ qw( ON OFF ) ],
493 synchronous => [ qw( ON OFF ) ],
494 # To avoid corruption after BEGIN starts, DBD uses BEGIN IMMEDIATE. Default is TRUE
495 sqlite_use_immediate_transaction => [ qw( 1 0 ) ],
496 cache_size => qr/^\d+$/
497 };
498 }
499  
500 129 ▶sub query_object { return( shift->_set_get_object( 'query_object', 'DB::Object::SQLite::Query', @_ ) ); }
Subroutine query_object: called CC=1 SCAR=0.0
Pod documented
501  
502 # https://www.sqlite.org/lang_replace.html
503 # https://www.sqlite.org/lang_conflict.html
504 # REPLACE is an alias for INSERT OR REPLACE
505 # https://www.sqlite.org/lang_insert.html
506 sub replace
507 {
508 0 ▶ my $self = shift( @_ );
Subroutine replace: not called CC=18 SCAR=58.3
Pod documented
509 0 ▶ my $data = shift( @_ ) if( @_ == 1 && ref( $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and 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
510 0 my @arg = @_;
511 0 my %arg = ();
512 0 my $select = '';
513 0 ▶ if( !%arg && $data && $self->_is_hash( $data => 'strict' ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not %arg and $data and $self->_is_hash($data, 'strict')) { } 0 0 0
elsif ($data and ref $data eq 'DB::Object::Statement') { } 0 0 0
Condition0%0/6
not %arg and $data and $self->_is_hash($data, "strict")
!ll&&!rl&&r
000
not %arg and $data
!ll&&!rl&&r
000
MC/DC0%0/3
not %arg and $data and $self->_is_hash($data, "strict")
not %arg $data $self->_is_hash($data, "strict")
Truth table0%0/4
not(A) and B and C
A= not %argB= $dataC= $self->_is_hash($data, "strict")
ABCresult
0XX0
10X0
1100
1111
514 {
515 0 %arg = %$data;
516 }
517 ▶ elsif( $data && ref( $data ) eq 'DB::Object::Statement' )
Condition0%0/3
$data and ref $data eq "DB::Object::Statement"
!ll&&!rl&&r
000
MC/DC0%0/2
$data and ref $data eq "DB::Object::Statement"
$data ref $data eq "DB::Object::Statement"
Truth table0%0/3
A and B
A= $dataB= ref $data eq "DB::Object::Statement"
ABresult
0X0
100
111
518 {
519 0 $select = $data->as_string();
520 }
521 0 ▶ %arg = @arg if( @arg );
Branch: 1 branch
BranchTrueFalseTotal
if @arg 0 0 0
522 my $table = $self->{table} ||
523 0 ▶ return( $self->error( "No table was provided to replace data." ) );
Condition0%0/2
$self->{'table'} || (return $self->error('No table was provided to replace data.'))
l!l
00
MC/DC0%0/1
$self->{'table'} || (return $self->error('No table was provided to replace data.'))
$self->{'table'}
Truth table0%0/2
A
A= $self->{'table'}
Aresult
11
00
524 0 ▶ my $structure = $self->structure || return( $self->pass_error );
Condition0%0/2
$self->structure || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->structure || (return $self->pass_error)
$self->structure
Truth table0%0/2
A
A= $self->structure
Aresult
11
00
525 0 my $null = $self->null();
526 0 my @avoid = ();
527 0 foreach my $field ( keys( %$structure ) )
528 {
529 # It is useless to insert a blank data in a field whose default value is NULL.
530 # Especially since a test on a NULL field may be made specifically.
531 0 ▶ push( @avoid, $field ) if( !CORE::exists( $arg{ $field } ) && $null->{ $field } );
Branch: 1 branch
BranchTrueFalseTotal
if not exists $arg{$field} and $null->{$field} 0 0 0
Condition0%0/3
not exists $arg{$field} and $null->{$field}
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $arg{$field} and $null->{$field}
not exists $arg{$field} $null->{$field}
Truth table0%0/3
not(A) and B
A= not exists $arg{$field}B= $null->{$field}
ABresult
0X0
100
111
532 }
533 0 my $db_data = $self->getdefault({
534 table => $table,
535 arg => \@arg,
536 avoid => \@avoid
537 });
538 0 my( $fields, $values ) = $db_data->format_statement();
539 0 $self->_reset_query();
540 0 delete( $self->{query_reset} );
541 0 $self->{binded_values} = $db_data->{binded_values};
542 0 ▶ my $query = $self->{query} = $select ? "REPLACE INTO $table $select" : "REPLACE INTO $table ($fields) VALUES($values)";
Branch: 1 branch
BranchTrueFalseTotal
$select ? : 0 0 0
543 # Everything meaningfull lies within the object
544 # If no bind should be done _save_bind does nothing
545 0 $self->_save_bind();
546 # Query string should lie within the object
547 # _cache_this sends back an object no matter what or unde() if an error occurs
548 0 my $sth = $self->_cache_this($query);
549 # STOP! No need to go further
550 0 ▶ if( !defined( $sth ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sth) 0 0 0
551 {
552 0 return( $self->error( "Error while preparing query to replace data into table '$table':\n$query", $self->errstr() ) );
553 }
554 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
555 {
556 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing query to replace data to table '${table}':\n$query") 0 0 0
557 return( $self->error( "Error while executing query to replace data to table '$table':\n$query" ) );
558 }
559 0 return( $sth );
560 }
561  
562 sub register_function
563 {
564 0 ▶ my $self = shift( @_ );
Subroutine register_function: not called CC=9 SCAR=45.0
Pod documented
565 0 ▶ return( $self->error( "I was expecting an hash reference of parameters as only argument." ) ) if( !$self->_is_hash( $_[0] => 'strict' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_hash($_[0], "strict") 0 0 0
566 0 my $opts = shift( @_ );
567 0 ▶ return( $self->error( "Parameter 'code' to register the private function must be a code reference." ) ) if( ref( $opts->{code} ) ne 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts->{"code"} ne "CODE" 0 0 0
568 0 ▶ $opts->{flags} = [] if( !CORE::exists( $opts->{flags} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"flags"} 0 0 0
569 0 ▶ return( $self->error( "Parameter 'flags' must be an array reference." ) ) if( !$self->_is_array( $opts->{flags} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_array($opts->{"flags"}) 0 0 0
570 0 ▶ $opts->{argc} = -1 if( !CORE::exists( $opts->{argc} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"argc"} 0 0 0
571 0 ▶ return( $self->error( "No name was provided for this private function." ) ) if( !CORE::exists( $opts->{name} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"name"} 0 0 0
572 0 my $name = $opts->{name};
573 0 ▶ return( $self->error( "Name provided for this private function \"$name\" contains illegal characters. Only alphabetical and numerical characters and underscore allowed with name starting with a letter." ) ) if( $name !~ /^[a-zA-Z][a-zA-Z0-9\_]+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[a-zA-Z][a-zA-Z0-9\_]+$/ 0 0 0
574 0 my $funcs = $self->{func};
575 0 $funcs->{ lc( $opts->{name} ) } = $opts;
576 0 ▶ $self->sql_function_register( $opts ) if( $self->{dbh} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dbh"} 0 0 0
577 0 return( $opts );
578 }
579  
580 sub remove_function
581 {
582 0 ▶ my $self = shift( @_ );
Subroutine remove_function: not called CC=3 SCAR=24.8
Pod documented
583 0 ▶ my $name = shift( @_ ) || return( $self->error( "No private function was provided to be removed." ) );
Condition0%0/2
shift @_ || (return $self->error('No private function was provided to be removed.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No private function was provided to be removed.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
584 0 my $funcs = $self->{func};
585 0 $name = lc( $name );
586 0 ▶ return( 0 ) if( !CORE::exists( $funcs->{ $name } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $funcs->{$name} 0 0 0
587 0 return( CORE::delete( $funcs->{ $name } ) );
588 }
589  
590 sub returning
591 {
592 0 ▶ my $self = shift( @_ );
Subroutine returning: not called CC=1 SCAR=6.9
Pod documented
593 0 my $q = $self->_reset_query;
594 0 return( $q->returning( @_ ) );
595 }
596  
597 sub rollback
598 {
599 0 ▶ return( shift->{dbh}->rollback() );
Subroutine rollback: not called CC=1 SCAR=6.9
Pod documented
600 }
601  
602 sub sql_function_register
603 {
604 68 ▶ my $self = shift( @_ );
Subroutine sql_function_register: called CC=6 SCAR=21.9
Pod documented
605 68 ▶ my $opts = shift( @_ ) || return( $self->error( "No private function hash reference provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No private function hash reference provided.'))
l!l
680
MC/DC0%0/1
shift @_ || (return $self->error('No private function hash reference provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
606 68 ▶ my $dbh = $self->{dbh} || return( $self->error( "No active database handler available." ) );
Condition50%1/2
$self->{'dbh'} || (return $self->error('No active database handler available.'))
l!l
680
MC/DC0%0/1
$self->{'dbh'} || (return $self->error('No active database handler available.'))
$self->{'dbh'}
Truth table50%1/2
A
A= $self->{'dbh'}
Aresult
11
00
607 68 ▶ $opts->{flags} = [] if( !exists( $opts->{flags} ) || ref( $opts->{flags} ) ne 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if not exists $opts->{"flags"} or ref $opts->{"flags"} ne "ARRAY" 68 0 68
Condition33%1/3
not exists $opts->{"flags"} or ref $opts->{"flags"} ne "ARRAY"
l!l&&r!l&&!r
6800
MC/DC0%0/2
not exists $opts->{"flags"} or ref $opts->{"flags"} ne "ARRAY"
not exists $opts->{"flags"} ref $opts->{"flags"} ne "ARRAY"
Truth table33%1/3
not(A) or B
A= not exists $opts->{"flags"}B= ref $opts->{"flags"} ne "ARRAY"
ABresult
1X1
011
000
608 68 my $flag;
609 68 my $eval = join( '|', @{$opts->{flags}} );
610 68 $flag = eval( $eval );
611 68 my $code = $opts->{code};
612 68 ▶ if( defined( $flag ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag) { } 0 68 68
613 {
614 0 ▶ $dbh->sqlite_create_function( $opts->{name}, $opts->{argc}, sub{ my @arg = @_; unshift( @arg, $self ); $code->( @arg ); }, $flag );
Subroutine __ANON__: not called CC=1 SCAR=6.9
615 }
616 else
617 {
618 68 ▶ $dbh->sqlite_create_function( $opts->{name}, $opts->{argc}, sub{ my @arg = @_; unshift( @arg, $self ); $code->( @arg ); } );
Subroutine __ANON__: not called CC=1 SCAR=3.5
619 }
620 68 $opts->{_registered_on} = time();
621 }
622  
623 # http://www.sqlite.org/c3ref/c_status_malloc_count.html
624 sub stat
625 {
626 0 ▶ my $self = shift( @_ );
Subroutine stat: not called CC=7 SCAR=40.3
Pod documented
627 0 my $opt = $self->_get_args_as_hash( @_ );
628 0 ▶ my $ref = $opt->{reset} ? DBD::SQLite::sqlite_status( 0 ) : DBD::SQLite::sqlite_status();
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'reset'} ? : 0 0 0
629 0 ▶ if( $opt->{type} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'type'}) { } 0 0 0
630 {
631 0 ▶ return( exists( $ref->{ $opt->{type} } ) ? $ref->{ $opt->{type} } : undef() );
Branch: 1 branch
BranchTrueFalseTotal
exists $$ref{$opt->{'type'}} ? : 0 0 0
632 }
633 else
634 {
635 0 ▶ return( wantarray() ? () : undef() ) if( !%$ref );
Branch: 2 branches
BranchTrueFalseTotal
unless %$ref 0 0 0
wantarray ? : 0 0 0
636 0 ▶ return( wantarray() ? %$ref : $ref );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
637 }
638 }
639  
640 # sub table_exists
641 # {
642 # my $self = shift( @_ );
643 # my $table = shift( @_ ) ||
644 # return( $self->error( "You must provide a table name to access the table methods." ) );
645 # my $cache_tables = $self->cache_tables;
646 # my $tables_in_cache = $cache_tables->get({
647 # host => 'localhost',
648 # driver => $self->driver,
649 # port => 0,
650 # database => $self->database,
651 # });
652 # foreach my $ref ( @$tables_in_cache )
653 # {
654 # return( 1 ) if( $ref->{name} eq $table );
655 # }
656 # # We did not find it, so let's try by checking directly the database
657 # my $def = $self->table_info( $table ) || return;
658 # return(0) if( !scalar( @$def ) );
659 # return(1);
660 # }
661  
662 sub table_info
663 {
664 4 ▶ my $self = shift( @_ );
Subroutine table_info: called CC=6 SCAR=18.3
Pod documented
665 4 ▶ my $table = shift( @_ ) ||
Condition50%1/2
shift @_ || (return $self->error('You must provide a table name to access the table methods.'))
l!l
40
MC/DC0%0/1
shift @_ || (return $self->error('You must provide a table name to access the table methods.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
666 return( $self->error( "You must provide a table name to access the table methods." ) );
667 4 my $opts = $self->_get_args_as_hash( @_ );
668 4 my $sql = <<'EOT';
669 SELECT
670 name
671 ,type
672 FROM sqlite_master
673 WHERE type IN ('table', 'view') AND name = ?
674 EOT
675 4 ▶ my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) );
Condition50%1/2
$self->{'dbh'} || (return $self->error('Could not find database handler.'))
l!l
40
MC/DC0%0/1
$self->{'dbh'} || (return $self->error('Could not find database handler.'))
$self->{'dbh'}
Truth table50%1/2
A
A= $self->{'dbh'}
Aresult
11
00
676 4 ▶ my $sth = $dbh->prepare_cached( $sql ) || return( $self->error( "An error occured while preparing query to check if table \"$table\" exists in database \"", $self->database, "\": ", $dbh->errstr ) );
Condition50%1/2
$dbh->prepare_cached($sql) || (return $self->error(qq[An error occured while preparing query to check if table "$table" exists in database "], $self->database, '": ', $dbh->errstr))
l!l
40
MC/DC0%0/1
$dbh->prepare_cached($sql) || (return $self->error(qq[An error occured while preparing query to check if table "$table" exists in database "], $self->database, '": ', $dbh->errstr))
$dbh->prepare_cached($sql)
Truth table50%1/2
A
A= $dbh->prepare_cached($sql)
Aresult
11
00
677 4 ▶ $sth->execute( $table ) || return( $self->error( "An error occured while executing query to check if table \"$table\" exists in database \"", $self->database, "\: ", $sth->errstr ) );
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($table) or return $self->error(qq[An error occured while executing query to check if table "$table" exists in database "], $self->database, ": ", $sth->errstr) 0 4 4
678 4 my $all = $sth->fetchall_arrayref( {} );
679 4 $sth->finish;
680 4 ▶ return( {} ) if( !scalar( @$all ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$all 1 3 4
681 3 return( $all->[0] );
682 }
683  
684 sub tables
685 {
686 9 ▶ my $self = shift( @_ );
Subroutine tables: called CC=3 SCAR=11.8
Pod documented
687 9 ▶ my $db = shift( @_ ) || $self->{database};
Condition33%1/3
shift @_ || $self->{'database'}
l!l&&r!l&&!r
090
MC/DC0%0/2
shift @_ || $self->{'database'}
shift @_ $self->{'database'}
Truth table33%1/3
A || B
A= shift @_B= $self->{'database'}
ABresult
1X1
011
000
688 9 ▶ my $all = $self->tables_info || return;
Condition50%1/2
$self->tables_info || (return)
l!l
90
MC/DC0%0/1
$self->tables_info || (return)
$self->tables_info
Truth table50%1/2
A
A= $self->tables_info
Aresult
11
00
689 9 my @tables = map( $_->{name}, @$all );
690 # return( wantarray() ? () : undef() ) if( !@tables );
691 # return( wantarray() ? @tables : \@tables );
692 9 return( \@tables );
693 }
694  
695 sub tables_info
696 {
697 11 ▶ my $self = shift( @_ );
Subroutine tables_info: called CC=5 SCAR=17.3
Pod documented
698 11 ▶ my $db = shift( @_ ) || $self->{database};
Condition33%1/3
shift @_ || $self->{'database'}
l!l&&r!l&&!r
0110
MC/DC0%0/2
shift @_ || $self->{'database'}
shift @_ $self->{'database'}
Truth table33%1/3
A || B
A= shift @_B= $self->{'database'}
ABresult
1X1
011
000
699 # Parameters are: ?, schema, table, and type
700 # my $sth = $self->{dbh}->table_info( undef, undef, $table, "TABLE,VIEW" );
701 # The original query was fetched by connecting to Postgres with psql -E and executing the command \z
702 # This revised query will fetch only tables, views, materialised view and foreign tables, but will avoid the mysterious view called sequence_setvals
703 # https://stackoverflow.com/questions/82875/how-to-list-the-tables-in-a-sqlite-database-file-that-was-opened-with-attach
704 11 my $query = <<SQL;
705 SELECT name FROM sqlite_master
706 WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'
707 UNION ALL
708 SELECT name FROM sqlite_temp_master
709 WHERE type IN ('table','view')
710 ORDER BY 1
711 SQL
712 11 ▶ my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) );
Condition50%1/2
$self->{'dbh'} || (return $self->error('Could not find database handler.'))
l!l
110
MC/DC0%0/1
$self->{'dbh'} || (return $self->error('Could not find database handler.'))
$self->{'dbh'}
Truth table50%1/2
A
A= $self->{'dbh'}
Aresult
11
00
713 11 ▶ my $sth = $dbh->prepare_cached( $query ) || return( $self->error( sprintf( "Error while preparing query $query: %s", $dbh->errstr ) ) );
Condition50%1/2
$dbh->prepare_cached($query) || (return $self->error(sprintf("Error while preparing query ${query}: %s", $dbh->errstr)))
l!l
110
MC/DC0%0/1
$dbh->prepare_cached($query) || (return $self->error(sprintf("Error while preparing query ${query}: %s", $dbh->errstr)))
$dbh->prepare_cached($query)
Truth table50%1/2
A
A= $dbh->prepare_cached($query)
Aresult
11
00
714 11 ▶ $sth->execute() || return( $self->error( sprintf( "Error while executing query $query: %s", $sth->errstr ) ) );
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error(sprintf("Error while executing query ${query}: %s", $sth->errstr)) 0 11 11
715 11 my $all = $sth->fetchall_arrayref( {} );
716 11 return( $all );
717 }
718  
719 0 ▶sub trace { return( shift->error( "Trace is unsupported on SQLite." ) ); }
Subroutine trace: not called CC=1 SCAR=6.9
Pod documented
720  
721 0 ▶sub unlock { return( shift->error( "unlock() does not work with SQLite." ) ); }
Subroutine unlock: not called CC=1 SCAR=6.9
Pod documented
722  
723 sub variables
724 {
725 0 ▶ return( shift->error( "variables is currently unsupported in Postgres" ) );
Subroutine variables: not called CC=1 SCAR=6.9
Pod documented
726 }
727  
728 # https://www.sqlite.org/versionnumbers.html
729 sub version
730 {
731 0 ▶ my $self = shift( @_ );
Subroutine version: not called CC=3 SCAR=24.8
Pod documented
732 # If we already have the information, let's use our cache instead of making a query
733 0 ▶ return( $self->{_db_version} ) if( length( $self->{_db_version} ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"_db_version"} 0 0 0
734 0 my $sql = 'SELECT sqlite_version()';
735 0 ▶ my $sth = $self->do( $sql ) || return( $self->error( "Unable to issue the sql statement '$sql' to get the server version: ", $self->errstr ) );
Condition0%0/2
$self->do($sql) || (return $self->error("Unable to issue the sql statement '${sql}' to get the server version: ", $self->errstr))
l!l
00
MC/DC0%0/1
$self->do($sql) || (return $self->error("Unable to issue the sql statement '${sql}' to get the server version: ", $self->errstr))
$self->do($sql)
Truth table0%0/2
A
A= $self->do($sql)
Aresult
11
00
736 0 my $ver = $sth->fetchrow;
737 0 $sth->finish;
738 # We cache it
739 0 $self->{_db_version} = version->parse( $ver );
740 0 return( $ver );
741 }
742  
743 sub _check_connect_param
744 {
745 2 ▶ my $self = shift( @_ );
Subroutine _check_connect_param: called CC=10 SCAR=25.1
746 2 my $param = $self->SUPER::_check_connect_param( @_ );
747 2 ▶ if( !$param->{database_file} && $param->{database} )
Branch: 1 branch
BranchTrueFalseTotal
if (not $param->{"database_file"} and $param->{"database"}) 2 0 2
Condition33%1/3
not $param->{"database_file"} and $param->{"database"}
!ll&&!rl&&r
002
MC/DC0%0/2
not $param->{"database_file"} and $param->{"database"}
not $param->{"database_file"} $param->{"database"}
Truth table33%1/3
not(A) and B
A= not $param->{"database_file"}B= $param->{"database"}
ABresult
0X0
100
111
748 {
749 2 my( $filename, $path, $ext );
750 2 ▶ my $uri = CORE::exists( $param->{uri} ) ? $param->{uri} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $param->{'uri'} ? : 2 0 2
751 2 ▶ my $db = $param->{database} ? $param->{database} : ( $uri->path_segments )[-1];
Branch: 1 branch
BranchTrueFalseTotal
$param->{'database'} ? : 2 0 2
752 2 ▶ $path = $uri ? $uri->path : $db;
Branch: 1 branch
BranchTrueFalseTotal
$uri ? : 2 0 2
753 # $db = Cwd::abs_path( $uri ? $uri->path : $db );
754 # $db = File::Spec->rel2abs( $path );
755 2 $db = $self->new_file( $path );
756 # If we cannot find the file and it does not end with .sqlite, let's add the extension
757 # So the user can provide the database parameter just like database => 'test' or database => './test'
758 2 ▶ $db = "$db.sqlite" if( !-e( $db ) && $db !~ /\.sqlite$/i );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $db or $db =~ /\.sqlite$/i 0 2 2
Condition66%2/3
-e $db or $db =~ /\.sqlite$/i
l!l&&r!l&&!r
110
MC/DC0%0/2
-e $db or $db =~ /\.sqlite$/i
-e $db $db =~ /\.sqlite$/i
Truth table66%2/3
A or B
A= -e $dbB= $db =~ /\.sqlite$/i
ABresult
1X1
011
000
759 # ( $filename, $path, $ext ) = File::Basename::fileparse( $db, qr/\.[^\.]+$/ );
760 2 ( $filename, $path, $ext ) = $db->baseinfo( qr/\.[^\.]+$/ );
761 2 $param->{database} = $filename;
762 2 $param->{database_file} = $self->{database_file} = $db;
763 }
764 2 ▶ $param->{host} = 'localhost' if( !length( $param->{host} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $param->{"host"} 2 0 2
765 2 ▶ $param->{port} = 0 if( !CORE::exists( $param->{port} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param->{"port"} 2 0 2
766 2 return( $param );
767 }
768  
769 sub _check_default_option
770 {
771 2 ▶ my $self = shift( @_ );
Subroutine _check_default_option: called CC=3 SCAR=11.0
772 2 my $opts = $self->_get_args_as_hash( @_ );
773 2 ▶ return( $self->error( "Provided option is not a hash reference." ) ) if( !$self->_is_hash( $opts => 'strict' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_hash($opts, "strict") 0 2 2
774 2 ▶ $opts->{sqlite_unicode} = 1 if( !CORE::exists( $opts->{sqlite_unicode} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"sqlite_unicode"} 1 1 2
775 2 return( $opts );
776 }
777  
778 sub _connection_options
779 {
780 2 ▶ my $self = shift( @_ );
Subroutine _connection_options: called CC=1 SCAR=0.0
781 2 my $param = shift( @_ );
782 2 my @sqlite_params = grep( /^sqlite_/, keys( %$param ) );
783 2 my $opt = $self->SUPER::_connection_options( $param );
784 2 @$opt{ @sqlite_params } = @$param{ @sqlite_params };
785 2 return( $opt );
786 }
787  
788 sub _connection_params2hash_driver
789 {
790 1 ▶ my $self = shift( @_ );
Subroutine _connection_params2hash_driver: called CC=2 SCAR=7.0
791 1 my $opts = $self->_get_args_as_hash( @_ );
792 1 ▶ $opts->{sqlite_unicode} = 1 if( !length( $opts->{sqlite_unicode} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $opts->{"sqlite_unicode"} 1 0 1
793 1 return( $opts );
794 }
795  
796 sub _connection_parameters
797 {
798 2 ▶ my $self = shift( @_ );
Subroutine _connection_parameters: called CC=1 SCAR=0.0
799 2 my $param = shift( @_ );
800 # Even though login, password, server, host are not used, I was hesitating, but decided to leave them as ok, and ignore them
801 # Or maybe should I issue an error when they are provided?
802 2 my $core = [qw( db login passwd host port driver database server opt uri debug cache_connections cache_dir cache_query cache_table unknown_field use_cache )];
803 2 my @sqlite_params = grep( /^sqlite_/, keys( %$param ) );
804 # See DBD::SQLite for the list of valid parameters
805 # E.g.: sqlite_open_flags sqlite_busy_timeout sqlite_use_immediate_transaction sqlite_see_if_its_a_number sqlite_allow_multiple_statements sqlite_unprepared_statements sqlite_unicode sqlite_allow_multiple_statements sqlite_use_immediate_transaction
806 2 push( @$core, @sqlite_params );
807 2 return( $core );
808 }
809  
810 sub _dbi_connect
811 {
812 2 ▶ my $self = shift( @_ );
Subroutine _dbi_connect: called CC=4 SCAR=13.9
813 2 my $dbh = $self->{dbh} = $self->SUPER::_dbi_connect( @_ );
814 # my $func = $self->{_func};
815 2 my $func = $self->{_func};
816 2 foreach my $k ( sort( keys( %$PRIVATE_FUNCTIONS ) ) )
817 {
818 68 my $this = $PRIVATE_FUNCTIONS->{ $k };
819 68 my $ref =
820 {
821 name => $k,
822 argc => $this->[0],
823 code => $this->[1],
824 };
825 68 $func->{ $k } = $ref;
826 }
827 2 foreach my $name ( sort( keys( %$func ) ) )
828 {
829 68 my $ref = $func->{ $name };
830 68 ▶ if( $ref->{_registered_on} )
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"_registered_on"}) 0 68 68
831 {
832 0 next;
833 }
834 68 $self->sql_function_register( $ref );
835 68 $ref->{_registered_on} = time();
836 }
837 2 return( $dbh );
838 }
839  
840 sub _dsn
841 {
842 2 ▶ my $self = shift( @_ );
Subroutine _dsn: called CC=4 SCAR=14.3
843 2 ▶ my $db = $self->{database_file} || return( $self->error( "No database file was specified." ) );
Condition50%1/2
$self->{'database_file'} || (return $self->error('No database file was specified.'))
l!l
20
MC/DC0%0/1
$self->{'database_file'} || (return $self->error('No database file was specified.'))
$self->{'database_file'}
Truth table50%1/2
A
A= $self->{'database_file'}
Aresult
11
00
844 # return( $self->error( "Database file \"$db\" does not exist." ) ) if( !-e( $db ) );
845 2 ▶ return( $self->error( "Database file \"$db\" is not writable." ) ) if( -e( $db ) && !-w( $db ) );
Branch: 1 branch
BranchTrueFalseTotal
if -e $db and not -w $db 0 2 2
Condition66%2/3
-e $db and not -w $db
!ll&&!rl&&r
110
MC/DC0%0/2
-e $db and not -w $db
-e $db not -w $db
Truth table66%2/3
A and not(B)
A= -e $dbB= not -w $db
ABresult
0X0
100
111
846 2 my @params = ( sprintf( 'dbi:%s:', $self->{driver} ) );
847 2 push( @params, sprintf( 'dbname=%s', $db ) );
848 2 return( join( ';', @params ) );
849 }
850  
851 sub _parse_timestamp
852 {
853 15 ▶ my $self = shift( @_ );
Subroutine _parse_timestamp: called CC=8 SCAR=25.2
854 15 my $str = shift( @_ );
855 # No value was actually provided
856 15 ▶ return if( !length( $str ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $str 0 15 15
857 # try-catch
858 15 local $@;
859 my $tz = eval
860 15 {
861 15 return( DateTime::TimeZone->new( name => 'local' ) );
862 };
863 15 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 15 0 15
864 {
865 15 $tz = DateTime::TimeZone->new( name => 'UTC' );
866 }
867 15 my $error = 0;
868 my $opt =
869 {
870 pattern => '%Y-%m-%d %T',
871 locale => 'en_GB',
872 time_zone => $tz->name,
873 0 ▶ on_error => sub{ $error++ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
874 15 };
875 # 2019-06-19 23:23:57.000000000+0900
876 # From PostgreSQL: 2019-06-20 11:02:36.306917+09
877 # ISO 8601: 2019-06-20T11:08:27
878 15 ▶ if( $str =~ /(\d{4})[-|\/](\d{1,2})[-|\/](\d{1,2})(?:[[:blank:]]+|T)(\d{1,2}:\d{1,2}:\d{1,2})(?:\.\d+)?((?:\+|\-)\d{2,4})?/ )
Branch: 3 branches
BranchTrueFalseTotal
if ($str =~ m[(\d{4})[-|/](\d{1,2})[-|/](\d{1,2})(?:[[:blank:]]+|T)(\d{1,2}:\d{1,2}:\d{1,2})(?:\.\d+)?((?:\+|\-)\d{2,4})?]) { } 14 1 15
elsif ($str =~ m[(\d{4})[-|/](\d{1,2})[-|/](\d{1,2})(?:[[:blank:]]+|T)(\d{1,2}:\d{1,2}:\d{1,2})]) { } 0 1 1
elsif ($str =~ m[^(\d{4})[-|/](\d{1,2})[-|/](\d{1,2})$]) { } 1 0 1
879 {
880 14 my( $date, $time, $zone ) = ( "$1-$2-$3", $4, $5 );
881 14 ▶ if( !length( $zone ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (length $zone) 0 14 14
882 {
883 0 my $dt = DateTime->now( time_zone => $tz );
884 0 my $offset = $dt->offset;
885 # e.g. 9 or possibly 9.5
886 0 my $offset_hour = ( $offset / 3600 );
887 # e.g. 9.5 => 0.5 * 60 = 30
888 0 my $offset_min = ( $offset_hour - CORE::int( $offset_hour ) ) * 60;
889 0 $zone = sprintf( '%+03d%02d', $offset_hour, $offset_min );
890 }
891 14 $date =~ tr/\//-/;
892 14 ▶ $zone .= '00' if( length( $zone ) == 3 );
Branch: 1 branch
BranchTrueFalseTotal
if length $zone == 3 0 14 14
893 14 $str = "$date $time$zone";
894 14 $opt->{pattern} = '%Y-%m-%d %T%z';
895 }
896 # From SQLite: 2019-06-20 02:03:14
897 # From MySQL: 2019-06-20 11:04:01
898 elsif( $str =~ /(\d{4})[-|\/](\d{1,2})[-|\/](\d{1,2})(?:[[:blank:]]+|T)(\d{1,2}:\d{1,2}:\d{1,2})/ )
899 {
900 0 my( $date, $time ) = ( "$1-$2-$3", $4 );
901 0 my $dt = DateTime->now( time_zone => $tz );
902 0 my $offset = $dt->offset;
903 # e.g. 9 or possibly 9.5
904 0 my $offset_hour = ( $offset / 3600 );
905 # e.g. 9.5 => 0.5 * 60 = 30
906 0 my $offset_min = ( $offset_hour - CORE::int( $offset_hour ) ) * 60;
907 0 my $offset_str = sprintf( '%+03d%02d', $offset_hour, $offset_min );
908 0 $date =~ tr/\//-/;
909 0 $str = "$date $time$offset_str";
910 0 $opt->{pattern} = '%Y-%m-%d %T%z';
911 }
912 elsif( $str =~ /^(\d{4})[-|\/](\d{1,2})[-|\/](\d{1,2})$/ )
913 {
914 1 $str = "$1-$2-$3";
915 1 $opt->{pattern} = '%Y-%m-%d';
916 }
917 15 my $strp = DateTime::Format::Strptime->new( %$opt );
918 15 my $dt = $strp->parse_datetime( $str );
919 15 return( $dt );
920 }
921  
922 # Private function
923 sub _ceiling
924 {
925 1 ▶ my $self = shift( @_ );
Subroutine _ceiling: called CC=1 SCAR=0.0
926 1 my @args = @_;
927 1 return( POSIX::ceil( $args[0] ) );
928 }
929  
930 sub _concat
931 {
932 1 ▶ my $self = shift( @_ );
Subroutine _concat: called CC=1 SCAR=0.0
933 1 my @args = @_;
934 1 return( join( '', @args ) );
935 }
936  
937 sub _curdate
938 {
939 1 ▶ my $self = shift( @_ );
Subroutine _curdate: called CC=2 SCAR=6.9
940 1 my @args = @_;
941  
942 # try-catch
943 1 local $@;
944 my $tz = eval
945 1 {
946 1 return( DateTime::TimeZone->new( name => 'local' ) );
947 };
948 1 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
949 {
950 1 $tz = DateTime::TimeZone->new( name => 'UTC' );
951 }
952 1 my $d = DateTime->from_epoch( epoch => time(), time_zone => $tz->name );
953 1 return( $d->ymd( '-' ) );
954 }
955  
956 sub _curtime
957 {
958 1 ▶ my $self = shift( @_ );
Subroutine _curtime: called CC=2 SCAR=6.9
959 1 my @args = @_;
960 # try-catch
961 1 local $@;
962 my $tz = eval
963 1 {
964 1 return( DateTime::TimeZone->new( name => 'local' ) );
965 };
966 1 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
967 {
968 1 $tz = DateTime::TimeZone->new( name => 'UTC' );
969 }
970 1 my $d = DateTime->now( time_zone => $tz->name );
971 1 return( $d->hms( ':' ) );
972 }
973  
974 # e.g. Monday
975 sub _dayname
976 {
977 1 ▶ my $self = shift( @_ );
Subroutine _dayname: called CC=2 SCAR=7.0
978 1 my @args = @_;
979 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
980 1 return( $dt->day_name );
981 }
982  
983 # E.g.: 17
984 sub _dayofmonth
985 {
986 1 ▶ my $self = shift( @_ );
Subroutine _dayofmonth: called CC=2 SCAR=7.0
987 1 my @args = @_;
988 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
989 1 return( $dt->day );
990 }
991  
992 # timestamp, [integer] 1 to 7
993 sub _dayofweek
994 {
995 1 ▶ my $self = shift( @_ );
Subroutine _dayofweek: called CC=2 SCAR=7.0
996 1 my @args = @_;
997 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
998 1 return( $dt->day_of_week );
999 }
1000  
1001 # E.g.: 170
1002 sub _dayofyear
1003 {
1004 1 ▶ my $self = shift( @_ );
Subroutine _dayofyear: called CC=2 SCAR=7.0
1005 1 my @args = @_;
1006 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
1007 1 return( $dt->day_of_year );
1008 }
1009  
1010 # http://stackoverflow.com/questions/10034636/postgres-longitude-longitude-query
1011 sub _distance_miles
1012 {
1013 1 ▶ my $self = shift( @_ );
Subroutine _distance_miles: called CC=1 SCAR=0.0
1014 1 my @args = @_;
1015 1 my( $lat1, $lon1, $lat2, $lon2 ) = @args;
1016 1 my $x = 69.1 * ( $lat2 - $lat1 );
1017 1 my $y = 69.1 * ( $lon2 - $lon1 ) * cos( $lat1 / 57.3 );
1018 1 return( sqrt( $x * $x + $y * $y ) );
1019 }
1020  
1021 sub _from_days
1022 {
1023 1 ▶ my $self = shift( @_ );
Subroutine _from_days: called CC=2 SCAR=6.9
1024 1 my @args = @_;
1025 1 my $from_days = $args[0];
1026 # try-catch
1027 1 local $@;
1028 my $tz = eval
1029 1 {
1030 1 return( DateTime::TimeZone->new( name => 'local' ) );
1031 };
1032 1 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
1033 {
1034 1 $tz = DateTime::TimeZone->new( name => 'UTC' );
1035 }
1036 1 my $origin = DateTime->new(
1037 year => 0,
1038 month => 1,
1039 day => 1,
1040 hour => 0,
1041 minute => 0,
1042 second => 0,
1043 time_zone => $tz,
1044 );
1045 1 my $epoch = DateTime->from_epoch( epoch => 0, time_zone => $tz );
1046 # https://stackoverflow.com/questions/821423/how-can-i-calculate-the-number-of-days-between-two-dates-in-perl#7111718
1047 1 my $epoch_days = $epoch->delta_days( $origin )->delta_days();
1048 1 my $days_since_epoch = $from_days - int( $epoch_days );
1049 1 my $dt = DateTime->from_epoch( epoch => ( $days_since_epoch * 86400 ), time_zone => $tz );
1050 1 return( $dt );
1051 }
1052  
1053 sub _from_unixtime
1054 {
1055 1 ▶ my $self = shift( @_ );
Subroutine _from_unixtime: called CC=3 SCAR=11.1
1056 1 my @args = @_;
1057 1 ▶ return if( $args[0] !~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $args[0] =~ /^\d+$/ 0 1 1
1058 # try-catch
1059 1 local $@;
1060 my $tz = eval
1061 1 {
1062 1 return( DateTime::TimeZone->new( name => 'local' ) );
1063 };
1064 1 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
1065 {
1066 1 $tz = DateTime::TimeZone->new( name => 'UTC' );
1067 }
1068 1 my $dt = DateTime->from_epoch( epoch => $args[0], time_zone => $tz->name );
1069 1 return( $dt->strftime( '%Y-%m-%d %T%z' ) );
1070 }
1071  
1072 sub _hour
1073 {
1074 1 ▶ my $self = shift( @_ );
Subroutine _hour: called CC=2 SCAR=7.0
1075 1 my @args = @_;
1076 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
1077 1 return( $dt->hour );
1078 }
1079  
1080 sub _lcase
1081 {
1082 1 ▶ my $self = shift( @_ );
Subroutine _lcase: called CC=1 SCAR=0.0
1083 1 my @args = @_;
1084 1 return( lc( $args[0] ) );
1085 }
1086  
1087 sub _left
1088 {
1089 1 ▶ my $self = shift( @_ );
Subroutine _left: called CC=2 SCAR=7.0
1090 1 my @args = @_;
1091 1 ▶ return( $args[0] ) if( $args[1] !~ /^\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $args[1] =~ /^\d+$/ 0 1 1
1092 1 return( substr( $args[0], 0, $args[1] ) );
1093 }
1094  
1095 sub _locate
1096 {
1097 1 ▶ my $self = shift( @_ );
Subroutine _locate: called CC=1 SCAR=0.0
1098 1 my @args = @_;
1099 1 return( CORE::index( $args[0], $args[1] ) );
1100 }
1101  
1102 sub _log10
1103 {
1104 1 ▶ my $self = shift( @_ );
Subroutine _log10: called CC=1 SCAR=0.0
1105 1 my @args = @_;
1106 1 return( log( $args[0] ) / log( 10 ) );
1107 }
1108  
1109 sub _minute
1110 {
1111 1 ▶ my $self = shift( @_ );
Subroutine _minute: called CC=2 SCAR=7.0
1112 1 my @args = @_;
1113 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
1114 1 return( $dt->minute );
1115 }
1116  
1117 sub _month
1118 {
1119 1 ▶ my $self = shift( @_ );
Subroutine _month: called CC=2 SCAR=7.0
1120 1 my @args = @_;
1121 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
1122 1 return( $dt->month );
1123 }
1124  
1125 sub _monthname
1126 {
1127 1 ▶ my $self = shift( @_ );
Subroutine _monthname: called CC=2 SCAR=7.0
1128 1 my @args = @_;
1129 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
1130 1 return( $dt->month_name );
1131 }
1132  
1133 sub _number_format
1134 {
1135 0 ▶ my $self = shift( @_ );
Subroutine _number_format: not called CC=2 SCAR=17.9
1136 0 my @args = @_;
1137 0 my( $num, $tho, $dec, $prec ) = @args;
1138 0 ▶ $self->_load_class( 'Module::Generic::Number' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Module::Generic::Number") or return $self->pass_error 0 0 0
1139 0 my $fmt = Module::Generic::Number->new( $num );
1140 # 1 means with trailing zeros
1141 0 return( $fmt->format(
1142 thousand => $tho,
1143 decimal => $dec,
1144 precision => $prec,
1145 decimal_fill => 1,
1146 ));
1147 }
1148  
1149 43 ▶sub _placeholder_regexp { return( $PLACEHOLDER_REGEXP ) }
Subroutine _placeholder_regexp: called CC=1 SCAR=0.0
1150  
1151 sub _power
1152 {
1153 1 ▶ my $self = shift( @_ );
Subroutine _power: called CC=1 SCAR=0.0
1154 1 my @args = @_;
1155 1 return( CORE::exp( $args[ 1 ] * CORE::log( $args[ 0 ] ) ) );
1156 }
1157  
1158 sub _quarter
1159 {
1160 1 ▶ my $self = shift( @_ );
Subroutine _quarter: called CC=2 SCAR=7.0
1161 1 my @args = @_;
1162 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
1163 1 return( $dt->quarter );
1164 }
1165  
1166 sub _rand
1167 {
1168 1 ▶ my $self = shift( @_ );
Subroutine _rand: called CC=1 SCAR=0.0
1169 1 my @args = @_;
1170 1 return( CORE::rand() );
1171 }
1172  
1173 sub _regexp
1174 {
1175 1 ▶ my $self = shift( @_ );
Subroutine _regexp: called CC=1 SCAR=0.0
1176 1 my @args = @_;
1177 1 my( $re, $what ) = @args;
1178 1 return( $what =~ /$re/ );
1179 }
1180  
1181 sub _replace
1182 {
1183 1 ▶ my $self = shift( @_ );
Subroutine _replace: called CC=1 SCAR=0.0
1184 1 my @args = @_;
1185 1 my( $str, $from, $to ) = @args;
1186 1 $str =~ s/($from)/$to/gs;
1187 1 return( $str );
1188 }
1189  
1190 sub _right
1191 {
1192 1 ▶ my $self = shift( @_ );
Subroutine _right: called CC=1 SCAR=0.0
1193 1 my @args = @_;
1194 1 return( CORE::substr( $args[0], CORE::length( $args[0] ) - $args[1] ) );
1195 }
1196  
1197 sub _second
1198 {
1199 1 ▶ my $self = shift( @_ );
Subroutine _second: called CC=2 SCAR=7.0
1200 1 my @args = @_;
1201 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
1202 1 return( $dt->second );
1203 }
1204  
1205 sub _space
1206 {
1207 1 ▶ my $self = shift( @_ );
Subroutine _space: called CC=1 SCAR=0.0
1208 1 my @args = @_;
1209 1 return( ' ' x CORE::int( $args[0] ) );
1210 }
1211  
1212 sub _sprintf
1213 {
1214 1 ▶ my $self = shift( @_ );
Subroutine _sprintf: called CC=2 SCAR=6.9
1215 1 my @args = @_;
1216 1 ▶ for( my $i = 0; $i < scalar( @args ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @args) 2 1 3
1217 {
1218 2 $args[$i] =~ s/'/\\'/g;
1219 }
1220 1 my $eval = "CORE::sprintf( '" . join( "', '", @args ) . "' )";
1221 1 my $res = eval( $eval );
1222 # return( CORE::sprintf( @args ) );
1223 1 return( $res );
1224 }
1225  
1226 sub _to_days
1227 {
1228 1 ▶ my $self = shift( @_ );
Subroutine _to_days: called CC=3 SCAR=11.1
1229 1 my @args = @_;
1230 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
1231 # try-catch
1232 1 local $@;
1233 my $tz = eval
1234 1 {
1235 1 return( DateTime::TimeZone->new( name => 'local' ) );
1236 };
1237 1 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
1238 {
1239 1 $tz = DateTime::TimeZone->new( name => 'UTC' );
1240 }
1241 1 my $origin = DateTime->new(
1242 year => 0,
1243 month => 1,
1244 day => 1,
1245 hour => 0,
1246 minute => 0,
1247 second => 0,
1248 time_zone => $tz->name,
1249 );
1250 # https://stackoverflow.com/questions/821423/how-can-i-calculate-the-number-of-days-between-two-dates-in-perl#7111718
1251 1 my $days = $dt->delta_days( $origin )->delta_days();
1252 1 return( $days );
1253 }
1254  
1255 # sub _truncate
1256 # {
1257 # my $self = shift( @_ );
1258 # my @args = @_;
1259 # return if( $args[1] !~ /^\-?\d+$/ );
1260 # return( CORE::substr( $args[0], 0, $args[1] ) );
1261 # }
1262  
1263 sub _ucase
1264 {
1265 1 ▶ my $self = shift( @_ );
Subroutine _ucase: called CC=1 SCAR=0.0
1266 1 my @args = @_;
1267 1 return( uc( $args[0] ) );
1268 }
1269  
1270 sub _unix_timestamp
1271 {
1272 1 ▶ my $self = shift( @_ );
Subroutine _unix_timestamp: called CC=2 SCAR=7.0
1273 1 my @args = @_;
1274 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
1275 1 return( $dt->epoch );
1276 }
1277  
1278 sub _week
1279 {
1280 1 ▶ my $self = shift( @_ );
Subroutine _week: called CC=2 SCAR=7.0
1281 1 my @args = @_;
1282 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
1283 1 return( $dt->week_number );
1284 }
1285  
1286 sub _weekday
1287 {
1288 1 ▶ my $self = shift( @_ );
Subroutine _weekday: called CC=2 SCAR=7.0
1289 1 my @args = @_;
1290 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
1291 1 return( $dt->day_of_week );
1292 }
1293  
1294 sub _year
1295 {
1296 1 ▶ my $self = shift( @_ );
Subroutine _year: called CC=2 SCAR=7.0
1297 1 my @args = @_;
1298 1 ▶ my $dt = $self->_parse_timestamp( $args[0] ) || return;
Condition50%1/2
$self->_parse_timestamp($args[0]) || (return)
l!l
10
MC/DC0%0/1
$self->_parse_timestamp($args[0]) || (return)
$self->_parse_timestamp($args[0])
Truth table50%1/2
A
A= $self->_parse_timestamp($args[0])
Aresult
11
00
1299 1 return( $dt->year );
1300 }
1301  
1302 DESTROY
1303 {
1304 # <https://perldoc.perl.org/perlobj#Destructors>
1305 1 ▶ CORE::local( $., $@, $!, $^E, $? );
Subroutine DESTROY: called CC=10 SCAR=31.1
1306 1 ▶ CORE::return if( ${^GLOBAL_PHASE} eq 'DESTRUCT' );
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 1 1
1307 1 my $self = CORE::shift( @_ );
1308 1 ▶ CORE::return if( !CORE::defined( $self ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 1 1
1309 1 ▶ my $class = ref( $self ) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
1310 1 ▶ if( $self->{sth} )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'sth'}) { } 0 1 1
elsif ($self->{'dbh'} and $class =~ /^AI\:\:DB\:\:Postgres$/) { } 0 2 2
1311 {
1312 # print( STDERR "DESTROY(): Terminating sth '$self' for query:\n$self->{ 'query' }\n" ) if( $DEBUG );
1313 0 $self->{sth}->finish();
1314 }
1315 ▶ elsif( $self->{dbh} && $class =~ /^AI\:\:DB\:\:Postgres$/ )
Condition33%1/3
$self->{"dbh"} and $class =~ /^AI\:\:DB\:\:Postgres$/
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"dbh"} and $class =~ /^AI\:\:DB\:\:Postgres$/
$self->{"dbh"} $class =~ /^AI\:\:DB\:\:Postgres$/
Truth table33%1/3
A and B
A= $self->{"dbh"}B= $class =~ /^AI\:\:DB\:\:Postgres$/
ABresult
0X0
100
111
1316 {
1317 0 ▶ local( $SIG{__WARN__} ) = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1318 # $self->{dbh}->disconnect();
1319 # if( $DEBUG )
1320 # {
1321 # my( $pack, $file, $line, $sub ) = ( caller( 0 ) )[ 0, 1, 2, 3 ];
1322 # my( $pack2, $file2, $line2, $sub2 ) = ( caller( 1 ) ) [ 0, 1, 2, 3 ];
1323 # print( STDERR "DESTROY database handle ($self) [$self->{ 'query' }]\ncalled within sub '$sub' ($sub2) from package '$pack' ($pack2) in file '$file' ($file2) at line '$line' ($line2).\n" );
1324 # }
1325 0 $self->disconnect();
1326 }
1327 1 my $locks = $self->{_locks};
1328 1 ▶ if( $locks && $self->_is_array( $locks ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($locks and $self->_is_array($locks)) 0 1 1
Condition33%1/3
$locks and $self->_is_array($locks)
!ll&&!rl&&r
100
MC/DC0%0/2
$locks and $self->_is_array($locks)
$locks $self->_is_array($locks)
Truth table33%1/3
A and B
A= $locksB= $self->_is_array($locks)
ABresult
0X0
100
111
1329 {
1330 0 foreach my $name ( @$locks )
1331 {
1332 0 $self->unlock( $name );
1333 }
1334 }
1335 }
1336  
1337 2 1;
1338  
1339 # NOTE: POD
1340 __END__
1341  
1342 =encoding utf8
1343  
1344 =head1 NAME
1345  
1346 DB::Object::SQLite - DB Object SQLite Driver
1347  
1348 =head1 SYNOPSIS
1349  
1350 use DB::Object;
1351  
1352 my $dbh = DB::Object->connect({
1353 driver => 'SQLite',
1354 conf_file => 'db-settings.json',
1355 database => 'webstore',
1356 host => 'localhost',
1357 login => 'store-admin',
1358 schema => 'auth',
1359 debug => 3,
1360 }) || bailout( "Unable to connect to sql server on host localhost: ", DB::Object->error );
1361  
1362 # Legacy regular query
1363 my $sth = $dbh->prepare( "SELECT login,name FROM login WHERE login='jack'" ) ||
1364 die( $dbh->errstr() );
1365 $sth->execute() || die( $sth->errstr() );
1366 my $ref = $sth->fetchrow_hashref();
1367 $sth->finish();
1368  
1369 # Get a list of databases;
1370 my @databases = $dbh->databases;
1371 # Doesn't exist? Create it:
1372 my $dbh2 = $dbh->create_db( 'webstore' );
1373 # Load some sql into it
1374 my $rv = $dbh2->do( $sql ) || die( $dbh->error );
1375  
1376 # Check a table exists
1377 $dbh->table_exists( 'customers' ) || die( "Cannot find the customers table!\n" );
1378  
1379 # Get list of tables, as array reference:
1380 my $tables = $dbh->tables;
1381  
1382 my $cust = $dbh->customers || die( "Cannot get customers object." );
1383 $cust->where( email => 'john@example.org' );
1384 my $str = $cust->delete->as_string;
1385 # Becomes: DELETE FROM customers WHERE email='john\@example.org'
1386  
1387 # Do some insert with transaction
1388 $dbh->begin_work;
1389 # Making some other inserts and updates here...
1390 my $cust_sth_ins = $cust->insert(
1391 first_name => 'Paul',
1392 last_name => 'Goldman',
1393 email => 'paul@example.org',
1394 active => 0,
1395 ) || do
1396 {
1397 # Rollback everything since the begin_work
1398 $dbh->rollback;
1399 die( "Error while create query to add data to table customers: " . $cust->error );
1400 };
1401 $result = $cust_sth_ins->as_string;
1402 # INSERT INTO customers (first_name, last_name, email, active) VALUES('Paul', 'Goldman', 'paul\@example.org', '0')
1403 $dbh->commit;
1404 # Get the last used insert id
1405 my $id = $dbh->last_insert_id();
1406  
1407 $cust->where( email => 'john@example.org' );
1408 $cust->order( 'last_name' );
1409 $cust->having( email => qr/\@example/ );
1410 $cust->limit( 10 );
1411 my $cust_sth_sel = $cust->select || die( "An error occurred while creating a query to select data frm table customers: " . $cust->error );
1412 # Becomes:
1413 # SELECT id, first_name, last_name, email, created, modified, active, created::ABSTIME::INTEGER AS created_unixtime, modified::ABSTIME::INTEGER AS modified_unixtime, CONCAT(first_name, ' ', last_name) AS name FROM customers WHERE email='john\@example.org' HAVING email ~ '\@example' ORDER BY last_name LIMIT 10
1414  
1415 $cust->reset;
1416 $cust->where( email => 'john@example.org' );
1417 my $cust_sth_upd = $cust->update( active => 0 )
1418 # Would become:
1419 # UPDATE ONLY customers SET active='0' WHERE email='john\@example.org'
1420  
1421 # Lets' dump the result of our query
1422 # First to STDERR
1423 $login->where( "login='jack'" );
1424 $login->select->dump();
1425 # Now dump the result to a file
1426 $login->select->dump( "my_file.txt" );
1427  
1428 =head1 VERSION
1429  
1430 v1.3.1
1431  
1432 =head1 DESCRIPTION
1433  
1434 This package inherits from L<DB::Object>, so any method not here, but there you can use.
1435  
1436 L<DB::Object::SQLite> is a SQL API much alike L<DBD::SQLite>.
1437 So why use a private module instead of using that great L<DBD::SQLite> package?
1438  
1439 At first, I started to inherit from C<DBI> to conform to C<perlmod> perl
1440 manual page and to general perl coding guidlines. It became very quickly a
1441 real hassle. Barely impossible to inherit, difficulty to handle error, too
1442 much dependent from an API that change its behaviour with new versions.
1443 In short, I wanted a better, more accurate control over the SQL connection.
1444  
1445 So, L<DB::Object::SQLite> acts as a convenient, modifiable wrapper that provide the
1446 programmer with an intuitive, user-friendly and hassle free interface.
1447  
1448 =head1 CONSTRUCTOR
1449  
1450 =head2 new
1451  
1452 Create a new instance of L<DB::Object::SQLite>. Nothing much to say.
1453  
1454 =head2 connect
1455  
1456 Same as L<DB::Object/connect>, only specific to SQLite.
1457  
1458 See L</_connection_params2hash>
1459  
1460 =head1 METHODS
1461  
1462 =head2 alias
1463  
1464 This is inherited from L<DB::Object/alias>
1465  
1466 =head2 as_string
1467  
1468 This is inherited from L<DB::Object/as_string>
1469  
1470 =head2 avoid
1471  
1472 This is inherited from L<DB::Object/avoid>
1473  
1474 =head2 attribute
1475  
1476 Sets or get the value of database connection parameters.
1477  
1478 If only one argument is provided, returns its value.
1479 If multiple arguments in a form of pair => value are provided, it sets the corresponding database parameters.
1480  
1481 The authorised parameters are:
1482  
1483 =over 4
1484  
1485 =item * C<ActiveKids>
1486  
1487 Is read-only.
1488  
1489 =item * C<AutoCommit>
1490  
1491 Can be changed.
1492  
1493 =item * C<AutoInactiveDestroy>
1494  
1495 Can be changed.
1496  
1497 =item * C<CachedKids>
1498  
1499 Is read-only.
1500  
1501 =item * C<ChildHandles>
1502  
1503 Is read-only.
1504  
1505 =item * C<ChopBlanks>
1506  
1507 Can be changed.
1508  
1509 =item * C<CursorName>
1510  
1511 Is read-only.
1512  
1513 =item * C<Driver>
1514  
1515 Is read-only.
1516  
1517 =item * C<ErrCount>
1518  
1519 Can be changed.
1520  
1521 =item * C<Executed>
1522  
1523 Is read-only.
1524  
1525 =item * C<FetchHashKeyName>
1526  
1527 Can be changed.
1528  
1529 =item * C<HandleError>
1530  
1531 Can be changed.
1532  
1533 =item * C<HandleSetErr>
1534  
1535 Can be changed.
1536  
1537 =item * C<InactiveDestroy>
1538  
1539 Can be changed.
1540  
1541 =item * C<Kids>
1542  
1543 Is read-only.
1544  
1545 =item * C<NAME>
1546  
1547 Is read-only.
1548  
1549 =item * C<NULLABLE>
1550  
1551 Is read-only.
1552  
1553 =item * C<NUM_OF_FIELDS>
1554  
1555 Is read-only.
1556  
1557 =item * C<NUM_OF_PARAMS>
1558  
1559 Is read-only.
1560  
1561 =item * C<Name>
1562  
1563 Is read-only.
1564  
1565 =item * C<PRECISION>
1566  
1567 Is read-only.
1568  
1569 =item * C<PrintError>
1570  
1571 Can be changed.
1572  
1573 =item * C<PrintWarn>
1574  
1575 Can be changed.
1576  
1577 =item * C<Profile>
1578  
1579 Can be changed.
1580  
1581 =item * C<RaiseError>
1582  
1583 Can be changed.
1584  
1585 =item * C<RowCacheSize>
1586  
1587 Is read-only.
1588  
1589 =item * C<RowsInCache>
1590  
1591 Is read-only.
1592  
1593 =item * C<SCALE>
1594  
1595 Is read-only.
1596  
1597 =item * C<ShowErrorStatement>
1598  
1599 Can be changed.
1600  
1601 =item * C<Statement>
1602  
1603 Is read-only.
1604  
1605 =item * C<TYPE>
1606  
1607 Is read-only.
1608  
1609 =item * C<Taint>
1610  
1611 Can be changed.
1612  
1613 =item * C<TaintIn>
1614  
1615 Can be changed.
1616  
1617 =item * C<TaintOut>
1618  
1619 Can be changed.
1620  
1621 =item * C<TraceLevel>
1622  
1623 Can be changed.
1624  
1625 =item * C<Type>
1626  
1627 Can be changed.
1628  
1629 =item * C<Username>
1630  
1631 Is read-only.
1632  
1633 =item * C<Warn>
1634  
1635 Can be changed.
1636  
1637 =item * C<sqlite_allow_multiple_statements>
1638  
1639 Can be changed.
1640  
1641 =item * C<sqlite_see_if_its_a_number>
1642  
1643 Can be changed.
1644  
1645 =item * C<sqlite_unicode>
1646  
1647 Can be changed.
1648  
1649 =item * C<sqlite_unprepared_statements>
1650  
1651 Is read-only.
1652  
1653 =item * C<sqlite_use_immediate_transaction>
1654  
1655 Can be changed.
1656  
1657 =item * C<sqlite_version>
1658  
1659 Is read-only.
1660  
1661 =back
1662  
1663 =head2 available_drivers
1664  
1665 Return the list of available drivers.
1666  
1667 This is an inherited method from L<DB::Object/available_drivers>
1668  
1669 =head2 begin_work
1670  
1671 Mark the beginning of a transaction.
1672  
1673 Any arguments provided are passed along to L<DBD::SQLite/begin_work>
1674  
1675 =head2 bind
1676  
1677 This is an inherited method from L<DB::Object/bind>
1678  
1679 =head2 cache
1680  
1681 This is an inherited method from L<DB::Object/cache>
1682  
1683 =head2 can_update_delete_limit
1684  
1685 Returns the boolean value for the SQLite compiled option C<ENABLE_UPDATE_DELETE_LIMIT> by calling L</has_compile_option>
1686  
1687 =head2 check_driver
1688  
1689 This is an inherited method from L<DB::Object/check_driver>
1690  
1691 =head2 commit
1692  
1693 Make any change to the database irreversible.
1694  
1695 This must be used only after having called L</begin_work>
1696  
1697 Any arguments provided are passed along to L<DBD::SQLite/commit>
1698  
1699 =head2 compile_options
1700  
1701 Returns the cached list of SQLite compiled options. The cached file is in the file C<sql_sqlite_compile_options.cfg> in the sytem directory.
1702  
1703 =head2 connect
1704  
1705 Same as L<DB::Object/connect>, only specific to SQLite.
1706  
1707 It sets C<sqlite_unicode> to a true value in the connection parameters returned by L</_connection_params2hash>
1708  
1709 See L</_connection_params2hash>
1710  
1711 =head2 copy
1712  
1713 This is an inherited method from L<DB::Object/copy>
1714  
1715 =head2 create_table
1716  
1717 This is an inherited method from L<DB::Object/create_table>
1718  
1719 =head2 data_sources
1720  
1721 This is an inherited method from L<DB::Object/data_sources>
1722  
1723 =head2 data_type
1724  
1725 This is an inherited method from L<DB::Object/data_type>
1726  
1727 =head2 database
1728  
1729 This is an inherited method from L<DB::Object/database>
1730  
1731 =head2 database_file
1732  
1733 Returns the file path to the database file.
1734  
1735 =head2 databases
1736  
1737 Returns a list of databases, which in SQLite, means a list of opened sqlite database files.
1738  
1739 =head2 datatype_dict
1740  
1741 Returns an hash reference of each data type with their equivalent C<constant>, regular expression (C<re>), constant C<name> and C<type> name.
1742  
1743 Each data type is an hash with the following properties for each type: C<constant>, C<name>, C<re>, C<type>
1744  
1745 =head2 delete
1746  
1747 This is an inherited method from L<DB::Object/database>
1748  
1749 =head2 disconnect
1750  
1751 This is an inherited method from L<DB::Object/disconnect>
1752  
1753 =head2 do
1754  
1755 This is an inherited method from L<DB::Object/do>
1756  
1757 =head2 enhance
1758  
1759 This is an inherited method from L<DB::Object/enhance>
1760  
1761 =head2 func
1762  
1763 Provided with a table name and a function name and this will call L<DB::SQLite> passing it the table name and the function name.
1764  
1765 It returns the value received from the function call.
1766  
1767 =head2 get_sql_type
1768  
1769 Provided with a data type as a string and this returns a SQLite constant suitable to be passed to L<DBI/bind_param>
1770  
1771 =head2 having
1772  
1773 A convenient wrapper to L<DB::Object::SQLite::Query/having>
1774  
1775 =head2 has_compile_option
1776  
1777 Provided with a compile option (the character case is irrelevant) and this will check if it exists or not.
1778  
1779 =head2 last_insert_id
1780  
1781 =head2 lock
1782  
1783 This is an unsupported feature in SQLIte
1784  
1785 =head2 on_conflict
1786  
1787 See L<DB::Object::SQLite::Tables/on_conflict>
1788  
1789 =head2 pragma
1790  
1791 This is still a work in progress.
1792  
1793 =head2 replace
1794  
1795 Just like for the INSERT query, L</replace> takes one optional argument representing a L<DB::Object::SQLite::Statement> SELECT object or a list of field-value pairs.
1796  
1797 If a SELECT statement is provided, it will be used to construct a query of the type of REPLACE INTO mytable SELECT FROM other_table
1798  
1799 Otherwise the query will be REPLACE INTO mytable (fields) VALUES(values)
1800  
1801 In scalar context, it execute the query and in list context it simply returns the statement handler.
1802  
1803 =head2 register_function
1804  
1805 This takes an hash reference of parameters and will register a new function by calling L<DBD::SQLite/sql_function_register>
1806  
1807 Possible options are:
1808  
1809 =over 4
1810  
1811 =item * C<code>
1812  
1813 Anonymous code to be executed when the function is called.
1814  
1815 =item * C<func>
1816  
1817 This is an hash reference representing registry of functions. The value for each key is the option hash reference.
1818  
1819 =item * C<flags>
1820  
1821 An array reference of flags
1822  
1823 =item * C<name>
1824  
1825 The function name
1826  
1827 =back
1828  
1829 =head2 remove_function
1830  
1831 Provided with a function name and this will remove it.
1832  
1833 It returns false if there is no function, or returns the options hash reference originally set for the function removed.
1834  
1835 =head2 returning
1836  
1837 A convenient wrapper to L<DB::Object::Postgres::Query/returning>
1838  
1839 =head2 rollback
1840  
1841 Will roll back any changes made to the database since the last transaction point marked with L</begin_work>
1842  
1843 =head2 sql_function_register
1844  
1845 Provided with an hash reference of options and this will register a sql function by calling L<DBD::SQLite/sqlite_create_function>
1846  
1847 Possible options are:
1848  
1849 =over 4
1850  
1851 =item * C<argc>
1852  
1853 The function arguments
1854  
1855 =item * C<code>
1856  
1857 Anonymous perl code to be executed when the function is called
1858  
1859 =item * C<flags>
1860  
1861 An array reference of flags. Those flags are joined with C<|> and L<perlfunc/eval>'ed
1862  
1863 =item * C<name>
1864  
1865 The function name
1866  
1867 =back
1868  
1869 =head2 stat
1870  
1871 Provided with an hash or hash reference of parameters and this will call L<DBD::SQLite/sqlite_status> and get the hash reference of values returned.
1872  
1873 If the option I<reset> is set, then this will call L<DBD::SQLite/sqlite_status> passing it 0 to reset it instead.
1874  
1875 If the option I<type> is specified, this will return the equivalent property from the stat hash reference returned by L<DBD::SQLite/sqlite_status>, otherwise, it will return the hash in list context and the hash reference of stat properties in scalar context.
1876  
1877 =head2 table_info
1878  
1879 Provided with a table name and this will retrieve the table information as an hash reference.
1880  
1881 Otherwise, if nothing can be found, it returns an empty hash reference.
1882  
1883 It takes no optional parameters.
1884  
1885 Information retrieved are:
1886  
1887 =over 4
1888  
1889 =item * C<name>
1890  
1891 The table name
1892  
1893 =item * C<type>
1894  
1895 The object type, which may be one of: C<table>, C<view>, C<materialized view>, C<special>, C<foreign table>
1896  
1897 =back
1898  
1899 =head2 tables
1900  
1901 Connects to the database and finds out the list of all available tables.
1902  
1903 Returns undef or empty list in scalar or list context respectively if no table found.
1904  
1905 Otherwise, it returns the list of table in list context or a reference of it in scalar context.
1906  
1907 =head2 tables_info
1908  
1909 Provided with a database or using by default the current database and this will issue a query to get an array reference of all tables.
1910  
1911 It returns the array reference.
1912  
1913 =head2 trace
1914  
1915 Trace is unsupported on SQLite.
1916  
1917 =head2 unlock
1918  
1919 Unlock is unsupported on SQLite.
1920  
1921 =head2 variables
1922  
1923 Variables are unsupported on SQLite.
1924  
1925 =head2 version
1926  
1927 This returns the, possibly cached, SQLite server version as a L<version> object.
1928  
1929 =head2 _check_connect_param
1930  
1931 This returns an hash reference of connection parameters.
1932  
1933 If there is no L</database_file> currently set, it will use the property I<uri>.
1934  
1935 The database is taken from the property I<database>, or derived from the last path segment of the I<uri>.
1936  
1937 The database file is made absolute and is et as the I<database_file> property
1938  
1939 The database is name, if not set, is derived from the base path of the I<database_file>
1940  
1941 The I<host> property is set to C<localhost> and I<port> property to C<0>
1942  
1943 It returns the hash reference of parameters thus processed.
1944  
1945 =head2 _check_default_option
1946  
1947 Provided with an hash or hash reference of options and this will check it and set some default value.
1948  
1949 The only default property this sets is C<sqlite_unicode> to true.
1950  
1951 It returns the hash reference of options.
1952  
1953 =head2 _connection_options
1954  
1955 Provided with an hash reference of parameters and this will check them and returns an hash reference of options who name start with C<sqlite_>
1956  
1957 =head2 _connection_parameters
1958  
1959 Provided with an hash or hash reference of connection parameters and this will extra all the properties that start with C<sqlite_/> and add them to an array of core properties: db login passwd host port driver database server opt uri debug
1960  
1961 It returns those properties as an array reference.
1962  
1963 =head2 _dbi_connect
1964  
1965 This calls L<DB::Connect/_dbi_connect> and do more driver specific processing.
1966  
1967 It will register all the functions set in the global hash reference C<$PRIVATE_FUNCTIONS> which is a function name to code reference pairs. For each of those function, they will be added by calling L</sql_function_register>
1968  
1969 It returns the database handler object (L<DBD::Object::SQLite>)
1970  
1971 =head2 _dsn
1972  
1973 Using the L</database_file> set and this will issue a connection to the SQLite database file.
1974  
1975 If the file does not exist or is not writable, this will return an error, otherwise this will return the string representing the dsn, which are connection parameters separated by C<;>
1976  
1977 =head2 _parse_timestamp
1978  
1979 Provided a string and this will parse it to return a L<DateTime> object.
1980  
1981 =head1 SQLITE FUNCTIONS AVAILABLE
1982  
1983 =head2 ceiling
1984  
1985 This is a sql function to be registered automatically upon connection to the SQLite database file.
1986  
1987 It leverages L<POSIX/ceil>
1988  
1989 =head2 concat
1990  
1991 This returns the arguments provided concatenated as a string.
1992  
1993 =head2 curdate
1994  
1995 Returns a string representing the year, month and date separated by a C<->
1996  
1997 This is computed using L<DateTime>
1998  
1999 =head2 curtime
2000  
2001 Returns a string representing the hours, minutes and seconds separated by a C<:>
2002  
2003 This is computed using L<DateTime>
2004  
2005 =head2 dayname
2006  
2007 Based on a datetime that is parsed using L</_parse_timestamp>, this returns the day name of the week, such as C<Monday>
2008  
2009 =head2 dayofmonth
2010  
2011 Based on a datetime that is parsed using L</_parse_timestamp>, this returns the day of the month, such as 17.
2012  
2013 =head2 dayofweek
2014  
2015 Based on a datetime that is parsed using L</_parse_timestamp>, this returns the day of the week as a number from 1 to 7 with 1 being Monday
2016  
2017 =head2 dayofyear
2018  
2019 Based on a datetime that is parsed using L</_parse_timestamp>, this returns the day of the year, such as a number from 1 to 365, or possibly 366 depending on the year.
2020  
2021 =head2 distance_miles
2022  
2023 Provided with an original latitude, longitude and a target latitude and longitude and this will calculate the distance between the 2.
2024  
2025 See the source L<StackOverflow post on which this function is based|http://stackoverflow.com/questions/10034636/postgres-longitude-longitude-query>.
2026  
2027 =head2 from_days
2028  
2029 Calculate the number of days since January 1st of year 0 and returns a L<DateTime> object.
2030  
2031 =head2 from_unixtime
2032  
2033 Provided with a unix timestamp, and this will return a datetime string such as C<YYYY-mm-dd HH:MM:SS>
2034  
2035 =head2 hour
2036  
2037 Provided with a date time, and this will parse it and return the hour.
2038  
2039 =head2 lcase
2040  
2041 Provided with a string and this returns its lower case value.
2042  
2043 =head2 left
2044  
2045 Provided with a string and an integer C<n> and this will return a substring capturing the nth first characters.
2046  
2047 =head2 locate
2048  
2049 This essentially does the same as L<perlfunc/index>
2050  
2051 =head2 log10
2052  
2053 Provided with a number and this returns its logarithm base 10.
2054  
2055 =head2 minute
2056  
2057 Provided with a date time, and this will parse it and return the minutes.
2058  
2059 =head2 month
2060  
2061 Provided with a date time, and this will parse it and return the month.
2062  
2063 =head2 monthname
2064  
2065 Provided with a date time, and this will parse it and return the month name.
2066  
2067 =head2 number_format
2068  
2069 Provided with a number, a thousand separator, a decimal separator and a decimal precision and this will format the number accordingly and return it as a string.
2070  
2071 =head2 power
2072  
2073 Provided with a number and a power, and this will return the number powered
2074  
2075 =head2 quarter
2076  
2077 Provided with a date time, and this will parse it and return the quarter.
2078  
2079 =head2 query_object
2080  
2081 Set or gets the SQLite query object (L<DB::Object::SQLite::Query>) used to process and format queries.
2082  
2083 =head2 rand
2084  
2085 This takes no argument and simply returns a random number using L<perlfunc/rand>
2086  
2087 =head2 regexp
2088  
2089 Provided with a regular expression and the string to test, and this will test the regular expression and return true if it matches or false otherwise.
2090  
2091 =head2 replace
2092  
2093 Provided with a string, some term to replace and a replacement string and this will do a perl substitution and return the resulting string.
2094  
2095 =head2 right
2096  
2097 Provided with a string and an integer n and this will return the nth right most characters.
2098  
2099 =head2 second
2100  
2101 Provided with a date time, and this will parse it and return the seconds.
2102  
2103 =head2 space
2104  
2105 Provided with an integer and this will return as much spaces.
2106  
2107 =head2 sprintf
2108  
2109 This behaves like L<perlfunc/sprintf> and provided with a template and some arguments, it will return a formatted string.
2110  
2111 =head2 to_days
2112  
2113 Provided with a date time, and this will return the number of days since January 1st of year 0.
2114  
2115 =head2 ucase
2116  
2117 This returns the string provided with all its characters in upper case.
2118  
2119 =head2 unix_timestamp
2120  
2121 Provided with a date time and this will returns its unix timestamp representation.
2122  
2123 =head2 week
2124  
2125 Provided with a date time, and this will parse it and return the week.
2126  
2127 =head2 weekday
2128  
2129 Provided with a date time, and this will parse it and return the day of the week.
2130  
2131 =head2 year
2132  
2133 Provided with a date time, and this will parse it and return the day of the year.
2134  
2135 =head1 SEE ALSO
2136  
2137 L<DBI>, L<Apache::DBI>
2138  
2139 =head1 AUTHOR
2140  
2141 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
2142  
2143 =head1 COPYRIGHT & LICENSE
2144  
2145 Copyright (c) 2019-2021 DEGUEST Pte. Ltd.
2146  
2147 You can use, copy, modify and redistribute this package and associated
2148 files under the same terms as Perl itself.
2149  
2150 =cut
« blib/lib/DB/Object/Fields.pm ↑ Summary lib/DB/Object/SQLite/Statement.pm »