Alzabo
view release on metacpan or search on metacpan
lib/Alzabo/Create/Table.pm view on Meta::CPAN
package Alzabo::Create::Table;
use strict;
use vars qw($VERSION);
use Alzabo::Create;
use Alzabo::Exceptions ( abbr => 'params_exception' );
use Params::Validate qw( :all );
Params::Validate::validation_options
( on_fail => sub { params_exception join '', @_ } );
use Tie::IxHash;
use base qw(Alzabo::Table);
$VERSION = 2.0;
1;
sub new
{
my $proto = shift;
my $class = ref $proto || $proto;
validate( @_, { schema => { isa => 'Alzabo::Create::Schema' },
name => { type => SCALAR },
attributes => { type => ARRAYREF,
optional => 1 },
comment => { type => UNDEF | SCALAR,
default => '' },
} );
my %p = @_;
my $self = bless {}, $class;
$self->{schema} = $p{schema};
$self->set_name($p{name});
$self->{columns} = Tie::IxHash->new;
$self->{pk} = [];
$self->{indexes} = Tie::IxHash->new;
my %attr;
tie %{ $self->{attributes} }, 'Tie::IxHash';
$self->set_attributes( @{ $p{attributes} } );
$self->set_comment( $p{comment} );
# Setting this prevents run time type errors.
$self->{fk} = {};
return $self;
}
sub set_name
{
my $self = shift;
validate_pos( @_, { type => SCALAR } );
my $name = shift;
params_exception "Table $name already exists in schema"
if $self->schema->has_table($name);
my @i;
if ($self->{indexes})
{
@i = $self->indexes;
$self->delete_index($_) foreach @i;
}
my $old_name = $self->{name};
$self->{name} = $name;
eval
{
$self->schema->rules->validate_table_name($self);
};
$self->add_index($_) foreach @i;
if ($@)
{
$self->{name} = $old_name;
rethrow_exception($@);
}
if ( $old_name && eval { $self->schema->table($old_name) } )
{
$self->schema->register_table_name_change( table => $self,
old_name => $old_name );
( run in 0.595 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )