lib/DB/Object/SQLite/Statement.pm

filter: Statementstmt 36.2% 21 / 58 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 30.9% 34 / 110 CC 15 SCAR 35.7
CC
15
Cov
24%
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 ## Database Object Interface - ~/lib/DB/Object/SQLite/Statement.pm
4 ## Version v0.301.0
5 ## Copyright(c) 2020 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2017/07/19
8 ## Modified 2026/03/22
9 ## All rights reserved
10 ##
11 ##
12 ## This program is free software; you can redistribute it and/or modify it
13 ## under the same terms as Perl itself.
14 ##----------------------------------------------------------------------------
15 # This package's purpose is to automatically terminate the statement object and
16 # separate them from the connection object (DB::Object).
17 # Connection object last longer than statement objects
18 ##----------------------------------------------------------------------------
19 package DB::Object::SQLite::Statement;
20 BEGIN
21 {
22 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶ use parent qw( DB::Object::SQLite DB::Object::Statement );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶ use vars qw( $VERSION $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶ our $DEBUG = 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 our $VERSION = 'v0.301.0';
28 };
29  
30 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶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 {
41 0 ▶ my $self = shift( @_ );
Subroutine distinct: not called CC=8 SCAR=42.8
Pod documented
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 {
100 0 ▶ my $self = shift( @_ );
Subroutine ignore: not called CC=7 SCAR=40.3
Pod documented
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 {
134 0 ▶ return( shift->error( "SELECT | DELETE | UPDATE ONLY is not supported by SQLite." ) );
Subroutine only: not called CC=1 SCAR=6.9
Pod documented
135 }
136  
137 sub priority
138 {
139 0 ▶ return( shift->error( "Priority is not supported in SQLite." ) );
Subroutine priority: not called CC=1 SCAR=6.9
Pod documented
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 {
152 0 ▶ my $self = shift( @_ );
Subroutine unprepared_statements: not called CC=2 SCAR=17.9
Pod documented
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 {
163 0 ▶ return( shift( @_ ) );
Subroutine wait: not called CC=1 SCAR=6.9
Pod documented
164 }
165  
166 # NOTE: DESTROY
167 DESTROY
168 ▶{
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 2 1;
174  
175 __END__
176  
177 =encoding utf-8
178  
179 =head1 NAME
180  
181 DB::Object::SQLite::Statement - SQLite Statement Object
182  
183 =head1 SYNOPSIS
184  
185 use DB::Object::SQLite::Statement;
186 my $this = DB::Object::SQLite::Statement->new || die( DB::Object::SQLite::Statement->error, "\n" );
187  
188 =head1 VERSION
189  
190 v0.301.0
191  
192 =head1 DESCRIPTION
193  
194 This is a SQLite specific statement object class.
195  
196 =head1 METHODS
197  
198 =head2 distinct
199  
200 Provided with an optional column and called upon a C<SELECT> query and this will modify the query to add the keyword C<DISTINCT>
201  
202 $sth->distinct;
203 # produces SELECT DISTINCT....
204 $sth->distinct( 'name' );
205 # produces SELECT DISTINCT ON (name)....
206  
207 See L<SQLite documentation for more information|https://sqlite.org/lang_select.html>
208  
209 =head2 dump
210  
211 This is inherited from L<DB::Object::Statement/dump>
212  
213 =head2 ignore
214  
215 This takes no argument and this will modify the queries of type C<insert>, or C<update> to add the keyword C<IGNORE>
216  
217 $sth->ignore;
218 # produces INSERT IGNORE....
219  
220 If called in void context, this will execute the prepare statement handler immediately.
221  
222 It returns the newly created statement handler.
223  
224 See L<SQLite documentation for more information|https://www.sqlite.org/lang_insert.html>
225  
226 =head2 only
227  
228 This returns an error as C<SELECT FROM ONLY> is not supported by SQLite and will return an error.
229  
230 =head2 priority
231  
232 This is unsupported under SQLite and if used returns an error.
233  
234 =head2 unprepared_statements
235  
236 This returns the natural SQLite statement property C<sqlite_unprepared_statements>, which provides the remaining unprepared statements in case where multiple statements were prepared.
237  
238 See L<DBD::SQLite documentation for more information|DBD::SQLite>
239  
240 =head2 wait
241  
242 This is unsupported under SQLite and it will be silently ignore, returning the current object.
243  
244 =head1 SEE ALSO
245  
246 L<perl>
247  
248 =head1 AUTHOR
249  
250 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
251  
252 =head1 COPYRIGHT & LICENSE
253  
254 Copyright (c) 2019-2021 DEGUEST Pte. Ltd.
255  
256 You can use, copy, modify and redistribute this package and associated
257 files under the same terms as Perl itself.
258  
259 =cut
« lib/DB/Object/SQLite.pm ↑ Summary lib/DB/Object/Fields/Unknown.pm »