| 1 | # -*- perl -*- | ||
| 2 | ##---------------------------------------------------------------------------- | ||
| 3 | ## Database Object Interface - ~/lib/DB/Object/Tables.pm | ||
| 4 | ## Version v1.2.2 | ||
| 5 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||
| 6 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 7 | ## Created 2017/07/19 | ||
| 8 | ## Modified 2026/08/05 | ||
| 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 separate the object of the tables from the main | ||
| 15 | # DB::Object package so that when they get DESTROY'ed, it does not interrupt | ||
| 16 | # the SQL connection | ||
| 17 | ##---------------------------------------------------------------------------- | ||
| 18 | package DB::Object::Tables; | ||
| 19 | BEGIN | ||
| 20 | { | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | warnings::register_categories( 'DB::Object' ); | ||
| 24 | use parent qw( DB::Object ); | ||
| 25 | use vars qw( $VERSION $DEBUG $EXCEPTION_CLASS ); | ||
| 26 | use DB::Object::Fields; | ||
| 27 | use Wanted; | ||
| 28 | our $DEBUG = 0; | ||
| 29 | our $EXCEPTION_CLASS = $DB::Object::EXCEPTION_CLASS; | ||
| 30 | our $VERSION = 'v1.2.2'; | ||
| 31 | }; | ||
| 32 | |||
| 33 | use strict; | ||
| 34 | use warnings; | ||
| 35 | |||
| 36 | sub new { return( shift->Module::Generic::new( @_ ) ); } | ||
| 37 | |||
| 38 | sub init | ||
| 39 | { | ||
| 40 | my $self = shift( @_ ); | ||
| 41 | my $table; | ||
| 42 | my @test_args; | ||
| 43 | if( @_ && | ||
| 44 | ( | ||
| 45 | # $self->init( $table_name ) | ||
| 46 | ( | ||
| 47 | @_ == 1 && | ||
| 48 | defined( $_[0] ) && | ||
| 49 | ( | ||
| 50 | !ref( $_[0] ) || | ||
| 51 | $self->_can_overload( $_[0] => '""' ) | ||
| 52 | ) | ||
| 53 | ) || | ||
| 54 | # $self->init( $table_name, $opts_hash_ref ); | ||
| 55 | ( | ||
| 56 | @_ == 2 && | ||
| 57 | defined( $_[0] ) && | ||
| 58 | defined( $_[1] ) && | ||
| 59 | ( | ||
| 60 | !ref( $_[0] ) || | ||
| 61 | $self->_can_overload( $_[0] => '""' ) | ||
| 62 | ) && | ||
| 63 | ref( $_[1] ) eq 'HASH' | ||
| 64 | ) || | ||
| 65 | # $self->init( $table_name, opt11 => val1, opt2 => val2 ); | ||
| 66 | ( | ||
| 67 | @_ > 2 && | ||
| 68 | defined( $_[0] ) && | ||
| 69 | ( | ||
| 70 | !ref( $_[0] ) || $self->_can_overload( $_[0] => '""' ) | ||
| 71 | ) && | ||
| 72 | ( @test_args = @_[1..$#_] ) && | ||
| 73 | !( @test_args % 2 ) | ||
| 74 | ) | ||
| 75 | ) ) | ||
| 76 | { | ||
| 77 | $table = shift( @_ ); | ||
| 78 | } | ||
| 79 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 80 | unless( defined( $table ) ) | ||
| 81 | { | ||
| 82 | if( !CORE::exists( $opts->{table} ) || !CORE::length( $opts->{table} // '' ) ) | ||
| 83 | { | ||
| 84 | return( $self->error( "You must provide a table name to create a table object." ) ); | ||
| 85 | } | ||
| 86 | else | ||
| 87 | { | ||
| 88 | $table = CORE::delete( $opts->{table} ); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | $self->{check} = {}; | ||
| 92 | $self->{dbo} = undef; | ||
| 93 | # $self->{default} = {}; | ||
| 94 | # Containing all the table field objects | ||
| 95 | $self->{fields} = {}; | ||
| 96 | # DB::Object::Fields | ||
| 97 | $self->{fields_object} = undef; | ||
| 98 | $self->{foreign} = {}; | ||
| 99 | $self->{indexes} = {}; | ||
| 100 | # $self->{null} = {}; | ||
| 101 | $self->{prefixed} = 0; | ||
| 102 | $self->{primary} = []; | ||
| 103 | $self->{query_object} = undef; | ||
| 104 | $self->{query_reset} = 0; | ||
| 105 | $self->{reverse} = 0; | ||
| 106 | # The schema name, if any | ||
| 107 | $self->{schema} = undef; | ||
| 108 | $self->{structure} = {}; | ||
| 109 | $self->{table_alias} = undef; | ||
| 110 | # $self->{types} = {}; | ||
| 111 | # An hash to contain table field to an hash of constant value and constant name: | ||
| 112 | # field => { constant => 12, name => PG_JSONB, type => 'jsonb' }; | ||
| 113 | $self->{types_const} = {}; | ||
| 114 | # The table type. It could be table or view | ||
| 115 | $self->{type} = undef; | ||
| 116 | $self->{_init_params_order} = [qw( dbo query_object )]; | ||
| 117 | $self->{_init_strict_use_sub} = 1; | ||
| 118 | $self->{_exception_class} = $EXCEPTION_CLASS; | ||
| 119 | $self->Module::Generic::init( %$opts ) || return( $self->pass_error ); | ||
| 120 | $self->dbo( $opts->{dbo} ); | ||
| 121 | $self->{table} = $table; | ||
| 122 | $self->{_cache_structure} = ''; | ||
| 123 | # Load table default, fields, structure informations | ||
| 124 | my $ref = $self->structure || return( $self->pass_error ); | ||
| 125 | # return( $self->error( "There is no table by the name of $table" ) ) if( !defined( $ref ) || !%$ref ); | ||
| 126 | return( $self ); | ||
| 127 | } | ||
| 128 | |||
| 129 | # Get/set alias | ||
| 130 | sub alias { return( shift->_method_to_query( 'alias', @_ ) ); } | ||
| 131 | |||
| 132 | sub alter | ||
| 133 | { | ||
| 134 | my $self = shift( @_ ); | ||
| 135 | # Expecting a reference to an array | ||
| 136 | my $spec; | ||
| 137 | if( @_ == 1 && $self->_is_array( $_[0] ) ) | ||
| 138 | { | ||
| 139 | $spec = shift( @_ ); | ||
| 140 | } | ||
| 141 | elsif( @_ ) | ||
| 142 | { | ||
| 143 | $spec = [@_]; | ||
| 144 | } | ||
| 145 | my $table = $self->{table} || | ||
| 146 | return( $self->error( "No table was provided." ) ); | ||
| 147 | return( $self->error( "No proper ALTER specification was provided." ) ) if( !defined( $spec ) || !@$spec ); | ||
| 148 | my $query = "ALTER TABLE $table " . CORE::join( ', ', @$spec ); | ||
| 149 | my $sth = $self->prepare( $query ) || | ||
| 150 | return( $self->error( "Error while preparing ALTER query to modify table '$table':\n", $self->errstr() ) ); | ||
| 151 | if( !defined( wantarray() ) ) | ||
| 152 | { | ||
| 153 | $sth->execute() || | ||
| 154 | return( $self->error( "Error while executing query to ALTER table '$table':\n", $self->as_string(), $sth->errstr() ) ); | ||
| 155 | } | ||
| 156 | return( $sth ); | ||
| 157 | } | ||
| 158 | |||
| 159 | sub as | ||
| 160 | { | ||
| 161 | my $self = shift( @_ ); | ||
| 162 | my $q = $self->_reset_query; | ||
| 163 | if( @_ ) | ||
| 164 | { | ||
| 165 | $self->{table_alias} = $_[0]; | ||
| 166 | $self->prefixed( length( $_[0] ) > 0 ? 1 : 0 ); | ||
| 167 | $q->table_alias( $self->{table_alias} ); | ||
| 168 | } | ||
| 169 | return( $self->{table_alias} ); | ||
| 170 | } | ||
| 171 | |||
| 172 | sub attach | ||
| 173 | { | ||
| 174 | my $self = shift( @_ ); | ||
| 175 | my $dbo = shift( @_ ) || | ||
| 176 | return( $self->error( "No DB::Object object was provided." ) ); | ||
| 177 | my $base_class = $self->base_class; | ||
| 178 | unless( $self->_is_a( $dbo => $base_class ) ) | ||
| 179 | { | ||
| 180 | return( $self->error( "Value provided is not a ${base_class} object." ) ); | ||
| 181 | } | ||
| 182 | $self->database_object( $dbo ) || return( $self->pass_error ); | ||
| 183 | $self->debug( $dbo->debug ); | ||
| 184 | if( my $qo = $self->query_object ) | ||
| 185 | { | ||
| 186 | unless( $self->_is_a( $qo => "${base_class}::Query" ) ) | ||
| 187 | { | ||
| 188 | die( "Query object found (", $self->_str_val( $qo ), ") in this statement object is actually not a query object (${base_class}::Query) !" ); | ||
| 189 | } | ||
| 190 | $self->_load_class( "${base_class}::Query" ) || return( $self->pass_error ); | ||
| 191 | # The attach method in the query object class can take either a table object, or a database object. | ||
| 192 | $qo->attach( $dbo ) || return( $self->pass_error( $qo->error ) ); | ||
| 193 | } | ||
| 194 | my $fields = $self->{fields}; | ||
| 195 | if( $fields && ref( $fields ) eq 'HASH' ) | ||
| 196 | { | ||
| 197 | foreach my $field ( keys( %$fields ) ) | ||
| 198 | { | ||
| 199 | my $fo = $fields->{ $field }; | ||
| 200 | next unless( $self->_is_a( $fo => 'DB::Object::Fields::Field' ) ); | ||
| 201 | $fo->table_object( $self ); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | return( $self ); | ||
| 205 | } | ||
| 206 | |||
| 207 | sub avoid { return( shift->_method_to_query( 'avoid', @_ ) ); } | ||
| 208 | |||
| 209 | sub check { return( shift->_set_get_hash_as_mix_object( 'check', @_ ) ); } | ||
| 210 | |||
| 211 | sub columns | ||
| 212 | { | ||
| 213 | my $self = shift( @_ ); | ||
| 214 | my $fields = $self->fields; | ||
| 215 | # my $cols = [sort{ $fields->{ $a } <=> $fields->{ $b } } keys( %$fields )]; | ||
| 216 | my $cols = [sort{ $fields->{ $a }->pos <=> $fields->{ $b }->pos } keys( %$fields )]; | ||
| 217 | return( $self->new_array( $cols ) ); | ||
| 218 | } | ||
| 219 | |||
| 220 | sub constant | ||
| 221 | { | ||
| 222 | my $self = shift( @_ ); | ||
| 223 | my( $pack, $file, $line ) = caller; | ||
| 224 | my $base_class = $self->database_object->base_class; | ||
| 225 | # This does not work for calls made internally | ||
| 226 | return( $self ) if( $pack =~ /^${base_class}\b/ ); | ||
| 227 | my $sth = $self->database_object->constant_queries_cache_get({ | ||
| 228 | pack => $pack, | ||
| 229 | file => $file, | ||
| 230 | line => $line, | ||
| 231 | }); | ||
| 232 | # $sth returned may be void if no cache was found or if the caller's file mod time has changed | ||
| 233 | my $q; | ||
| 234 | if( $sth ) | ||
| 235 | { | ||
| 236 | $q = $sth->query_object; | ||
| 237 | $self->query_object( $q ); | ||
| 238 | } | ||
| 239 | else | ||
| 240 | { | ||
| 241 | $q = $self->_reset_query; | ||
| 242 | } | ||
| 243 | $q->constant({ | ||
| 244 | sth => $sth, | ||
| 245 | pack => $pack, | ||
| 246 | file => $file, | ||
| 247 | line => $line, | ||
| 248 | }); | ||
| 249 | return( $self ); | ||
| 250 | } | ||
| 251 | |||
| 252 | # sub create must be superseded by sub classes | ||
| 253 | sub create | ||
| 254 | { | ||
| 255 | my $self = shift( @_ ); | ||
| 256 | my $class = ref( $self ); | ||
| 257 | return( $self->error( "create() is not implemented by $class." ) ); | ||
| 258 | } | ||
| 259 | |||
| 260 | sub create_info | ||
| 261 | { | ||
| 262 | my $self = shift( @_ ); | ||
| 263 | my $class = ref( $self ); | ||
| 264 | return( $self->error( "create_info() is not implemented by $class." ) ); | ||
| 265 | } | ||
| 266 | |||
| 267 | sub database { return( shift->database_object->database ); } | ||
| 268 | |||
| 269 | sub database_object { return( shift->_set_get_object_without_init( 'dbo', 'DB::Object', @_ ) ); } | ||
| 270 | |||
| 271 | # sub dbh { return( shift->_set_get( 'dbh', @_ ) ); } | ||
| 272 | |||
| 273 | sub dbo { return( shift->_set_get_object_without_init( 'dbo', 'DB::Object', @_ ) ); } | ||
| 274 | |||
| 275 | sub default | ||
| 276 | { | ||
| 277 | my $self = shift( @_ ); | ||
| 278 | $self->structure || return( $self->pass_error ); | ||
| 279 | my $fields = $self->{fields}; | ||
| 280 | my $default = +{ map{ defined( $fields->{ $_ }->default ) ? ( $_ => $fields->{ $_ }->default ) : () } keys( %$fields ) }; | ||
| 281 | return( $default ); | ||
| 282 | } | ||
| 283 | |||
| 284 | sub delete | ||
| 285 | { | ||
| 286 | my $self = shift( @_ ); | ||
| 287 | my $q = $self->_reset_query; | ||
| 288 | # If the user wants to execute this, then we reset the query, | ||
| 289 | # but if the user wants to call other methods chained like as_string we don't do anything | ||
| 290 | # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) ); | ||
| 291 | if( want('VOID') || want('OBJECT') ) | ||
| 292 | { | ||
| 293 | CORE::delete( $self->{query_reset} ) if( want('VOID') ); | ||
| 294 | # return( $q->select( @_ ) ); | ||
| 295 | # return( $q->select( @_ ) ) if( !defined( wantarray() ) ); | ||
| 296 | return( $q->delete( @_ ) ); | ||
| 297 | } | ||
| 298 | # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) ); | ||
| 299 | # return( $q->delete( @_ ) ); | ||
| 300 | # return( $q->delete( @_ ) ) if( !defined( wantarray() ) ); | ||
| 301 | if( wantarray() ) | ||
| 302 | { | ||
| 303 | my( @val ) = $q->delete( @_ ) || return( $self->pass_error( $q->error ) ); | ||
| 304 | $self->reset; | ||
| 305 | return( @val ); | ||
| 306 | } | ||
| 307 | else | ||
| 308 | { | ||
| 309 | my $val = $q->delete( @_ ) || return( $self->pass_error( $q->error ) ); | ||
| 310 | $self->reset; | ||
| 311 | return( $val ); | ||
| 312 | } | ||
| 313 | } | ||
| 314 | |||
| 315 | sub drop | ||
| 316 | { | ||
| 317 | my $self = shift( @_ ); | ||
| 318 | my $table = $self->{table} || | ||
| 319 | return( $self->error( "No table was provided to drop." ) ); | ||
| 320 | my $query = "DROP TABLE $table"; | ||
| 321 | my $sth = $self->prepare( $query ) || | ||
| 322 | return( $self->error( "Error while preparing query to drop table '$table':\n$query", $self->errstr() ) ); | ||
| 323 | if( !defined( wantarray() ) ) | ||
| 324 | { | ||
| 325 | $sth->execute || | ||
| 326 | return( $self->error( "Error while executing query to drop table '$table':\n$query", $sth->errstr() ) ); | ||
| 327 | } | ||
| 328 | return( $sth ); | ||
| 329 | } | ||
| 330 | |||
| 331 | # sub enhance { return( shift->_method_to_query( 'enhance', @_ ) ); } | ||
| 332 | sub enhance | ||
| 333 | { | ||
| 334 | my $self = shift( @_ ); | ||
| 335 | if( my $dbo = $self->database_object ) | ||
| 336 | { | ||
| 337 | return( $dbo->enhance( @_ ) ); | ||
| 338 | } | ||
| 339 | else | ||
| 340 | { | ||
| 341 | return( $self->_method_to_query( 'enhance', @_ ) ); | ||
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | sub exists | ||
| 346 | { | ||
| 347 | my $self = shift( @_ ); | ||
| 348 | my $class = ref( $self ); | ||
| 349 | return( $self->error( "exists() is not implemented by $class." ) ); | ||
| 350 | } | ||
| 351 | |||
| 352 | sub fields | ||
| 353 | { | ||
| 354 | my $self = shift( @_ ); | ||
| 355 | # The method 'structure' will fetch and cache the table structure, and set the instance property 'fields' containing an hash reference of all table fields to their DB::Object::Fields::Field instance. | ||
| 356 | $self->structure || return( $self->pass_error ); | ||
| 357 | my $fields = $self->{fields}; | ||
| 358 | if( @_ ) | ||
| 359 | { | ||
| 360 | my $field = shift( @_ ); | ||
| 361 | return( $self->error( "No field name was provided." ) ) if( !CORE::length( $field // '' ) ); | ||
| 362 | my $obj = $fields->{ $field } || return( $self->error( "No field object found for \"", ( $field // 'undef' ), "\"." ) ); | ||
| 363 | # NOTE: We return the canonical Field object directly rather than cloning it. | ||
| 364 | # The immediate caller (_initiate_field_object) overwrites table_object, | ||
| 365 | # query_object, prefixed, and debug right after this call, so a full Clone::clone | ||
| 366 | # of all the field data attributes (name, type, pos, is_nullable, etc.) is wasteful. | ||
| 367 | # The data attributes are read-only after structure() populates them. | ||
| 368 | return( $obj ); | ||
| 369 | } | ||
| 370 | # return( +{ map{ $_ => $fields->{ $_ }->clone } keys( %$fields ) } ); | ||
| 371 | my $ref = {}; | ||
| 372 | # NOTE: Return the canonical Field objects directly rather than cloning each one. | ||
| 373 | # Fields are populated once by structure() and their data attributes (name, type, | ||
| 374 | # pos, is_nullable, etc.) are never modified after that. The mutable attributes | ||
| 375 | # (table_object, query_object, prefixed, debug) are always overwritten by the caller. | ||
| 376 | foreach my $f ( keys( %$fields ) ) | ||
| 377 | { | ||
| 378 | $fields->{ $f }->debug( $self->debug ); | ||
| 379 | $ref->{ $f } = $fields->{ $f }; | ||
| 380 | } | ||
| 381 | return( $ref ); | ||
| 382 | } | ||
| 383 | |||
| 384 | sub fields_as_array | ||
| 385 | { | ||
| 386 | my $self = shift( @_ ); | ||
| 387 | $self->structure || return( $self->pass_error ); | ||
| 388 | my $fields = $self->{fields}; | ||
| 389 | return( $self->new_array( [sort{ $fields->{ $a }->pos <=> $fields->{ $b }->pos } keys( %$fields )] ) ); | ||
| 390 | } | ||
| 391 | |||
| 392 | sub field_exists | ||
| 393 | { | ||
| 394 | my $self = shift( @_ ); | ||
| 395 | my $field = shift( @_ ) || return( $self->error( "No field was provided." ) ); | ||
| 396 | $self->structure || return( $self->pass_error ); | ||
| 397 | my $fields = $self->{fields}; | ||
| 398 | return( CORE::exists( $fields->{ $field } ) ); | ||
| 399 | } | ||
| 400 | |||
| 401 | sub fields_object | ||
| 402 | { | ||
| 403 | my $self = shift( @_ ); | ||
| 404 | my $o = $self->{fields_object}; | ||
| 405 | # This will make sure we have a query object which DB::Object::Fields and DB::Object::Field need | ||
| 406 | $self->_reset_query; | ||
| 407 | my $qo = $self->query_object; | ||
| 408 | $qo->table_object( $self ); | ||
| 409 | if( $o && $self->_is_object( $o ) ) | ||
| 410 | { | ||
| 411 | $o->prefixed( $self->{prefixed} ); | ||
| 412 | $o->query_object( $qo ); | ||
| 413 | return( $o ); | ||
| 414 | } | ||
| 415 | my $db_name = $self->database_object->database; | ||
| 416 | $db_name =~ tr/-/_/; | ||
| 417 | $db_name =~ s/\_{2,}/_/g; | ||
| 418 | $db_name = join( '', map( ucfirst( lc( $_ ) ), split( /\_/, $db_name ) ) ); | ||
| 419 | my $name = $self->name; | ||
| 420 | my $new_class = $name; | ||
| 421 | $new_class =~ tr/-/_/; | ||
| 422 | $new_class =~ s/\_{2,}/_/g; | ||
| 423 | $new_class = join( '', map( ucfirst( lc( $_ ) ), split( /\_/, $new_class ) ) ); | ||
| 424 | my $class = ref( $self ) . "\::${db_name}\::${new_class}"; | ||
| 425 | if( !$self->_is_class_loaded( $class ) ) | ||
| 426 | { | ||
| 427 | my $perl = <<EOT; | ||
| 428 | package $class; | ||
| 429 | BEGIN | ||
| 430 | { | ||
| 431 | use strict; | ||
| 432 | use parent qw( DB::Object::Fields ); | ||
| 433 | }; | ||
| 434 | |||
| 435 | 1; | ||
| 436 | |||
| 437 | EOT | ||
| 438 | my $rc = eval( $perl ); | ||
| 439 | die( "Unable to dynamically create module $class: $@" ) if( $@ ); | ||
| 440 | } | ||
| 441 | else | ||
| 442 | { | ||
| 443 | # Class already exists | ||
| 444 | } | ||
| 445 | $o = $class->new( | ||
| 446 | prefixed => $self->{prefixed}, | ||
| 447 | # For table alias | ||
| 448 | # query_object => $self->query_object, | ||
| 449 | # query_object => $qo, | ||
| 450 | table_object => $self, | ||
| 451 | debug => $self->debug, | ||
| 452 | ); | ||
| 453 | $o->prefixed( $self->{prefixed} ); | ||
| 454 | $self->{fields_object} = $o; | ||
| 455 | return( $o ); | ||
| 456 | } | ||
| 457 | |||
| 458 | sub fo { return( shift->fields_object( @_ ) ); } | ||
| 459 | |||
| 460 | sub foreign { return( shift->_set_get_hash_as_mix_object( 'foreign', @_ ) ); } | ||
| 461 | |||
| 462 | sub format_statement($;\%\%@) { return( shift->_method_to_query( 'format_statement', @_ ) ); } | ||
| 463 | |||
| 464 | sub format_update($;%) { return( shift->_method_to_query( 'format_update', @_ ) ); } | ||
| 465 | |||
| 466 | sub from_unixtime { return( shift->_method_to_query( 'from_unixtime', @_ ) ); } | ||
| 467 | |||
| 468 | sub get_query_object { return( shift->_reset_query ); } | ||
| 469 | |||
| 470 | sub group { return( shift->_method_to_query( 'group', @_ ) ); } | ||
| 471 | |||
| 472 | sub having { return( shift->_method_to_query( 'having', @_ ) ); } | ||
| 473 | |||
| 474 | # sub indexes { return( shift->_set_get_class_array_object( 'indexes', { | ||
| 475 | # is_primary => { type => 'boolean' }, | ||
| 476 | # is_unique => { type => 'boolean' }, | ||
| 477 | # fields => { type => 'array_as_object' }, | ||
| 478 | # }, @_ ) ); } | ||
| 479 | sub indexes { return( shift->_set_get_hash_as_mix_object( 'indexes', @_ ) ); } | ||
| 480 | |||
| 481 | sub insert | ||
| 482 | { | ||
| 483 | my $self = shift( @_ ); | ||
| 484 | my $q = $self->_reset_query; | ||
| 485 | # If the user wants to execute this, then we reset the query, | ||
| 486 | # but if the user wants to call other methods chained like as_string we don't do anything | ||
| 487 | # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) ); | ||
| 488 | if( want('VOID') || want('OBJECT') ) | ||
| 489 | { | ||
| 490 | CORE::delete( $self->{query_reset} ) if( want('VOID') ); | ||
| 491 | # return( $q->select( @_ ) ); | ||
| 492 | # return( $q->select( @_ ) ) if( !defined( wantarray() ) ); | ||
| 493 | return( $q->insert( @_ ) ); | ||
| 494 | } | ||
| 495 | # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) ); | ||
| 496 | # return( $q->insert( @_ ) ) if( !defined( wantarray() ) ); | ||
| 497 | if( wantarray() ) | ||
| 498 | { | ||
| 499 | my( @val ) = $q->insert( @_ ) || return( $self->pass_error( $q->error ) ); | ||
| 500 | $self->reset; | ||
| 501 | return( @val ); | ||
| 502 | } | ||
| 503 | else | ||
| 504 | { | ||
| 505 | my $val = $q->insert( @_ ) || return( $self->pass_error( $q->error ) ); | ||
| 506 | $self->reset; | ||
| 507 | return( $val ); | ||
| 508 | } | ||
| 509 | } | ||
| 510 | |||
| 511 | sub limit { return( shift->_method_to_query( 'limit', @_ ) ); } | ||
| 512 | |||
| 513 | sub local { return( shift->_method_to_query( 'local', @_ ) ); } | ||
| 514 | |||
| 515 | sub lock | ||
| 516 | { | ||
| 517 | my $self = shift( @_ ); | ||
| 518 | my $class = ref( $self ); | ||
| 519 | return( $self->error( "lock() is not implemented by $class." ) ); | ||
| 520 | } | ||
| 521 | |||
| 522 | sub name | ||
| 523 | { | ||
| 524 | # Read-only | ||
| 525 | return( shift->{table} ); | ||
| 526 | } | ||
| 527 | |||
| 528 | sub new_check | ||
| 529 | { | ||
| 530 | my $self = shift( @_ ); | ||
| 531 | my $args = $self->_get_args_as_hash( @_ ); | ||
| 532 | $self->_load_class( 'DB::Object::Constraint::Check' ) || | ||
| 533 | return( $self->pass_error ); | ||
| 534 | $args->{debug} = $self->debug if( !CORE::exists( $args->{debug} ) || !defined( $args->{debug} ) ); | ||
| 535 | my $this = DB::Object::Constraint::Check->new( %$args ) || | ||
| 536 | return( $self->pass_error( DB::Object::Constraint::Check->error ) ); | ||
| 537 | return( $this ); | ||
| 538 | } | ||
| 539 | |||
| 540 | sub new_foreign | ||
| 541 | { | ||
| 542 | my $self = shift( @_ ); | ||
| 543 | my $args = $self->_get_args_as_hash( @_ ); | ||
| 544 | $self->_load_class( 'DB::Object::Constraint::Foreign' ) || | ||
| 545 | return( $self->pass_error ); | ||
| 546 | $args->{debug} = $self->debug if( !CORE::exists( $args->{debug} ) || !defined( $args->{debug} ) ); | ||
| 547 | my $this = DB::Object::Constraint::Foreign->new( %$args ) || | ||
| 548 | return( $self->pass_error( DB::Object::Constraint::Foreign->error ) ); | ||
| 549 | return( $this ); | ||
| 550 | } | ||
| 551 | |||
| 552 | sub new_index | ||
| 553 | { | ||
| 554 | my $self = shift( @_ ); | ||
| 555 | my $args = $self->_get_args_as_hash( @_ ); | ||
| 556 | $self->_load_class( 'DB::Object::Constraint::Index' ) || | ||
| 557 | return( $self->pass_error ); | ||
| 558 | $args->{debug} = $self->debug if( !CORE::exists( $args->{debug} ) || !defined( $args->{debug} ) ); | ||
| 559 | my $this = DB::Object::Constraint::Index->new( %$args ) || | ||
| 560 | return( $self->pass_error( DB::Object::Constraint::Index->error ) ); | ||
| 561 | return( $this ); | ||
| 562 | } | ||
| 563 | |||
| 564 | sub no_bind { return( shift->_set_get_boolean({ | ||
| 565 | field => 'no_bind', | ||
| 566 | callbacks => | ||
| 567 | { | ||
| 568 | set => sub | ||
| 569 | { | ||
| 570 | my $self = shift( @_ ); | ||
| 571 | my $val = shift( @_ ); | ||
| 572 | return if( !defined( $val ) ); | ||
| 573 | return( $val ) if( !$val ); | ||
| 574 | my $q = $self->_reset_query; | ||
| 575 | my $where = $q->where(); | ||
| 576 | my $group = $q->group(); | ||
| 577 | my $order = $q->order(); | ||
| 578 | my $limit = $q->limit(); | ||
| 579 | my $binded_where = $q->binded_where; | ||
| 580 | my $binded_group = $q->binded_group; | ||
| 581 | my $binded_order = $q->binded_order; | ||
| 582 | my $binded_limit = $q->binded_limit; | ||
| 583 | # Replace the placeholders by their corresponding value | ||
| 584 | # and have them re-processed by their corresponding method | ||
| 585 | if( $where && @$binded_where ) | ||
| 586 | { | ||
| 587 | $where =~ s/(=\s*\?)/"='" . quotemeta( $binded_where->[ $#+ ] ) . "'"/ge; | ||
| 588 | $self->where( $where ); | ||
| 589 | } | ||
| 590 | if( $group && @$binded_group ) | ||
| 591 | { | ||
| 592 | $group =~ s/(=\s*\?)/"='" . quotemeta( $binded_group->[ $#+ ] ) . "'"/ge; | ||
| 593 | $self->group( $group ); | ||
| 594 | } | ||
| 595 | if( $order && @$binded_order ) | ||
| 596 | { | ||
| 597 | $order =~ s/(=\s*\?)/"='" . quotemeta( $binded_order->[ $#+ ] ) . "'"/ge; | ||
| 598 | $self->order( $order ); | ||
| 599 | } | ||
| 600 | if( $limit && @$binded_limit ) | ||
| 601 | { | ||
| 602 | # $limit =~ s/(=\s*\?)/"='" . quotemeta( $binded_limit[ $#+ ] ) . "'"/ge; | ||
| 603 | $self->limit( @$binded_limit ); | ||
| 604 | } | ||
| 605 | $q->reset_bind; | ||
| 606 | return( $val ); | ||
| 607 | }, | ||
| 608 | } | ||
| 609 | }, @_ ) ); } | ||
| 610 | |||
| 611 | sub null | ||
| 612 | { | ||
| 613 | my $self = shift( @_ ); | ||
| 614 | $self->structure || return( $self->pass_error ); | ||
| 615 | my $fields = $self->fields; | ||
| 616 | # my $null = $self->{null}; | ||
| 617 | my $null = +{ map{ $_ => ( $fields->{ $_ }->is_nullable ? 1 : 0 ) } keys( %$fields ) }; | ||
| 618 | return( $self->_clone( $null ) ); | ||
| 619 | } | ||
| 620 | |||
| 621 | sub on_conflict { return( shift->error( "The on conflict clause is not supported by this driver." ) ); } | ||
| 622 | |||
| 623 | sub optimize | ||
| 624 | { | ||
| 625 | my $self = shift( @_ ); | ||
| 626 | my $class = ref( $self ); | ||
| 627 | return( $self->error( "optimize() is not implemented by $class." ) ); | ||
| 628 | } | ||
| 629 | |||
| 630 | sub order { return( shift->_method_to_query( 'order', @_ ) ); } | ||
| 631 | |||
| 632 | sub parent { return( shift->error( "The table parent() method is not supported by this driver." ) ); } | ||
| 633 | |||
| 634 | sub prefix | ||
| 635 | { | ||
| 636 | my $self = shift( @_ ); | ||
| 637 | my @val = (); | ||
| 638 | my $alias = $self->query_object->table_alias; | ||
| 639 | return( $alias ) if( $alias && $self->{prefixed} > 0 ); | ||
| 640 | CORE::push( @val, $self->database_object->database ) if( $self->{prefixed} > 2 ); | ||
| 641 | CORE::push( @val, $self->schema ) if( $self->{prefixed} > 1 && $self->schema ); | ||
| 642 | CORE::push( @val, $self->name ) if( $self->{prefixed} > 0 ); | ||
| 643 | return( '' ) if( !scalar( @val ) ); | ||
| 644 | return( CORE::join( '.', @val ) ); | ||
| 645 | } | ||
| 646 | |||
| 647 | sub prefix_database { return( shift->{prefixed} > 2 ); } | ||
| 648 | |||
| 649 | sub prefix_schema { return( shift->{prefixed} > 1 ); } | ||
| 650 | |||
| 651 | sub prefix_table { return( shift->{prefixed} > 0 ); } | ||
| 652 | |||
| 653 | # This the prefix intended for field in query | ||
| 654 | sub prefixed | ||
| 655 | { | ||
| 656 | my $self = shift( @_ ); | ||
| 657 | if( @_ ) | ||
| 658 | { | ||
| 659 | $self->{prefixed} = ( $_[0] =~ /^\d+$/ ? $_[0] : ( $_[0] ? 1 : 0 ) ); | ||
| 660 | } | ||
| 661 | else | ||
| 662 | { | ||
| 663 | $self->{prefixed} = 1; | ||
| 664 | } | ||
| 665 | my $fo = $self->{fields_object}; | ||
| 666 | $fo->prefixed( $self->{prefixed} ) if( $fo ); | ||
| 667 | # return( want( 'OBJECT' ) ? $self : $self->{prefixed} ); | ||
| 668 | return( $self->{prefixed} ); | ||
| 669 | } | ||
| 670 | |||
| 671 | sub primary | ||
| 672 | { | ||
| 673 | my $self = shift( @_ ); | ||
| 674 | $self->structure || return( $self->pass_error ); | ||
| 675 | my $primary = $self->{primary}; | ||
| 676 | # return( wantarray() ? () : undef() ) if( !$primary || !@$primary ); | ||
| 677 | # return( wantarray() ? @$primary : \@$primary ); | ||
| 678 | return( $self->_clone( $primary ) ); | ||
| 679 | } | ||
| 680 | |||
| 681 | # In PostgreSQL, Oracle, SQL server this would be schema_name.table_name | ||
| 682 | sub qualified_name { return( shift->name ); } | ||
| 683 | |||
| 684 | sub query_object { return( shift->_set_get_object_without_init( 'query_object', 'DB::Object::Query', @_ ) ); } | ||
| 685 | |||
| 686 | sub query_reset { return( shift->_set_get_scalar( 'query_reset', @_ ) ); } | ||
| 687 | |||
| 688 | sub rename | ||
| 689 | { | ||
| 690 | my $self = shift( @_ ); | ||
| 691 | my $class = ref( $self ); | ||
| 692 | return( $self->error( "rename() is not implemented by $class." ) ); | ||
| 693 | } | ||
| 694 | |||
| 695 | sub repair | ||
| 696 | { | ||
| 697 | my $self = shift( @_ ); | ||
| 698 | my $class = ref( $self ); | ||
| 699 | return( $self->error( "repair() is not implemented by $class." ) ); | ||
| 700 | } | ||
| 701 | |||
| 702 | sub replace | ||
| 703 | { | ||
| 704 | my $self = shift( @_ ); | ||
| 705 | my $q = $self->_reset_query; | ||
| 706 | # If the user wants to execute this, then we reset the query, | ||
| 707 | # but if the user wants to call other methods chained like as_string we don't do anything | ||
| 708 | # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) ); | ||
| 709 | if( want('VOID') || want('OBJECT') ) | ||
| 710 | { | ||
| 711 | CORE::delete( $self->{query_reset} ) if( want('VOID') ); | ||
| 712 | # return( $q->select( @_ ) ); | ||
| 713 | # return( $q->select( @_ ) ) if( !defined( wantarray() ) ); | ||
| 714 | return( $q->replace( @_ ) ); | ||
| 715 | } | ||
| 716 | # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) ); | ||
| 717 | # return( $q->replace( @_ ) ); | ||
| 718 | # return( $q->replace( @_ ) ) if( !defined( wantarray() ) ); | ||
| 719 | if( wantarray() ) | ||
| 720 | { | ||
| 721 | my( @val ) = $q->replace( @_ ) || return( $self->pass_error( $q->error ) ); | ||
| 722 | return( @val ); | ||
| 723 | } | ||
| 724 | else | ||
| 725 | { | ||
| 726 | my $val = $q->replace( @_ ) || return( $self->pass_error( $q->error ) ); | ||
| 727 | return( $val ); | ||
| 728 | } | ||
| 729 | } | ||
| 730 | |||
| 731 | sub reset | ||
| 732 | { | ||
| 733 | my $self = shift( @_ ); | ||
| 734 | CORE::delete( $self->{query_reset} ); | ||
| 735 | $self->_reset_query( @_ ) || return( $self->pass_error ); | ||
| 736 | CORE::delete( $self->{fields_object} ); | ||
| 737 | # To allow chaining of commands | ||
| 738 | return( $self ); | ||
| 739 | } | ||
| 740 | |||
| 741 | sub reset_structure | ||
| 742 | { | ||
| 743 | my $self = shift( @_ ); | ||
| 744 | if( !CORE::length( $self->{_reset_structure} // '' ) && scalar( @_ ) ) | ||
| 745 | { | ||
| 746 | $self->{_reset_structure} = scalar( @_ ); | ||
| 747 | } | ||
| 748 | return( $self ); | ||
| 749 | } | ||
| 750 | |||
| 751 | # Modelled after PostgreSQL and available since 3.35.0 released 2021-03-12 | ||
| 752 | # <https://www.sqlite.org/lang_returning.html> | ||
| 753 | sub returning { return( shift->_method_to_query( 'returning', @_ ) ); } | ||
| 754 | |||
| 755 | sub reverse | ||
| 756 | { | ||
| 757 | my $self = shift( @_ ); | ||
| 758 | if( @_ ) | ||
| 759 | { | ||
| 760 | my $q = $self->_reset_query; | ||
| 761 | $self->{reverse} = shift( @_ ); | ||
| 762 | $q->reverse( $self->{reverse} ); | ||
| 763 | } | ||
| 764 | elsif( Wanted::want( 'VOID' ) ) | ||
| 765 | { | ||
| 766 | my $q = $self->_reset_query; | ||
| 767 | $self->{reverse}++; | ||
| 768 | $q->reverse( $self->{reverse} ); | ||
| 769 | } | ||
| 770 | return( $self->{reverse} ); | ||
| 771 | } | ||
| 772 | |||
| 773 | sub schema { return( shift->_set_get_scalar( 'schema', @_ ) ); } | ||
| 774 | |||
| 775 | sub select | ||
| 776 | { | ||
| 777 | my $self = shift( @_ ); | ||
| 778 | my $q = $self->_reset_query; | ||
| 779 | # If the user wants to execute this, then we reset the query, | ||
| 780 | # but if the user wants to call other methods chained like as_string we don't do anything | ||
| 781 | # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) ); | ||
| 782 | if( want('VOID') || want('OBJECT') ) | ||
| 783 | { | ||
| 784 | CORE::delete( $self->{query_reset} ) if( want('VOID') ); | ||
| 785 | # return( $q->select( @_ ) ); | ||
| 786 | # return( $q->select( @_ ) ) if( !defined( wantarray() ) ); | ||
| 787 | return( $q->select( @_ ) ); | ||
| 788 | } | ||
| 789 | |||
| 790 | if( wantarray() ) | ||
| 791 | { | ||
| 792 | my( @val ) = $q->select( @_ ) || return( $self->pass_error( $q->error ) ); | ||
| 793 | # a statement handler is returned and we reset the query so that other calls would not use the previous DB::Object::Query object | ||
| 794 | $self->reset; | ||
| 795 | return( @val ); | ||
| 796 | } | ||
| 797 | else | ||
| 798 | { | ||
| 799 | my $val = $q->select( @_ ) || return( $self->pass_error( $q->error ) ); | ||
| 800 | $self->reset; | ||
| 801 | return( $val ); | ||
| 802 | } | ||
| 803 | } | ||
| 804 | |||
| 805 | sub sort | ||
| 806 | { | ||
| 807 | my $self = shift( @_ ); | ||
| 808 | if( @_ ) | ||
| 809 | { | ||
| 810 | my $q = $self->_reset_query; | ||
| 811 | my $v = shift( @_ ); | ||
| 812 | $self->{reverse} = ( ( defined( $v ) && $v > 0 ) ? 0 : 1 ); | ||
| 813 | $q->sort( $self->{reverse} ); | ||
| 814 | } | ||
| 815 | elsif( Wanted::want( 'VOID' ) ) | ||
| 816 | { | ||
| 817 | my $q = $self->_reset_query; | ||
| 818 | $self->{reverse} = 0; | ||
| 819 | $q->sort( $self->{reverse} ); | ||
| 820 | } | ||
| 821 | return( $self->{reverse} ); | ||
| 822 | } | ||
| 823 | |||
| 824 | sub stat | ||
| 825 | { | ||
| 826 | my $self = shift( @_ ); | ||
| 827 | my $class = ref( $self ); | ||
| 828 | return( $self->error( "stat() is not implemented by $class." ) ); | ||
| 829 | } | ||
| 830 | |||
| 831 | # sub structure must be superseded by sub classes | ||
| 832 | sub structure | ||
| 833 | { | ||
| 834 | my $self = shift( @_ ); | ||
| 835 | my $class = ref( $self ); | ||
| 836 | return( $self->error( "structure() is not implemented by $class." ) ); | ||
| 837 | } | ||
| 838 | |||
| 839 | sub table { return( shift->{table} ); } | ||
| 840 | |||
| 841 | sub tie | ||
| 842 | { | ||
| 843 | my $self = shift( @_ ); | ||
| 844 | my $q = $self->_reset_query; | ||
| 845 | return( $q->tie( @_ ) ); | ||
| 846 | } | ||
| 847 | |||
| 848 | sub type { return( shift->_set_get_scalar( 'type', @_ ) ); } | ||
| 849 | |||
| 850 | sub types | ||
| 851 | { | ||
| 852 | my $self = shift( @_ ); | ||
| 853 | $self->structure || return( $self->pass_error ); | ||
| 854 | # my $types = $self->{types}; | ||
| 855 | my $fields = $self->fields; | ||
| 856 | my $types = +{ map{ $_ => $fields->{ $_ }->type } keys( %$fields ) }; | ||
| 857 | # return( $self->_clone( $types ) ); | ||
| 858 | return( $types ); | ||
| 859 | } | ||
| 860 | |||
| 861 | sub types_const | ||
| 862 | { | ||
| 863 | my $self = shift( @_ ); | ||
| 864 | $self->structure || return( $self->pass_error ); | ||
| 865 | my $fields = $self->fields; | ||
| 866 | # my $types = $self->{types_const}; | ||
| 867 | my $types = +{ map{ $_ => $fields->{ $_ }->datatype } keys( %$fields ) }; | ||
| 868 | # return( $self->_clone( $types ) ); | ||
| 869 | return( $types ); | ||
| 870 | } | ||
| 871 | |||
| 872 | sub unlock | ||
| 873 | { | ||
| 874 | my $self = shift( @_ ); | ||
| 875 | my $class = ref( $self ); | ||
| 876 | return( $self->error( "unlock() is not implemented by $class." ) ); | ||
| 877 | } | ||
| 878 | |||
| 879 | sub unix_timestamp { return( shift->_method_to_query( 'unix_timestamp', @_ ) ); } | ||
| 880 | |||
| 881 | sub update | ||
| 882 | { | ||
| 883 | my $self = shift( @_ ); | ||
| 884 | my $q = $self->_reset_query; | ||
| 885 | # If the user wants to execute this, then we reset the query, | ||
| 886 | # but if the user wants to call other methods chained like as_string we don't do anything | ||
| 887 | # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) ); | ||
| 888 | if( want('VOID') || want('OBJECT') ) | ||
| 889 | { | ||
| 890 | CORE::delete( $self->{query_reset} ) if( want('VOID') ); | ||
| 891 | # return( $q->select( @_ ) ); | ||
| 892 | # return( $q->select( @_ ) ) if( !defined( wantarray() ) ); | ||
| 893 | return( $q->update( @_ ) ); | ||
| 894 | } | ||
| 895 | # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) ); | ||
| 896 | # return( $q->update( @_ ) ); | ||
| 897 | # return( $q->update( @_ ) ) if( !defined( wantarray() ) ); | ||
| 898 | if( wantarray() ) | ||
| 899 | { | ||
| 900 | my( @val ) = $q->update( @_ ) || return( $self->pass_error( $q->error ) ); | ||
| 901 | $self->reset; | ||
| 902 | return( @val ); | ||
| 903 | } | ||
| 904 | else | ||
| 905 | { | ||
| 906 | my $val = $q->update( @_ ) || return( $self->pass_error( $q->error ) ); | ||
| 907 | $self->reset; | ||
| 908 | return( $val ); | ||
| 909 | } | ||
| 910 | } | ||
| 911 | |||
| 912 | sub where { return( shift->_method_to_query( 'where', @_ ) ); } | ||
| 913 | |||
| 914 | sub _method_to_query | ||
| 915 | { | ||
| 916 | my $self = shift( @_ ); | ||
| 917 | my $meth = shift( @_ ); | ||
| 918 | my $q = $self->_reset_query; | ||
| 919 | my $code = $q->can( $meth ) || return( $self->error( "Query class '", ref( $q ), "' has no method '$meth'." ) ); | ||
| 920 | my $rv = $code->( $q, @_ ); | ||
| 921 | return( $self->pass_error( $q->error ) ) if( !defined( $rv ) && $q->error ); | ||
| 922 | return( $rv ); | ||
| 923 | } | ||
| 924 | |||
| 925 | sub AUTOLOAD | ||
| 926 | { | ||
| 927 | my( $method ) = our $AUTOLOAD =~ /([^:]+)$/; | ||
| 928 | no overloading; | ||
| 929 | my $self = shift( @_ ); | ||
| 930 | my $fields = $self->fields; | ||
| 931 | # User called a field on a table object, instead of using the method fields_object or its shortcut 'fo' | ||
| 932 | if( CORE::exists( $fields->{ $method } ) ) | ||
| 933 | { | ||
| 934 | warn( "You have called a field name '$method' using a table object. This practice is discouraged, although it works for now. Best to use something like: \$tbl->fo->$method rather than just \$tbl->$method" ) if( $self->_is_warnings_enabled( 'DB::Object' ) ); | ||
| 935 | return( $self->fields_object->_initiate_field_object( $method ) ); | ||
| 936 | } | ||
| 937 | else | ||
| 938 | { | ||
| 939 | warn( "You called table '", $self->name, "' object \$tbl->$method, but no such method exist.\n" ) if( $self->_is_warnings_enabled( 'DB::Object' ) ); | ||
| 940 | return( $self->error( "You called table '", $self->name, "' object \$tbl->$method, but no such method exist." ) ); | ||
| 941 | } | ||
| 942 | }; | ||
| 943 | |||
| 944 | # NOTE: DESTROY | ||
| 945 | sub DESTROY | ||
| 946 | { | ||
| 947 | # Do nothing | ||
| 948 | # DB::Object::Tables are never destroyed. | ||
| 949 | # They are just gateway to tables, and they are cached by DB::Object::table() | ||
| 950 | # print( STDERR "DESTROY'ing table $self ($self->{ 'table' })\n" ); | ||
| 951 | }; | ||
| 952 | |||
| 953 | # NOTE: For CBOR and Sereal | ||
| 954 | sub FREEZE | ||
| 955 | { | ||
| 956 | my $self = CORE::shift( @_ ); | ||
| 957 | my $serialiser = CORE::shift( @_ ) // ''; | ||
| 958 | my $class = CORE::ref( $self ); | ||
| 959 | my @props = qw( | ||
| 960 | check fields fields_object foreign indexes parent prefixed primary query_object | ||
| 961 | query_reset reverse schema structure table table_alias type types_const | ||
| 962 | _cache_structure | ||
| 963 | ); | ||
| 964 | my $hash = {}; | ||
| 965 | foreach my $prop ( @props ) | ||
| 966 | { | ||
| 967 | if( CORE::exists( $self->{ $prop } ) && | ||
| 968 | defined( $self->{ $prop } ) && | ||
| 969 | # Because we cannot reliably freeze code reference, and we do not usually need to. | ||
| 970 | CORE::ref( $self->{ $prop } ) ne 'CODE' ) | ||
| 971 | { | ||
| 972 | $hash->{ $prop } = $self->{ $prop }; | ||
| 973 | } | ||
| 974 | } | ||
| 975 | $hash->{_serial_version} = $DB::Object::SERIALISATION_VERSION; | ||
| 976 | # Return an array reference rather than a list so this works with Sereal and CBOR | ||
| 977 | # Before Sereal version 4.023, Sereal did not support multiple values returned | ||
| 978 | if( $serialiser eq 'Sereal' ) | ||
| 979 | { | ||
| 980 | require Sereal::Encoder; | ||
| 981 | require version; | ||
| 982 | |||
| 983 | if( version->parse( Sereal::Encoder->VERSION ) < version->parse( '4.023' ) ) | ||
| 984 | { | ||
| 985 | CORE::return( [$class, $hash] ); | ||
| 986 | } | ||
| 987 | } | ||
| 988 | # But Storable want a list with the first element being the serialised element | ||
| 989 | CORE::return( $class, $hash ); | ||
| 990 | } | ||
| 991 | |||
| 992 | sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); } | ||
| 993 | |||
| 994 | sub STORABLE_thaw { return( shift->THAW( @_ ) ); } | ||
| 995 | |||
| 996 | sub STORABLE_thaw_post_processing | ||
| 997 | { | ||
| 998 | my $obj = shift( @_ ); | ||
| 999 | my @keys = %$obj; | ||
| 1000 | my $class = ref( $obj ); | ||
| 1001 | my $hash = {}; | ||
| 1002 | @$hash{ @keys } = @$obj{ @keys }; | ||
| 1003 | my $self = bless( $hash => $class ); | ||
| 1004 | if( $self->{query_object} ) | ||
| 1005 | { | ||
| 1006 | $self->{query_object}->attach( $self ); | ||
| 1007 | } | ||
| 1008 | return( $self ); | ||
| 1009 | } | ||
| 1010 | |||
| 1011 | # NOTE: CBOR will call the THAW method with the stored classname as first argument, the constant string CBOR as second argument, and all values returned by FREEZE as remaining arguments. | ||
| 1012 | # NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze | ||
| 1013 | sub THAW | ||
| 1014 | { | ||
| 1015 | # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue | ||
| 1016 | # 'CBOR' as the second value. | ||
| 1017 | my( $self, undef, @args ) = @_; | ||
| 1018 | my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args; | ||
| 1019 | my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self ); | ||
| 1020 | my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {}; | ||
| 1021 | if( $class !~ /\A[A-Za-z_]\w*(?:::\w+)*\z/ ) | ||
| 1022 | { | ||
| 1023 | die( "THAW: Invalid class name '$class'" ); | ||
| 1024 | } | ||
| 1025 | |||
| 1026 | my $sv = CORE::delete( $hash->{_serial_version} ) // 0; | ||
| 1027 | if( $sv != $DB::Object::SERIALISATION_VERSION ) | ||
| 1028 | { | ||
| 1029 | require( $EXCEPTION_CLASS ); | ||
| 1030 | die( $EXCEPTION_CLASS->new({ | ||
| 1031 | code => 409, # Conflict | ||
| 1032 | message => sprintf( | ||
| 1033 | "Serialisation version mismatch in %s: expected %d, got %d. Please purge the cache.", | ||
| 1034 | $class, | ||
| 1035 | $DB::Object::SERIALISATION_VERSION, | ||
| 1036 | $sv, | ||
| 1037 | ) | ||
| 1038 | }) ); | ||
| 1039 | } | ||
| 1040 | |||
| 1041 | # Since this class is inherited, we also need to load from which we depend, if necessary. | ||
| 1042 | if( $class ne 'DB::Object::Tables' ) | ||
| 1043 | { | ||
| 1044 | require DB::Object; | ||
| 1045 | require DB::Object::Tables; | ||
| 1046 | my @supported_classes = CORE::values( %$DB::Object::DRIVER2PACK ); | ||
| 1047 | push( @supported_classes, 'DB::Object' ); | ||
| 1048 | my $ok_classes = CORE::join( '|', CORE::map{ CORE::quotemeta( $_ ) } @supported_classes ); | ||
| 1049 | my $base_class = ( $class =~ /^($ok_classes)/ )[0]; | ||
| 1050 | eval( "require $base_class;" ); | ||
| 1051 | if( $@ ) | ||
| 1052 | { | ||
| 1053 | require( $EXCEPTION_CLASS ); | ||
| 1054 | die( $EXCEPTION_CLASS->new( "Failure to load $base_class: $@" ) ); | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | eval( "require $class;" ); | ||
| 1058 | if( $@ ) | ||
| 1059 | { | ||
| 1060 | require( $EXCEPTION_CLASS ); | ||
| 1061 | die( $EXCEPTION_CLASS->new( "Failure to load $class: $@" ) ); | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | # If inheritance is still missing (or incomplete) for any reason, repair it explicitly. | ||
| 1065 | no strict 'refs'; | ||
| 1066 | |||
| 1067 | my $stash = \%{"${class}\::"}; | ||
| 1068 | my $isa_ref; | ||
| 1069 | # We check if the 'ISA' symbol exists in the stash and its ARRAY slot is defined | ||
| 1070 | if( exists( $stash->{ISA} ) && | ||
| 1071 | defined( *{"${class}\::ISA"}{ARRAY} ) ) | ||
| 1072 | { | ||
| 1073 | $isa_ref = *{"${class}\::ISA"}{ARRAY}; | ||
| 1074 | } | ||
| 1075 | else | ||
| 1076 | { | ||
| 1077 | # # Force creation of @ISA array slot | ||
| 1078 | # @{"${class}::ISA"} = (); | ||
| 1079 | # $isa_ref = *{"${class}::ISA"}{ARRAY}; | ||
| 1080 | # But I want it to die, so this can be dealt with, and not swept under the rug. | ||
| 1081 | die( "Unable to find the \@ISA value in $class. The package contains the following stashes:\n", $self->Module::Generic::dump( $stash ) ); | ||
| 1082 | } | ||
| 1083 | |||
| 1084 | my $has_tble = 0; | ||
| 1085 | foreach my $p ( @$isa_ref ) | ||
| 1086 | { | ||
| 1087 | # Same as what we can find in modules DB::Object::(Postgres|SQLite|Mysql)::Tables | ||
| 1088 | $has_tble = 1 if( CORE::defined( $p ) && $p eq 'DB::Object::Tables' ); | ||
| 1089 | } | ||
| 1090 | |||
| 1091 | if( !$has_tble ) | ||
| 1092 | { | ||
| 1093 | @$isa_ref = ( 'DB::Object::Tables' ); | ||
| 1094 | } | ||
| 1095 | } | ||
| 1096 | my $new; | ||
| 1097 | # Storable pattern requires to modify the object it created rather than returning a new one | ||
| 1098 | if( CORE::ref( $self ) ) | ||
| 1099 | { | ||
| 1100 | foreach( CORE::keys( %$hash ) ) | ||
| 1101 | { | ||
| 1102 | $self->{ $_ } = CORE::delete( $hash->{ $_ } ); | ||
| 1103 | } | ||
| 1104 | $new = $self; | ||
| 1105 | } | ||
| 1106 | else | ||
| 1107 | { | ||
| 1108 | $new = CORE::bless( $hash => $class ); | ||
| 1109 | } | ||
| 1110 | CORE::return( $new ); | ||
| 1111 | } | ||
| 1112 | |||
| 1113 | 1; | ||
| 1114 | # NOTE: POD | ||
| 1115 | __END__ |