Geoffrey

 view release on metacpan or  search on metacpan

lib/Geoffrey/Action/Table.pm  view on Meta::CPAN

sub action {
    my ($self, $s_action) = @_;
    $s_action = join q//, map {ucfirst} split /_/, $s_action;
    require Geoffrey::Utils;
    return Geoffrey::Utils::action_obj_from_name(
        $s_action,
        dbh       => $self->dbh,
        converter => $self->converter,
        dryrun    => $self->dryrun
    );
}

sub add {
    my ($self, $hr_params) = @_;
    if (!$hr_params || !$hr_params->{name}) {
        require Geoffrey::Exception::RequiredValue;
        Geoffrey::Exception::RequiredValue::throw_table_name(__PACKAGE__);
    }
    my @columns           = ();
    my $ar_constraints    = [];
    my $constraint_action = $self->constraint_action;
    my $column_action     = $self->column_action;
    if ($hr_params->{template}) {
        my $templates = $self->_hr_merge_templates($hr_params->{template}, $hr_params->{name});
        push @columns, @{$templates->{columns}};
        push @{$ar_constraints}, @{$templates->{constraints}};
    }
    $constraint_action->for_table(1);
    $column_action->for_table(1);
    for my $hr_column (@{$hr_params->{columns}}) {
        $hr_column->{schema} = $hr_params->{schema} if exists $hr_params->{schema};
        $hr_column->{table} = $hr_params->{name};
        my $const = $constraint_action->add($hr_params->{name}, $hr_column, $ar_constraints);
        push @columns, $column_action->add($hr_column, $const);
    }
    for (@{$hr_params->{constraints}}) {
        $_->{schema} = $hr_params->{schema} if exists $hr_params->{schema};
        $constraint_action->add($hr_params->{name}, $_, $ar_constraints);
    }
    push @columns, @{$ar_constraints};
    if (scalar @columns == 0 && !$self->converter->can_create_empty_table) {
        require Geoffrey::Exception::NotSupportedException;
        Geoffrey::Exception::NotSupportedException::throw_empty_table($self->converter,
            $hr_params);
    }
    $constraint_action->for_table(0);
    $column_action->for_table(0);

    #prepare finaly created table to SQL
    require Geoffrey::Utils;
    my $sql = Geoffrey::Utils::replace_spare( $self->converter->table->add, [
        ($hr_params->{schema} ? $hr_params->{schema} . q/./ : q//) . $self->prefix . $hr_params->{name} . $self->postfix,
        join(q/,/, @columns),
        $hr_params->{engine}, $hr_params->{charset}
    ]);
    return $self->do($sql);
}

sub alter {
    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__ . '::alter', 'hash');
    }
    if (!$hr_params->{name}) {
        require Geoffrey::Exception::General;
        Geoffrey::Exception::General::throw_no_table_name('to alter');
    }
    my @ar_result = ();
    require Geoffrey::Utils;
    for (@{$hr_params->{alter}}) {
        my ($s_sub, $s_action) = Geoffrey::Utils::parse_package_sub($_->{action});
        my $obj_action = $self->action($s_action);
        if (!$s_sub || !$obj_action->can($s_sub)) {
            require Geoffrey::Exception::RequiredValue;
            Geoffrey::Exception::RequiredValue::throw_action_sub($s_action);
        }
        $_->{table} = $hr_params->{name};
        push @ar_result, $obj_action->$s_sub($_);
    }
    return \@ar_result;
}

sub drop {
    my ($self, $hr_params) = @_;
    require Ref::Util;
    my $s_name = Ref::Util::is_hashref($hr_params) ? $hr_params->{name} : undef;
    if (!$s_name) {
        require Geoffrey::Exception::General;
        Geoffrey::Exception::General::throw_no_table_name('to drop');
    }
    require Geoffrey::Utils;
    return $self->do(Geoffrey::Utils::replace_spare($self->converter->table->drop, [$s_name]));
}

sub list_from_schema {
    my ($self, $schema) = @_;
    return [map { $_->{name} } @{$self->do_arrayref($self->converter->table->list($schema), [])}];
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Geoffrey::Action::Table - Action handler for tables

=head1 VERSION

Version 0.000206

=head1 DESCRIPTION

=head1 SYNOPSIS

=head1 SUBROUTINES/METHODS

=head2 postfix

=head2 prefix

=head2 constraint_action

=head2 column_action

=head2 index_action

=head2 action

=head2 add

Create new table.

=head2 alter

Decides type of alter table
Run command of alter table

=head2 drop

Drop defined table.



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