File Coverage

blib/lib/Geoffrey/Action/Column.pm
Criterion Covered Total %
statement 62 65 95.3
branch 27 30 90.0
condition 13 18 72.2
subroutine 10 10 100.0
pod 5 5 100.0
total 117 128 91.4


line stmt bran cond sub pod time code
1             package Geoffrey::Action::Column;
2              
3 4     4   2084 use utf8;
  4         10  
  4         28  
4 4     4   230 use 5.016;
  4         15  
5 4     4   56 use strict;
  4         7  
  4         97  
6 4     4   25 use warnings;
  4         8  
  4         360  
7              
8             $Geoffrey::Action::Column::VERSION = '0.000206';
9              
10 4     4   26 use parent 'Geoffrey::Role::Action';
  4         6  
  4         67  
11              
12             sub add {
13 86     86 1 3999 my ($self, $hr_params, $constraint) = @_;
14 86         929 require Ref::Util;
15 86 100       2778 if (!Ref::Util::is_hashref($hr_params)) {
16 1         924 require Geoffrey::Exception::General;
17 1         6 Geoffrey::Exception::General::throw_wrong_ref(__PACKAGE__ . '::add', 'hash');
18             }
19 85 100       211 return $self->appending($hr_params->{table}, $hr_params, $constraint) if $self->for_table;
20 8         24 my $tables = $self->converter->table;
21 8 100 66     103 if (!$tables || !$tables->can('add_column')) {
22 1         5 require Geoffrey::Exception::NotSupportedException;
23 1         4 Geoffrey::Exception::NotSupportedException::throw_column('add', $self->converter);
24             }
25 7 100       23 if (defined $hr_params->{primarykey}) {
26 1         704 require Geoffrey::Exception::RequiredValue;
27             Geoffrey::Exception::RequiredValue::throw_column_default($hr_params->{table},
28 1         8 $hr_params->{name});
29             }
30 6         560 require Geoffrey::Utils;
31             my $sql = Geoffrey::Utils::replace_spare(
32             $tables->add_column,
33             [
34             $hr_params->{table},
35             (
36             join q/ /,
37             (
38 6   66     20 $hr_params->{name}, $self->converter()->type($hr_params),
      66        
39             $constraint // (), $self->defaults($hr_params) // ()))]);
40 5         43 return $self->do($sql);
41              
42             }
43              
44             sub drop {
45 5     5 1 2537 my ($self, $hr_params) = @_;
46 5         39 require Ref::Util;
47 5 100       31 if (!Ref::Util::is_hashref($hr_params)) {
48 1         4 require Geoffrey::Exception::General;
49 1         5 Geoffrey::Exception::General::throw_wrong_ref(__PACKAGE__ . '::drop', 'hash');
50             }
51 4         22 my $table = $self->converter->table;
52 4 100 66     82 if (!$table || !$table->can('drop_column')) {
53 3         17 require Geoffrey::Exception::NotSupportedException;
54 3         16 Geoffrey::Exception::NotSupportedException::throw_column('drop', $self->converter);
55             }
56 1         6 require Geoffrey::Utils;
57             return [
58             map {
59             $self->do(
60 1         5 Geoffrey::Utils::replace_spare($table->drop_column, [$hr_params->{table}, $_]))
61 1         3 } @{$hr_params->{dropcolumn}}];
  1         4  
62             }
63              
64             sub list_from_schema {
65 3     3 1 66 my ($self, $schema, $table) = @_;
66 3         16 my $converter = $self->converter;
67 3         18 return $converter->colums_information(
68             $self->do_arrayref($converter->table->s_list_columns($schema), [$table]));
69             }
70              
71             sub appending {
72 77     77 1 183 my ($self, $s_table_name, $hr_params, $constraint) = @_;
73 77 100       279 my $b_primarykey = (defined $hr_params->{primarykey}) ? 1 : 0;
74 77 100 100     293 my $b_has_value = (exists $hr_params->{default} || exists $hr_params->{foreignkey}) ? 1 : 0;
75 77 50 66     228 if ($b_primarykey && !$b_has_value) {
76 0         0 require Geoffrey::Exception::RequiredValue;
77 0         0 Geoffrey::Exception::RequiredValue::throw_column_default($s_table_name, $hr_params->{name});
78             }
79             return join q/ /,
80             (
81 77         212 $hr_params->{name}, $self->converter()->type($hr_params),
82             $constraint, $self->defaults($hr_params),
83             );
84             }
85              
86             sub defaults {
87 86     86 1 2711 my ($self, $params) = @_;
88 86 100       468 return () if !defined $params->{default};
89 49         152 my $defaults = $self->converter()->defaults;
90 49         136 my $default_by_key = $defaults->{$params->{default}};
91              
92 49 100       118 unless ($default_by_key) {
93 37 100       155 return 'DEFAULT ' . $params->{default} if $params->{default} eq q/''/;
94 30         113 return 'DEFAULT ' . $self->converter()->convert_defaults($params);
95             }
96              
97 12 100       182 if ($params->{default} eq 'autoincrement') {
    50          
98 8 50       72 return $self->sequences->add($params) if $defaults->{$params->{default}} eq 'sequence';
99 8         86 return $defaults->{$params->{default}};
100             }
101             elsif (defined $defaults->{$params->{default}}) {
102 4         35 return 'DEFAULT ' . $defaults->{$params->{default}};
103             }
104 0           return ();
105             }
106              
107             1;
108              
109             __END__