Geoffrey

 view release on metacpan or  search on metacpan

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

package Geoffrey::Action::Constraint::Unique;

use utf8;
use 5.016;
use strict;
use warnings;
use Ref::Util;
use Time::HiRes qw/ time /;

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

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

sub add {
    my ( $self, $s_table_name, $hr_params ) = @_;
    my $unique = $self->converter->unique;
    if ( !$unique ) {
        require Geoffrey::Exception::NotSupportedException;
        Geoffrey::Exception::NotSupportedException::throw_unique( 'add', $self->converter );
    }
    if ( !$s_table_name ) {
        require Geoffrey::Exception::RequiredValue;
        Geoffrey::Exception::RequiredValue::throw_table_name( __PACKAGE__ . '::add' );
    }
    return () unless $hr_params;
    if ( !Ref::Util::is_hashref($hr_params) ) {
        require Geoffrey::Exception::General;
        Geoffrey::Exception::General::throw_wrong_ref( __PACKAGE__ . '::add', 'hash' );
    }
    if ( !$hr_params->{columns} || scalar @{ $hr_params->{columns} } == 0 ) {
        require Geoffrey::Exception::RequiredValue;
        Geoffrey::Exception::RequiredValue::throw_table_column( __PACKAGE__ . '::add' );
    }

    $s_table_name =~ s/"//g;
    my $gentime = time;
    $gentime =~ s/\.//g;
    $hr_params->{name} ||= 'uidx_' . $s_table_name . '_' . $gentime;

    require Geoffrey::Utils;
    return Geoffrey::Utils::replace_spare( $unique->add,
        [ $hr_params->{name}, join q/,/, @{ $hr_params->{columns} } ] )
      if $self->for_table;
    return $self->do(
        Geoffrey::Utils::replace_spare(
            $unique->append,
            [ $hr_params->{name}, $s_table_name, join q/,/, @{ $hr_params->{columns} } ]
        )
    );

}

sub alter {
    my ( $self, $s_table_name, $hr_column_params ) = @_;
    my $unique = $self->converter->unique;
    if ( !$unique ) {
        require Geoffrey::Exception::NotSupportedException;
        Geoffrey::Exception::NotSupportedException::throw_unique( 'alter', $self->converter );
    }
    return [
        $self->drop( $s_table_name, $hr_column_params ),
        $self->append( $s_table_name, $hr_column_params ),
    ];
}

sub drop {
    my ( $self, $s_table_name, $hr_column_params ) = @_;
    my $unique = $self->converter->unique;
    if ( !$unique ) {
        require Geoffrey::Exception::NotSupportedException;
        Geoffrey::Exception::NotSupportedException::throw_unique( 'drop', $self->converter );
    }
    if ( !$s_table_name ) {
        require Geoffrey::Exception::RequiredValue;
        Geoffrey::Exception::RequiredValue::throw_table_name( __PACKAGE__ . '::drop' );
    }
    require Geoffrey::Utils;
    return Geoffrey::Utils::replace_spare( $unique->drop,
        [ $s_table_name, $hr_column_params->{name}, ] );
}

sub list_from_schema {
    my ( $self, $schema ) = @_;
    my $unique = $self->converter->unique;
    if ( !$unique ) {
        require Geoffrey::Exception::NotSupportedException;



( run in 0.656 second using v1.01-cache-2.11-cpan-39bf76dae61 )