| 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 | ## | ||
| 12 | ## This program is free software; you can redistribute it and/or modify it | ||
| 13 | ## under the same terms as Perl itself. | ||
| 14 | ##---------------------------------------------------------------------------- | ||
| 15 | #---------------------------------------------------------------------------- | ||
| 16 | # DB/Object/Mysql/Statement.pm | ||
| 17 | # Version 0.3 | ||
| 18 | # Copyright(c) 2019 Jacques Deguest | ||
| 19 | # Author: Jacques Deguest <jack@deguest.jp> | ||
| 20 | # Created 2017/07/19 | ||
| 21 | # Modified 2019/06/17 | ||
| 22 | # All rights reserved. | ||
| 23 | # | ||
| 24 | # This program is free software; you can redistribute it and/or modify it | ||
| 25 | # under the same terms as Perl itself. | ||
| 26 | #---------------------------------------------------------------------------- | ||
| 27 | # This package's purpose is to automatically terminate the statement object and | ||
| 28 | # separate them from the connection object (DB::Object). | ||
| 29 | # Connection object last longer than statement objects | ||
| 30 | #---------------------------------------------------------------------------- | ||
| 31 | package DB::Object::Mysql::Statement; | ||
| 32 | BEGIN | ||
| 33 | { | ||
| 34 | use strict; | ||
| 35 | use warnings; | ||
| 36 | use parent qw( DB::Object::Statement DB::Object::Mysql ); | ||
| 37 | use vars qw( $VERSION $DEBUG ); | ||
| 38 | our $DEBUG = 0; | ||
| 39 | our $VERSION = 'v0.300.2'; | ||
| 40 | }; | ||
| 41 | |||
| 42 | use strict; | ||
| 43 | use warnings; | ||
| 44 | |||
| 45 | # Inherited from DB::Object::Statement | ||
| 46 | # sub bind_param | ||
| 47 | |||
| 48 | # sub commit is called by dbh, so it is in DB::Object::Postgres | ||
| 49 | |||
| 50 | # Customised for MySQL | ||
| 51 | sub distinct | ||
| 52 | { | ||
| 53 | my $self = shift( @_ ); | ||
| 54 | my $query = $self->{query} || | ||
| 55 | return( $self->error( "No query to set as to be ignored." ) ); | ||
| 56 | |||
| 57 | my $type = uc( ( $query =~ /^\s*(\S+)\s+/ )[ 0 ] ); | ||
| 58 | # ALTER for table alteration statements (DB::Object::Tables | ||
| 59 | my @allowed = qw( SELECT ); | ||
| 60 | my $allowed = CORE::join( '|', @allowed ); | ||
| 61 | if( !scalar( grep{ /^$type$/i } @allowed ) ) | ||
| 62 | { | ||
| 63 | return( $self->error( "You may not flag statement of type \U$type\E to be distinct:\n$query" ) ); | ||
| 64 | } | ||
| 65 | # Incompatible. Do not bother going further | ||
| 66 | return( $self ) if( $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+(?:DISTINCT|DISTINCTROW|ALL)[[:blank:]]+/i ); | ||
| 67 | |||
| 68 | $query =~ s/^([[:blank:]]*)($allowed)([[:blank:]]+)/$1$2 DISTINCT /; | ||
| 69 | my $sth = $self->_cache_this( $query ) || | ||
| 70 | return( $self->error( "Error while preparing new ignored query:\n$query" ) ); | ||
| 71 | if( !defined( wantarray() ) ) | ||
| 72 | { | ||
| 73 | $sth->execute() || | ||
| 74 | return( $self->error( "Error while executing new ignored query:\n$query" ) ); | ||
| 75 | } | ||
| 76 | return( $sth ); | ||
| 77 | } | ||
| 78 | |||
| 79 | # Customised for MySQL | ||
| 80 | sub dump | ||
| 81 | { | ||
| 82 | my $self = shift( @_ ); | ||
| 83 | my $args = @_ == 1 ? shift( @_ ) : { @_ }; | ||
| 84 | my $vsep = ","; | ||
| 85 | my $hsep = "\n"; | ||
| 86 | my $width = 35; | ||
| 87 | $self->_load_class( 'DateTime' ) || return( $self->pass_error ); | ||
| 88 | $self->_load_class( 'Module::Generic::File' ) || return( $self->pass_error ); | ||
| 89 | my $fh = Module::Generic::File->stdout() || | ||
| 90 | return( $self->pass_error( Module::Generic::File->error ) ); | ||
| 91 | $vsep = $args->{vsep} if( exists( $args->{vsep} ) ); | ||
| 92 | $hsep = $args->{hsep} if( exists( $args->{hsep} ) ); | ||
| 93 | $width = $args->{width} if( exists( $args->{width} ) ); | ||
| 94 | my @fields = (); | ||
| 95 | # my @fields = @{$self->{sth}->FETCH( 'NAME' )}; | ||
| 96 | my $fields_ref = $self->{sth}->{NAME}; | ||
| 97 | if( defined( $fields_ref ) && ref( $fields_ref ) eq 'ARRAY' ) | ||
| 98 | { | ||
| 99 | @fields = @$fields_ref; | ||
| 100 | } | ||
| 101 | else | ||
| 102 | { | ||
| 103 | return( $self->error( "No array reference of fields could be retrieved from statement '$self->{sth}'." ) ); | ||
| 104 | } | ||
| 105 | return( $self->error( "No query to dump." ) ) if( !exists( $self->{sth} ) ); | ||
| 106 | if( exists( $args->{file} ) ) | ||
| 107 | { | ||
| 108 | # new_file is inherited from Module::Generic and uses Module::Generic::File | ||
| 109 | my $file = $self->new_file( $args->{file} ) || return( $self->pass_error ); | ||
| 110 | $fh = $file->open( '>', { binmode => 'utf8' }) || return( $self->error( "Unable to open file $file in write mode: $!" ) ); | ||
| 111 | my @header = sort{ $a <=> $b } @fields; | ||
| 112 | my $date = DateTime->now; | ||
| 113 | my $table = $self->{table}; | ||
| 114 | $fh->printf( "# Generated on %s for table $table\n", $date->strftime( '%c' ) ); | ||
| 115 | $fh->print( "# ", CORE::join( "\t", @header ), "\n" ); | ||
| 116 | my @data = (); | ||
| 117 | while( @data = $self->fetchrow() ) | ||
| 118 | { | ||
| 119 | $fh->print( CORE::join( "\t", @data ), "\n" ); | ||
| 120 | } | ||
| 121 | $fh->close(); | ||
| 122 | $self->finish(); | ||
| 123 | return( $self ); | ||
| 124 | } | ||
| 125 | elsif( exists( $args->{fh} ) ) | ||
| 126 | { | ||
| 127 | if( !fileno( $args->{fh} ) ) | ||
| 128 | { | ||
| 129 | return( $self->error( "The file descriptor provided does not seem to be valid (not open)" ) ); | ||
| 130 | } | ||
| 131 | $fh = IO::File->new_from_fd( $args->{fh}, 'w' ) || return( $self->error( $! ) ); | ||
| 132 | } | ||
| 133 | my $max = 0; | ||
| 134 | # foreach my $field ( keys( %$fields ) ) | ||
| 135 | foreach my $field ( @fields ) | ||
| 136 | { | ||
| 137 | $max = length( $field ) if( length( $field ) > $max ); | ||
| 138 | } | ||
| 139 | my $template = ''; | ||
| 140 | # foreach my $field ( sort{ $fields->{ $a } <=> $fields->{ $b } } keys( %$fields ) ) | ||
| 141 | foreach my $field ( @fields ) | ||
| 142 | { | ||
| 143 | $template .= "$field" . ( '.' x ( $max - length( $field ) ) ) . ": %s\n"; | ||
| 144 | } | ||
| 145 | $template .= "\n"; | ||
| 146 | my @data = (); | ||
| 147 | while( @data = $self->fetchrow() ) | ||
| 148 | { | ||
| 149 | $fh->printf( $template, @data ); | ||
| 150 | } | ||
| 151 | $self->finish(); | ||
| 152 | return( $self ); | ||
| 153 | } | ||
| 154 | |||
| 155 | # Inherited from DB::Object::Statement | ||
| 156 | # sub execute | ||
| 157 | |||
| 158 | # Inherited from DB::Object::Statement | ||
| 159 | # sub executed | ||
| 160 | |||
| 161 | # Inherited from DB::Object::Statement | ||
| 162 | # sub fetchall_arrayref($@) | ||
| 163 | |||
| 164 | # Inherited from DB::Object::Statement | ||
| 165 | # sub fetchcol($;$) | ||
| 166 | |||
| 167 | # Inherited from DB::Object::Statement | ||
| 168 | # sub fetchhash(@) | ||
| 169 | |||
| 170 | # Inherited from DB::Object::Statement | ||
| 171 | # sub fetchrow(@) | ||
| 172 | |||
| 173 | # Inherited from DB::Object::Statement | ||
| 174 | # sub finish | ||
| 175 | |||
| 176 | sub ignore | ||
| 177 | { | ||
| 178 | my $self = shift( @_ ); | ||
| 179 | my $query = $self->{query} || | ||
| 180 | return( $self->error( "No query to set as to be ignored." ) ); | ||
| 181 | |||
| 182 | my $type = uc( ( $query =~ /^[[:blank:]]*(\S+)[[:blank:]]+/ )[ 0 ] ); | ||
| 183 | # ALTER for table alteration statements (DB::Object::Tables | ||
| 184 | my @allowed = qw( INSERT UPDATE ALTER ); | ||
| 185 | my $allowed = CORE::join( '|', @allowed ); | ||
| 186 | if( !scalar( grep{ /^$type$/i } @allowed ) ) | ||
| 187 | { | ||
| 188 | return( $self->error( "You may not flag statement of type \U$type\E to be ignored:\n$query" ) ); | ||
| 189 | } | ||
| 190 | # Incompatible. Do not bother going further | ||
| 191 | return( $self ) if( $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+(?:DELAYED|LOW_PRIORITY|HIGH_PRIORITY)[[:blank:]]+/i ); | ||
| 192 | return( $self ) if( $type eq 'ALTER' && $query !~ /^[[:blank:]]*$type[[:blank:]]+TABLE[[:blank:]]+/i ); | ||
| 193 | |||
| 194 | $query =~ s/^([[:blank:]]*)($allowed)([[:blank:]]+)/$1$2 IGNORE /; | ||
| 195 | my $sth = $self->_cache_this( $query ) || | ||
| 196 | return( $self->error( "Error while preparing new ignored query:\n$query" ) ); | ||
| 197 | if( !defined( wantarray() ) ) | ||
| 198 | { | ||
| 199 | $sth->execute() || | ||
| 200 | return( $self->error( "Error while executing new ignored query:\n$query" ) ); | ||
| 201 | } | ||
| 202 | return( $sth ); | ||
| 203 | } | ||
| 204 | |||
| 205 | # Inherited from DB::Object::Statement | ||
| 206 | # sub join | ||
| 207 | |||
| 208 | # Inherited from DB::Object::Statement | ||
| 209 | # sub object | ||
| 210 | |||
| 211 | sub only | ||
| 212 | { | ||
| 213 | return( shift->error( "SELECT | DELETE | UPDATE ONLY is not supported by Mysql." ) ); | ||
| 214 | } | ||
| 215 | |||
| 216 | sub priority | ||
| 217 | { | ||
| 218 | my $self = shift( @_ ); | ||
| 219 | my $prio = shift( @_ ); | ||
| 220 | my $map = | ||
| 221 | { | ||
| 222 | 0 => 'LOW_PRIORITY', | ||
| 223 | 1 => 'HIGH_PRIORITY', | ||
| 224 | }; | ||
| 225 | # Bad argument. Do not bother | ||
| 226 | return( $self ) if( !exists( $map->{ $prio } ) ); | ||
| 227 | |||
| 228 | my $query = $self->{query} || | ||
| 229 | return( $self->error( "No query to set priority for was provided." ) ); | ||
| 230 | my $type = uc( ( $query =~ /^[[:blank:]]*(\S+)[[:blank:]]+/ )[ 0 ] ); | ||
| 231 | my @allowed = qw( DELETE INSERT REPLACE SELECT UPDATE ); | ||
| 232 | my $allowed = CORE::join( '|', @allowed ); | ||
| 233 | # Ignore if not allowed | ||
| 234 | if( !scalar( grep{ /^$type$/i } @allowed ) ) | ||
| 235 | { | ||
| 236 | $self->error( "You may not set priority on statement of type \U$type\E:\n$query" ); | ||
| 237 | return( $self ); | ||
| 238 | } | ||
| 239 | # Incompatible. Do not bother going further | ||
| 240 | return( $self ) if( $query =~ /^\s*(?:$allowed)\s+(?:DELAYED|LOW_PRIORITY|HIGH_PRIORITY)\s+/i ); | ||
| 241 | # SELECT with something else than HIGH_PRIORITY is incompatible, so do not bother to go further | ||
| 242 | return( $self ) if( $prio != 1 && $type =~ /^(?:SELECT)$/i ); | ||
| 243 | return( $self ) if( $prio != 0 && $type =~ /^(?:DELETE|INSERT|REPLACE|UPDATE)$/i ); | ||
| 244 | |||
| 245 | $query =~ s/^([[:blank:]]*)($allowed)([[:blank:]]+)/$1$2 $map->{ $prio } /i; | ||
| 246 | my $sth = $self->_cache_this( $query ) || | ||
| 247 | return( $self->error( "Error while preparing new low priority query:\n$query" ) ); | ||
| 248 | if( !defined( wantarray() ) ) | ||
| 249 | { | ||
| 250 | $sth->execute() || | ||
| 251 | return( $self->error( "Error while executing new low priority query:\n$query" ) ); | ||
| 252 | } | ||
| 253 | return( $sth ); | ||
| 254 | } | ||
| 255 | |||
| 256 | # rollback is called using the dbh handler and is located in DB::Object::Postgres | ||
| 257 | |||
| 258 | # Inherited from DB::Object::Statement | ||
| 259 | # sub rows(@) | ||
| 260 | |||
| 261 | # Inherited from DB::Object::Statement | ||
| 262 | # sub undo | ||
| 263 | |||
| 264 | sub wait | ||
| 265 | { | ||
| 266 | my $self = shift( @_ ); | ||
| 267 | my $query = $self->{query} || | ||
| 268 | return( $self->error( "No query to set as to be delayed." ) ); | ||
| 269 | my $type = ( $query =~ /^[[:blank:]]*(\S+)[[:blank:]]+/ )[ 0 ]; | ||
| 270 | my @allowed = qw( INSERT REPLACE ); | ||
| 271 | my $allowed = CORE::join( '|', @allowed ); | ||
| 272 | # Ignore if not allowed | ||
| 273 | if( !scalar( grep{ /^$type$/i } @allowed ) ) | ||
| 274 | { | ||
| 275 | $self->error( "You may not use wait (delayed query) on statement of type \U$type\E:\n$query" ); | ||
| 276 | return( $self ); | ||
| 277 | } | ||
| 278 | # Incompatible. Do not bother going further | ||
| 279 | return( $self ) if( $query =~ /^[[:blank:]]*(?:$allowed)[[:blank:]]+(?:DELAYED|LOW_PRIORITY|HIGH_PRIORITY)[[:blank:]]+/i ); | ||
| 280 | $query =~ s/^([[:blank:]]*)($allowed)([[:blank:]]+)/$1$2 DELAYED /i; | ||
| 281 | my $sth = $self->_cache_this( $query ) || | ||
| 282 | return( $self->error( "Error while preparing new delayed query:\n$query" ) ); | ||
| 283 | if( !defined( wantarray() ) ) | ||
| 284 | { | ||
| 285 | $sth->execute() || | ||
| 286 | return( $self->error( "Error while executing new delayed query:\n$query" ) ); | ||
| 287 | } | ||
| 288 | return( $sth ); | ||
| 289 | } | ||
| 290 | |||
| 291 | DESTROY | ||
| 292 | { | ||
| 293 | # Do nothing but existing so it is handled by this package | ||
| 294 | # print( STDERR "DESTROY'ing statement $self ($self->{ 'query' })\n" ); | ||
| 295 | }; | ||
| 296 | |||
| 297 | 1; | ||
| 298 | |||
| 299 | __END__ | ||
| 300 | |||
| 301 | =encoding utf-8 | ||
| 302 | |||
| 303 | =head1 NAME | ||
| 304 | |||
| 305 | DB::Object::Mysql::Query - Statement Object for MySQL | ||
| 306 | |||
| 307 | =head1 SYNOPSIS | ||
| 308 | |||
| 309 | use DB::Object::Mysql::Statement; | ||
| 310 | my $this = DB::Object::Mysql::Statement->new || die( DB::Object::Mysql::Statement->error, "\n" ); | ||
| 311 | |||
| 312 | =head1 VERSION | ||
| 313 | |||
| 314 | v0.300.2 | ||
| 315 | |||
| 316 | =head1 DESCRIPTION | ||
| 317 | |||
| 318 | This is a MySQL specific statement object. | ||
| 319 | |||
| 320 | =head1 METHODS | ||
| 321 | |||
| 322 | =head2 distinct | ||
| 323 | |||
| 324 | This takes no argument and this will modify the query to add the keyword C<DISTINCT> | ||
| 325 | |||
| 326 | $sth->distinct; | ||
| 327 | # produces SELECT DISTINCT.... | ||
| 328 | $sth->distinct( 'name' ); | ||
| 329 | # produces SELECT DISTINCT ON (name).... | ||
| 330 | |||
| 331 | If called in void context, this will execute the prepare statement handler immediately. | ||
| 332 | |||
| 333 | It returns the newly created statement handler. | ||
| 334 | |||
| 335 | See L<MySQL documentation for more information|https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html> | ||
| 336 | |||
| 337 | =head2 dump | ||
| 338 | |||
| 339 | This will dump the result of the query to STDOUT or to a file if I<file> argument is provided, or if a filehandle is provided with I<fh>, it will be used to print out the data. | ||
| 340 | |||
| 341 | It takes also a I<vsep>, which defaults to a command and a I<hsep> which defaults to a new line. | ||
| 342 | |||
| 343 | It returns the current object. | ||
| 344 | |||
| 345 | =head2 ignore | ||
| 346 | |||
| 347 | This takes no argument and this will modify the queries of type C<alter>, C<insert>, C<update> to add the keyword C<IGNORE> | ||
| 348 | |||
| 349 | $sth->ignore; | ||
| 350 | # produces INSERT IGNORE.... | ||
| 351 | |||
| 352 | If called in void context, this will execute the prepare statement handler immediately. | ||
| 353 | |||
| 354 | It returns the newly created statement handler. | ||
| 355 | |||
| 356 | See L<MySQL documentation for more information|https://dev.mysql.com/doc/refman/5.7/en/insert.html> | ||
| 357 | |||
| 358 | =head2 only | ||
| 359 | |||
| 360 | This returns an error as C<SELECT FROM ONLY> is not supported by MySQL. | ||
| 361 | |||
| 362 | =head2 priority | ||
| 363 | |||
| 364 | Provided with a priority integer that can be 0 or 1 with 0 being C<LOW_PRIORITY> and 1 being C<HIGH_PRIORITY> and this will adjust the query formatted to add the priority. This works only on Mysql drive though. | ||
| 365 | |||
| 366 | If used on queries other than C<DELETE>, C<INSERT>, C<REPLACE>, C<SELECT>, C<UPDATE> an error will be returned. | ||
| 367 | |||
| 368 | If called in void context, this will execute the newly create statement handler immediately. | ||
| 369 | |||
| 370 | It returns the newly create statement handler. | ||
| 371 | |||
| 372 | =head2 wait | ||
| 373 | |||
| 374 | $sth->wait || die( $sth->error ); | ||
| 375 | |||
| 376 | This takes no parameter and only works on queries of type C<INSERT> or C<UPDATE>. It will modify the previously prepared query to add the keyword C<DELAYED> | ||
| 377 | |||
| 378 | If called in void context, this will execute the prepare statement handler immediately. | ||
| 379 | |||
| 380 | It returns the newly created statement handler. | ||
| 381 | |||
| 382 | See L<MySQL documentation for more information|https://dev.mysql.com/doc/refman/5.6/en/insert-delayed.html> | ||
| 383 | |||
| 384 | =head1 SEE ALSO | ||
| 385 | |||
| 386 | L<perl> | ||
| 387 | |||
| 388 | =head1 AUTHOR | ||
| 389 | |||
| 390 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 391 | |||
| 392 | =head1 COPYRIGHT & LICENSE | ||
| 393 | |||
| 394 | Copyright (c) 2019-2021 DEGUEST Pte. Ltd. | ||
| 395 | |||
| 396 | You can use, copy, modify and redistribute this package and associated | ||
| 397 | files under the same terms as Perl itself. | ||
| 398 | |||
| 399 | =cut |