blib/lib/DB/Object/SQLite/Statement.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 110 CC 9 SCAR 24.8
CC
9
Cov
0%
distinct
30.0
ignore
20.0
unprepared_statements
6.0
CRAP 12.0
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 use strict;
23 use warnings;
24 use parent qw( DB::Object::SQLite DB::Object::Statement );
25 use vars qw( $VERSION $DEBUG );
26 our $DEBUG = 0;
27 our $VERSION = 'v0.301.0';
28 };
29  
30 use strict;
31 use warnings;
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 my $self = shift( @_ );
42 my $what = shift( @_ );
43 my $query = $self->{query} ||
44 return( $self->error( "No query to set as to be ignored." ) );
45  
46 my $type = uc( ( $query =~ /^\s*(\S+)\s+/ )[ 0 ] );
47 # ALTER for table alteration statements (DB::Object::Tables
48 my @allowed = qw( SELECT );
49 my $allowed = CORE::join( '|', @allowed );
50 if( !scalar( grep{ /^$type$/i } @allowed ) )
51 {
52 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 return( $self ) if( $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+(?:DISTINCT|ALL)[[:blank:]]+/i );
56 my $clause = defined( $what ) ? "DISTINCT ON ($what)" : "DISTINCT";
57  
58 $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 my $sth = $self->_cache_this( $query ) ||
63 return( $self->error( "Error while preparing new ignored query:\n$query" ) );
64 if( !defined( wantarray() ) )
65 {
66 $sth->execute() ||
67 return( $self->error( "Error while executing new ignored query:\n$query" ) );
68 }
69 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 my $self = shift( @_ );
101 my $query = $self->{query} ||
102 return( $self->error( "No query to set as to be ignored." ) );
103  
104 my $type = uc( ( $query =~ /^[[:blank:]]*(\S+)[[:blank:]]+/ )[ 0 ] );
105 # ALTER for table alteration statements (DB::Object::Tables
106 my @allowed = qw( INSERT UPDATE );
107 my $allowed = CORE::join( '|', @allowed );
108 if( !scalar( grep{ /^$type$/i } @allowed ) )
109 {
110 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 return( $self ) if( $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+OR[[:blank:]]+IGNORE[[:blank:]]+/i );
114  
115 $query =~ s/^([[:blank:]]*)($allowed)([[:blank:]]+)/$1$2 OR IGNORE$3/;
116 my $sth = $self->_cache_this( $query ) ||
117 return( $self->error( "Error while preparing new ignored query:\n$query" ) );
118 if( !defined( wantarray() ) )
119 {
120 $sth->execute() ||
121 return( $self->error( "Error while executing new ignored query:\n$query" ) );
122 }
123 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 return( shift->error( "SELECT | DELETE | UPDATE ONLY is not supported by SQLite." ) );
135 }
136  
137 sub priority
138 {
139 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 {
152 my $self = shift( @_ );
153 if( $self->{dbh}->{sth} )
154 {
155 return( $self->{dbh}->{sth}->{sqlite_unprepared_statements} );
156 }
157 return;
158 }
159  
160 # Does nothing in SQLite. This is a Mysql feature
161 sub wait
162 {
163 return( shift( @_ ) );
164 }
165  
166 # NOTE: DESTROY
167 DESTROY
168 {
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 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