File Coverage

blib/lib/Geoffrey/Action/Table.pm
Criterion Covered Total %
statement 118 120 98.3
branch 28 32 87.5
condition 14 19 73.6
subroutine 15 15 100.0
pod 9 9 100.0
total 184 195 94.3


line stmt bran cond sub pod time code
1             package Geoffrey::Action::Table;
2              
3 7     7   267606 use utf8;
  7         13  
  7         65  
4 7     7   418 use 5.016;
  7         28  
5 7     7   70 use strict;
  7         24  
  7         269  
6 7     7   34 use warnings;
  7         11  
  7         674  
7              
8             $Geoffrey::Action::Table::VERSION = '0.000206';
9              
10 7     7   958 use parent 'Geoffrey::Role::Action';
  7         705  
  7         54  
11              
12             sub _hr_merge_templates {
13 8     8   30 my ($self, $s_template, $s_table_name) = @_;
14 8 100 66     98 if (($self->{template} && !$self->{template}->template($s_template)) || !$self->{template}) {
      66        
15 1         922 require Geoffrey::Exception::Template;
16 1         5 Geoffrey::Exception::Template::throw_template_not_found($s_template);
17             }
18 7         37 my $ar_template_columns = [];
19 7         15 my $ar_template_constraints = [];
20 7         24 $self->column_action->for_table(1);
21 7         23 $self->constraint_action->for_table(1);
22 7         14 for (@{$self->{template}->template($s_template)}) {
  7         33  
23 38         102 $_->{table} = $s_table_name;
24 38         63 push @{$ar_template_columns},
  38         171  
25             $self->column_action->add($_,
26             $self->constraint_action->add($s_table_name, $_, $ar_template_constraints));
27             }
28 7         30 $self->column_action->for_table(0);
29 7         21 $self->constraint_action->for_table(0);
30 7         39 return {columns => $ar_template_columns, constraints => $ar_template_constraints};
31             }
32              
33             sub postfix {
34 12 100 100 12 1 133 return $_[0]->{postfix} // q~~ if !defined $_[1];
35 2         5 $_[0]->{postfix} = $_[1];
36 2         9 return $_[0]->{postfix};
37             }
38              
39             sub prefix {
40 12 100 100 12 1 1814 return $_[0]->{prefix} // q~~ if !defined $_[1];
41 2         11 $_[0]->{prefix} = $_[1];
42 2         12 return $_[0]->{prefix};
43             }
44              
45             sub constraint_action {
46 64     64 1 101 my $self = shift;
47 64 100       305 return $self->{constraint_action} if ($self->{constraint_action});
48 10         3149 require Geoffrey::Action::Constraint;
49             $self->{constraint_action}
50 10         64 = Geoffrey::Action::Constraint->new(converter => $self->converter, dbh => $self->dbh,);
51 10         27 return $self->{constraint_action};
52             }
53              
54             sub column_action {
55 64     64 1 224 my $self = shift;
56 64 100       280 return $self->{column_action} if ($self->{column_action});
57 10         2732 require Geoffrey::Action::Column;
58             $self->{column_action}
59 10         54 = Geoffrey::Action::Column->new(converter => $self->converter, dbh => $self->dbh,);
60 10         34 return $self->{column_action};
61             }
62              
63             sub action {
64 13     13 1 34 my ($self, $s_action) = @_;
65 13         36 $s_action = join q//, map {ucfirst} split /_/, $s_action;
  12         37  
66 13         64 require Geoffrey::Utils;
67 13         46 return Geoffrey::Utils::action_obj_from_name(
68             $s_action,
69             dbh => $self->dbh,
70             converter => $self->converter,
71             dryrun => $self->dryrun
72             );
73             }
74              
75             sub add {
76 13     13 1 221 my ($self, $hr_params) = @_;
77 13 100 66     92 if (!$hr_params || !$hr_params->{name}) {
78 1         769 require Geoffrey::Exception::RequiredValue;
79 1         6 Geoffrey::Exception::RequiredValue::throw_table_name(__PACKAGE__);
80             }
81 12         38 my @columns = ();
82 12         24 my $ar_constraints = [];
83 12         45 my $constraint_action = $self->constraint_action;
84 12         41 my $column_action = $self->column_action;
85 12 100       81 if ($hr_params->{template}) {
86 8         49 my $templates = $self->_hr_merge_templates($hr_params->{template}, $hr_params->{name});
87 7         15 push @columns, @{$templates->{columns}};
  7         26  
88 7         14 push @{$ar_constraints}, @{$templates->{constraints}};
  7         13  
  7         50  
89             }
90 11         47 $constraint_action->for_table(1);
91 11         71 $column_action->for_table(1);
92 11         17 for my $hr_column (@{$hr_params->{columns}}) {
  11         40  
93 39 50       126 $hr_column->{schema} = $hr_params->{schema} if exists $hr_params->{schema};
94 39         95 $hr_column->{table} = $hr_params->{name};
95 39         112 my $const = $constraint_action->add($hr_params->{name}, $hr_column, $ar_constraints);
96 39         110 push @columns, $column_action->add($hr_column, $const);
97             }
98 11         25 for (@{$hr_params->{constraints}}) {
  11         41  
99 0 0       0 $_->{schema} = $hr_params->{schema} if exists $hr_params->{schema};
100 0         0 $constraint_action->add($hr_params->{name}, $_, $ar_constraints);
101             }
102 11         21 push @columns, @{$ar_constraints};
  11         30  
103 11 100 66     66 if (scalar @columns == 0 && !$self->converter->can_create_empty_table) {
104 1         8 require Geoffrey::Exception::NotSupportedException;
105 1         6 Geoffrey::Exception::NotSupportedException::throw_empty_table($self->converter,
106             $hr_params);
107             }
108 10         42 $constraint_action->for_table(0);
109 10         50 $column_action->for_table(0);
110              
111             #prepare finaly created table to SQL
112 10         766 require Geoffrey::Utils;
113             my $sql = Geoffrey::Utils::replace_spare( $self->converter->table->add, [
114             ($hr_params->{schema} ? $hr_params->{schema} . q/./ : q//) . $self->prefix . $hr_params->{name} . $self->postfix,
115             join(q/,/, @columns),
116             $hr_params->{engine}, $hr_params->{charset}
117 10 50       48 ]);
118 10         69 return $self->do($sql);
119             }
120              
121             sub alter {
122 13     13 1 553 my ($self, $hr_params) = @_;
123 13         86 require Ref::Util;
124 13 100       64 if (!Ref::Util::is_hashref($hr_params)) {
125 1         713 require Geoffrey::Exception::General;
126 1         7 Geoffrey::Exception::General::throw_wrong_ref(__PACKAGE__ . '::alter', 'hash');
127             }
128 12 100       36 if (!$hr_params->{name}) {
129 1         2 require Geoffrey::Exception::General;
130 1         3 Geoffrey::Exception::General::throw_no_table_name('to alter');
131             }
132 11         18 my @ar_result = ();
133 11         36 require Geoffrey::Utils;
134 11         20 for (@{$hr_params->{alter}}) {
  11         30  
135 13         54 my ($s_sub, $s_action) = Geoffrey::Utils::parse_package_sub($_->{action});
136 13         47 my $obj_action = $self->action($s_action);
137 12 100 66     173 if (!$s_sub || !$obj_action->can($s_sub)) {
138 1         6 require Geoffrey::Exception::RequiredValue;
139 1         7 Geoffrey::Exception::RequiredValue::throw_action_sub($s_action);
140             }
141 11         41 $_->{table} = $hr_params->{name};
142 11         53 push @ar_result, $obj_action->$s_sub($_);
143             }
144 3         31 return \@ar_result;
145             }
146              
147             sub drop {
148 3     3 1 1820 my ($self, $hr_params) = @_;
149 3         688 require Ref::Util;
150 3 100       5846 my $s_name = Ref::Util::is_hashref($hr_params) ? $hr_params->{name} : undef;
151 3 100       14 if (!$s_name) {
152 2         2010 require Geoffrey::Exception::General;
153 2         10 Geoffrey::Exception::General::throw_no_table_name('to drop');
154             }
155 1         6 require Geoffrey::Utils;
156 1         5 return $self->do(Geoffrey::Utils::replace_spare($self->converter->table->drop, [$s_name]));
157             }
158              
159             sub list_from_schema {
160 3     3 1 10 my ($self, $schema) = @_;
161 3         9 return [map { $_->{name} } @{$self->do_arrayref($self->converter->table->list($schema), [])}];
  19         1500  
  3         19  
162             }
163              
164             1;
165              
166             __END__