Geoffrey

 view release on metacpan or  search on metacpan

lib/Geoffrey/Action/Constraint/ForeignKey.pm  view on Meta::CPAN

package Geoffrey::Action::Constraint::ForeignKey;

use utf8;
use 5.016;
use strict;
use warnings;

$Geoffrey::Action::Constraint::ForeignKey::VERSION = '0.000206';

use parent 'Geoffrey::Role::Action';

sub add {
    my ($self, $hr_params) = @_;
    require Ref::Util;
    if (!Ref::Util::is_hashref($hr_params)) {
        require Geoffrey::Exception::General;
        Geoffrey::Exception::General::throw_wrong_ref(__PACKAGE__ . '::add', 'hash');
    }
    if (!$self->converter->foreign_key) {
        require Geoffrey::Exception::NotSupportedException;
        Geoffrey::Exception::NotSupportedException::throw_foreignkey('add', $self->converter);
    }
    if (!exists $hr_params->{table}) {
        require Geoffrey::Exception::RequiredValue;
        Geoffrey::Exception::RequiredValue::throw_table_name();
    }
    if (   !exists $hr_params->{column}
        || !exists $hr_params->{reftable}
        || !exists $hr_params->{refcolumn})
    {
        require Geoffrey::Exception::RequiredValue;
        Geoffrey::Exception::RequiredValue::throw_reftable_missing($hr_params->{table})
            if (!exists $hr_params->{reftable});
        Geoffrey::Exception::RequiredValue::throw_refcolumn_missing($hr_params->{table})
            if (!exists $hr_params->{refcolumn});
        Geoffrey::Exception::RequiredValue::throw_table_column($hr_params->{table})
            if (!exists $hr_params->{column});
    }
    $hr_params->{reftable} =~ s/"//g;
    $hr_params->{table} =~ s/"//g;
    require Geoffrey::Utils;
    my $s_sql = Geoffrey::Utils::replace_spare(
        $self->converter->foreign_key->add,
        [
            $hr_params->{column},
            (exists $hr_params->{schema} ? $hr_params->{schema} . q/./ : q//)
                . $hr_params->{reftable},
            $hr_params->{refcolumn},
            qq~fkey_$hr_params->{table}~ . q~_~ . time
        ]);
    return $s_sql if $self->for_table;
    return $self->do($s_sql);

}

sub alter {
    my ($self, $hr_params) = @_;
    if (!exists $hr_params->{name}) {
        require Geoffrey::Exception::RequiredValue;
        Geoffrey::Exception::RequiredValue::throw_index_name(__PACKAGE__ . '::alter');
    }
    if (!$self->converter->foreign_key) {
        require Geoffrey::Exception::NotSupportedException;
        Geoffrey::Exception::NotSupportedException::throw_foreignkey('alter', $self->converter);
    }
    return [$self->drop($hr_params), $self->add($hr_params),];
}

sub drop {
    my ($self, $hr_params) = @_;
    if (!$self->converter->foreign_key) {
        require Geoffrey::Exception::NotSupportedException;
        Geoffrey::Exception::NotSupportedException::throw_foreignkey('drop',
            $self->converter->foreign_key);
    }



( run in 2.404 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )