File Coverage

blib/lib/Geoffrey/Action/Constraint/ForeignKey.pm
Criterion Covered Total %
statement 56 58 96.5
branch 23 28 82.1
condition 3 6 50.0
subroutine 9 9 100.0
pod 4 4 100.0
total 95 105 90.4


line stmt bran cond sub pod time code
1             package Geoffrey::Action::Constraint::ForeignKey;
2              
3 7     7   254041 use utf8;
  7         16  
  7         79  
4 7     7   435 use 5.016;
  7         28  
5 7     7   40 use strict;
  7         12  
  7         238  
6 7     7   33 use warnings;
  7         13  
  7         705  
7              
8             $Geoffrey::Action::Constraint::ForeignKey::VERSION = '0.000206';
9              
10 7     7   46 use parent 'Geoffrey::Role::Action';
  7         66  
  7         54  
11              
12             sub add {
13 25     25 1 10599 my ($self, $hr_params) = @_;
14 25         982 require Ref::Util;
15 25 100       4876 if (!Ref::Util::is_hashref($hr_params)) {
16 1         833 require Geoffrey::Exception::General;
17 1         8 Geoffrey::Exception::General::throw_wrong_ref(__PACKAGE__ . '::add', 'hash');
18             }
19 24 100       187 if (!$self->converter->foreign_key) {
20 1         7 require Geoffrey::Exception::NotSupportedException;
21 1         7 Geoffrey::Exception::NotSupportedException::throw_foreignkey('add', $self->converter);
22             }
23 23 100       78 if (!exists $hr_params->{table}) {
24 1         948 require Geoffrey::Exception::RequiredValue;
25 1         5 Geoffrey::Exception::RequiredValue::throw_table_name();
26             }
27 22 50 66     166 if ( !exists $hr_params->{column}
      33        
28             || !exists $hr_params->{reftable}
29             || !exists $hr_params->{refcolumn})
30             {
31 3         8 require Geoffrey::Exception::RequiredValue;
32             Geoffrey::Exception::RequiredValue::throw_reftable_missing($hr_params->{table})
33 3 100       10 if (!exists $hr_params->{reftable});
34             Geoffrey::Exception::RequiredValue::throw_refcolumn_missing($hr_params->{table})
35 2 100       9 if (!exists $hr_params->{refcolumn});
36             Geoffrey::Exception::RequiredValue::throw_table_column($hr_params->{table})
37 1 50       5 if (!exists $hr_params->{column});
38             }
39 19         67 $hr_params->{reftable} =~ s/"//g;
40 19         47 $hr_params->{table} =~ s/"//g;
41 19         839 require Geoffrey::Utils;
42             my $s_sql = Geoffrey::Utils::replace_spare(
43             $self->converter->foreign_key->add,
44             [
45             $hr_params->{column},
46             (exists $hr_params->{schema} ? $hr_params->{schema} . q/./ : q//)
47             . $hr_params->{reftable},
48             $hr_params->{refcolumn},
49 19 50       58 qq~fkey_$hr_params->{table}~ . q~_~ . time
50             ]);
51 19 100       161 return $s_sql if $self->for_table;
52 2         17 return $self->do($s_sql);
53              
54             }
55              
56             sub alter {
57 6     6 1 4405 my ($self, $hr_params) = @_;
58 6 50       62 if (!exists $hr_params->{name}) {
59 0         0 require Geoffrey::Exception::RequiredValue;
60 0         0 Geoffrey::Exception::RequiredValue::throw_index_name(__PACKAGE__ . '::alter');
61             }
62 6 100       30 if (!$self->converter->foreign_key) {
63 1         12 require Geoffrey::Exception::NotSupportedException;
64 1         17 Geoffrey::Exception::NotSupportedException::throw_foreignkey('alter', $self->converter);
65             }
66 5         20 return [$self->drop($hr_params), $self->add($hr_params),];
67             }
68              
69             sub drop {
70 10     10 1 3619 my ($self, $hr_params) = @_;
71 10 100       44 if (!$self->converter->foreign_key) {
72 1         10 require Geoffrey::Exception::NotSupportedException;
73 1         6 Geoffrey::Exception::NotSupportedException::throw_foreignkey('drop',
74             $self->converter->foreign_key);
75             }
76 9         702 require Geoffrey::Utils;
77             my $s_sql = Geoffrey::Utils::replace_spare($self->converter->foreign_key->drop,
78 9         29 [$hr_params->{table}, $hr_params->{name}]);
79 2 50       21 return $s_sql if $self->for_table;
80 2         29 return $self->do($s_sql);
81             }
82              
83             sub list_from_schema {
84 4     4 1 3530 my ($self, $schema) = @_;
85 4         23 my $converter = $self->converter;
86 4 100       15 if (!$self->converter->foreign_key) {
87 1         11 require Geoffrey::Exception::NotSupportedException;
88 1         4 Geoffrey::Exception::NotSupportedException::throw_foreignkey('list', $self->converter);
89             }
90 3         13 return $converter->foreignkey_information(
91             $self->do_arrayref($self->converter->foreign_key->list($schema)));
92             }
93              
94             1;
95              
96             __END__