lib/DB/Object/SQLite/Statement.pm

filter: Statementstmt 37.2% 22 / 59 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 7 / 14 Podpod 100% 6 / 6 totaltotal 31.5% 35 / 111 CC 15 SCAR 35.7
CC
15
Cov
25%
distinct
72.0
ignore
56.0
unprepared_statements
6.0
CRAP 35.5
« lib/DB/Object/SQLite.pm ↑ Summary lib/DB/Object/Fields/Unknown.pm »
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## SQL API Abstraction - ~/lib/DB/Object/SQLite/Statement.pm
4 ## Version v0.301.1
5 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2017/07/19
8 ## Modified 2026/10/01
9 ## All rights reserved
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 # This package's purpose is to automatically terminate the statement object and
15 # separate them from the connection object (DB::Object).
16 # Connection object last longer than statement objects
17 ##----------------------------------------------------------------------------
18 package DB::Object::SQLite::Statement;
19 BEGIN
20 3 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 warnings::register_categories( 'DB::Object' );
24 3 ▶ use parent qw( DB::Object::Statement );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶ use vars qw( $VERSION $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 our $DEBUG = 0;
27 3 our $VERSION = 'v0.301.1';
28 };
29  
30 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # Inherited from DB::Object::Statement
34 # sub bind_param
35  
36 # sub commit is called by dbh, so it is in DB::Object::Postgres
37  
38 # Customised for Postgres
39 sub distinct
40 0 ▶{
Subroutine distinct: not called CC=8 SCAR=42.8
Pod documented
41 0 my $self = shift( @_ );
42 0 my $what = shift( @_ );
43 my $query = $self->{query} ||
44 0 ▶ return( $self->error( "No query to set as to be ignored." ) );
Condition0%0/2
$self->{'query'} || (return $self->error('No query to set as to be ignored.'))
l!l
00
MC/DC0%0/1
$self->{'query'} || (return $self->error('No query to set as to be ignored.'))
$self->{'query'}
Truth table0%0/2
A
A= $self->{'query'}
Aresult
11
00
45  
46 0 my $type = uc( ( $query =~ /^\s*(\S+)\s+/ )[ 0 ] );
47 # ALTER for table alteration statements (DB::Object::Tables
48 0 my @allowed = qw( SELECT );
49 0 my $allowed = CORE::join( '|', @allowed );
50 0 ▶ if( !scalar( grep{ /^$type$/i } @allowed ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar grep({/^$type$/i;} @allowed)) 0 0 0
51 {
52 0 return( $self->error( "You may not flag statement of type \U$type\E to be distinct:\n$query" ) );
53 }
54 # Incompatible. Do not bother going further
55 0 ▶ return( $self ) if( $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+(?:DISTINCT|ALL)[[:blank:]]+/i );
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+(?:DISTINCT|ALL)[[:blank:]]+/i 0 0 0
56 0 ▶ my $clause = defined( $what ) ? "DISTINCT ON ($what)" : "DISTINCT";
Branch: 1 branch
BranchTrueFalseTotal
defined $what ? : 0 0 0
57  
58 0 $query =~ s/^([[:blank:]]*)($allowed)([[blank:]]+)/$1$2 $clause /;
59 # my $sth = $self->prepare( $query ) ||
60 # $self->{ 'query' } = $query;
61 # saving parameters to bind later on must have been done previously
62 0 ▶ my $sth = $self->_cache_this( $query ) ||
Condition0%0/2
$self->_cache_this($query) || (return $self->error("Error while preparing new ignored query:\n$query"))
l!l
00
MC/DC0%0/1
$self->_cache_this($query) || (return $self->error("Error while preparing new ignored query:\n$query"))
$self->_cache_this($query)
Truth table0%0/2
A
A= $self->_cache_this($query)
Aresult
11
00
63 return( $self->error( "Error while preparing new ignored query:\n$query" ) );
64 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
65 {
66 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing new ignored query:\n$query") 0 0 0
67 return( $self->error( "Error while executing new ignored query:\n$query" ) );
68 }
69 0 return( $sth );
70 }
71  
72 # Inherited from DB::Object
73 # sub dump
74  
75 # Inherited from DB::Object::Statement
76 # sub execute
77  
78 # Inherited from DB::Object::Statement
79 # sub executed
80  
81 # Inherited from DB::Object::Statement
82 # sub fetchall_arrayref($@)
83  
84 # Inherited from DB::Object::Statement
85 # sub fetchcol($;$)
86  
87 # Inherited from DB::Object::Statement
88 # sub fetchhash(@)
89  
90 # Inherited from DB::Object::Statement
91 # sub fetchrow(@)
92  
93 # Inherited from DB::Object::Statement
94 # sub finish
95  
96 # https://sqlite.org/lang_insert.html
97 # https://sqlite.org/lang_update.html
98 sub ignore
99 0 ▶{
Subroutine ignore: not called CC=7 SCAR=40.3
Pod documented
100 0 my $self = shift( @_ );
101 my $query = $self->{query} ||
102 0 ▶ return( $self->error( "No query to set as to be ignored." ) );
Condition0%0/2
$self->{'query'} || (return $self->error('No query to set as to be ignored.'))
l!l
00
MC/DC0%0/1
$self->{'query'} || (return $self->error('No query to set as to be ignored.'))
$self->{'query'}
Truth table0%0/2
A
A= $self->{'query'}
Aresult
11
00
103  
104 0 my $type = uc( ( $query =~ /^[[:blank:]]*(\S+)[[:blank:]]+/ )[ 0 ] );
105 # ALTER for table alteration statements (DB::Object::Tables
106 0 my @allowed = qw( INSERT UPDATE );
107 0 my $allowed = CORE::join( '|', @allowed );
108 0 ▶ if( !scalar( grep{ /^$type$/i } @allowed ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar grep({/^$type$/i;} @allowed)) 0 0 0
109 {
110 0 return( $self->error( "You may not flag statement of type \U$type\E to be ignored:\n$query" ) );
111 }
112 # Already done. Do not bother going further
113 0 ▶ return( $self ) if( $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+OR[[:blank:]]+IGNORE[[:blank:]]+/i );
Branch: 1 branch
BranchTrueFalseTotal
if $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+OR[[:blank:]]+IGNORE[[:blank:]]+/i 0 0 0
114  
115 0 $query =~ s/^([[:blank:]]*)($allowed)([[:blank:]]+)/$1$2 OR IGNORE$3/;
116 0 ▶ my $sth = $self->_cache_this( $query ) ||
Condition0%0/2
$self->_cache_this($query) || (return $self->error("Error while preparing new ignored query:\n$query"))
l!l
00
MC/DC0%0/1
$self->_cache_this($query) || (return $self->error("Error while preparing new ignored query:\n$query"))
$self->_cache_this($query)
Truth table0%0/2
A
A= $self->_cache_this($query)
Aresult
11
00
117 return( $self->error( "Error while preparing new ignored query:\n$query" ) );
118 0 ▶ if( !defined( wantarray() ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
119 {
120 0 ▶ $sth->execute() ||
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return $self->error("Error while executing new ignored query:\n$query") 0 0 0
121 return( $self->error( "Error while executing new ignored query:\n$query" ) );
122 }
123 0 return( $sth );
124 }
125  
126 # Inherited from DB::Object::Statement
127 # sub join
128  
129 # Inherited from DB::Object::Statement
130 # sub object
131  
132 sub only
133 0 ▶{
Subroutine only: not called CC=1 SCAR=6.9
Pod documented
134 0 return( shift->error( "SELECT | DELETE | UPDATE ONLY is not supported by SQLite." ) );
135 }
136  
137 sub priority
138 0 ▶{
Subroutine priority: not called CC=1 SCAR=6.9
Pod documented
139 0 return( shift->error( "Priority is not supported in SQLite." ) );
140 }
141  
142 # rollback is called using the dbh handler and is located in DB::Object::Postgres
143  
144 # Inherited from DB::Object::Statement
145 # sub rows(@)
146  
147 # Inherited from DB::Object::Statement
148 # sub undo
149  
150 sub unprepared_statements
151 0 ▶{
Subroutine unprepared_statements: not called CC=2 SCAR=17.9
Pod documented
152 0 my $self = shift( @_ );
153 0 ▶ if( $self->{dbh}->{sth} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dbh"}{"sth"}) 0 0 0
154 {
155 0 return( $self->{dbh}->{sth}->{sqlite_unprepared_statements} );
156 }
157 0 return;
158 }
159  
160 # Does nothing in SQLite. This is a Mysql feature
161 sub wait
162 0 ▶{
Subroutine wait: not called CC=1 SCAR=6.9
Pod documented
163 0 return( shift( @_ ) );
164 }
165  
166 # NOTE: DESTROY
167 DESTROY
168 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
169 # Do nothing but existing so it is handled by this package
170 # print( STDERR "DESTROY'ing statement $self ($self->{ 'query' })\n" );
171 };
172  
173 3 1;
174 # NOTE: POD
175 __END__
« lib/DB/Object/SQLite.pm ↑ Summary lib/DB/Object/Fields/Unknown.pm »