File Coverage

blib/lib/Geoffrey/Action/Constraint/Unique.pm
Criterion Covered Total %
statement 59 67 88.0
branch 15 20 75.0
condition 3 6 50.0
subroutine 11 11 100.0
pod 4 4 100.0
total 92 108 85.1


line stmt bran cond sub pod time code
1             package Geoffrey::Action::Constraint::Unique;
2              
3 6     6   190795 use utf8;
  6         15  
  6         42  
4 6     6   318 use 5.016;
  6         21  
5 6     6   32 use strict;
  6         11  
  6         155  
6 6     6   28 use warnings;
  6         11  
  6         336  
7 6     6   2808 use Ref::Util;
  6         13245  
  6         421  
8 6     6   44 use Time::HiRes qw/ time /;
  6         11  
  6         58  
9              
10             $Geoffrey::Action::Constraint::Unique::VERSION = '0.000206';
11              
12 6     6   604 use parent 'Geoffrey::Role::Action';
  6         14  
  6         40  
13              
14             sub add {
15 11     11 1 6223 my ( $self, $s_table_name, $hr_params ) = @_;
16 11         41 my $unique = $self->converter->unique;
17 11 50       109 if ( !$unique ) {
18 0         0 require Geoffrey::Exception::NotSupportedException;
19 0         0 Geoffrey::Exception::NotSupportedException::throw_unique( 'add', $self->converter );
20             }
21 11 100       28 if ( !$s_table_name ) {
22 2         638 require Geoffrey::Exception::RequiredValue;
23 2         6 Geoffrey::Exception::RequiredValue::throw_table_name( __PACKAGE__ . '::add' );
24             }
25 9 50       23 return () unless $hr_params;
26 9 100       27 if ( !Ref::Util::is_hashref($hr_params) ) {
27 2         549 require Geoffrey::Exception::General;
28 2         11 Geoffrey::Exception::General::throw_wrong_ref( __PACKAGE__ . '::add', 'hash' );
29             }
30 7 100 66     25 if ( !$hr_params->{columns} || scalar @{ $hr_params->{columns} } == 0 ) {
  5         28  
31 2         13 require Geoffrey::Exception::RequiredValue;
32 2         8 Geoffrey::Exception::RequiredValue::throw_table_column( __PACKAGE__ . '::add' );
33             }
34              
35 5         15 $s_table_name =~ s/"//g;
36 5         18 my $gentime = time;
37 5         73 $gentime =~ s/\.//g;
38 5   33     21 $hr_params->{name} ||= 'uidx_' . $s_table_name . '_' . $gentime;
39              
40 5         665 require Geoffrey::Utils;
41             return Geoffrey::Utils::replace_spare( $unique->add,
42 5 100       21 [ $hr_params->{name}, join q/,/, @{ $hr_params->{columns} } ] )
  2         22  
43             if $self->for_table;
44             return $self->do(
45             Geoffrey::Utils::replace_spare(
46             $unique->append,
47 3         13 [ $hr_params->{name}, $s_table_name, join q/,/, @{ $hr_params->{columns} } ]
  3         19  
48             )
49             );
50              
51             }
52              
53             sub alter {
54 1     1 1 55 my ( $self, $s_table_name, $hr_column_params ) = @_;
55 1         6 my $unique = $self->converter->unique;
56 1 50       5 if ( !$unique ) {
57 0         0 require Geoffrey::Exception::NotSupportedException;
58 0         0 Geoffrey::Exception::NotSupportedException::throw_unique( 'alter', $self->converter );
59             }
60             return [
61 1         7 $self->drop( $s_table_name, $hr_column_params ),
62             $self->append( $s_table_name, $hr_column_params ),
63             ];
64             }
65              
66             sub drop {
67 2     2 1 5 my ( $self, $s_table_name, $hr_column_params ) = @_;
68 2         5 my $unique = $self->converter->unique;
69 2 50       4 if ( !$unique ) {
70 0         0 require Geoffrey::Exception::NotSupportedException;
71 0         0 Geoffrey::Exception::NotSupportedException::throw_unique( 'drop', $self->converter );
72             }
73 2 100       10 if ( !$s_table_name ) {
74 1         9 require Geoffrey::Exception::RequiredValue;
75 1         7 Geoffrey::Exception::RequiredValue::throw_table_name( __PACKAGE__ . '::drop' );
76             }
77 1         7 require Geoffrey::Utils;
78             return Geoffrey::Utils::replace_spare( $unique->drop,
79 1         4 [ $s_table_name, $hr_column_params->{name}, ] );
80             }
81              
82             sub list_from_schema {
83 4     4 1 12 my ( $self, $schema ) = @_;
84 4         21 my $unique = $self->converter->unique;
85 4 50       55 if ( !$unique ) {
86 0         0 require Geoffrey::Exception::NotSupportedException;
87 0         0 Geoffrey::Exception::NotSupportedException::throw_unique( 'list', $self->converter );
88             }
89 4         13 return $self->converter->unique_information( $self->do_arrayref( $unique->list($schema) ) );
90             }
91              
92             1;
93              
94             __END__