| 1 | # -*- perl -*- | ||
| 2 | ##---------------------------------------------------------------------------- | ||
| 3 | ## Database Object Interface - ~/lib/DB/Object/Mysql.pm | ||
| 4 | ## Version v1.5.1 | ||
| 5 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||
| 6 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 7 | ## Created 2017/07/19 | ||
| 8 | ## Modified 2026/03/26 | ||
| 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 is the subclassable module for driver specific ones. | ||
| 16 | package DB::Object::Mysql; | ||
| 17 | BEGIN | ||
| 18 | { | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use parent qw( DB::Object ); | ||
| 22 | use vars qw( | ||
| 23 | $VERSION $CACHE_SIZE | ||
| 24 | $DEBUG $ERROR $MOD_PERL $USE_BIND $USE_CACHE $PLACEHOLDER_REGEXP | ||
| 25 | $DATATYPES_DICT $EXCEPTION_CLASS | ||
| 26 | ); | ||
| 27 | eval{ require DBD::mysql; }; | ||
| 28 | die( $@ ) if( $@ ); | ||
| 29 | # <https://dev.mysql.com/doc/refman/8.0/en/data-types.html> | ||
| 30 | # the 'constant' property in the dictionary hash is added in structure() | ||
| 31 | # See also: SELECT DISTINCT(data_type) FROM information_schema.columns ORDER by data_type | ||
| 32 | # but this does not provide a complete list of datatype | ||
| 33 | our $DATATYPES_DICT = | ||
| 34 | { | ||
| 35 | bigint => { | ||
| 36 | constant => '', | ||
| 37 | name => 'SQL_BIGINT', | ||
| 38 | re => qr/^(bigint)/, | ||
| 39 | type => 'bigint', | ||
| 40 | }, | ||
| 41 | binary => { | ||
| 42 | constant => '', | ||
| 43 | name => 'SQL_BINARY', | ||
| 44 | re => qr/^binary/, | ||
| 45 | type => 'binary', | ||
| 46 | }, | ||
| 47 | bit => { | ||
| 48 | constant => '', | ||
| 49 | name => 'SQL_BIT', | ||
| 50 | re => qr/^(bit)/, | ||
| 51 | type => 'bit', | ||
| 52 | }, | ||
| 53 | blob => { | ||
| 54 | constant => '', | ||
| 55 | name => 'SQL_BLOB', | ||
| 56 | re => qr/^blob/, | ||
| 57 | type => 'blob', | ||
| 58 | }, | ||
| 59 | curve => { | ||
| 60 | constant => '', | ||
| 61 | name => 'SQL_UNKNOWN_TYPE', | ||
| 62 | re => qr/^(curve)\b/, | ||
| 63 | type => 'curve', | ||
| 64 | }, | ||
| 65 | date => { | ||
| 66 | constant => '', | ||
| 67 | name => 'SQL_DATE', | ||
| 68 | re => qr/^(date)\b/, | ||
| 69 | type => 'date', | ||
| 70 | }, | ||
| 71 | datetime => { | ||
| 72 | constant => '', | ||
| 73 | name => 'SQL_DATETIME', | ||
| 74 | re => qr/^(datetime)/, | ||
| 75 | type => 'datetime', | ||
| 76 | }, | ||
| 77 | decimal => { | ||
| 78 | alias => [qw( dec )], | ||
| 79 | constant => '', | ||
| 80 | name => 'SQL_DECIMAL', | ||
| 81 | re => qr/^(dec|decimal)/, | ||
| 82 | type => 'decimal', | ||
| 83 | }, | ||
| 84 | double => { | ||
| 85 | alias => [ 'double precision' ], | ||
| 86 | constant => '', | ||
| 87 | name => 'SQL_DOUBLE', | ||
| 88 | re => qr/^(double\s+precision|double)/, | ||
| 89 | type => 'double', | ||
| 90 | }, | ||
| 91 | enum => { | ||
| 92 | constant => '', | ||
| 93 | name => 'SQL_UNKNOWN_TYPE', | ||
| 94 | re => qr/^(enum)\b/, | ||
| 95 | type => 'enum', | ||
| 96 | }, | ||
| 97 | float => { | ||
| 98 | constant => '', | ||
| 99 | name => 'SQL_FLOAT', | ||
| 100 | re => qr/^(float)/, | ||
| 101 | type => 'float', | ||
| 102 | }, | ||
| 103 | geometry => { | ||
| 104 | constant => '', | ||
| 105 | name => 'SQL_UNKNOWN_TYPE', | ||
| 106 | re => qr/^(geometry)\b/, | ||
| 107 | type => 'geometry', | ||
| 108 | }, | ||
| 109 | geometrycollection => { | ||
| 110 | constant => '', | ||
| 111 | name => 'SQL_UNKNOWN_TYPE', | ||
| 112 | re => qr/^(geometrycollection)\b/, | ||
| 113 | type => 'geometrycollection', | ||
| 114 | }, | ||
| 115 | int => { | ||
| 116 | alias => [qw( integer )], | ||
| 117 | constant => '', | ||
| 118 | name => 'SQL_INTEGER', | ||
| 119 | re => qr/^(integer|int)/, | ||
| 120 | type => 'int', | ||
| 121 | }, | ||
| 122 | json => { | ||
| 123 | constant => '', | ||
| 124 | name => 'SQL_UNKNOWN_TYPE', | ||
| 125 | re => qr/^(json)\b/, | ||
| 126 | type => 'json', | ||
| 127 | }, | ||
| 128 | linestring => { | ||
| 129 | constant => '', | ||
| 130 | name => 'SQL_UNKNOWN_TYPE', | ||
| 131 | re => qr/^(linestring)\b/, | ||
| 132 | type => 'linestring', | ||
| 133 | }, | ||
| 134 | longlob => { | ||
| 135 | constant => '', | ||
| 136 | name => 'SQL_BLOB', | ||
| 137 | re => qr/^(longlob)\b/, | ||
| 138 | type => 'longlob', | ||
| 139 | }, | ||
| 140 | longtext => { | ||
| 141 | constant => '', | ||
| 142 | name => 'SQL_LONGVARCHAR', | ||
| 143 | re => qr/^(longtext)\b/, | ||
| 144 | type => 'longtext', | ||
| 145 | }, | ||
| 146 | mediumblob => { | ||
| 147 | constant => '', | ||
| 148 | name => 'SQL_BLOB', | ||
| 149 | re => qr/^(mediumblob)\b/, | ||
| 150 | type => 'mediumblob', | ||
| 151 | }, | ||
| 152 | mediumint => { | ||
| 153 | constant => '', | ||
| 154 | name => 'SQL_BIT', | ||
| 155 | re => qr/^(mediumint)/, | ||
| 156 | type => 'mediumint', | ||
| 157 | }, | ||
| 158 | mediumtext => { | ||
| 159 | constant => '', | ||
| 160 | name => 'SQL_LONGVARCHAR', | ||
| 161 | re => qr/^(mediumtext)\b/, | ||
| 162 | type => 'mediumtext', | ||
| 163 | }, | ||
| 164 | multicurve => { | ||
| 165 | constant => '', | ||
| 166 | name => 'SQL_UNKNOWN_TYPE', | ||
| 167 | re => qr/^(multicurve)\b/, | ||
| 168 | type => 'multicurve', | ||
| 169 | }, | ||
| 170 | multilinestring => { | ||
| 171 | constant => '', | ||
| 172 | name => 'SQL_UNKNOWN_TYPE', | ||
| 173 | re => qr/^(multilinestring)\b/ , | ||
| 174 | type => 'multilinestring', | ||
| 175 | }, | ||
| 176 | multipoint => { | ||
| 177 | constant => '', | ||
| 178 | name => 'SQL_UNKNOWN_TYPE', | ||
| 179 | re => qr/^(multipoint)\b/, | ||
| 180 | type => 'multipoint', | ||
| 181 | }, | ||
| 182 | multipolygon => { | ||
| 183 | constant => '', | ||
| 184 | name => 'SQL_UNKNOWN_TYPE', | ||
| 185 | re => qr/^(multipolygon)\b/, | ||
| 186 | type => 'multipolygon', | ||
| 187 | }, | ||
| 188 | point => { | ||
| 189 | constant => '', | ||
| 190 | name => 'SQL_UNKNOWN_TYPE', | ||
| 191 | re => qr/^(point)\b/, | ||
| 192 | type => 'point', | ||
| 193 | }, | ||
| 194 | polygon => { | ||
| 195 | constant => '', | ||
| 196 | name => 'SQL_UNKNOWN_TYPE', | ||
| 197 | re => qr/^(polygon)\b/, | ||
| 198 | type => 'polygon', | ||
| 199 | }, | ||
| 200 | set => { | ||
| 201 | constant => '', | ||
| 202 | name => 'SQL_UNKNOWN_TYPE', | ||
| 203 | re => qr/^(set)\b/, | ||
| 204 | type => 'set', | ||
| 205 | }, | ||
| 206 | smallint => { | ||
| 207 | constant => '', | ||
| 208 | name => 'SQL_SMALLINT', | ||
| 209 | re => qr/^(smallint)/, | ||
| 210 | type => 'smallint', | ||
| 211 | }, | ||
| 212 | text => { | ||
| 213 | constant => '', | ||
| 214 | name => 'SQL_LONGVARCHAR', | ||
| 215 | re => qr/^text/, | ||
| 216 | type => 'text', | ||
| 217 | }, | ||
| 218 | timestamp => { | ||
| 219 | constant => '', | ||
| 220 | name => 'SQL_TIMESTAMP', | ||
| 221 | re => qr/^(timestamp)/, | ||
| 222 | type => 'timestamp', | ||
| 223 | }, | ||
| 224 | tinyblob => { | ||
| 225 | constant => '', | ||
| 226 | name => 'SQL_BLOB', | ||
| 227 | re => qr/^(tinyblob)\b/, | ||
| 228 | type => 'tinyblob', | ||
| 229 | }, | ||
| 230 | tinyint => { | ||
| 231 | constant => '', | ||
| 232 | name => 'SQL_TINYINT', | ||
| 233 | re => qr/^(tinyint)/, | ||
| 234 | type => 'tinyint', | ||
| 235 | }, | ||
| 236 | tinytext => { | ||
| 237 | constant => '', | ||
| 238 | name => 'SQL_LONGVARCHAR', | ||
| 239 | re => qr/^(tinytext)\b/, | ||
| 240 | type => 'tinytext', | ||
| 241 | }, | ||
| 242 | varbinary => { | ||
| 243 | constant => '', | ||
| 244 | name => 'SQL_VARBINARY', | ||
| 245 | re => qr/^varbinary/, | ||
| 246 | type => 'varbinary', | ||
| 247 | }, | ||
| 248 | varchar => { | ||
| 249 | alias => [qw( char character ), 'character varying' ], | ||
| 250 | constant => '', | ||
| 251 | name => 'SQL_VARCHAR', | ||
| 252 | re => qr/^((?:character varying|varchar)|(?:(?:character|char)\b(?![[:blank:]]+varying)))/, | ||
| 253 | type => 'varchar', | ||
| 254 | }, | ||
| 255 | year => { | ||
| 256 | constant => '', | ||
| 257 | name => 'SQL_INTERVAL_YEAR', | ||
| 258 | re => qr/^(year)/, | ||
| 259 | type => 'year', | ||
| 260 | }, | ||
| 261 | }; | ||
| 262 | # DBI->trace(5); | ||
| 263 | our $PLACEHOLDER_REGEXP = qr/(?<![?\w])\?(?![?\w])/; | ||
| 264 | our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS; | ||
| 265 | our $VERSION = 'v1.5.1'; | ||
| 266 | }; | ||
| 267 | |||
| 268 | use strict; | ||
| 269 | use warnings; | ||
| 270 | our $DEBUG = 0; | ||
| 271 | our $CACHE_SIZE = 10; | ||
| 272 | our $USE_BIND = 0; | ||
| 273 | our $USE_CACHE = 0; | ||
| 274 | our $MOD_PERL = 0; | ||
| 275 | if( $INC{ 'Apache/DBI.pm' } && | ||
| 276 | substr( $ENV{GATEWAY_INTERFACE}|| '', 0, 8 ) eq 'CGI-Perl' ) | ||
| 277 | { | ||
| 278 | $MOD_PERL++; | ||
| 279 | } | ||
| 280 | # Actually the one in DB::Object is used, because DBD::mysql has no datatype constants of its own | ||
| 281 | # our $DATATYPES = {}; | ||
| 282 | foreach my $type ( keys( %$DATATYPES_DICT ) ) | ||
| 283 | { | ||
| 284 | if( CORE::exists( $DATATYPES_DICT->{ $type }->{alias} ) && | ||
| 285 | ref( $DATATYPES_DICT->{ $type }->{alias} ) eq 'ARRAY' && | ||
| 286 | scalar( @{$DATATYPES_DICT->{ $type }->{alias}} ) ) | ||
| 287 | { | ||
| 288 | foreach my $alias ( @{$DATATYPES_DICT->{ $type }->{alias}} ) | ||
| 289 | { | ||
| 290 | next if( CORE::exists( $DATATYPES_DICT->{ $alias } ) ); | ||
| 291 | $DATATYPES_DICT->{ $alias } = $DATATYPES_DICT->{ $type }; | ||
| 292 | } | ||
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | my $keys = $DBI::EXPORT_TAGS{sql_types}; | ||
| 297 | my @constants_to_ignore = qw( | ||
| 298 | SQL_GUID SQL_WLONGVARCHAR SQL_WVARCHAR SQL_WCHAR SQL_LONGVARBINARY SQL_LONGVARCHAR | ||
| 299 | SQL_UNKNOWN_TYPE SQL_ALL_TYPES SQL_NUMERIC SQL_REAL SQL_INTERVAL SQL_TIME SQL_BOOLEAN | ||
| 300 | SQL_UDT SQL_UDT_LOCATOR SQL_ROW SQL_REF SQL_BLOB_LOCATOR SQL_CLOB SQL_CLOB_LOCATOR | ||
| 301 | SQL_ARRAY SQL_ARRAY_LOCATOR SQL_MULTISET SQL_MULTISET_LOCATOR SQL_TYPE_DATE | ||
| 302 | SQL_TYPE_TIME SQL_TYPE_TIMESTAMP SQL_TYPE_TIME_WITH_TIMEZONE SQL_TYPE_TIMESTAMP_WITH_TIMEZONE | ||
| 303 | SQL_INTERVAL_YEAR SQL_INTERVAL_MONTH SQL_INTERVAL_DAY SQL_INTERVAL_HOUR SQL_INTERVAL_MINUTE | ||
| 304 | SQL_INTERVAL_SECOND SQL_INTERVAL_YEAR_TO_MONTH SQL_INTERVAL_DAY_TO_HOUR | ||
| 305 | SQL_INTERVAL_DAY_TO_MINUTE SQL_INTERVAL_DAY_TO_SECOND SQL_INTERVAL_HOUR_TO_MINUTE | ||
| 306 | SQL_INTERVAL_HOUR_TO_SECOND SQL_INTERVAL_MINUTE_TO_SECOND | ||
| 307 | ); | ||
| 308 | foreach my $c ( @$keys ) | ||
| 309 | { | ||
| 310 | next if( scalar( grep( /^$c$/, @constants_to_ignore ) ) ); | ||
| 311 | if( $c =~ /^SQL_(\w+)$/ ) | ||
| 312 | { | ||
| 313 | my $type = lc( $1 ); | ||
| 314 | my $code = \&{"DBI::$c"}; | ||
| 315 | my $val = $code->(); | ||
| 316 | if( !CORE::exists( $DATATYPES_DICT->{ $type } ) ) | ||
| 317 | { | ||
| 318 | warn( "Unknown MySQL constant DBI::${c}" ) if( DB::Object::Mysql->_is_warnings_enabled( 'DB::Object' ) ); | ||
| 319 | next; | ||
| 320 | } | ||
| 321 | $DATATYPES_DICT->{ $type }->{constant} = $val; | ||
| 322 | } | ||
| 323 | } | ||
| 324 | |||
| 325 | sub init | ||
| 326 | { | ||
| 327 | my $self = shift( @_ ); | ||
| 328 | $self->{_exception_class} = $EXCEPTION_CLASS; | ||
| 329 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 330 | $self->{driver} = 'mysql'; | ||
| 331 | return( $self ); | ||
| 332 | } | ||
| 333 | # End of generic routines | ||
| 334 | |||
| 335 | # ROUTINES PROPRIETAIRE | ||
| 336 | sub attribute($;$@) | ||
| 337 | { | ||
| 338 | my $self = shift( @_ ); | ||
| 339 | # $h->{AttributeName} = ...; # set/write | ||
| 340 | # ... = $h->{AttributeName}; # get/read | ||
| 341 | # 1 means that the attribute may be modified | ||
| 342 | # 0 mneas that the attribute may only be read | ||
| 343 | my $name = shift( @_ ) if( @_ == 1 ); | ||
| 344 | my %arg = ( @_ ); | ||
| 345 | my %attr = | ||
| 346 | ( | ||
| 347 | Warn => 1, | ||
| 348 | Active => 0, | ||
| 349 | Kids => 0, | ||
| 350 | ActiveKids => 0, | ||
| 351 | CachedKids => 0, | ||
| 352 | InactiveDestroy => 1, | ||
| 353 | PrintError => 1, | ||
| 354 | RaiseError => 1, | ||
| 355 | ChopBlanks => 1, | ||
| 356 | LongReadLen => 1, | ||
| 357 | LongTruncOk => 1, | ||
| 358 | AutoCommit => 1, | ||
| 359 | Name => 0, | ||
| 360 | RowCacheSize => 0, | ||
| 361 | NUM_OF_FIELDS => 0, | ||
| 362 | NUM_OF_PARAMS => 0, | ||
| 363 | NAME => 0, | ||
| 364 | TYPE => 0, | ||
| 365 | PRECISION => 0, | ||
| 366 | SCALE => 0, | ||
| 367 | NULLABLE => 0, | ||
| 368 | CursorName => 0, | ||
| 369 | Statement => 0, | ||
| 370 | RowsInCache => 0, | ||
| 371 | ); | ||
| 372 | # Only those attribute exist | ||
| 373 | # Using an a non existing attribute produce an exception, so we better avoid | ||
| 374 | if( $name ) | ||
| 375 | { | ||
| 376 | return( $self->{dbh}->{ $name } ) if( exists( $attr{ $name } ) ); | ||
| 377 | } | ||
| 378 | else | ||
| 379 | { | ||
| 380 | my $value; | ||
| 381 | while( ( $name, $value ) = each( %arg ) ) | ||
| 382 | { | ||
| 383 | # We intend to modifiy the value of an attribute | ||
| 384 | # we are allowed to modify this value if it is true | ||
| 385 | if( exists( $attr{ $name } ) && | ||
| 386 | defined( $value ) && | ||
| 387 | $attr{ $name } ) | ||
| 388 | { | ||
| 389 | $self->{dbh}->{ $name } = $value; | ||
| 390 | } | ||
| 391 | } | ||
| 392 | } | ||
| 393 | } | ||
| 394 | |||
| 395 | sub begin_work($;$@) | ||
| 396 | { | ||
| 397 | my $self = shift( @_ ); | ||
| 398 | $self->{transaction} = 1; | ||
| 399 | $self->{AutoCommit_previous} = $self->{dbh}->{AutoCommit}; | ||
| 400 | $self->{dbh}->{AutoCommit} = 0; | ||
| 401 | return( $self ); | ||
| 402 | } | ||
| 403 | |||
| 404 | sub commit($;$@) | ||
| 405 | { | ||
| 406 | my $self = shift( @_ ); | ||
| 407 | $self->{transaction} = 0; | ||
| 408 | $self->{dbh}->commit( @_ ); | ||
| 409 | $self->{dbh}->{AutoCommit} = $self->{AutoCommit_previous} if( length( $self->{AutoCommit_previous} ) ); | ||
| 410 | return( $self ); | ||
| 411 | } | ||
| 412 | |||
| 413 | # NOTE: sub connect is inherited | ||
| 414 | |||
| 415 | # NOTE: sub constant_to_datatype is inherited | ||
| 416 | |||
| 417 | sub create_db | ||
| 418 | { | ||
| 419 | my $self = shift( @_ ); | ||
| 420 | my $name = shift( @_ ) || return( $self->error( "No database name to create was provided." ) ); | ||
| 421 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 422 | my $params = []; | ||
| 423 | # https://dev.mysql.com/doc/refman/5.6/en/create-database.html | ||
| 424 | push( @$params, sprintf( 'CHARACTER SET = %s', $opts->{charset} ) ) if( $opts->{charset} ); | ||
| 425 | push( @$params, sprintf( 'COLLATE = %s', $opts->{collate} ) ) if( $opts->{collate} ); | ||
| 426 | my $sql = "CREATE DATABASE " . ( $opts->{if_not_exists} ? 'IF NOT EXISTS ' : '' ) . $name; | ||
| 427 | if( scalar( @$params ) ) | ||
| 428 | { | ||
| 429 | $sql .= ' ' . join( ' ', @$params ); | ||
| 430 | } | ||
| 431 | my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) ); | ||
| 432 | my( $sth, $rc ); | ||
| 433 | # try-catch | ||
| 434 | local $@; | ||
| 435 | $sth = eval | ||
| 436 | { | ||
| 437 | $dbh->prepare( $sql ); | ||
| 438 | }; | ||
| 439 | if( $@ ) | ||
| 440 | { | ||
| 441 | return( $self->error( "An error occured while prepareing SQL query to create database: ", $@ ) ); | ||
| 442 | } | ||
| 443 | $sth or return( $self->error( "An error occured while prepareing SQL query to create database: ", $dbh->errstr ) ); | ||
| 444 | |||
| 445 | $rc = eval | ||
| 446 | { | ||
| 447 | $dbh->prepare( $sql ); | ||
| 448 | }; | ||
| 449 | if( $@ ) | ||
| 450 | { | ||
| 451 | return( $self->error( "An error occured while prepareing SQL query to create database: ", $@ ) ); | ||
| 452 | } | ||
| 453 | $rc or return( $self->error( "An error occured while prepareing SQL query to create database: ", $dbh->errstr ) ); | ||
| 454 | |||
| 455 | # try-catch | ||
| 456 | eval | ||
| 457 | { | ||
| 458 | $sth->finish; | ||
| 459 | }; | ||
| 460 | if( $@ ) | ||
| 461 | { | ||
| 462 | return( $self->error( "An unexpected error occurred while trying to finish the SQL query to create database: ", $@, "\n$sql" ) ); | ||
| 463 | } | ||
| 464 | |||
| 465 | my $ref = {}; | ||
| 466 | my @keys = qw( host port login passwd opt debug ); | ||
| 467 | @$ref{ @keys } = @$self{ @keys }; | ||
| 468 | $ref->{database} = $name; | ||
| 469 | $dbh = $self->connect( $ref ) || return( $self->error( "I could create the database \"$name\" but oddly enough, I could not connect to it with user \"$ref->{login}\" on host \"$ref->{host}\" with port \"$ref->{port}\"." ) ); | ||
| 470 | return( $dbh ); | ||
| 471 | } | ||
| 472 | |||
| 473 | sub databases | ||
| 474 | { | ||
| 475 | my $self = shift( @_ ); | ||
| 476 | # return( $self->error( "Not connected to PostgreSQL server yet. Issue $dbh->connect first." ) ) if( !$self->{ 'dbh' } ); | ||
| 477 | my $dbh; | ||
| 478 | # If there is no connection yet, then create one using the postgres login. | ||
| 479 | # There should not be a live user and database just to check what databases there are. | ||
| 480 | if( !$self->{dbh} ) | ||
| 481 | { | ||
| 482 | my $con = | ||
| 483 | { | ||
| 484 | 'database' => 'mysql', | ||
| 485 | }; | ||
| 486 | $con->{mysql_read_default_file} = '/etc/my.cnf' if( -f( '/etc/my.cnf' ) ); | ||
| 487 | if( CORE::exists( $ENV{ 'DB_MYSQL_CON' } ) ) | ||
| 488 | { | ||
| 489 | @$con{ qw( host login passwd ) } = split( /;/, $ENV{ 'DB_MYSQL_CON' } ); | ||
| 490 | } | ||
| 491 | else | ||
| 492 | { | ||
| 493 | @$con{ qw( host login passwd ) } = @_; | ||
| 494 | } | ||
| 495 | |||
| 496 | # try-catch | ||
| 497 | local $@; | ||
| 498 | $dbh = eval | ||
| 499 | { | ||
| 500 | $self->connect( $con ); | ||
| 501 | }; | ||
| 502 | if( $@ ) | ||
| 503 | { | ||
| 504 | return( $self->error( "Error trying to connect to the MySQL server: $@" ) ); | ||
| 505 | } | ||
| 506 | $dbh or return( $self->pass_error ); | ||
| 507 | } | ||
| 508 | else | ||
| 509 | { | ||
| 510 | $dbh = $self; | ||
| 511 | } | ||
| 512 | my $temp = $dbh->do( "SHOW DATABASES" )->fetchall_arrayref; | ||
| 513 | my @dbases = map( $_->[0], @$temp ); | ||
| 514 | return( @dbases ); | ||
| 515 | } | ||
| 516 | |||
| 517 | # NOTE: sub datatype_dict is inherited | ||
| 518 | |||
| 519 | # NOTE: sub datatype_to_constant is inherited | ||
| 520 | |||
| 521 | # NOTE: sub datatypes is in inherited | ||
| 522 | |||
| 523 | sub get_sql_type | ||
| 524 | { | ||
| 525 | my $self = shift( @_ ); | ||
| 526 | my $type = shift( @_ ) || return( $self->error( "No sql type was provided to get its constant." ) ); | ||
| 527 | $type = lc( $type ); | ||
| 528 | if( CORE::exists( $DATATYPES_DICT->{ $type } ) && | ||
| 529 | $type ne $DATATYPES_DICT->{ $type }->{type} ) | ||
| 530 | { | ||
| 531 | $type = $DATATYPES_DICT->{ $type }->{type}; | ||
| 532 | } | ||
| 533 | my $const; | ||
| 534 | if( substr( $type, 0, 4 ) eq 'sql_' ) | ||
| 535 | { | ||
| 536 | $const = $self->{dbh}->can( "DBI::\U${type}\E" ); | ||
| 537 | } | ||
| 538 | else | ||
| 539 | { | ||
| 540 | $const = $self->{dbh}->can( "DBI::SQL_\U${type}\E" ); | ||
| 541 | } | ||
| 542 | return( '' ) if( !defined( $const ) ); | ||
| 543 | return( $const->() ); | ||
| 544 | } | ||
| 545 | |||
| 546 | # Specific to Mysql (Postgres also uses it) | ||
| 547 | sub having | ||
| 548 | { | ||
| 549 | my $self = shift( @_ ); | ||
| 550 | my $q = $self->_reset_query; | ||
| 551 | return( $q->having( @_ ) ); | ||
| 552 | } | ||
| 553 | |||
| 554 | sub last_insert_id | ||
| 555 | { | ||
| 556 | my $self = shift( @_ ); | ||
| 557 | my $query = $self->{dbh}->prepare( "SELECT LAST_INSERT_ID()" ) || return; | ||
| 558 | $query->execute(); | ||
| 559 | my $val = $query->fetchrow(); | ||
| 560 | $query->finish(); | ||
| 561 | return( $val ); | ||
| 562 | } | ||
| 563 | |||
| 564 | sub lock | ||
| 565 | { | ||
| 566 | my $self = shift( @_ ); | ||
| 567 | my $str = shift( @_ ); | ||
| 568 | my $timeout = shift( @_ ) || 2; | ||
| 569 | # my $res = $self->select( "GET_LOCK( '$str', $timeout )" )->fetchrow(); | ||
| 570 | my $sth = $self->{dbh}->prepare( "SELECT GET_LOCK( '$str', $timeout )" ) || | ||
| 571 | return( $self->error( "Error while preparing query to get lock '$str': ", $self->{dbh}->errstr() ) ); | ||
| 572 | $sth->execute() || | ||
| 573 | return( $self->error( "Error while executing query to get lock '$str': ", $sth->errstr() ) ); | ||
| 574 | my $res = $sth->fetchrow; | ||
| 575 | $sth->finish(); | ||
| 576 | $self->{ '_locks' } ||= []; | ||
| 577 | push( @{ $self->{_locks} }, $str ) if( $res && $res ne 'NULL' ); | ||
| 578 | return( $res eq 'NULL' ? undef() : $res ); | ||
| 579 | } | ||
| 580 | |||
| 581 | sub query_object { return( shift->_set_get_object( 'query_object', 'DB::Object::Mysql::Query', @_ ) ); } | ||
| 582 | |||
| 583 | sub rollback | ||
| 584 | { | ||
| 585 | my $self = shift( @_ ); | ||
| 586 | $self->{transaction} = 0; | ||
| 587 | $self->{dbh}->{AutoCommit} = $self->{AutoCommit_previous} if( length( $self->{AutoCommit_previous} ) ); | ||
| 588 | return( $self->{dbh}->rollback() ); | ||
| 589 | } | ||
| 590 | |||
| 591 | sub stat | ||
| 592 | { | ||
| 593 | my $self = shift( @_ ); | ||
| 594 | my $type = lc( shift( @_ ) ); | ||
| 595 | my $sth = $self->{dbh}->prepare( "SHOW STATUS" ); | ||
| 596 | $sth->execute(); | ||
| 597 | my @data = (); | ||
| 598 | my $ref = {}; | ||
| 599 | while( @data = $sth->fetchrow() ) | ||
| 600 | { | ||
| 601 | $ref->{ lc( $data[ 0 ] ) } = $data[ 1 ]; | ||
| 602 | } | ||
| 603 | $sth->finish(); | ||
| 604 | if( $type ) | ||
| 605 | { | ||
| 606 | return( exists( $ref->{ $type } ) ? $ref->{ $type } : undef() ); | ||
| 607 | } | ||
| 608 | else | ||
| 609 | { | ||
| 610 | return( wantarray() ? () : undef() ) if( !%$ref ); | ||
| 611 | return( wantarray() ? %$ref : $ref ); | ||
| 612 | } | ||
| 613 | } | ||
| 614 | |||
| 615 | # sub table_exists inherited from DB::Object | ||
| 616 | |||
| 617 | sub table_info | ||
| 618 | { | ||
| 619 | my $self = shift( @_ ); | ||
| 620 | my $table = shift( @_ ) || | ||
| 621 | return( $self->error( "You must provide a table name to access the table methods." ) ); | ||
| 622 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 623 | my $db = $opts->{database} || $self->database; | ||
| 624 | # my $sth = $self->{dbh}->table_info( undef(), undef(), $table ); | ||
| 625 | my $sql = <<EOT; | ||
| 626 | SELECT | ||
| 627 | table_schema AS "schema" | ||
| 628 | ,table_name AS "name" | ||
| 629 | ,table_type AS "type" | ||
| 630 | FROM information_schema.tables | ||
| 631 | WHERE table_name=? | ||
| 632 | EOT | ||
| 633 | my $dbh = $self->{dbh} || return( $self->error( "Could not find database handler." ) ); | ||
| 634 | my $sth = $dbh->prepare_cached( $sql ) || return( $self->error( "An error occured while preparing query to check if table \"$table\" exists in our database: ", $dbh->errstr ) ); | ||
| 635 | $sth->execute( $table ) || return( $self->error( "An error occured while executing query to check if table \"$table\" exists in our database: ", $sth->errstr ) ); | ||
| 636 | my $all = $sth->fetchall_arrayref( {} ); | ||
| 637 | $sth->finish; | ||
| 638 | return( [] ) if( !scalar( @$all ) ); | ||
| 639 | foreach my $ref ( @$all ) | ||
| 640 | { | ||
| 641 | # example: BASE TABLE | ||
| 642 | if( $ref->{type} =~ /^(?:(?:\S+)[[:blank:]]+)?(.*?)$/ ) | ||
| 643 | { | ||
| 644 | $ref->{type} = $2; | ||
| 645 | } | ||
| 646 | } | ||
| 647 | return( $all ) if( $opts->{anywhere} ); | ||
| 648 | foreach my $ref ( @$all ) | ||
| 649 | { | ||
| 650 | if( $ref->{schema} eq $db ) | ||
| 651 | { | ||
| 652 | return( $ref ); | ||
| 653 | } | ||
| 654 | } | ||
| 655 | return( [] ); | ||
| 656 | } | ||
| 657 | |||
| 658 | sub tables | ||
| 659 | { | ||
| 660 | my $self = shift( @_ ); | ||
| 661 | my $db = shift( @_ ) || $self->{ 'database' }; | ||
| 662 | my $sth = $self->{dbh}->prepare( "SHOW TABLES FROM $db" ) || | ||
| 663 | return( $self->error( "Error while preparing query to get all tables from database '$db'." ) ); | ||
| 664 | $sth->execute(); | ||
| 665 | my $ref = $sth->fetchall_arrayref(); | ||
| 666 | $sth->finish; | ||
| 667 | my @tables = map{ $_->[0] } @$ref; | ||
| 668 | return( \@tables ); | ||
| 669 | } | ||
| 670 | |||
| 671 | sub tables_info | ||
| 672 | { | ||
| 673 | my $self = shift( @_ ); | ||
| 674 | my $db = shift( @_ ) || $self->{database}; | ||
| 675 | my @tables = (); | ||
| 676 | my $query = <<EOT; | ||
| 677 | SELECT | ||
| 678 | * | ||
| 679 | ,table_schema AS "database" | ||
| 680 | ,table_name AS "name" | ||
| 681 | ,table_type AS "type" | ||
| 682 | FROM information_schema.tables | ||
| 683 | WHERE table_schema=? | ||
| 684 | EOT | ||
| 685 | my $sth = $self->{dbh}->prepare_cached( $query ) || return( $self->error( sprintf( "Error while preparing query $query: %s", $self->{dbh}->errstr ) ) ); | ||
| 686 | $sth->execute( $db ) || return( $self->error( sprintf( "Error while executing query $query: %s", $sth->errstr ) ) ); | ||
| 687 | my $all = $sth->fetchall_arrayref( {} ); | ||
| 688 | return( $all ); | ||
| 689 | } | ||
| 690 | |||
| 691 | sub unlock | ||
| 692 | { | ||
| 693 | my $self = shift( @_ ); | ||
| 694 | my $str = shift( @_ ) || | ||
| 695 | return( $self->error( "No lock string identifier provided." ) ); | ||
| 696 | # my $res = $self->select( "RELEASE_LOCK( '$str' )" )->fetchrow(); | ||
| 697 | my $sth = $self->{dbh}->prepare( "SELECT RELEASE_LOCK( '$str' )" ) || | ||
| 698 | return( $self->error( "Error while preparing query to release lock '$str': ", $self->errstr() ) ); | ||
| 699 | $sth->execute() || | ||
| 700 | return( $self->error( "Error while executing query to release lock '$str': ", $sth->errstr() ) ); | ||
| 701 | my $res = $sth->fetchrow; | ||
| 702 | $sth->finish(); | ||
| 703 | # Take out the lock from the saved locks pile (used by DESTROY) | ||
| 704 | my $locks = $self->{ '_locks' } ||= []; | ||
| 705 | my @new = grep{ !/$str/ } @$locks; | ||
| 706 | $locks = [ @new ]; | ||
| 707 | return( $res eq 'NULL' ? undef() : $res ); | ||
| 708 | } | ||
| 709 | |||
| 710 | sub variables | ||
| 711 | { | ||
| 712 | my $self = shift( @_ ); | ||
| 713 | my $type = shift( @_ ); | ||
| 714 | $self->error( "Variable '$type' is a read-only value." ) if( @_ ); | ||
| 715 | my $vars = $self->{ 'variables' } ||= {}; | ||
| 716 | if( !%$vars ) | ||
| 717 | { | ||
| 718 | my $sth = $self->{dbh}->prepare( "SHOW VARIABLES" ) || | ||
| 719 | return( $self->error( "SHOW VARIABLES is not supported." ) ); | ||
| 720 | $sth->execute(); | ||
| 721 | my $ref = $self->fetchall_arrayref(); | ||
| 722 | my %vars = map{ lc( $_->[ 0 ] ) => $_->[ 1 ] } @$ref; | ||
| 723 | $vars = \%vars if( %vars ); | ||
| 724 | $sth->finish(); | ||
| 725 | } | ||
| 726 | my @found = grep{ /$type/i } keys( %$vars ); | ||
| 727 | return( '' ) if( !scalar( @found ) ); | ||
| 728 | return( $vars->{ $found[ 0 ] } ); | ||
| 729 | } | ||
| 730 | |||
| 731 | # https://dev.mysql.com/doc/refman/8.0/en/show-variables.html | ||
| 732 | sub version | ||
| 733 | { | ||
| 734 | my $self = shift( @_ ); | ||
| 735 | # If we already have the information, let's use our cache instead of making a query | ||
| 736 | return( $self->{_db_version} ) if( length( $self->{_db_version} ) ); | ||
| 737 | my $sql = 'SELECT @@innodb_version'; | ||
| 738 | my $sth = $self->do( $sql ) || return( $self->error( "Unable to issue the sql statement '$sql' to get the server version: ", $self->errstr ) ); | ||
| 739 | my $ver = $sth->fetchrow; | ||
| 740 | $sth->finish; | ||
| 741 | # We cache it | ||
| 742 | $self->{_db_version} = version->parse( $ver ); | ||
| 743 | return( $ver ); | ||
| 744 | } | ||
| 745 | |||
| 746 | sub _connection_options | ||
| 747 | { | ||
| 748 | my $self = shift( @_ ); | ||
| 749 | my $param = shift( @_ ); | ||
| 750 | # This should really not be an option. This decode utf8 in database | ||
| 751 | $param->{mysql_enable_utf8} = 1 if( !CORE::exists( $param->{mysql_enable_utf8} ) ); | ||
| 752 | my @mysql_params = grep( /^mysql_/, keys( %$param ) ); | ||
| 753 | my $opt = $self->SUPER::_connection_options( $param ); | ||
| 754 | @$opt{ @mysql_params } = @$param{ @mysql_params }; | ||
| 755 | return( $opt ); | ||
| 756 | } | ||
| 757 | |||
| 758 | # NOTE: sub _connection_params2hash_driver is not necessary here. We use our parent's one. | ||
| 759 | |||
| 760 | sub _connection_parameters | ||
| 761 | { | ||
| 762 | my $self = shift( @_ ); | ||
| 763 | my $param = shift( @_ ); | ||
| 764 | my $core = [qw( db login passwd host port driver database server opt uri debug cache_connections cache_dir cache_query cache_table unknown_field use_cache )]; | ||
| 765 | my @mysql_params = grep( /^mysql_/, keys( %$param ) ); | ||
| 766 | # See DBD::mysql for the list of valid parameters | ||
| 767 | # E.g.: mysql_client_found_rows, mysql_compression mysql_connect_timeout mysql_write_timeout mysql_read_timeout mysql_init_command mysql_skip_secure_auth mysql_read_default_file mysql_read_default_group mysql_socket mysql_ssl mysql_ssl_client_key mysql_ssl_client_cert mysql_ssl_ca_file mysql_ssl_ca_path mysql_ssl_cipher mysql_local_infile mysql_multi_statements mysql_server_prepare mysql_server_prepare_disable_fallback mysql_embedded_options mysql_embedded_groups mysql_conn_attrs | ||
| 768 | push( @$core, @mysql_params ); | ||
| 769 | return( $core ); | ||
| 770 | } | ||
| 771 | |||
| 772 | sub _dsn | ||
| 773 | { | ||
| 774 | my $self = shift( @_ ); | ||
| 775 | # "DBI:mysql:database=$sql_db;host=$sql_host;port=$sql_port;mysql_read_default_file=/etc/my.cnf" | ||
| 776 | my @params = ( sprintf( 'dbi:%s:database=%s', @$self{ qw( driver database ) } ) ); | ||
| 777 | if( $self->{host} ) | ||
| 778 | { | ||
| 779 | $self->_load_class( 'Net::IP' ) || return( $self->pass_error ); | ||
| 780 | my $ip = Net::IP->new( $self->{host} ); | ||
| 781 | if( $ip ) | ||
| 782 | { | ||
| 783 | if( $ip->version == 6 ) | ||
| 784 | { | ||
| 785 | push( @params, sprintf( 'host=[%s]', $ip->ip ) ); | ||
| 786 | } | ||
| 787 | else | ||
| 788 | { | ||
| 789 | push( @params, sprintf( 'host=%s', $ip->ip ) ); | ||
| 790 | } | ||
| 791 | } | ||
| 792 | else | ||
| 793 | { | ||
| 794 | push( @params, sprintf( 'host=%s', $self->{host} ) ); | ||
| 795 | } | ||
| 796 | } | ||
| 797 | # my @params = sprintf( "dbi:%s:database=%s;host=%s", @$self{ qw( driver database server ) } ); | ||
| 798 | push( @params, sprintf( 'port=%d', $self->{port} ) ) if( $self->{port} ); | ||
| 799 | push( @params, sprintf( 'mysql_read_default_file=%s', $self->{mysql_read_default_file} ) ) if( $self->{mysql_read_default_file} ); | ||
| 800 | return( join( ';', @params ) ); | ||
| 801 | } | ||
| 802 | |||
| 803 | sub _placeholder_regexp { return( $PLACEHOLDER_REGEXP ) } | ||
| 804 | |||
| 805 | # NOTE: DESTROY | ||
| 806 | DESTROY | ||
| 807 | { | ||
| 808 | # <https://perldoc.perl.org/perlobj#Destructors> | ||
| 809 | CORE::local( $., $@, $!, $^E, $? ); | ||
| 810 | CORE::return if( ${^GLOBAL_PHASE} eq 'DESTRUCT' ); | ||
| 811 | my $self = CORE::shift( @_ ); | ||
| 812 | CORE::return if( !CORE::defined( $self ) ); | ||
| 813 | my $class = ref( $self ) || $self; | ||
| 814 | if( $self->{sth} ) | ||
| 815 | { | ||
| 816 | print( STDERR "DESTROY(): Terminating sth '$self' for query:\n$self->{query}\n" ) if( $DEBUG || $self->{debug} ); | ||
| 817 | $self->{sth}->finish(); | ||
| 818 | } | ||
| 819 | elsif( $self->{dbh} && $class eq 'DB::Object' ) | ||
| 820 | { | ||
| 821 | local( $SIG{__WARN__} ) = sub { }; | ||
| 822 | # $self->{ 'dbh' }->disconnect(); | ||
| 823 | if( $DEBUG || $self->{debug} ) | ||
| 824 | { | ||
| 825 | my( $pack, $file, $line, $sub ) = ( caller(0) )[0, 1, 2, 3]; | ||
| 826 | my( $pack2, $file2, $line2, $sub2 ) = ( caller(1) ) [0, 1, 2, 3]; | ||
| 827 | print( STDERR "DESTROY database handle ($self) [$self->{query}]\ncalled within sub '$sub' ($sub2) from package '$pack' ($pack2) in file '$file' ($file2) at line '$line' ($line2).\n" ); | ||
| 828 | } | ||
| 829 | $self->disconnect(); | ||
| 830 | } | ||
| 831 | my $locks = $self->{_locks}; | ||
| 832 | if( $locks && $self->_is_array( $locks ) ) | ||
| 833 | { | ||
| 834 | foreach my $name ( @$locks ) | ||
| 835 | { | ||
| 836 | $self->unlock( $name ); | ||
| 837 | } | ||
| 838 | } | ||
| 839 | } | ||
| 840 | |||
| 841 | 1; | ||
| 842 | # NOTE: POD | ||
| 843 | __END__ | ||
| 844 | |||
| 845 | =encoding utf8 | ||
| 846 | |||
| 847 | =head1 NAME | ||
| 848 | |||
| 849 | DB::Object::Mysql - Mysql Database Object | ||
| 850 | |||
| 851 | =head1 SYNOPSIS | ||
| 852 | |||
| 853 | use DB::Object; | ||
| 854 | |||
| 855 | my $dbh = DB::Object->connect({ | ||
| 856 | driver => 'mysql', | ||
| 857 | conf_file => 'db-settings.json', | ||
| 858 | database => 'webstore', | ||
| 859 | host => 'localhost', | ||
| 860 | login => 'store-admin', | ||
| 861 | debug => 3, | ||
| 862 | }) || bailout( "Unable to connect to Mysql server on host localhost: ", DB::Object->error ); | ||
| 863 | |||
| 864 | # Legacy regular query | ||
| 865 | my $sth = $dbh->prepare( "SELECT login,name FROM login WHERE login='jack'" ) || | ||
| 866 | die( $dbh->errstr() ); | ||
| 867 | $sth->execute() || die( $sth->errstr() ); | ||
| 868 | my $ref = $sth->fetchrow_hashref(); | ||
| 869 | $sth->finish(); | ||
| 870 | |||
| 871 | # Get a list of databases; | ||
| 872 | my @databases = $dbh->databases; | ||
| 873 | # Doesn't exist? Create it: | ||
| 874 | my $dbh2 = $dbh->create_db( 'webstore' ); | ||
| 875 | # Load some sql into it | ||
| 876 | my $rv = $dbh2->do( $sql ) || die( $dbh->error ); | ||
| 877 | |||
| 878 | # Check a table exists | ||
| 879 | $dbh->table_exists( 'customers' ) || die( "Cannot find the customers table!\n" ); | ||
| 880 | |||
| 881 | # Get list of tables, as array reference: | ||
| 882 | my $tables = $dbh->tables; | ||
| 883 | |||
| 884 | my $cust = $dbh->customers || die( "Cannot get customers object." ); | ||
| 885 | $cust->where( email => 'john@example.org' ); | ||
| 886 | my $str = $cust->delete->as_string; | ||
| 887 | # Becomes: DELETE FROM customers WHERE email='john\@example.org' | ||
| 888 | |||
| 889 | # Do some insert with transaction | ||
| 890 | $dbh->begin_work; | ||
| 891 | # Making some other inserts and updates here... | ||
| 892 | my $cust_sth_ins = $cust->insert( | ||
| 893 | first_name => 'Paul', | ||
| 894 | last_name => 'Goldman', | ||
| 895 | email => 'paul@example.org', | ||
| 896 | active => 0, | ||
| 897 | ) || do | ||
| 898 | { | ||
| 899 | # Rollback everything since the begin_work | ||
| 900 | $dbh->rollback; | ||
| 901 | die( "Error while create query to add data to table customers: " . $cust->error ); | ||
| 902 | }; | ||
| 903 | $result = $cust_sth_ins->as_string; | ||
| 904 | # INSERT INTO customers (first_name, last_name, email, active) VALUES('Paul', 'Goldman', 'paul\@example.org', '0') | ||
| 905 | $dbh->commit; | ||
| 906 | # Get the last used insert id | ||
| 907 | my $id = $dbh->last_insert_id(); | ||
| 908 | |||
| 909 | $cust->where( email => 'john@example.org' ); | ||
| 910 | $cust->order( 'last_name' ); | ||
| 911 | $cust->having( email => qr/\@example/ ); | ||
| 912 | $cust->limit( 10 ); | ||
| 913 | my $cust_sth_sel = $cust->select || die( "An error occurred while creating a query to select data frm table customers: " . $cust->error ); | ||
| 914 | # Becomes: | ||
| 915 | # SELECT id, first_name, last_name, email, created, modified, active, created::ABSTIME::INTEGER AS created_unixtime, modified::ABSTIME::INTEGER AS modified_unixtime, CONCAT(first_name, ' ', last_name) AS name FROM customers WHERE email='john\@example.org' HAVING email ~ '\@example' ORDER BY last_name LIMIT 10 | ||
| 916 | |||
| 917 | $cust->reset; | ||
| 918 | $cust->where( email => 'john@example.org' ); | ||
| 919 | my $cust_sth_upd = $cust->update( active => 0 ) | ||
| 920 | # Would become: | ||
| 921 | # UPDATE ONLY customers SET active='0' WHERE email='john\@example.org' | ||
| 922 | |||
| 923 | # Lets' dump the result of our query | ||
| 924 | # First to STDERR | ||
| 925 | $login->where( "login='jack'" ); | ||
| 926 | $login->select->dump(); | ||
| 927 | # Now dump the result to a file | ||
| 928 | $login->select->dump( "my_file.txt" ); | ||
| 929 | |||
| 930 | =head1 VERSION | ||
| 931 | |||
| 932 | v1.5.1 | ||
| 933 | |||
| 934 | =head1 DESCRIPTION | ||
| 935 | |||
| 936 | This package inherits from L<DB::Object>, so any method not mentioned here, but listed there, you can use. | ||
| 937 | |||
| 938 | L<DB::Object> is a SQL API much alike C<DBI>. | ||
| 939 | So why use a private module instead of using that great C<DBI> package? | ||
| 940 | |||
| 941 | At first, I started to inherit from C<DBI> to conform to C<perlmod> perl manual page and to general perl coding guidlines. It became very quickly a real hassle. Barely impossible to inherit, difficulty to handle error, too much dependent from an API that change its behaviour with new versions. | ||
| 942 | In short, I wanted a better, more accurate control over the SQL connection. | ||
| 943 | |||
| 944 | So, L<DB::Object> acts as a convenient, modifiable wrapper that provide the programmer with an intuitive, user-friendly and hassle free interface. | ||
| 945 | |||
| 946 | =head1 CONSTRUCTOR | ||
| 947 | |||
| 948 | =head2 new | ||
| 949 | |||
| 950 | Create a new instance of L<DB::Object>. Nothing much to say. | ||
| 951 | |||
| 952 | =head2 connect | ||
| 953 | |||
| 954 | Create a new instance of L<DB::Object>, but also attempts a connection to SQL server. | ||
| 955 | |||
| 956 | You can specify the following arguments: | ||
| 957 | |||
| 958 | =over 4 | ||
| 959 | |||
| 960 | =item * C<database> | ||
| 961 | |||
| 962 | The database name you wish to connect to | ||
| 963 | |||
| 964 | =item * C<login> | ||
| 965 | |||
| 966 | The login used to access that database | ||
| 967 | |||
| 968 | =item * C<password> | ||
| 969 | |||
| 970 | The password that goes along | ||
| 971 | |||
| 972 | =item * C<server> | ||
| 973 | |||
| 974 | The server, that is hostname of the machine serving a SQL server. | ||
| 975 | |||
| 976 | =item * C<driver> | ||
| 977 | |||
| 978 | The driver you want to use. It needs to be of the same type than the server you want to connect to. If you are connecting to a MySQL server, you would use C<mysql>, if you would connecto to an Oracle server, you would use C<oracle>. | ||
| 979 | |||
| 980 | You need to make sure that those driver are properly installed in the system before attempting to connect. | ||
| 981 | |||
| 982 | To install the required driver, you could start with the command line: | ||
| 983 | |||
| 984 | perl -MCPAN -e shell | ||
| 985 | |||
| 986 | which will provide you a special shell to install modules in a convenient way. | ||
| 987 | |||
| 988 | =back | ||
| 989 | |||
| 990 | =head1 METHODS | ||
| 991 | |||
| 992 | =head2 alias | ||
| 993 | |||
| 994 | Get/set alias for table fields in SELECT queries. The hash provided thus contain a list of field => alias pairs. | ||
| 995 | |||
| 996 | =head2 as_string | ||
| 997 | |||
| 998 | Return the sql query as a string. | ||
| 999 | |||
| 1000 | =head2 avoid | ||
| 1001 | |||
| 1002 | Set the provided list of table fields to avoid when returning the query result. | ||
| 1003 | The list of fields can be provided either as an array of a reference to an array. | ||
| 1004 | |||
| 1005 | =head2 attribute | ||
| 1006 | |||
| 1007 | Sets or get the value of database connection parameters. | ||
| 1008 | |||
| 1009 | If only one argument is provided, returns its value. | ||
| 1010 | If multiple arguments in a form of pair => value are provided, it sets the corresponding database parameters. | ||
| 1011 | |||
| 1012 | The authorised parameters are: | ||
| 1013 | |||
| 1014 | =over 4 | ||
| 1015 | |||
| 1016 | =item * C<Warn> | ||
| 1017 | |||
| 1018 | Can be overridden. | ||
| 1019 | |||
| 1020 | =item * C<Active> | ||
| 1021 | |||
| 1022 | Read-only. | ||
| 1023 | |||
| 1024 | =item * C<Kids> | ||
| 1025 | |||
| 1026 | Read-only. | ||
| 1027 | |||
| 1028 | =item * C<ActiveKids> | ||
| 1029 | |||
| 1030 | Read-only. | ||
| 1031 | |||
| 1032 | =item * C<CachedKids> | ||
| 1033 | |||
| 1034 | Read-only. | ||
| 1035 | |||
| 1036 | =item * C<InactiveDestroy> | ||
| 1037 | |||
| 1038 | Can be overridden. | ||
| 1039 | |||
| 1040 | =item * C<PrintError> | ||
| 1041 | |||
| 1042 | Can be overridden. | ||
| 1043 | |||
| 1044 | =item * C<RaiseError> | ||
| 1045 | |||
| 1046 | Can be overridden. | ||
| 1047 | |||
| 1048 | =item * C<ChopBlanks> | ||
| 1049 | |||
| 1050 | Can be overridden. | ||
| 1051 | |||
| 1052 | =item * C<LongReadLen> | ||
| 1053 | |||
| 1054 | Can be overridden. | ||
| 1055 | |||
| 1056 | =item * C<LongTruncOk> | ||
| 1057 | |||
| 1058 | Can be overridden. | ||
| 1059 | |||
| 1060 | =item * C<AutoCommit> | ||
| 1061 | |||
| 1062 | Can be overridden. | ||
| 1063 | |||
| 1064 | =item * C<Name> | ||
| 1065 | |||
| 1066 | Read-only. | ||
| 1067 | |||
| 1068 | =item * C<RowCacheSize> | ||
| 1069 | |||
| 1070 | Read-only. | ||
| 1071 | |||
| 1072 | =item * C<NUM_OF_FIELDS> | ||
| 1073 | |||
| 1074 | Read-only. | ||
| 1075 | |||
| 1076 | =item * C<NUM_OF_PARAMS> | ||
| 1077 | |||
| 1078 | Read-only. | ||
| 1079 | |||
| 1080 | =item * C<NAME> | ||
| 1081 | |||
| 1082 | Read-only. | ||
| 1083 | |||
| 1084 | =item * C<TYPE> | ||
| 1085 | |||
| 1086 | Read-only. | ||
| 1087 | |||
| 1088 | =item * C<PRECISION> | ||
| 1089 | |||
| 1090 | Read-only. | ||
| 1091 | |||
| 1092 | =item * C<SCALE> | ||
| 1093 | |||
| 1094 | Read-only. | ||
| 1095 | |||
| 1096 | =item * C<NULLABLE> | ||
| 1097 | |||
| 1098 | Read-only. | ||
| 1099 | |||
| 1100 | =item * C<CursorName> | ||
| 1101 | |||
| 1102 | Read-only. | ||
| 1103 | |||
| 1104 | =item * C<Statement> | ||
| 1105 | |||
| 1106 | Read-only. | ||
| 1107 | |||
| 1108 | =item * C<RowsInCache> | ||
| 1109 | |||
| 1110 | Read-only. | ||
| 1111 | |||
| 1112 | =back | ||
| 1113 | |||
| 1114 | =head2 available_drivers | ||
| 1115 | |||
| 1116 | Return the list of available drivers. | ||
| 1117 | |||
| 1118 | =head2 begin_work | ||
| 1119 | |||
| 1120 | Mark the beginning of a transaction. | ||
| 1121 | |||
| 1122 | Any arguments provided are passed along to L<DBD::mysql/begin_work> | ||
| 1123 | |||
| 1124 | =head2 bind | ||
| 1125 | |||
| 1126 | If no values to bind to the underlying query is provided, L<DB::Object/bind> simply activate the bind value feature. | ||
| 1127 | |||
| 1128 | If values are provided, they are allocated to the statement object and will be applied when the query will be executed. | ||
| 1129 | |||
| 1130 | Example: | ||
| 1131 | |||
| 1132 | $dbh->bind() | ||
| 1133 | # or | ||
| 1134 | $dbh->bind->where( "something" ) | ||
| 1135 | # or | ||
| 1136 | $dbh->bind->select->fetchrow_hashref() | ||
| 1137 | # and then later | ||
| 1138 | $dbh->bind( 'thingy' )->select->fetchrow_hashref() | ||
| 1139 | |||
| 1140 | =head2 cache | ||
| 1141 | |||
| 1142 | Activate caching. | ||
| 1143 | |||
| 1144 | $tbl->cache->select->fetchrow_hashref(); | ||
| 1145 | |||
| 1146 | =head2 check_driver | ||
| 1147 | |||
| 1148 | Check that the driver set in I<$SQL_DRIVER> in ~/etc/common.cfg is indeed available. | ||
| 1149 | |||
| 1150 | It does this by calling L<DB::Object/available_drivers>. | ||
| 1151 | |||
| 1152 | =head2 commit | ||
| 1153 | |||
| 1154 | Make any change to the database irreversible. | ||
| 1155 | |||
| 1156 | This must be used only after having called L</begin_work> | ||
| 1157 | |||
| 1158 | Any arguments provided are passed along to L<DBD::mysql/commit> | ||
| 1159 | |||
| 1160 | =head2 copy | ||
| 1161 | |||
| 1162 | Provided with either a reference to an hash or an hash of key => value pairs, L<DB::Object/copy> will first execute a select statement on the table object, then fetch the row of data, then replace the key-value pair in the result by the ones provided, and finally will perform an insert. | ||
| 1163 | |||
| 1164 | Return false if no data to copy were provided, otherwise it always returns true. | ||
| 1165 | |||
| 1166 | =head2 create_db | ||
| 1167 | |||
| 1168 | Provided with a database name and some optional parameters and this will prepare and execute the query to create the database. | ||
| 1169 | |||
| 1170 | Upon failure, this will return an error, and upon success, this will connect to the newly created database and return the database handler. | ||
| 1171 | |||
| 1172 | Possible options are: | ||
| 1173 | |||
| 1174 | =over 4 | ||
| 1175 | |||
| 1176 | =item * C<charset> | ||
| 1177 | |||
| 1178 | The character encoding. | ||
| 1179 | |||
| 1180 | =item * C<collate> | ||
| 1181 | |||
| 1182 | Sets the C<COLLATE> attribute | ||
| 1183 | |||
| 1184 | =item * C<if_not_exists> | ||
| 1185 | |||
| 1186 | Add the condition C<IF NOT EXISTS> | ||
| 1187 | |||
| 1188 | =back | ||
| 1189 | |||
| 1190 | See L<MySQL documentation for more information|https://dev.mysql.com/doc/refman/5.7/en/creating-database.html> | ||
| 1191 | |||
| 1192 | =head2 create_table | ||
| 1193 | |||
| 1194 | The idea is to create a table with the givern parameters. | ||
| 1195 | |||
| 1196 | =head2 databases | ||
| 1197 | |||
| 1198 | Returns a list of all available databases. | ||
| 1199 | |||
| 1200 | =head2 data_sources | ||
| 1201 | |||
| 1202 | Given an optional list of options, this return the data source of the database handler. | ||
| 1203 | |||
| 1204 | =head2 datatype_dict | ||
| 1205 | |||
| 1206 | Returns an hash reference of each data type with their equivalent C<constant>, regular expression (C<re>), constant C<name> and C<type> name. | ||
| 1207 | |||
| 1208 | Each data type is an hash with the following properties for each type: C<constant>, C<name>, C<re>, C<type> | ||
| 1209 | |||
| 1210 | |||
| 1211 | =head2 data_type | ||
| 1212 | |||
| 1213 | Given a reference to an array or an array of data type, L<DB::Object/data_type> will check their availability in the database driver. | ||
| 1214 | |||
| 1215 | If nothing found, it return an empty list in list context, or undef in scalar context. | ||
| 1216 | |||
| 1217 | If something was found, it returns a hash in list context or a reference to a hash in list context. | ||
| 1218 | |||
| 1219 | =head2 database | ||
| 1220 | |||
| 1221 | Return the name of the current database. | ||
| 1222 | |||
| 1223 | =head2 delete | ||
| 1224 | |||
| 1225 | L<DB::Object/delete> will format a delete query based on previously set parameters, such as L<DB::Object/where>. | ||
| 1226 | |||
| 1227 | L<DB::Object/delete> will refuse to execute a query without a where condition. To achieve this, one must prepare the delete query on his/her own by using the L<DB::Object/do> method and passing the sql query directly. | ||
| 1228 | |||
| 1229 | $tbl->where( "login" => "jack" ); | ||
| 1230 | $tbl->limit( 1 ); | ||
| 1231 | my $rows_affected = $tbl->delete(); | ||
| 1232 | # or passing the where condition directly to delete | ||
| 1233 | my( $sth ) = $tbl->delete( "login" => "jack" ); | ||
| 1234 | |||
| 1235 | =head2 disconnect | ||
| 1236 | |||
| 1237 | Disconnect from database. Returns the return code. | ||
| 1238 | |||
| 1239 | my $rc = $dbh->disconnect; | ||
| 1240 | |||
| 1241 | =head2 do | ||
| 1242 | |||
| 1243 | Execute a sql query directly passed with possible attributes and values to bind. | ||
| 1244 | |||
| 1245 | The attributes list will be used to L<DB::Object/prepare> the query and the bind values will be used when executing the query. | ||
| 1246 | |||
| 1247 | It returns the statement handler or the number of rows affected. | ||
| 1248 | |||
| 1249 | Example: | ||
| 1250 | |||
| 1251 | $rc = $dbh->do( $statement ) || die( $dbh->errstr ); | ||
| 1252 | $rc = $dbh->do( $statement, \%attr ) || die( $dbh->errstr ); | ||
| 1253 | $rv = $dbh->do( $statement, \%attr, @bind_values ) || die( $dbh->errstr ); | ||
| 1254 | my( $rows_deleted ) = $dbh->do( | ||
| 1255 | q{ | ||
| 1256 | DELETE FROM table WHERE status = ? | ||
| 1257 | }, undef(), 'DONE' ) || die( $dbh->errstr ); | ||
| 1258 | |||
| 1259 | =head2 enhance | ||
| 1260 | |||
| 1261 | Toggle the enhance mode on/off. | ||
| 1262 | |||
| 1263 | When on, the functions I<from_unixtime> and I<unix_timestamp> will be used on date/time field to translate from and to unix time seamlessly. | ||
| 1264 | |||
| 1265 | =head2 from_unixtime | ||
| 1266 | |||
| 1267 | Set the list of fields that are to be treated as unix time and converted accordingly after the sql query is executed. | ||
| 1268 | |||
| 1269 | It returns the list of fields in list context or a reference to an array in scalar context. | ||
| 1270 | |||
| 1271 | =head2 format_statement | ||
| 1272 | |||
| 1273 | Format the sql statement. | ||
| 1274 | |||
| 1275 | In list context, it returns 2 strings: one comma-separated list of fields and one comma-separated list of values. In scalar context, it only returns a comma-separated string of fields. | ||
| 1276 | |||
| 1277 | =head2 format_update | ||
| 1278 | |||
| 1279 | Formats update query based on the following arguments provided: | ||
| 1280 | |||
| 1281 | =over 4 | ||
| 1282 | |||
| 1283 | =item * C<data> | ||
| 1284 | |||
| 1285 | An array of key-value pairs to be used in the update query. This array can be provided as the prime argument as a reference to an array, an array, or as the I<data> element of a hash or a reference to a hash provided. | ||
| 1286 | |||
| 1287 | Why an array if eventually we build a list of key-value pair? Because the order of the fields may be important, and if the key-value pair list is provided, L<DB::Object/format_update> honors the order in which the fields are provided. | ||
| 1288 | |||
| 1289 | =back | ||
| 1290 | |||
| 1291 | L</format_update> will then iterate through each field-value pair, and perform some work: | ||
| 1292 | |||
| 1293 | If the field being reviewed was provided to L<DB::Object/from_unixtime>, then L</format_update> will enclose it in the function FROM_UNIXTIME() as in: | ||
| 1294 | |||
| 1295 | FROM_UNIXTIME(field_name) | ||
| 1296 | |||
| 1297 | If the the given value is a reference to a scalar, it will be used as-is, ie. it will not be enclosed in quotes or anything. This is useful if you want to control which function to use around that field. | ||
| 1298 | |||
| 1299 | If the given value is another field or looks like a function having parenthesis, or if the value is a question mark, the value will be used as-is. | ||
| 1300 | |||
| 1301 | If L<DB::Object/bind> is off, the value will be escaped and the pair field='value' created. | ||
| 1302 | |||
| 1303 | If the field is a SET data type and the value is a number, the value will be used as-is without surrounding single quote. | ||
| 1304 | |||
| 1305 | If L<DB::Object/bind> is enabled, a question mark will be used as the value and the original value will be saved as value to bind upon executing the query. | ||
| 1306 | |||
| 1307 | Finally, otherwise the value is escaped and surrounded by single quotes. | ||
| 1308 | |||
| 1309 | L<DB::Object/format_update> returns a string representing the comma-separated list of fields that will be used. | ||
| 1310 | |||
| 1311 | =head2 get_sql_type | ||
| 1312 | |||
| 1313 | Provided with a SQL type, irrespective of the character case, and this will return the equivalent constant value. | ||
| 1314 | |||
| 1315 | Values are taken from C<DBI::SQL_*> | ||
| 1316 | |||
| 1317 | =head2 getdefault | ||
| 1318 | |||
| 1319 | Does some preparation work such as: | ||
| 1320 | |||
| 1321 | =over 4 | ||
| 1322 | |||
| 1323 | =item 1 | ||
| 1324 | |||
| 1325 | the date/time field to use the FROM_UNIXTIME and UNIX_TIMESTAMP functions | ||
| 1326 | |||
| 1327 | =item 2 | ||
| 1328 | |||
| 1329 | removing from the query the fields to avoid, ie the ones set with the L<DB::Object/avoid> method. | ||
| 1330 | |||
| 1331 | =item 3 | ||
| 1332 | |||
| 1333 | set the fields alias based on the information provided with the L<DB::Object/alias> method. | ||
| 1334 | |||
| 1335 | =item 4 | ||
| 1336 | |||
| 1337 | if a field last_name and first_name exist, it will also create an alias I<name> based on the concatenation of the 2. | ||
| 1338 | |||
| 1339 | =item 5 | ||
| 1340 | |||
| 1341 | it will set the default values provided. This is used for UPDATE queries. | ||
| 1342 | |||
| 1343 | =back | ||
| 1344 | |||
| 1345 | It returns a new L<DB::Object::Tables> object with all the data prepared within. | ||
| 1346 | |||
| 1347 | =head2 group | ||
| 1348 | |||
| 1349 | Format the group by portion of the query. | ||
| 1350 | |||
| 1351 | It returns an empty list in list context of undef in scalar context if no group by clause was build. | ||
| 1352 | Otherwise, it returns the value of the group by clause as a string in list context and the full group by clause in scalar context. | ||
| 1353 | |||
| 1354 | In list context, it returns: $group_by | ||
| 1355 | |||
| 1356 | In scalar context, it returns: GROUP BY $group_by | ||
| 1357 | |||
| 1358 | =head2 having | ||
| 1359 | |||
| 1360 | A convenient wrapper to L<DB::Object::Mysql::Query/having> | ||
| 1361 | |||
| 1362 | =head2 insert | ||
| 1363 | |||
| 1364 | Prepares an INSERT query using the field-value pairs provided. | ||
| 1365 | |||
| 1366 | If a L<DB::Object::Statement> object is provided as first argument, it will considered as a SELECT query to be used in the INSERT query, as in: INSERT INTO my table SELECT FROM another_table | ||
| 1367 | |||
| 1368 | Otherwise, L<DB::Object/insert> will build the query based on the fields provided. | ||
| 1369 | |||
| 1370 | In scalar context, it returns the result of L<DB::Object/execute> and in list context, it returns the statement object. | ||
| 1371 | |||
| 1372 | =head2 last_insert_id | ||
| 1373 | |||
| 1374 | Get the id of the primary key from the last insert. | ||
| 1375 | |||
| 1376 | =head2 limit | ||
| 1377 | |||
| 1378 | Set or get the limit for the future statement. | ||
| 1379 | |||
| 1380 | If only one argument is provided, it is assumed to be the end limit. If 2 are provided, they wil be the start and end. | ||
| 1381 | |||
| 1382 | It returns a list of the start and end limit in list context, and the string of the LIMIT in scalar context, such as: LIMIT 1, 10 | ||
| 1383 | |||
| 1384 | =head2 local | ||
| 1385 | |||
| 1386 | Not sure what it does. I forgot. | ||
| 1387 | |||
| 1388 | =head2 lock | ||
| 1389 | |||
| 1390 | Set a lock using a lock identifier and a timeout. | ||
| 1391 | By default the timeout is 2 seconds. | ||
| 1392 | |||
| 1393 | If the lock failed (NULL), it returns undef(), otherwise, it returns the return value. | ||
| 1394 | |||
| 1395 | =head2 no_bind | ||
| 1396 | |||
| 1397 | When invoked, L<DB::Object/no_bind> will change any preparation made so far for caching the query with bind parameters, and instead substitute the value in lieu of the question mark placeholder. | ||
| 1398 | |||
| 1399 | =head2 no_cache | ||
| 1400 | |||
| 1401 | Disable caching of queries. | ||
| 1402 | |||
| 1403 | =head2 order | ||
| 1404 | |||
| 1405 | Prepares the ORDER BY clause and returns the value of the clause in list context or the ORDER BY clause in full in scalar context, ie. "ORDER BY $clause" | ||
| 1406 | |||
| 1407 | =head2 param | ||
| 1408 | |||
| 1409 | If only a single parameter is provided, its value is return. If a list of parameters is provided they are set accordingly using the C<SET> sql command. | ||
| 1410 | |||
| 1411 | Supported parameters are: | ||
| 1412 | |||
| 1413 | =over 4 | ||
| 1414 | |||
| 1415 | =item SQL_AUTO_IS_NULL | ||
| 1416 | |||
| 1417 | =item AUTOCOMMIT | ||
| 1418 | |||
| 1419 | =item SQL_BIG_TABLES | ||
| 1420 | |||
| 1421 | =item SQL_BIG_SELECTS | ||
| 1422 | |||
| 1423 | =item SQL_BUFFER_RESULT | ||
| 1424 | |||
| 1425 | =item SQL_LOW_PRIORITY_UPDATES | ||
| 1426 | |||
| 1427 | =item SQL_MAX_JOIN_SIZE | ||
| 1428 | |||
| 1429 | =item SQL_SAFE_MODE | ||
| 1430 | |||
| 1431 | =item SQL_SELECT_LIMIT | ||
| 1432 | |||
| 1433 | =item SQL_LOG_OFF | ||
| 1434 | |||
| 1435 | =item SQL_LOG_UPDATE | ||
| 1436 | |||
| 1437 | =item TIMESTAMP | ||
| 1438 | |||
| 1439 | =item INSERT_ID | ||
| 1440 | |||
| 1441 | =item LAST_INSERT_ID | ||
| 1442 | |||
| 1443 | =back | ||
| 1444 | |||
| 1445 | If unsupported parameters are provided, they are considered to be private and not passed to the database handler. | ||
| 1446 | |||
| 1447 | It then execute the query and return undef() in case of error. | ||
| 1448 | |||
| 1449 | Otherwise, it returns the object used to call the method. | ||
| 1450 | |||
| 1451 | =head2 ping | ||
| 1452 | |||
| 1453 | Evals a SELECT 1 statement and returns 0 if errors occurred or the return value. | ||
| 1454 | |||
| 1455 | =head2 prepare | ||
| 1456 | |||
| 1457 | Prepares the query using the options provided. The options are the same as the one in L<DBI/prepare> method. | ||
| 1458 | |||
| 1459 | It returns a L<DB::Object::Statement> object upon success or undef if an error occurred. The error can then be retrieved using L<DB::Object/errstr> or L<DB::Object/error>. | ||
| 1460 | |||
| 1461 | =head2 prepare_cached | ||
| 1462 | |||
| 1463 | Same as L<DB::Object/prepare> except the query is cached. | ||
| 1464 | |||
| 1465 | =head2 query | ||
| 1466 | |||
| 1467 | It prepares and executes the given SQL query with the options provided and return undef() upon error or the statement handler upon success. | ||
| 1468 | |||
| 1469 | =head2 query_object | ||
| 1470 | |||
| 1471 | Set or gets the PostgreSQL query object (L<DB::Object::Mysql::Query>) used to process and format queries. | ||
| 1472 | |||
| 1473 | =head2 replace | ||
| 1474 | |||
| 1475 | Just like for the INSERT query, L<DB::Object::Mysql::Tables/replace> takes one optional argument representing a L<DB::Object::Statement> SELECT object or a list of field-value pairs. | ||
| 1476 | |||
| 1477 | If a SELECT statement is provided, it will be used to construct a query of the type of REPLACE INTO mytable SELECT FROM other_table | ||
| 1478 | |||
| 1479 | Otherwise the query will be REPLACE INTO mytable (fields) VALUES(values) | ||
| 1480 | |||
| 1481 | In scalar context, it execute the query and in list context it simply returns the statement handler. | ||
| 1482 | |||
| 1483 | =head2 reset | ||
| 1484 | |||
| 1485 | This is used to reset a prepared query to its default values. If a field is a date/time type, its default value will be set to NOW() | ||
| 1486 | |||
| 1487 | It execute an update with the reseted value and return the number of affected rows. | ||
| 1488 | |||
| 1489 | =head2 reverse | ||
| 1490 | |||
| 1491 | Get or set the reverse mode. | ||
| 1492 | |||
| 1493 | =head2 rollback | ||
| 1494 | |||
| 1495 | Will roll back any changes made to the database since the last transaction point marked with L</begin_work> | ||
| 1496 | |||
| 1497 | =head2 select | ||
| 1498 | |||
| 1499 | Given an optional list of fields to fetch, L<DB::Object/select> prepares a SELECT query. | ||
| 1500 | |||
| 1501 | If no field was provided, L<DB::Object/select> will use default value where appropriate like the NOW() for date/time fields. | ||
| 1502 | |||
| 1503 | L<DB::Object/select> calls upon L<DB::Object/tie>, L<DB::Object/where>, L<DB::Object/group>, L<DB::Object/order>, L<DB::Object/limit> and L<DB::Object/local> to build the query. | ||
| 1504 | |||
| 1505 | In scalar context, it execute the query and return it. In list context, it just returns the statement handler. | ||
| 1506 | |||
| 1507 | =head2 set | ||
| 1508 | |||
| 1509 | Issues a query to C<SET> the given SQL variable. | ||
| 1510 | |||
| 1511 | If any error occurred, undef will be returned and an error set, otherwise it returns true. | ||
| 1512 | |||
| 1513 | =head2 sort | ||
| 1514 | |||
| 1515 | It toggles sort mode on and consequently disable reverse mode. | ||
| 1516 | |||
| 1517 | =head2 stat | ||
| 1518 | |||
| 1519 | Issue a SHOW STATUS query and if a particular $type is provided, it will returns its value if it exists, otherwise it will return undef. | ||
| 1520 | |||
| 1521 | In absence of particular $type provided, it returns the hash list of values returns or a reference to the hash list in scalar context. | ||
| 1522 | |||
| 1523 | =head2 state | ||
| 1524 | |||
| 1525 | Queries the DBI state and return its value. | ||
| 1526 | |||
| 1527 | =head2 table | ||
| 1528 | |||
| 1529 | Given a table name, L<DB::Object/table> will return a L<DB::Object::Tables> object. The object is cached for re-use. | ||
| 1530 | |||
| 1531 | =head2 table_info | ||
| 1532 | |||
| 1533 | Provided with a table name and some optional parameters and this will retrieve the table information. | ||
| 1534 | |||
| 1535 | It returns an array reference of tables information found if no schema was provided or if I<anywhere> is true. | ||
| 1536 | |||
| 1537 | If a schema was provided, and the table found it returns an hash reference for that table. | ||
| 1538 | |||
| 1539 | Otherwise, if nothing can be found, it returns an empty array reference. | ||
| 1540 | |||
| 1541 | Optional parameters are: | ||
| 1542 | |||
| 1543 | =over 4 | ||
| 1544 | |||
| 1545 | =item * C<anywhere> | ||
| 1546 | |||
| 1547 | If true, it will search anywhere. | ||
| 1548 | |||
| 1549 | =item * C<schema> | ||
| 1550 | |||
| 1551 | A database schema. | ||
| 1552 | |||
| 1553 | =back | ||
| 1554 | |||
| 1555 | =head2 table_push | ||
| 1556 | |||
| 1557 | Add the given table name to the stack of cached table names. | ||
| 1558 | |||
| 1559 | =head2 tables | ||
| 1560 | |||
| 1561 | Connects to the database and finds out the list of all available tables. | ||
| 1562 | |||
| 1563 | Returns undef or empty list in scalar or list context respectively if no table found. | ||
| 1564 | |||
| 1565 | Otherwise, it returns the list of table in list context or a reference of it in scalar context. | ||
| 1566 | |||
| 1567 | =head2 table_info | ||
| 1568 | |||
| 1569 | Provided with a table name and this returns all the matching table information from the MySQL system. | ||
| 1570 | |||
| 1571 | It returns an array reference of tables information found if no schema was provided or if C<anywhere> is true. | ||
| 1572 | |||
| 1573 | If a schema was provided, and the table found it returns an hash reference for that table. | ||
| 1574 | |||
| 1575 | Otherwise, if nothing can be found, it returns an empty array reference. | ||
| 1576 | |||
| 1577 | It takes the following options: | ||
| 1578 | |||
| 1579 | =over 4 | ||
| 1580 | |||
| 1581 | =item * C<anywhere> | ||
| 1582 | |||
| 1583 | If true, this will have this method an array reference of hash reference for each match. | ||
| 1584 | |||
| 1585 | =item * C<database> | ||
| 1586 | |||
| 1587 | An optional database name to restrict the search. | ||
| 1588 | |||
| 1589 | =back | ||
| 1590 | |||
| 1591 | Information retrieved are: | ||
| 1592 | |||
| 1593 | =over 4 | ||
| 1594 | |||
| 1595 | =item * C<name> | ||
| 1596 | |||
| 1597 | The table name | ||
| 1598 | |||
| 1599 | =item * C<schema> | ||
| 1600 | |||
| 1601 | Database schema, if any. | ||
| 1602 | |||
| 1603 | =item * C<type> | ||
| 1604 | |||
| 1605 | The object type, which may be one of: C<table>, C<view>, C<materialized view>, C<special>, C<foreign table> | ||
| 1606 | |||
| 1607 | =back | ||
| 1608 | |||
| 1609 | =head2 tables_info | ||
| 1610 | |||
| 1611 | Provided with a database name and this returns all the tables information. | ||
| 1612 | |||
| 1613 | Information retrieved from the MySQL system tables for every table found in the given database are: | ||
| 1614 | |||
| 1615 | =over 4 | ||
| 1616 | |||
| 1617 | =item * C<name> | ||
| 1618 | |||
| 1619 | The object name | ||
| 1620 | |||
| 1621 | =item * C<database> | ||
| 1622 | |||
| 1623 | Database name. | ||
| 1624 | |||
| 1625 | =item * C<type> | ||
| 1626 | |||
| 1627 | The object type, which may be one of: C<base table>, C<view> | ||
| 1628 | |||
| 1629 | =back | ||
| 1630 | |||
| 1631 | =head2 tables_refresh | ||
| 1632 | |||
| 1633 | Rebuild the list of available database table. | ||
| 1634 | |||
| 1635 | Returns the list of table in list context or a reference of it in scalar context. | ||
| 1636 | |||
| 1637 | =head2 tie | ||
| 1638 | |||
| 1639 | If provided a hash or a hash ref, it sets the list of fields and their corresponding perl variable to bind their values to. | ||
| 1640 | |||
| 1641 | In list context, it returns the list of those field-variable pair, or a reference to it in scalar context. | ||
| 1642 | |||
| 1643 | =head2 unix_timestamp | ||
| 1644 | |||
| 1645 | Provided a list of fields or a reference to it, this sets the fields to be treated for seamless conversion from and to unix time. | ||
| 1646 | |||
| 1647 | =head2 unlock | ||
| 1648 | |||
| 1649 | Given a lock identifier, L<DB::Object/unlock> releases the lock previously set with L<DB::Object/lock>. It executes the underlying sql command and returns undef() if the result is NULL or the value returned otherwise. | ||
| 1650 | |||
| 1651 | =head2 update | ||
| 1652 | |||
| 1653 | Given a list of field-value pairs, L<DB::Object/update> prepares a sql update query. | ||
| 1654 | |||
| 1655 | It calls upon L<DB::Object/where> and L<DB::Object/limit> as previously set. | ||
| 1656 | |||
| 1657 | It returns undef and sets an error if it failed to prepare the update statement. In scalar context, it execute the query. In list context, it simply return the statement handler. | ||
| 1658 | |||
| 1659 | =head2 use | ||
| 1660 | |||
| 1661 | Given a database, it switch to it, but before it checks that the database exists. | ||
| 1662 | If the database is different than the current one, it sets the I<multi_db> parameter, which will have the fields in the queries be prefixed by their respective database name. | ||
| 1663 | |||
| 1664 | It returns the database handler. | ||
| 1665 | |||
| 1666 | =head2 use_cache | ||
| 1667 | |||
| 1668 | Sets or get the I<use_cache> parameter. | ||
| 1669 | |||
| 1670 | =head2 use_bind | ||
| 1671 | |||
| 1672 | Sets or get the I<use_cache> parameter. | ||
| 1673 | |||
| 1674 | =head2 variables | ||
| 1675 | |||
| 1676 | Query the SQL variable $type | ||
| 1677 | |||
| 1678 | It returns a blank string if nothing was found, or the value found. | ||
| 1679 | |||
| 1680 | =head2 version | ||
| 1681 | |||
| 1682 | Returns the MySQL database server version. | ||
| 1683 | |||
| 1684 | This information is cached per object for efficiency. | ||
| 1685 | |||
| 1686 | =head2 where | ||
| 1687 | |||
| 1688 | Build the where clause based on the field-value hash provided. | ||
| 1689 | |||
| 1690 | It returns the where clause in list context or the full where clause in scalar context, ie "WHERE $clause" | ||
| 1691 | |||
| 1692 | =head2 _cache_this | ||
| 1693 | |||
| 1694 | Provided with a query, this will cache it for future re-use. | ||
| 1695 | |||
| 1696 | It does some check and maintenance job to ensure the cache does not get too big whenever it exceed the value of $CACHE_SIZE set in the main config file. | ||
| 1697 | |||
| 1698 | It returns the cached statement as an L<DB::Object::Statement> object. | ||
| 1699 | |||
| 1700 | =head2 _clean_statement | ||
| 1701 | |||
| 1702 | Given a query string or a reference to it, it cleans the statement by removing leading and trailing space before and after line breaks. | ||
| 1703 | |||
| 1704 | =head2 _cleanup | ||
| 1705 | |||
| 1706 | Removes object attributes, namely where, selected_fields, group_by, order_by, limit, alias, avoid, local, and as_string | ||
| 1707 | |||
| 1708 | =head2 _make_sth | ||
| 1709 | |||
| 1710 | Given a package name and a hashref, this build a statement object with all the necessary parameters. | ||
| 1711 | |||
| 1712 | It also sets the query time to the current time with the parameter I<query_time> | ||
| 1713 | |||
| 1714 | It returns an object of the given $package. | ||
| 1715 | |||
| 1716 | =head2 _reset_query | ||
| 1717 | |||
| 1718 | Being called using a statement handler, this reset the object by removing all the parameters set by various subroutine calls, such as L<DB::Object/where>, L<DB::Object/group>, L<DB::Object/order>, L<DB::Object/avoid>, L<DB::Object/limit>, etc. | ||
| 1719 | |||
| 1720 | =head2 _save_bind | ||
| 1721 | |||
| 1722 | This saves/cache the bin query and return the object used to call it. | ||
| 1723 | |||
| 1724 | =head2 _value2bind | ||
| 1725 | |||
| 1726 | Given a sql query and a array reference, L<DB::Object/_value2bind> parse the query and interpolate values for placeholder (?). | ||
| 1727 | |||
| 1728 | It returns true. | ||
| 1729 | |||
| 1730 | =head1 SEE ALSO | ||
| 1731 | |||
| 1732 | L<DB::Object> | ||
| 1733 | |||
| 1734 | L<DBI>, L<Apache::DBI> | ||
| 1735 | |||
| 1736 | =head1 AUTHOR | ||
| 1737 | |||
| 1738 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 1739 | |||
| 1740 | =head1 COPYRIGHT & LICENSE | ||
| 1741 | |||
| 1742 | Copyright (c) 2019-2021 DEGUEST Pte. Ltd. | ||
| 1743 | |||
| 1744 | You can use, copy, modify and redistribute this package and associated | ||
| 1745 | files under the same terms as Perl itself. | ||
| 1746 | |||
| 1747 | =cut |