blib/lib/DB/Object/Tables.pm untested

filter: Statementstmt 0.0% 0 / 538 Branchbran 0.0% 0 / 158 Conditioncond 0.0% 0 / 233 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 102 Podpod 0.0% 0 / 85 totaltotal 0.0% 0 / 1116 CC 80 SCAR 33.5
CC
80
Cov
0%
THAW
210.0
no_bind
56.0
attach
42.0
CRAP 28.6
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## Database Object Interface - ~/lib/DB/Object/Tables.pm
4 ## Version v1.2.1
5 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2017/07/19
8 ## Modified 2026/03/27
9 ## All rights reserved
10 ##
11 ##
12 ## This program is free software; you can redistribute it and/or modify it
13 ## under the same terms as Perl itself.
14 ##----------------------------------------------------------------------------
15 # This package's purpose is to separate the object of the tables from the main
16 # DB::Object package so that when they get DESTROY'ed, it does not interrupt
17 # the SQL connection
18 ##----------------------------------------------------------------------------
19 package DB::Object::Tables;
20 BEGIN
21 {
22 use strict;
23 use warnings;
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.1';
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  
333 sub exists
334 {
335 my $self = shift( @_ );
336 my $class = ref( $self );
337 return( $self->error( "exists() is not implemented by $class." ) );
338 }
339  
340 sub fields
341 {
342 my $self = shift( @_ );
343 # 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.
344 $self->structure || return( $self->pass_error );
345 my $fields = $self->{fields};
346 if( @_ )
347 {
348 my $field = shift( @_ );
349 return( $self->error( "No field name was provided." ) ) if( !CORE::length( $field // '' ) );
350 my $obj = $fields->{ $field } || return( $self->error( "No field object found for \"", ( $field // 'undef' ), "\"." ) );
351 # NOTE: We return the canonical Field object directly rather than cloning it.
352 # The immediate caller (_initiate_field_object) overwrites table_object,
353 # query_object, prefixed, and debug right after this call, so a full Clone::clone
354 # of all the field data attributes (name, type, pos, is_nullable, etc.) is wasteful.
355 # The data attributes are read-only after structure() populates them.
356 return( $obj );
357 }
358 # return( +{ map{ $_ => $fields->{ $_ }->clone } keys( %$fields ) } );
359 my $ref = {};
360 # NOTE: Return the canonical Field objects directly rather than cloning each one.
361 # Fields are populated once by structure() and their data attributes (name, type,
362 # pos, is_nullable, etc.) are never modified after that. The mutable attributes
363 # (table_object, query_object, prefixed, debug) are always overwritten by the caller.
364 foreach my $f ( keys( %$fields ) )
365 {
366 $fields->{ $f }->debug( $self->debug );
367 $ref->{ $f } = $fields->{ $f };
368 }
369 return( $ref );
370 }
371  
372 sub fields_as_array
373 {
374 my $self = shift( @_ );
375 $self->structure || return( $self->pass_error );
376 my $fields = $self->{fields};
377 return( $self->new_array( [sort{ $fields->{ $a }->pos <=> $fields->{ $b }->pos } keys( %$fields )] ) );
378 }
379  
380 sub field_exists
381 {
382 my $self = shift( @_ );
383 my $field = shift( @_ ) || return( $self->error( "No field was provided." ) );
384 $self->structure || return( $self->pass_error );
385 my $fields = $self->{fields};
386 return( CORE::exists( $fields->{ $field } ) );
387 }
388  
389 sub fields_object
390 {
391 my $self = shift( @_ );
392 my $o = $self->{fields_object};
393 # This will make sure we have a query object which DB::Object::Fields and DB::Object::Field need
394 $self->_reset_query;
395 my $qo = $self->query_object;
396 $qo->table_object( $self );
397 if( $o && $self->_is_object( $o ) )
398 {
399 $o->prefixed( $self->{prefixed} );
400 $o->query_object( $qo );
401 return( $o );
402 }
403 my $db_name = $self->database_object->database;
404 $db_name =~ tr/-/_/;
405 $db_name =~ s/\_{2,}/_/g;
406 $db_name = join( '', map( ucfirst( lc( $_ ) ), split( /\_/, $db_name ) ) );
407 my $name = $self->name;
408 my $new_class = $name;
409 $new_class =~ tr/-/_/;
410 $new_class =~ s/\_{2,}/_/g;
411 $new_class = join( '', map( ucfirst( lc( $_ ) ), split( /\_/, $new_class ) ) );
412 my $class = ref( $self ) . "\::${db_name}\::${new_class}";
413 if( !$self->_is_class_loaded( $class ) )
414 {
415 my $perl = <<EOT;
416 package $class;
417 BEGIN
418 {
419 use strict;
420 use parent qw( DB::Object::Fields );
421 };
422  
423 1;
424  
425 EOT
426 my $rc = eval( $perl );
427 die( "Unable to dynamically create module $class: $@" ) if( $@ );
428 }
429 else
430 {
431 # Class already exists
432 }
433 $o = $class->new(
434 prefixed => $self->{prefixed},
435 # For table alias
436 # query_object => $self->query_object,
437 # query_object => $qo,
438 table_object => $self,
439 debug => $self->debug,
440 );
441 $o->prefixed( $self->{prefixed} );
442 $self->{fields_object} = $o;
443 return( $o );
444 }
445  
446 sub fo { return( shift->fields_object( @_ ) ); }
447  
448 sub foreign { return( shift->_set_get_hash_as_mix_object( 'foreign', @_ ) ); }
449  
450 sub format_statement($;\%\%@) { return( shift->_method_to_query( 'format_statement', @_ ) ); }
451  
452 sub format_update($;%) { return( shift->_method_to_query( 'format_update', @_ ) ); }
453  
454 sub from_unixtime { return( shift->_method_to_query( 'from_unixtime', @_ ) ); }
455  
456 sub get_query_object { return( shift->_reset_query ); }
457  
458 sub group { return( shift->_method_to_query( 'group', @_ ) ); }
459  
460 # sub indexes { return( shift->_set_get_class_array_object( 'indexes', {
461 # is_primary => { type => 'boolean' },
462 # is_unique => { type => 'boolean' },
463 # fields => { type => 'array_as_object' },
464 # }, @_ ) ); }
465 sub indexes { return( shift->_set_get_hash_as_mix_object( 'indexes', @_ ) ); }
466  
467 sub insert
468 {
469 my $self = shift( @_ );
470 my $q = $self->_reset_query;
471 # If the user wants to execute this, then we reset the query,
472 # but if the user wants to call other methods chained like as_string we don't do anything
473 # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) );
474 if( want('VOID') || want('OBJECT') )
475 {
476 CORE::delete( $self->{query_reset} ) if( want('VOID') );
477 # return( $q->select( @_ ) );
478 # return( $q->select( @_ ) ) if( !defined( wantarray() ) );
479 return( $q->insert( @_ ) );
480 }
481 # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) );
482 # return( $q->insert( @_ ) ) if( !defined( wantarray() ) );
483 if( wantarray() )
484 {
485 my( @val ) = $q->insert( @_ ) || return( $self->pass_error( $q->error ) );
486 $self->reset;
487 return( @val );
488 }
489 else
490 {
491 my $val = $q->insert( @_ ) || return( $self->pass_error( $q->error ) );
492 $self->reset;
493 return( $val );
494 }
495 }
496  
497 sub limit { return( shift->_method_to_query( 'limit', @_ ) ); }
498  
499 sub local { return( shift->_method_to_query( 'local', @_ ) ); }
500  
501 sub lock
502 {
503 my $self = shift( @_ );
504 my $class = ref( $self );
505 return( $self->error( "lock() is not implemented by $class." ) );
506 }
507  
508 sub name
509 {
510 # Read-only
511 return( shift->{table} );
512 }
513  
514 sub new_check
515 {
516 my $self = shift( @_ );
517 my $args = $self->_get_args_as_hash( @_ );
518 $self->_load_class( 'DB::Object::Constraint::Check' ) ||
519 return( $self->pass_error );
520 $args->{debug} = $self->debug if( !CORE::exists( $args->{debug} ) || !defined( $args->{debug} ) );
521 my $this = DB::Object::Constraint::Check->new( %$args ) ||
522 return( $self->pass_error( DB::Object::Constraint::Check->error ) );
523 return( $this );
524 }
525  
526 sub new_foreign
527 {
528 my $self = shift( @_ );
529 my $args = $self->_get_args_as_hash( @_ );
530 $self->_load_class( 'DB::Object::Constraint::Foreign' ) ||
531 return( $self->pass_error );
532 $args->{debug} = $self->debug if( !CORE::exists( $args->{debug} ) || !defined( $args->{debug} ) );
533 my $this = DB::Object::Constraint::Foreign->new( %$args ) ||
534 return( $self->pass_error( DB::Object::Constraint::Foreign->error ) );
535 return( $this );
536 }
537  
538 sub new_index
539 {
540 my $self = shift( @_ );
541 my $args = $self->_get_args_as_hash( @_ );
542 $self->_load_class( 'DB::Object::Constraint::Index' ) ||
543 return( $self->pass_error );
544 $args->{debug} = $self->debug if( !CORE::exists( $args->{debug} ) || !defined( $args->{debug} ) );
545 my $this = DB::Object::Constraint::Index->new( %$args ) ||
546 return( $self->pass_error( DB::Object::Constraint::Index->error ) );
547 return( $this );
548 }
549  
550 sub no_bind { return( shift->_set_get_boolean({
551 field => 'no_bind',
552 callbacks =>
553 {
554 set => sub
555 {
556 my $self = shift( @_ );
557 my $val = shift( @_ );
558 return if( !defined( $val ) );
559 return( $val ) if( !$val );
560 my $q = $self->_reset_query;
561 my $where = $q->where();
562 my $group = $q->group();
563 my $order = $q->order();
564 my $limit = $q->limit();
565 my $binded_where = $q->binded_where;
566 my $binded_group = $q->binded_group;
567 my $binded_order = $q->binded_order;
568 my $binded_limit = $q->binded_limit;
569 # Replace the placeholders by their corresponding value
570 # and have them re-processed by their corresponding method
571 if( $where && @$binded_where )
572 {
573 $where =~ s/(=\s*\?)/"='" . quotemeta( $binded_where->[ $#+ ] ) . "'"/ge;
574 $self->where( $where );
575 }
576 if( $group && @$binded_group )
577 {
578 $group =~ s/(=\s*\?)/"='" . quotemeta( $binded_group->[ $#+ ] ) . "'"/ge;
579 $self->group( $group );
580 }
581 if( $order && @$binded_order )
582 {
583 $order =~ s/(=\s*\?)/"='" . quotemeta( $binded_order->[ $#+ ] ) . "'"/ge;
584 $self->order( $order );
585 }
586 if( $limit && @$binded_limit )
587 {
588 # $limit =~ s/(=\s*\?)/"='" . quotemeta( $binded_limit[ $#+ ] ) . "'"/ge;
589 $self->limit( @$binded_limit );
590 }
591 $q->reset_bind;
592 return( $val );
593 },
594 }
595 }, @_ ) ); }
596  
597 sub null
598 {
599 my $self = shift( @_ );
600 $self->structure || return( $self->pass_error );
601 my $fields = $self->fields;
602 # my $null = $self->{null};
603 my $null = +{ map{ $_ => ( $fields->{ $_ }->is_nullable ? 1 : 0 ) } keys( %$fields ) };
604 return( $self->_clone( $null ) );
605 }
606  
607 sub on_conflict { return( shift->error( "The on conflict clause is not supported by this driver." ) ); }
608  
609 sub optimize
610 {
611 my $self = shift( @_ );
612 my $class = ref( $self );
613 return( $self->error( "optimize() is not implemented by $class." ) );
614 }
615  
616 sub order { return( shift->_method_to_query( 'order', @_ ) ); }
617  
618 sub parent { return( shift->error( "The table parent() method is not supported by this driver." ) ); }
619  
620 sub prefix
621 {
622 my $self = shift( @_ );
623 my @val = ();
624 my $alias = $self->query_object->table_alias;
625 return( $alias ) if( $alias && $self->{prefixed} > 0 );
626 CORE::push( @val, $self->database_object->database ) if( $self->{prefixed} > 2 );
627 CORE::push( @val, $self->schema ) if( $self->{prefixed} > 1 && $self->schema );
628 CORE::push( @val, $self->name ) if( $self->{prefixed} > 0 );
629 return( '' ) if( !scalar( @val ) );
630 return( CORE::join( '.', @val ) );
631 }
632  
633 sub prefix_database { return( shift->{prefixed} > 2 ); }
634  
635 sub prefix_schema { return( shift->{prefixed} > 1 ); }
636  
637 sub prefix_table { return( shift->{prefixed} > 0 ); }
638  
639 # This the prefix intended for field in query
640 sub prefixed
641 {
642 my $self = shift( @_ );
643 if( @_ )
644 {
645 $self->{prefixed} = ( $_[0] =~ /^\d+$/ ? $_[0] : ( $_[0] ? 1 : 0 ) );
646 }
647 else
648 {
649 $self->{prefixed} = 1;
650 }
651 my $fo = $self->{fields_object};
652 $fo->prefixed( $self->{prefixed} ) if( $fo );
653 # return( want( 'OBJECT' ) ? $self : $self->{prefixed} );
654 return( $self->{prefixed} );
655 }
656  
657 sub primary
658 {
659 my $self = shift( @_ );
660 $self->structure || return( $self->pass_error );
661 my $primary = $self->{primary};
662 # return( wantarray() ? () : undef() ) if( !$primary || !@$primary );
663 # return( wantarray() ? @$primary : \@$primary );
664 return( $self->_clone( $primary ) );
665 }
666  
667 # In PostgreSQL, Oracle, SQL server this would be schema_name.table_name
668 sub qualified_name { return( shift->name ); }
669  
670 sub query_object { return( shift->_set_get_object_without_init( 'query_object', 'DB::Object::Query', @_ ) ); }
671  
672 sub query_reset { return( shift->_set_get_scalar( 'query_reset', @_ ) ); }
673  
674 sub rename
675 {
676 my $self = shift( @_ );
677 my $class = ref( $self );
678 return( $self->error( "rename() is not implemented by $class." ) );
679 }
680  
681 sub repair
682 {
683 my $self = shift( @_ );
684 my $class = ref( $self );
685 return( $self->error( "repair() is not implemented by $class." ) );
686 }
687  
688 sub replace
689 {
690 my $self = shift( @_ );
691 my $q = $self->_reset_query;
692 # If the user wants to execute this, then we reset the query,
693 # but if the user wants to call other methods chained like as_string we don't do anything
694 # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) );
695 if( want('VOID') || want('OBJECT') )
696 {
697 CORE::delete( $self->{query_reset} ) if( want('VOID') );
698 # return( $q->select( @_ ) );
699 # return( $q->select( @_ ) ) if( !defined( wantarray() ) );
700 return( $q->replace( @_ ) );
701 }
702 # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) );
703 # return( $q->replace( @_ ) );
704 # return( $q->replace( @_ ) ) if( !defined( wantarray() ) );
705 if( wantarray() )
706 {
707 my( @val ) = $q->replace( @_ ) || return( $self->pass_error( $q->error ) );
708 return( @val );
709 }
710 else
711 {
712 my $val = $q->replace( @_ ) || return( $self->pass_error( $q->error ) );
713 return( $val );
714 }
715 }
716  
717 sub reset
718 {
719 my $self = shift( @_ );
720 CORE::delete( $self->{query_reset} );
721 $self->_reset_query( @_ ) || return( $self->pass_error );
722 CORE::delete( $self->{fields_object} );
723 # To allow chaining of commands
724 return( $self );
725 }
726  
727 sub reset_structure
728 {
729 my $self = shift( @_ );
730 if( !CORE::length( $self->{_reset_structure} // '' ) && scalar( @_ ) )
731 {
732 $self->{_reset_structure} = scalar( @_ );
733 }
734 return( $self );
735 }
736  
737 # Modelled after PostgreSQL and available since 3.35.0 released 2021-03-12
738 # <https://www.sqlite.org/lang_returning.html>
739 sub returning { return( shift->_method_to_query( 'returning', @_ ) ); }
740  
741 sub reverse
742 {
743 my $self = shift( @_ );
744 if( @_ )
745 {
746 my $q = $self->_reset_query;
747 $self->{reverse}++;
748 $q->reverse( $self->{reverse} );
749 }
750 return( $self->{reverse} );
751 }
752  
753 sub schema { return( shift->_set_get_scalar( 'schema', @_ ) ); }
754  
755 sub select
756 {
757 my $self = shift( @_ );
758 my $q = $self->_reset_query;
759 # If the user wants to execute this, then we reset the query,
760 # but if the user wants to call other methods chained like as_string we don't do anything
761 # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) );
762 if( want('VOID') || want('OBJECT') )
763 {
764 CORE::delete( $self->{query_reset} ) if( want('VOID') );
765 # return( $q->select( @_ ) );
766 # return( $q->select( @_ ) ) if( !defined( wantarray() ) );
767 return( $q->select( @_ ) );
768 }
769  
770 if( wantarray() )
771 {
772 my( @val ) = $q->select( @_ ) || return( $self->pass_error( $q->error ) );
773 # a statement handler is returned and we reset the query so that other calls would not use the previous DB::Object::Query object
774 $self->reset;
775 return( @val );
776 }
777 else
778 {
779 my $val = $q->select( @_ ) || return( $self->pass_error( $q->error ) );
780 $self->reset;
781 return( $val );
782 }
783 }
784  
785 sub sort
786 {
787 my $self = shift( @_ );
788 if( @_ )
789 {
790 my $q = $self->_reset_query;
791 $self->{reverse} = 0;
792 $q->sort( $self->{reverse} );
793 }
794 return( $self->{reverse} );
795 }
796  
797 sub stat
798 {
799 my $self = shift( @_ );
800 my $class = ref( $self );
801 return( $self->error( "stat() is not implemented by $class." ) );
802 }
803  
804 # sub structure must be superseded by sub classes
805 sub structure
806 {
807 my $self = shift( @_ );
808 my $class = ref( $self );
809 return( $self->error( "structure() is not implemented by $class." ) );
810 }
811  
812 sub table { return( shift->{table} ); }
813  
814 sub tie
815 {
816 my $self = shift( @_ );
817 my $q = $self->_reset_query;
818 return( $q->tie( @_ ) );
819 }
820  
821 sub type { return( shift->_set_get_scalar( 'type', @_ ) ); }
822  
823 sub types
824 {
825 my $self = shift( @_ );
826 $self->structure || return( $self->pass_error );
827 # my $types = $self->{types};
828 my $fields = $self->fields;
829 my $types = +{ map{ $_ => $fields->{ $_ }->type } keys( %$fields ) };
830 # return( $self->_clone( $types ) );
831 return( $types );
832 }
833  
834 sub types_const
835 {
836 my $self = shift( @_ );
837 $self->structure || return( $self->pass_error );
838 my $fields = $self->fields;
839 # my $types = $self->{types_const};
840 my $types = +{ map{ $_ => $fields->{ $_ }->datatype } keys( %$fields ) };
841 # return( $self->_clone( $types ) );
842 return( $types );
843 }
844  
845 sub unlock
846 {
847 my $self = shift( @_ );
848 my $class = ref( $self );
849 return( $self->error( "unlock() is not implemented by $class." ) );
850 }
851  
852 sub unix_timestamp { return( shift->_method_to_query( 'unix_timestamp', @_ ) ); }
853  
854 sub update
855 {
856 my $self = shift( @_ );
857 my $q = $self->_reset_query;
858 # If the user wants to execute this, then we reset the query,
859 # but if the user wants to call other methods chained like as_string we don't do anything
860 # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) );
861 if( want('VOID') || want('OBJECT') )
862 {
863 CORE::delete( $self->{query_reset} ) if( want('VOID') );
864 # return( $q->select( @_ ) );
865 # return( $q->select( @_ ) ) if( !defined( wantarray() ) );
866 return( $q->update( @_ ) );
867 }
868 # CORE::delete( $self->{query_reset} ) if( !defined( wantarray() ) );
869 # return( $q->update( @_ ) );
870 # return( $q->update( @_ ) ) if( !defined( wantarray() ) );
871 if( wantarray() )
872 {
873 my( @val ) = $q->update( @_ ) || return( $self->pass_error( $q->error ) );
874 $self->reset;
875 return( @val );
876 }
877 else
878 {
879 my $val = $q->update( @_ ) || return( $self->pass_error( $q->error ) );
880 $self->reset;
881 return( $val );
882 }
883 }
884  
885 sub where { return( shift->_method_to_query( 'where', @_ ) ); }
886  
887 sub _method_to_query
888 {
889 my $self = shift( @_ );
890 my $meth = shift( @_ );
891 my $q = $self->_reset_query;
892 my $code = $q->can( $meth ) || return( $self->error( "Query class '", ref( $q ), "' has no method '$meth'." ) );
893 my $rv = $code->( $q, @_ );
894 return( $self->pass_error( $q->error ) ) if( !defined( $rv ) && $q->error );
895 return( $rv );
896 }
897  
898 sub AUTOLOAD
899 {
900 my( $method ) = our $AUTOLOAD =~ /([^:]+)$/;
901 no overloading;
902 my $self = shift( @_ );
903 my $fields = $self->fields;
904 # User called a field on a table object, instead of using the method fields_object or its shortcut 'fo'
905 if( CORE::exists( $fields->{ $method } ) )
906 {
907 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\n" );
908 return( $self->fields_object->_initiate_field_object( $method ) );
909 }
910 else
911 {
912 warn( "You called table '", $self->name, "' object \$tbl->$method, but no such method exist.\n" );
913 return( $self->error( "You called table '", $self->name, "' object \$tbl->$method, but no such method exist." ) );
914 }
915 };
916  
917 # NOTE: DESTROY
918 sub DESTROY
919 {
920 # Do nothing
921 # DB::Object::Tables are never destroyed.
922 # They are just gateway to tables, and they are cached by DB::Object::table()
923 # print( STDERR "DESTROY'ing table $self ($self->{ 'table' })\n" );
924 };
925  
926 # NOTE: For CBOR and Sereal
927 sub FREEZE
928 {
929 my $self = CORE::shift( @_ );
930 my $serialiser = CORE::shift( @_ ) // '';
931 my $class = CORE::ref( $self );
932 my @props = qw(
933 check fields fields_object foreign indexes parent prefixed primary query_object
934 query_reset reverse schema structure table table_alias type types_const
935 _cache_structure
936 );
937 my $hash = {};
938 foreach my $prop ( @props )
939 {
940 if( CORE::exists( $self->{ $prop } ) &&
941 defined( $self->{ $prop } ) &&
942 # Because we cannot reliably freeze code reference, and we do not usually need to.
943 CORE::ref( $self->{ $prop } ) ne 'CODE' )
944 {
945 $hash->{ $prop } = $self->{ $prop };
946 }
947 }
948 $hash->{_serial_version} = $DB::Object::SERIALISATION_VERSION;
949 # Return an array reference rather than a list so this works with Sereal and CBOR
950 # Before Sereal version 4.023, Sereal did not support multiple values returned
951 if( $serialiser eq 'Sereal' )
952 {
953 require Sereal::Encoder;
954 require version;
955
956 if( version->parse( Sereal::Encoder->VERSION ) < version->parse( '4.023' ) )
957 {
958 CORE::return( [$class, $hash] );
959 }
960 }
961 # But Storable want a list with the first element being the serialised element
962 CORE::return( $class, $hash );
963 }
964  
965 sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
966  
967 sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
968  
969 sub STORABLE_thaw_post_processing
970 {
971 my $obj = shift( @_ );
972 my @keys = %$obj;
973 my $class = ref( $obj );
974 my $hash = {};
975 @$hash{ @keys } = @$obj{ @keys };
976 my $self = bless( $hash => $class );
977 if( $self->{query_object} )
978 {
979 $self->{query_object}->attach( $self );
980 }
981 return( $self );
982 }
983  
984 # 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.
985 # NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
986 sub THAW
987 {
988 # STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue
989 # 'CBOR' as the second value.
990 my( $self, undef, @args ) = @_;
991 my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
992 my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
993 my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
994 if( $class !~ /\A[A-Za-z_]\w*(?:::\w+)*\z/ )
995 {
996 die( "THAW: Invalid class name '$class'" );
997 }
998  
999 my $sv = CORE::delete( $hash->{_serial_version} ) // 0;
1000 if( $sv != $DB::Object::SERIALISATION_VERSION )
1001 {
1002 require( $EXCEPTION_CLASS );
1003 die( $EXCEPTION_CLASS->new({
1004 code => 409, # Conflict
1005 message => sprintf(
1006 "Serialisation version mismatch in %s: expected %d, got %d. Please purge the cache.",
1007 $class,
1008 $DB::Object::SERIALISATION_VERSION,
1009 $sv,
1010 )
1011 }) );
1012 }
1013  
1014 # Since this class is inherited, we also need to load from which we depend, if necessary.
1015 if( $class ne 'DB::Object::Tables' )
1016 {
1017 require DB::Object;
1018 require DB::Object::Tables;
1019 my @supported_classes = CORE::values( %$DB::Object::DRIVER2PACK );
1020 push( @supported_classes, 'DB::Object' );
1021 my $ok_classes = CORE::join( '|', CORE::map{ CORE::quotemeta( $_ ) } @supported_classes );
1022 my $base_class = ( $class =~ /^($ok_classes)/ )[0];
1023 eval( "require $base_class;" );
1024 if( $@ )
1025 {
1026 require( $EXCEPTION_CLASS );
1027 die( $EXCEPTION_CLASS->new( "Failure to load $base_class: $@" ) );
1028 }
1029  
1030 eval( "require $class;" );
1031 if( $@ )
1032 {
1033 require( $EXCEPTION_CLASS );
1034 die( $EXCEPTION_CLASS->new( "Failure to load $class: $@" ) );
1035 }
1036  
1037 # If inheritance is still missing (or incomplete) for any reason, repair it explicitly.
1038 no strict 'refs';
1039  
1040 my $stash = \%{"${class}\::"};
1041 my $isa_ref;
1042 # We check if the 'ISA' symbol exists in the stash and its ARRAY slot is defined
1043 if( exists( $stash->{ISA} ) &&
1044 defined( *{"${class}\::ISA"}{ARRAY} ) )
1045 {
1046 $isa_ref = *{"${class}\::ISA"}{ARRAY};
1047 }
1048 else
1049 {
1050 # # Force creation of @ISA array slot
1051 # @{"${class}::ISA"} = ();
1052 # $isa_ref = *{"${class}::ISA"}{ARRAY};
1053 # But I want it to die, so this can be dealt with, and not swept under the rug.
1054 die( "Unable to find the \@ISA value in $class. The package contains the following stashes:\n", $self->Module::Generic::dump( $stash ) );
1055 }
1056  
1057 my $has_tble = 0;
1058 my $has_base = 0;
1059  
1060 foreach my $p ( @$isa_ref )
1061 {
1062 # Same as what we can find in modules DB::Object::(Postgres|SQLite|Mysql)::Tables
1063 $has_tble = 1 if( CORE::defined( $p ) && $p eq 'DB::Object::Tables' );
1064 $has_base = 1 if( CORE::defined( $p ) && $p eq $base_class );
1065 }
1066  
1067 if( !$has_tble || !$has_base )
1068 {
1069 @$isa_ref = ( 'DB::Object::Tables', $base_class );
1070 }
1071 }
1072 my $new;
1073 # Storable pattern requires to modify the object it created rather than returning a new one
1074 if( CORE::ref( $self ) )
1075 {
1076 foreach( CORE::keys( %$hash ) )
1077 {
1078 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
1079 }
1080 $new = $self;
1081 }
1082 else
1083 {
1084 $new = CORE::bless( $hash => $class );
1085 }
1086 CORE::return( $new );
1087 }
1088  
1089 1;
1090 # NOTE: POD
1091 __END__
1092  
1093 =encoding utf8
1094  
1095 =head1 NAME
1096  
1097 DB::Object::Tables - Database Table Object
1098  
1099 =head1 SYNOPSIS
1100  
1101 =head1 VERSION
1102  
1103 v1.2.1
1104  
1105 =head1 DESCRIPTION
1106  
1107 This is the table object package used to represent and manipulate table objects.
1108  
1109 =head1 CONSTRUCTOR
1110  
1111 =head2 new
1112  
1113 my $tbl = DB::Object::Tables->new( 'my_table' ) || die( DB::Object::Tables->error );
1114  
1115 Creates a new L<DB::Object::Tables> object.
1116  
1117 A table name may be provided as first argument.
1118  
1119 It may also take an hash of arguments, that also are method of the same name.
1120  
1121 It will call L</structure> to get the table structure from database and returns an error if it fails.
1122  
1123 Possible arguments are:
1124  
1125 =over 4
1126  
1127 =item * C<debug>
1128  
1129 Toggles debug mode on/off
1130  
1131 =back
1132  
1133 =head1 METHODS
1134  
1135 =head2 alias
1136  
1137 This is a convenient wrapper around L<DB::Object::Query/alias>
1138  
1139 It takes a column name to alias hash and sets those aliases for the following query.
1140  
1141 Get/set alias for table fields in SELECT queries. The hash provided thus contain a list of field => alias pairs.
1142  
1143 =head2 alter
1144  
1145 Provided with an array or array reference of specification for the alter and this will prepare the proper query.
1146  
1147 The specification array or array reference will be joined with a comma
1148  
1149 If called in void context, the resulting statement handler will be executed immediately.
1150  
1151 This returns the resulting statement handler.
1152  
1153 =head2 as
1154  
1155 Provided with a table alias and this will call L<DB::Object::Query/table_alias> passing it whatever arguments were provided.
1156  
1157 =head2 attach
1158  
1159 $tbl->attach( $db_object );
1160  
1161 Provided with a L<DB::Object>, or one of its inheriting classes, and this will attach that database object to this table object.
1162  
1163 It returns the current table object upon success, or upon error, it set an error an returns an empty list in list context, or C<undef> in scalar context.
1164  
1165 =head2 avoid
1166  
1167 Takes a list of array reference of column to avoid in the next query.
1168  
1169 This is a convenient wrapper around L<DB::Object::Query/avoid>
1170  
1171 =head2 check
1172  
1173 Sets or gets the L<hash object|Module::Generic::Hash> of L<check constraint objects|DB::Object::Constraint::Check> for this table.
1174  
1175 Each key in the hash represents the foreign key constraint name and its value is an L<check constraint object|DB::Object::Constraint::Check> that contains the following methods:
1176  
1177 =over 4
1178  
1179 =item * C<expr>
1180  
1181 The check constraint expression
1182  
1183 =item * C<fields>
1184  
1185 The L<array object|Module::Generic::Array> of table columns associated with this check constraint.
1186  
1187 =item * C<name>
1188  
1189 The check constraint name.
1190  
1191 =back
1192  
1193 =head2 columns
1194  
1195 Returns an L<array object|Module::Generic::Array> of the table columns.
1196  
1197 This information is provided by L</fields>, which is in turn provided by L</structure>
1198  
1199 =head2 constant
1200  
1201 Sets the query object constant for statement caching and return our current object.
1202  
1203 =head2 create
1204  
1205 This must be implemented by the driver package, so check L<DB::Object::Mysql::Tables/create>, L<DB::Object::Postgres::Tables/create> or L<DB::Object::SQLite::Tables/create>
1206  
1207 =head2 create_info
1208  
1209 This must be implemented by the driver package, so check L<DB::Object::Mysql::Tables/create_info>, L<DB::Object::Postgres::Tables/create_info> or L<DB::Object::SQLite::Tables/create_info>
1210  
1211 =head2 database
1212  
1213 Returns the name of the current database by calling L<DB::Object/database>
1214  
1215 =head2 database_object
1216  
1217 Returns the database object (L<DB::Object>)
1218  
1219 =head2 dbh
1220  
1221 Returns the database handler (L<DBI>)
1222  
1223 =head2 dbo
1224  
1225 Sets or get the L<database object|DB::Object>, which can be one of L<DB::Object::Mysql>, L<DB::Object::Postgres> or L<DB::Object::SQLite>
1226  
1227 =head2 default
1228  
1229 This calls L</structure> which may return cached data.
1230  
1231 Returns an hash in list context and an hash reference in scalar representing column to its default values pairs.
1232  
1233 If nothing is found, it returns an empty list in list context and L<perlfunc/undef> in scalar context.
1234  
1235 =head2 delete
1236  
1237 L</delete> will format a delete query based on previously set parameters, such as L</where>.
1238  
1239 L</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</do> method and passing the sql query directly.
1240  
1241 $tbl->where( login => 'jack' );
1242 $tbl->limit(1);
1243 my $rows_affected = $tbl->delete();
1244 # or passing the where condition directly to delete
1245 my $sth = $tbl->delete( login => 'jack' );
1246  
1247 =head2 drop
1248  
1249 This will prepare the query to drop the current table.
1250  
1251 In void context, this will execute the resulting statement handler.
1252  
1253 It returns the resulting statement handler
1254  
1255 =head2 enhance
1256  
1257 Sets or gets the boolean value. When true, this will instruct the query object to make certain enhancements to the SQL query.
1258  
1259 =head2 exists
1260  
1261 This must be implemented by the driver package, so check L<DB::Object::Mysql::Tables/exists>, L<DB::Object::Postgres::Tables/exists> or L<DB::Object::SQLite::Tables/exists>
1262  
1263 =head2 field_exists
1264  
1265 Provided with a field name, and this returns a boolean value as to whether that field exists in the table or not.
1266  
1267 =head2 fields_as_array
1268  
1269 Returns the table fields name as an L<array object|Module::Generic::Array>
1270  
1271 =head2 fields
1272  
1273 This calls L</structure> which may return cached data.
1274  
1275 Returns an hash of fields to their corresponding L<object|DB::Object::Fields::Field>. Those objects are instantiated once by the L<structure method|DB::Object::Tables/structure>. If you plan on making change, make sure to clone them first.
1276  
1277 If nothing is found, it returns an empty list in list context and L<perlfunc/undef> in scalar context.
1278  
1279 It takes an optional parameter representing a field name, and will return its corresponding object, such as:
1280  
1281 my $tbl = $dbh->my_database_table || die( "No table 'my_database_table' in database" );
1282 my $field_object = $tbl->fields( 'my_table_field' ):
1283  
1284 =head2 fields_object
1285  
1286 my $tbl = $dbh->user || die( "No table \"user\" found in database\n" );
1287 # get the field object for "name"
1288 my $name = $tbl->fields_object->name
1289 # Do something with it
1290 my $expr = $name == 'joe';
1291 # Resulting in an DB::Object::Fields::Overloaded object
1292  
1293 This returns the cached object if there is one.
1294  
1295 This will dynamically create a package based on the database and table name. For example a database C<Foo> and a table C<Bar> would result in the following dynamically created package: C<DB::Object::Tables::Foo::Bar>
1296  
1297 This new package will inherit from L<DB::Object::Fields>, which enable the dynamic loading of column object using C<AUTOLOAD>
1298  
1299 This will instantiate an object from this newly created package, cache it and return it.
1300  
1301 =head2 fo
1302  
1303 This is a convenient shortcut for L</fields_object>
1304  
1305 my $tbl = $dbh->user || die( "No table \"user\" found in database\n" );
1306 # get the field object for "name"
1307 my $name = $tbl->fo->name
1308  
1309 =head2 foreign
1310  
1311 Sets or gets the L<hash object|Module::Generic::Hash> of L<foreign key constraint objects|DB::Object::Constraint::Foreign> for this table.
1312  
1313 Each key in the hash represents the foreign key constraint name and its value is an L<foreign key constraint object|DB::Object::Constraint::Foreign> that contains the following methods:
1314  
1315 =over 4
1316  
1317 =item * C<match>
1318  
1319 Typical value is C<full>, C<partial> and C<simple>
1320  
1321 =item * C<on_delete>
1322  
1323 The action the database is to take upon deletion. For example: C<nothing>, C<restrict>, C<cascade>, C<null> or C<default>
1324  
1325 =item * C<on_update>
1326  
1327 The action the database is to take upon update. For example: C<nothing>, C<restrict>, C<cascade>, C<null> or C<default>
1328  
1329 =item * C<table>
1330  
1331 The table name of the foreign key.
1332  
1333 =item * C<fields>
1334  
1335 The L<array object|Module::Generic::Array> of associated column names for this foreign key constraint.
1336  
1337 =item * C<name>
1338  
1339 The foreign key constraint name.
1340  
1341 =back
1342  
1343 =head2 format_statement
1344  
1345 This is a convenient wrapper around L<DB::Object::Query/format_statement>
1346  
1347 Format the sql statement for queries of types C<select>, C<delete> and C<insert>
1348  
1349 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.
1350  
1351 =head2 format_update
1352  
1353 This is a convenient wrapper around L<DB::Object::Query/format_update>
1354  
1355 Formats update query based on the following arguments provided:
1356  
1357 =over 4
1358  
1359 =item * C<data>
1360  
1361 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.
1362  
1363 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</format_update> honors the order in which the fields are provided.
1364  
1365 =back
1366  
1367 L</format_update> will then iterate through each field-value pair, and perform some work:
1368  
1369 If the field being reviewed was provided to B<from_unixtime>, then L</format_update> will enclose it in the function FROM_UNIXTIME() as in:
1370  
1371 FROM_UNIXTIME(field_name)
1372  
1373 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.
1374  
1375 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.
1376  
1377 If L<DB::Object/bind> is off, the value will be escaped and the pair field='value' created.
1378  
1379 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.
1380  
1381 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.
1382  
1383 Finally, otherwise the value is escaped and surrounded by single quotes.
1384  
1385 L</format_update> returns a string representing the comma-separated list of fields that will be used.
1386  
1387 =head2 from_unixtime
1388  
1389 Provided with an array or array reference of table columns and this will set the list of fields that are to be treated as unix time and converted accordingly after the sql query is executed.
1390  
1391 It returns the list of fields in list context or a reference to an array in scalar context.
1392  
1393 =head2 get_query_object
1394  
1395 Get the L<DB::Object::Query> object. If none is set yet, it will instantiate one automatically.
1396  
1397 =head2 group
1398  
1399 This is a convenient wrapper around L<DB::Object::Query/group>
1400  
1401 =head2 indexes
1402  
1403 my $idx = $tbl->indexes;
1404 my $in0 = $idx->{some_index};
1405 say "Is primary: ", $in0->is_primary ? 'yes' : 'no';
1406 say "Is unique: ", $in0->is_unique ? 'yes' : 'no';
1407 say "Associated fields: ", $in0->fields->join( ', ' );
1408  
1409 Sets or gets the L<hash object|Module::Generic::Hash> of L<index objects|DB::Object::Constraint::Index> for this table.
1410  
1411 Each key in the hash represents the index name and its value is an L<index object|DB::Object::Constraint::Index> that contains the following methods:
1412  
1413 =over 4
1414  
1415 =item * C<fields>
1416  
1417 An L<array object|Module::Generic::Array> of table field names.
1418  
1419 =item * C<is_primary>
1420  
1421 Boolean value whether this index is the table primary index.
1422  
1423 =item * C<is_unique>
1424  
1425 Boolean value whether this is a unique index.
1426  
1427 =back
1428  
1429 =head2 insert
1430  
1431 This is a convenient wrapper around L<DB::Object::Query/insert>
1432  
1433 =head2 limit
1434  
1435 This is a convenient wrapper around L<DB::Object::Query/limit>
1436  
1437 =head2 local
1438  
1439 This is a convenient wrapper around L<DB::Object::Query/local>
1440  
1441 =head2 lock
1442  
1443 This must be implemented by the driver package, so check L<DB::Object::Mysql::Tables/lock>, L<DB::Object::Postgres::Tables/lock> or L<DB::Object::SQLite::Tables/lock>
1444  
1445 =head2 name
1446  
1447 Returns the table name. This is read-only.
1448  
1449 =head2 new_check
1450  
1451 This takes an hash or an hash reference of parameters and instantiate a new L<DB::Object::Constraint::Check> object.
1452  
1453 If no C<debug> parameter is provided, the one of the current table object will be used.
1454  
1455 It returns the new object upon success, or upon error, it sets an L<exception object|Module::Generic::Exception> and return C<undef> in scalar context, or an empty list in list context.
1456  
1457 =head2 new_foreign
1458  
1459 This takes an hash or an hash reference of parameters and instantiate a new L<DB::Object::Constraint::Foreign> object.
1460  
1461 If no C<debug> parameter is provided, the one of the current table object will be used.
1462  
1463 It returns the new object upon success, or upon error, it sets an L<exception object|Module::Generic::Exception> and return C<undef> in scalar context, or an empty list in list context.
1464  
1465 =head2 new_index
1466  
1467 This takes an hash or an hash reference of parameters and instantiate a new L<DB::Object::Constraint::Index> object.
1468  
1469 If no C<debug> parameter is provided, the one of the current table object will be used.
1470  
1471 It returns the new object upon success, or upon error, it sets an L<exception object|Module::Generic::Exception> and return C<undef> in scalar context, or an empty list in list context.
1472  
1473 =head2 no_bind
1474  
1475 Boolean. Sets the C<no bind> flags to true or false.
1476  
1477 =head2 null
1478  
1479 This calls L</structure> which may return cached data.
1480  
1481 Returns an hash in list context and an hash reference in scalar representing column to its default null values pairs.
1482  
1483 If nothing is found, it returns an empty list in list context and L<perlfunc/undef> in scalar context.
1484  
1485 =head2 on_conflict
1486  
1487 The SQL C<ON CONFLICT> clause needs to be implemented by the driver and is currently supported only by L<DB::Object::Postgres> and L<DB::Object::SQLite>.
1488  
1489 =head2 optimize
1490  
1491 This must be implemented by the driver package, so check L<DB::Object::Mysql::Tables/optimize>, L<DB::Object::Postgres::Tables/optimize> or L<DB::Object::SQLite::Tables/optimize>
1492  
1493 =head2 order
1494  
1495 This is a convenient wrapper around L<DB::Object::Query/order>
1496  
1497 Prepares the C<ORDER BY> clause and returns the value of the clause in list context or the C<ORDER BY> clause in full in scalar context, ie. "ORDER BY $clause"
1498  
1499 =head2 parent
1500  
1501 For the drivers who support it, this will represent the parent table if the current table inherits from another table.
1502  
1503 =head2 prefix
1504  
1505 Based on the prefix level, this will return a string with the database name if prefix is higher than 2, with the schema if the prefix level is higher than 1 and with the table name if the prefix level is higher than 0.
1506  
1507 The resulting string is used as prefix to table columns when preparing queries.
1508  
1509 =head2 prefix_database
1510  
1511 Returns true if L</prefixed> is higher than 2.
1512  
1513 =head2 prefix_schema
1514  
1515 Returns true if L</prefixed> is higher than 1.
1516  
1517 =head2 prefix_table
1518  
1519 Returns true if L</prefixed> is higher than 0.
1520  
1521 =head2 prefixed
1522  
1523 Sets or gets the prefix level. 0 being no prefix and 2 implying the use of the database name in prefix.
1524  
1525 =head2 primary
1526  
1527 This calls L</structure> which may return cached data.
1528  
1529 Returns an hash in list context and an hash reference in scalar representing column to primary keys pairs. If a column has no primary keys, its value would be empty.
1530  
1531 If nothing is found, it returns an empty list in list context and L<perlfunc/undef> in scalar context.
1532  
1533 =head2 qualified_name
1534  
1535 Returns the table name. This is read-only.
1536  
1537 =head2 query_object
1538  
1539 Returns the query object (L<DB::Object::Query>)
1540  
1541 =head2 query_reset
1542  
1543 Reset the query object (L<DB::Object::Query>)
1544  
1545 =head2 rename
1546  
1547 This must be implemented by the driver package, so check L<DB::Object::Mysql::Tables/rename>, L<DB::Object::Postgres::Tables/rename> or L<DB::Object::SQLite::Tables/rename>
1548  
1549 =head2 repair
1550  
1551 This must be implemented by the driver package, so check L<DB::Object::Mysql::Tables/repair>, L<DB::Object::Postgres::Tables/repair> or L<DB::Object::SQLite::Tables/repair>
1552  
1553 =head2 replace
1554  
1555 Just like for the C<INSERT> query, L</replace> takes one optional argument representing a L<DB::Object::Statement> C<SELECT> object or a list of field-value pairs.
1556  
1557 If a C<SELECT> statement is provided, it will be used to construct a query of the type of C<REPLACE INTO mytable SELECT FROM other_table>
1558  
1559 Otherwise the query will be C<REPLACE INTO mytable (fields) VALUES(values)>
1560  
1561 In scalar context, it execute the query and in list context it simply returns the statement handler.
1562  
1563 =head2 reset
1564  
1565 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()
1566  
1567 It execute an update with the reseted value and return the number of affected rows.
1568  
1569 =head2 reset_structure
1570  
1571 Resets the cache for the L</structure> method
1572  
1573 =head2 returning
1574  
1575 The SQL C<RETURNING> clause needs to be implemented by the driver and is currently supported only by and L<DB::Object::Postgres> (see L<DB::Object::Postgres::Query/returning>) and L<DB::Object::SQLite> (see L<DB::Object::SQLite::Query/returning>).
1576  
1577 =head2 reverse
1578  
1579 Get or set the reverse mode.
1580  
1581 =head2 schema
1582  
1583 Returns the schema name, if any. For example, with PostgreSQL, the default schema name would be C<public>.
1584  
1585 =head2 select
1586  
1587 Given an optional list of fields to fetch, L</select> prepares a C<SELECT> query.
1588  
1589 If no field was provided, L</select> will use default value where appropriate like the C<NOW()> for date/time fields.
1590  
1591 L<DB::Object::Query/select> calls upon L<DB::Object::Query/tie>, L<DB::Object::Query/where>, L<DB::Object::Query/group>, L<DB::Object::Query/order>, L<DB::Object::Query/limit>, L<DB::Object::Query/local>, and possibly more depending on the driver implementation, to build the query.
1592  
1593 In scalar context, it execute the query and return it. In list context, it just returns the statement handler.
1594  
1595 =head2 sort
1596  
1597 It toggles sort mode on and consequently disable reverse mode.
1598  
1599 =head2 stat
1600  
1601 This must be implemented by the driver package, so check L<DB::Object::Mysql::Tables/stat>, L<DB::Object::Postgres::Tables/stat> or L<DB::Object::SQLite::Tables/stat>
1602  
1603 =head2 structure
1604  
1605 The implementation is driver specific.
1606  
1607 This must be implemented by the driver package, so check L<DB::Object::Mysql::Tables/structure>, L<DB::Object::Postgres::Tables/structure> or L<DB::Object::SQLite::Tables/structure>
1608  
1609 This returns a cached data for speed. See L</reset_structure> to reset that cache.
1610  
1611 =head2 table
1612  
1613 Returns the table name. This is read-only.
1614  
1615 =head2 tie
1616  
1617 This is a convenient wrapper around L<DB::Object::Query/tie>
1618  
1619 =head2 type
1620  
1621 The table type
1622  
1623 =head2 types
1624  
1625 This calls L</structure> which may return cached data.
1626  
1627 Returns an hash in list context and an hash reference in scalar representing column to data type.
1628  
1629 If nothing is found, it returns an empty list in list context and L<perlfunc/undef> in scalar context.
1630  
1631 =head2 types_const
1632  
1633 This calls L</structure> which may return cached data.
1634  
1635 Returns an hash in list context and an hash reference in scalar representing column to hash that defines the driver constant for this data type:
1636  
1637 some_column => { constant => 17, name => 'PG_JSONB', type => 'jsonb' }
1638  
1639 This is used to help manage binded value with the right type, or helps when converting an hash into json.
1640  
1641 If nothing is found, it returns an empty list in list context and L<perlfunc/undef> in scalar context.
1642  
1643 =head2 unix_timestamp
1644  
1645 This is a convenient wrapper around L<DB::Object::Query/unix_timestamp>
1646  
1647 =head2 unlock
1648  
1649 This must be implemented by the driver package, so check L<DB::Object::Mysql::Tables/unlock>, L<DB::Object::Postgres::Tables/unlock> or L<DB::Object::SQLite::Tables/unlock>
1650  
1651 =head2 update
1652  
1653 Given a list of field-value pairs, L</update> prepares a sql update query.
1654  
1655 It calls upon L<DB::Object::Query/where> and L<DB::Object::Query/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 where
1660  
1661 This is a convenient wrapper around L<DB::Object::Query/where>
1662  
1663 =head1 AUTHOR
1664  
1665 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
1666  
1667 =head1 SEE ALSO
1668  
1669 L<DB::Object::Mysql::Tables>, L<DB::Object::Postgres::Tables> or L<DB::Object::SQLite::Tables>
1670  
1671 =head1 AUTHOR
1672  
1673 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
1674  
1675 =head1 COPYRIGHT & LICENSE
1676  
1677 Copyright (c) 2019-2021 DEGUEST Pte. Ltd.
1678  
1679 You can use, copy, modify and redistribute this package and associated
1680 files under the same terms as Perl itself.
1681  
1682 =cut