Alzabo

 view release on metacpan or  search on metacpan

lib/Alzabo/Create/ColumnDefinition.pm  view on Meta::CPAN

package Alzabo::Create::ColumnDefinition;

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 base qw(Alzabo::ColumnDefinition);

$VERSION = 2.0;

1;

sub new
{
    my $proto = shift;
    my $class = ref $proto || $proto;

    my $self = bless {}, $class;

    $self->_init(@_);

    return $self;
}

sub _init
{
    my $self = shift;

    validate( @_, { owner => { isa => 'Alzabo::Create::Column' },
                    type  => { type => SCALAR },
                    length => { type => UNDEF | SCALAR,
                                optional => 1 },
                    precision  => { type => UNDEF | SCALAR,
                                    optional => 1 },
                  } );
    my %p = @_;

    $p{type} =
        $p{owner}->table->schema->rules->validate_column_type( $p{type}, $p{owner}->table );
    foreach ( qw( owner type ) )
    {
        $self->{$_} = $p{$_} if exists $p{$_};
    }
}

sub alter
{
    my $self = shift;

    validate( @_, { type  => { type => SCALAR },
                    length => { type => UNDEF | SCALAR,
                                optional => 1 },
                    precision  => { type => UNDEF | SCALAR,
                                    optional => 1 },
                  } );
    my %p = @_;

    my $old_type = $self->{type};
    my $old_length = $self->{length};
    my $old_precision = $self->{precision};

    $self->{length} = $p{length} if exists $p{length};
    $self->{precision} = $p{precision} if exists $p{precision};

    eval
    {
        $self->{type} =
            $self->owner->table->schema->rules->validate_column_type($p{type}, $self->owner->table);
        $self->owner->table->schema->rules->validate_primary_key($self->owner)
            if $self->owner->is_primary_key;
        $self->owner->table->schema->rules->validate_column_length($self->owner);
    };
    if ($@)
    {
        $self->{type} = $old_type;
        $self->{length} = $old_length;
        $self->{precision} = $old_precision;



( run in 2.183 seconds using v1.01-cache-2.11-cpan-e93a5daba3e )