DB-Object
view release on metacpan or search on metacpan
lib/DB/Object/Tables.pm view on Meta::CPAN
# NOTE: DESTROY
sub DESTROY
{
# Do nothing
# DB::Object::Tables are never destroyed.
# They are just gateway to tables, and they are cached by DB::Object::table()
# print( STDERR "DESTROY'ing table $self ($self->{ 'table' })\n" );
};
# NOTE: For CBOR and Sereal
sub FREEZE
{
my $self = CORE::shift( @_ );
my $serialiser = CORE::shift( @_ ) // '';
my $class = CORE::ref( $self );
my @props = qw(
check fields fields_object foreign indexes parent prefixed primary query_object
query_reset reverse schema structure table table_alias type types_const
_cache_structure
);
my $hash = {};
foreach my $prop ( @props )
{
if( CORE::exists( $self->{ $prop } ) &&
defined( $self->{ $prop } ) &&
# Because we cannot reliably freeze code reference, and we do not usually need to.
CORE::ref( $self->{ $prop } ) ne 'CODE' )
{
$hash->{ $prop } = $self->{ $prop };
}
}
$hash->{_serial_version} = $DB::Object::SERIALISATION_VERSION;
# Return an array reference rather than a list so this works with Sereal and CBOR
# Before Sereal version 4.023, Sereal did not support multiple values returned
if( $serialiser eq 'Sereal' )
{
require Sereal::Encoder;
require version;
if( version->parse( Sereal::Encoder->VERSION ) < version->parse( '4.023' ) )
{
CORE::return( [$class, $hash] );
}
}
# But Storable want a list with the first element being the serialised element
CORE::return( $class, $hash );
}
sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
sub STORABLE_thaw_post_processing
{
my $obj = shift( @_ );
my @keys = %$obj;
my $class = ref( $obj );
my $hash = {};
@$hash{ @keys } = @$obj{ @keys };
my $self = bless( $hash => $class );
if( $self->{query_object} )
{
$self->{query_object}->attach( $self );
}
return( $self );
}
# 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.
# NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
sub THAW
{
# STORABLE_thaw would issue $cloning as the 2nd argument, while CBOR would issue
# 'CBOR' as the second value.
my( $self, undef, @args ) = @_;
my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
if( $class !~ /\A[A-Za-z_]\w*(?:::\w+)*\z/ )
{
die( "THAW: Invalid class name '$class'" );
}
my $sv = CORE::delete( $hash->{_serial_version} ) // 0;
if( $sv != $DB::Object::SERIALISATION_VERSION )
{
require( $EXCEPTION_CLASS );
die( $EXCEPTION_CLASS->new({
code => 409, # Conflict
message => sprintf(
"Serialisation version mismatch in %s: expected %d, got %d. Please purge the cache.",
$class,
$DB::Object::SERIALISATION_VERSION,
$sv,
)
}) );
}
# Since this class is inherited, we also need to load from which we depend, if necessary.
if( $class ne 'DB::Object::Tables' )
{
require DB::Object;
require DB::Object::Tables;
my @supported_classes = CORE::values( %$DB::Object::DRIVER2PACK );
push( @supported_classes, 'DB::Object' );
my $ok_classes = CORE::join( '|', CORE::map{ CORE::quotemeta( $_ ) } @supported_classes );
my $base_class = ( $class =~ /^($ok_classes)/ )[0];
eval( "require $base_class;" );
if( $@ )
{
require( $EXCEPTION_CLASS );
die( $EXCEPTION_CLASS->new( "Failure to load $base_class: $@" ) );
}
eval( "require $class;" );
if( $@ )
{
require( $EXCEPTION_CLASS );
die( $EXCEPTION_CLASS->new( "Failure to load $class: $@" ) );
}
# If inheritance is still missing (or incomplete) for any reason, repair it explicitly.
no strict 'refs';
my $stash = \%{"${class}\::"};
my $isa_ref;
# We check if the 'ISA' symbol exists in the stash and its ARRAY slot is defined
if( exists( $stash->{ISA} ) &&
defined( *{"${class}\::ISA"}{ARRAY} ) )
{
$isa_ref = *{"${class}\::ISA"}{ARRAY};
}
else
{
# # Force creation of @ISA array slot
# @{"${class}::ISA"} = ();
# $isa_ref = *{"${class}::ISA"}{ARRAY};
# But I want it to die, so this can be dealt with, and not swept under the rug.
die( "Unable to find the \@ISA value in $class. The package contains the following stashes:\n", $self->Module::Generic::dump( $stash ) );
}
my $has_tble = 0;
my $has_base = 0;
foreach my $p ( @$isa_ref )
{
# Same as what we can find in modules DB::Object::(Postgres|SQLite|Mysql)::Tables
$has_tble = 1 if( CORE::defined( $p ) && $p eq 'DB::Object::Tables' );
$has_base = 1 if( CORE::defined( $p ) && $p eq $base_class );
}
if( !$has_tble || !$has_base )
{
@$isa_ref = ( 'DB::Object::Tables', $base_class );
}
}
my $new;
# Storable pattern requires to modify the object it created rather than returning a new one
if( CORE::ref( $self ) )
{
foreach( CORE::keys( %$hash ) )
{
$self->{ $_ } = CORE::delete( $hash->{ $_ } );
}
$new = $self;
}
else
{
$new = CORE::bless( $hash => $class );
}
CORE::return( $new );
}
1;
# NOTE: POD
__END__
=encoding utf8
=head1 NAME
DB::Object::Tables - Database Table Object
=head1 SYNOPSIS
=head1 VERSION
v1.2.1
=head1 DESCRIPTION
This is the table object package used to represent and manipulate table objects.
=head1 CONSTRUCTOR
=head2 new
my $tbl = DB::Object::Tables->new( 'my_table' ) || die( DB::Object::Tables->error );
Creates a new L<DB::Object::Tables> object.
A table name may be provided as first argument.
It may also take an hash of arguments, that also are method of the same name.
It will call L</structure> to get the table structure from database and returns an error if it fails.
Possible arguments are:
=over 4
=item * C<debug>
Toggles debug mode on/off
=back
=head1 METHODS
=head2 alias
This is a convenient wrapper around L<DB::Object::Query/alias>
It takes a column name to alias hash and sets those aliases for the following query.
Get/set alias for table fields in SELECT queries. The hash provided thus contain a list of field => alias pairs.
=head2 alter
( run in 0.834 second using v1.01-cache-2.11-cpan-f56aa216473 )