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

filter: Statementstmt 0.0% 0 / 139 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 287 CC 27 SCAR 39.9
CC
27
Cov
0%
dump
132.0
priority
56.0
ignore
30.0
CRAP 54.3
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## Database Object Interface - ~/lib/DB/Object/Mysql/Statement.pm
4 ## Version v0.300.2
5 ## Copyright(c) 2019 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2017/07/19
8 ## Modified 2024/09/04
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::Mysql::Statement;
19 BEGIN
20 {
21 use strict;
22 use warnings;
23 warnings::register_categories( 'DB::Object' );
24 use parent qw( DB::Object::Statement );
25 use vars qw( $VERSION $DEBUG );
26 our $DEBUG = 0;
27 our $VERSION = 'v0.300.2';
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 MySQL
39 sub distinct
40 {
41 my $self = shift( @_ );
42 my $query = $self->{query} ||
43 return( $self->error( "No query to set as to be ignored." ) );
44  
45 my $type = uc( ( $query =~ /^\s*(\S+)\s+/ )[ 0 ] );
46 # ALTER for table alteration statements (DB::Object::Tables
47 my @allowed = qw( SELECT );
48 my $allowed = CORE::join( '|', @allowed );
49 if( !scalar( grep{ /^$type$/i } @allowed ) )
50 {
51 return( $self->error( "You may not flag statement of type \U$type\E to be distinct:\n$query" ) );
52 }
53 # Incompatible. Do not bother going further
54 return( $self ) if( $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+(?:DISTINCT|DISTINCTROW|ALL)[[:blank:]]+/i );
55  
56 $query =~ s/^([[:blank:]]*)($allowed)([[:blank:]]+)/$1$2 DISTINCT /;
57 my $sth = $self->_cache_this( $query ) ||
58 return( $self->error( "Error while preparing new ignored query:\n$query" ) );
59 if( !defined( wantarray() ) )
60 {
61 $sth->execute() ||
62 return( $self->error( "Error while executing new ignored query:\n$query" ) );
63 }
64 return( $sth );
65 }
66  
67 # Customised for MySQL
68 sub dump
69 {
70 my $self = shift( @_ );
71 my $args = @_ == 1 ? shift( @_ ) : { @_ };
72 my $vsep = ",";
73 my $hsep = "\n";
74 my $width = 35;
75 $self->_load_class( 'DateTime::Lite' ) || return( $self->pass_error );
76 $self->_load_class( 'Module::Generic::File' ) || return( $self->pass_error );
77 my $fh = Module::Generic::File->stdout() ||
78 return( $self->pass_error( Module::Generic::File->error ) );
79 $vsep = $args->{vsep} if( exists( $args->{vsep} ) );
80 $hsep = $args->{hsep} if( exists( $args->{hsep} ) );
81 $width = $args->{width} if( exists( $args->{width} ) );
82 my @fields = ();
83 # my @fields = @{$self->{sth}->FETCH( 'NAME' )};
84 my $fields_ref = $self->{sth}->{NAME};
85 if( defined( $fields_ref ) && ref( $fields_ref ) eq 'ARRAY' )
86 {
87 @fields = @$fields_ref;
88 }
89 else
90 {
91 return( $self->error( "No array reference of fields could be retrieved from statement '$self->{sth}'." ) );
92 }
93 return( $self->error( "No query to dump." ) ) if( !exists( $self->{sth} ) );
94 if( exists( $args->{file} ) )
95 {
96 # new_file is inherited from Module::Generic and uses Module::Generic::File
97 my $file = $self->new_file( $args->{file} ) || return( $self->pass_error );
98 $fh = $file->open( '>', { binmode => 'utf8' }) || return( $self->error( "Unable to open file $file in write mode: $!" ) );
99 my @header = sort{ $a <=> $b } @fields;
100 my $date = DateTime::Lite->now;
101 my $table = $self->{table};
102 $fh->printf( "# Generated on %s for table $table\n", $date->strftime( '%c' ) );
103 $fh->print( "# ", CORE::join( "\t", @header ), "\n" );
104 my @data = ();
105 while( @data = $self->fetchrow() )
106 {
107 $fh->print( CORE::join( "\t", @data ), "\n" );
108 }
109 $fh->close();
110 $self->finish();
111 return( $self );
112 }
113 elsif( exists( $args->{fh} ) )
114 {
115 if( !fileno( $args->{fh} ) )
116 {
117 return( $self->error( "The file descriptor provided does not seem to be valid (not open)" ) );
118 }
119 $fh = IO::File->new_from_fd( $args->{fh}, 'w' ) || return( $self->error( $! ) );
120 }
121 my $max = 0;
122 # foreach my $field ( keys( %$fields ) )
123 foreach my $field ( @fields )
124 {
125 $max = length( $field ) if( length( $field ) > $max );
126 }
127 my $template = '';
128 # foreach my $field ( sort{ $fields->{ $a } <=> $fields->{ $b } } keys( %$fields ) )
129 foreach my $field ( @fields )
130 {
131 $template .= "$field" . ( '.' x ( $max - length( $field ) ) ) . ": %s\n";
132 }
133 $template .= "\n";
134 my @data = ();
135 while( @data = $self->fetchrow() )
136 {
137 $fh->printf( $template, @data );
138 }
139 $self->finish();
140 return( $self );
141 }
142  
143 # Inherited from DB::Object::Statement
144 # sub execute
145  
146 # Inherited from DB::Object::Statement
147 # sub executed
148  
149 # Inherited from DB::Object::Statement
150 # sub fetchall_arrayref($@)
151  
152 # Inherited from DB::Object::Statement
153 # sub fetchcol($;$)
154  
155 # Inherited from DB::Object::Statement
156 # sub fetchhash(@)
157  
158 # Inherited from DB::Object::Statement
159 # sub fetchrow(@)
160  
161 # Inherited from DB::Object::Statement
162 # sub finish
163  
164 sub ignore
165 {
166 my $self = shift( @_ );
167 my $query = $self->{query} ||
168 return( $self->error( "No query to set as to be ignored." ) );
169  
170 my $type = uc( ( $query =~ /^[[:blank:]]*(\S+)[[:blank:]]+/ )[ 0 ] );
171 # ALTER for table alteration statements (DB::Object::Tables
172 my @allowed = qw( INSERT UPDATE ALTER );
173 my $allowed = CORE::join( '|', @allowed );
174 if( !scalar( grep{ /^$type$/i } @allowed ) )
175 {
176 return( $self->error( "You may not flag statement of type \U$type\E to be ignored:\n$query" ) );
177 }
178 # Incompatible. Do not bother going further
179 return( $self ) if( $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+(?:DELAYED|LOW_PRIORITY|HIGH_PRIORITY)[[:blank:]]+/i );
180 return( $self ) if( $type eq 'ALTER' && $query !~ /^[[:blank:]]*$type[[:blank:]]+TABLE[[:blank:]]+/i );
181  
182 $query =~ s/^([[:blank:]]*)($allowed)([[:blank:]]+)/$1$2 IGNORE /;
183 my $sth = $self->_cache_this( $query ) ||
184 return( $self->error( "Error while preparing new ignored query:\n$query" ) );
185 if( !defined( wantarray() ) )
186 {
187 $sth->execute() ||
188 return( $self->error( "Error while executing new ignored query:\n$query" ) );
189 }
190 return( $sth );
191 }
192  
193 # Inherited from DB::Object::Statement
194 # sub join
195  
196 # Inherited from DB::Object::Statement
197 # sub object
198  
199 sub only
200 {
201 return( shift->error( "SELECT | DELETE | UPDATE ONLY is not supported by Mysql." ) );
202 }
203  
204 sub priority
205 {
206 my $self = shift( @_ );
207 my $prio = shift( @_ );
208 my $map =
209 {
210 0 => 'LOW_PRIORITY',
211 1 => 'HIGH_PRIORITY',
212 };
213 # Bad argument. Do not bother
214 return( $self ) if( !exists( $map->{ $prio } ) );
215  
216 my $query = $self->{query} ||
217 return( $self->error( "No query to set priority for was provided." ) );
218 my $type = uc( ( $query =~ /^[[:blank:]]*(\S+)[[:blank:]]+/ )[ 0 ] );
219 my @allowed = qw( DELETE INSERT REPLACE SELECT UPDATE );
220 my $allowed = CORE::join( '|', @allowed );
221 # Ignore if not allowed
222 if( !scalar( grep{ /^$type$/i } @allowed ) )
223 {
224 $self->error( "You may not set priority on statement of type \U$type\E:\n$query" );
225 return( $self );
226 }
227 # Incompatible. Do not bother going further
228 return( $self ) if( $query =~ /^\s*(?:$allowed)\s+(?:DELAYED|LOW_PRIORITY|HIGH_PRIORITY)\s+/i );
229 # SELECT with something else than HIGH_PRIORITY is incompatible, so do not bother to go further
230 return( $self ) if( $prio != 1 && $type =~ /^(?:SELECT)$/i );
231 return( $self ) if( $prio != 0 && $type =~ /^(?:DELETE|INSERT|REPLACE|UPDATE)$/i );
232  
233 $query =~ s/^([[:blank:]]*)($allowed)([[:blank:]]+)/$1$2 $map->{ $prio } /i;
234 my $sth = $self->_cache_this( $query ) ||
235 return( $self->error( "Error while preparing new low priority query:\n$query" ) );
236 if( !defined( wantarray() ) )
237 {
238 $sth->execute() ||
239 return( $self->error( "Error while executing new low priority query:\n$query" ) );
240 }
241 return( $sth );
242 }
243  
244 # rollback is called using the dbh handler and is located in DB::Object::Postgres
245  
246 # Inherited from DB::Object::Statement
247 # sub rows(@)
248  
249 # Inherited from DB::Object::Statement
250 # sub undo
251  
252 sub wait
253 {
254 my $self = shift( @_ );
255 my $query = $self->{query} ||
256 return( $self->error( "No query to set as to be delayed." ) );
257 my $type = ( $query =~ /^[[:blank:]]*(\S+)[[:blank:]]+/ )[ 0 ];
258 my @allowed = qw( INSERT REPLACE );
259 my $allowed = CORE::join( '|', @allowed );
260 # Ignore if not allowed
261 if( !scalar( grep{ /^$type$/i } @allowed ) )
262 {
263 $self->error( "You may not use wait (delayed query) on statement of type \U$type\E:\n$query" );
264 return( $self );
265 }
266 # Incompatible. Do not bother going further
267 return( $self ) if( $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+(?:DELAYED|LOW_PRIORITY|HIGH_PRIORITY)[[:blank:]]+/i );
268 $query =~ s/^([[:blank:]]*)($allowed)([[:blank:]]+)/$1$2 DELAYED /i;
269 my $sth = $self->_cache_this( $query ) ||
270 return( $self->error( "Error while preparing new delayed query:\n$query" ) );
271 if( !defined( wantarray() ) )
272 {
273 $sth->execute() ||
274 return( $self->error( "Error while executing new delayed query:\n$query" ) );
275 }
276 return( $sth );
277 }
278  
279 DESTROY
280 {
281 # Do nothing but existing so it is handled by this package
282 # print( STDERR "DESTROY'ing statement $self ($self->{ 'query' })\n" );
283 };
284  
285 1;
286 # NOTE: POD
287 __END__