line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Geoffrey::Action::Table; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
20446
|
use utf8; |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
43
|
|
4
|
7
|
|
|
7
|
|
342
|
use 5.016; |
|
7
|
|
|
|
|
32
|
|
5
|
7
|
|
|
7
|
|
35
|
use strict; |
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
168
|
|
6
|
7
|
|
|
7
|
|
38
|
use warnings; |
|
7
|
|
|
|
|
18
|
|
|
7
|
|
|
|
|
379
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$Geoffrey::Action::Table::VERSION = '0.000204'; |
9
|
|
|
|
|
|
|
|
10
|
7
|
|
|
7
|
|
1860
|
use parent 'Geoffrey::Role::Action'; |
|
7
|
|
|
|
|
1128
|
|
|
7
|
|
|
|
|
56
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub _hr_merge_templates { |
13
|
8
|
|
|
8
|
|
25
|
my ($self, $s_template, $s_table_name) = @_; |
14
|
8
|
100
|
66
|
|
|
111
|
if (($self->{template} && !$self->{template}->template($s_template)) || !$self->{template}) { |
|
|
|
66
|
|
|
|
|
15
|
1
|
|
|
|
|
822
|
require Geoffrey::Exception::Template; |
16
|
1
|
|
|
|
|
7
|
Geoffrey::Exception::Template::throw_template_not_found($s_template); |
17
|
|
|
|
|
|
|
} |
18
|
7
|
|
|
|
|
22
|
my $ar_template_columns = []; |
19
|
7
|
|
|
|
|
20
|
my $ar_template_constraints = []; |
20
|
7
|
|
|
|
|
22
|
$self->column_action->for_table(1); |
21
|
7
|
|
|
|
|
21
|
$self->constraint_action->for_table(1); |
22
|
7
|
|
|
|
|
13
|
for (@{$self->{template}->template($s_template)}) { |
|
7
|
|
|
|
|
28
|
|
23
|
38
|
|
|
|
|
102
|
$_->{table} = $s_table_name; |
24
|
38
|
|
|
|
|
53
|
push @{$ar_template_columns}, |
|
38
|
|
|
|
|
94
|
|
25
|
|
|
|
|
|
|
$self->column_action->add($_, |
26
|
|
|
|
|
|
|
$self->constraint_action->add($s_table_name, $_, $ar_template_constraints)); |
27
|
|
|
|
|
|
|
} |
28
|
7
|
|
|
|
|
23
|
$self->column_action->for_table(0); |
29
|
7
|
|
|
|
|
19
|
$self->constraint_action->for_table(0); |
30
|
7
|
|
|
|
|
36
|
return {columns => $ar_template_columns, constraints => $ar_template_constraints}; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub postfix { |
34
|
12
|
100
|
100
|
12
|
1
|
148
|
return $_[0]->{postfix} // q~~ if !defined $_[1]; |
35
|
2
|
|
|
|
|
5
|
$_[0]->{postfix} = $_[1]; |
36
|
2
|
|
|
|
|
7
|
return $_[0]->{postfix}; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub prefix { |
40
|
12
|
100
|
100
|
12
|
1
|
1683
|
return $_[0]->{prefix} // q~~ if !defined $_[1]; |
41
|
2
|
|
|
|
|
8
|
$_[0]->{prefix} = $_[1]; |
42
|
2
|
|
|
|
|
14
|
return $_[0]->{prefix}; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub constraint_action { |
46
|
64
|
|
|
64
|
1
|
104
|
my $self = shift; |
47
|
64
|
100
|
|
|
|
267
|
return $self->{constraint_action} if ($self->{constraint_action}); |
48
|
10
|
|
|
|
|
1698
|
require Geoffrey::Action::Constraint; |
49
|
|
|
|
|
|
|
$self->{constraint_action} |
50
|
10
|
|
|
|
|
64
|
= Geoffrey::Action::Constraint->new(converter => $self->converter, dbh => $self->dbh,); |
51
|
10
|
|
|
|
|
24
|
return $self->{constraint_action}; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub column_action { |
55
|
64
|
|
|
64
|
1
|
125
|
my $self = shift; |
56
|
64
|
100
|
|
|
|
268
|
return $self->{column_action} if ($self->{column_action}); |
57
|
10
|
|
|
|
|
1787
|
require Geoffrey::Action::Column; |
58
|
|
|
|
|
|
|
$self->{column_action} |
59
|
10
|
|
|
|
|
41
|
= Geoffrey::Action::Column->new(converter => $self->converter, dbh => $self->dbh,); |
60
|
10
|
|
|
|
|
24
|
return $self->{column_action}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub action { |
64
|
13
|
|
|
13
|
1
|
32
|
my ($self, $s_action) = @_; |
65
|
13
|
|
|
|
|
36
|
$s_action = join q//, map {ucfirst} split /_/, $s_action; |
|
12
|
|
|
|
|
43
|
|
66
|
13
|
|
|
|
|
68
|
require Geoffrey::Utils; |
67
|
13
|
|
|
|
|
54
|
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
|
194
|
my ($self, $hr_params) = @_; |
77
|
13
|
100
|
66
|
|
|
96
|
if (!$hr_params || !$hr_params->{name}) { |
78
|
1
|
|
|
|
|
538
|
require Geoffrey::Exception::RequiredValue; |
79
|
1
|
|
|
|
|
4
|
Geoffrey::Exception::RequiredValue::throw_table_name(__PACKAGE__); |
80
|
|
|
|
|
|
|
} |
81
|
12
|
|
|
|
|
30
|
my @columns = (); |
82
|
12
|
|
|
|
|
29
|
my $ar_constraints = []; |
83
|
12
|
|
|
|
|
69
|
my $constraint_action = $self->constraint_action; |
84
|
12
|
|
|
|
|
54
|
my $column_action = $self->column_action; |
85
|
12
|
100
|
|
|
|
48
|
if ($hr_params->{template}) { |
86
|
8
|
|
|
|
|
41
|
my $templates = $self->_hr_merge_templates($hr_params->{template}, $hr_params->{name}); |
87
|
7
|
|
|
|
|
14
|
push @columns, @{$templates->{columns}}; |
|
7
|
|
|
|
|
23
|
|
88
|
7
|
|
|
|
|
12
|
push @{$ar_constraints}, @{$templates->{constraints}}; |
|
7
|
|
|
|
|
55
|
|
|
7
|
|
|
|
|
30
|
|
89
|
|
|
|
|
|
|
} |
90
|
11
|
|
|
|
|
54
|
$constraint_action->for_table(1); |
91
|
11
|
|
|
|
|
45
|
$column_action->for_table(1); |
92
|
11
|
|
|
|
|
25
|
for my $hr_column (@{$hr_params->{columns}}) { |
|
11
|
|
|
|
|
39
|
|
93
|
39
|
50
|
|
|
|
103
|
$hr_column->{schema} = $hr_params->{schema} if exists $hr_params->{schema}; |
94
|
39
|
|
|
|
|
78
|
$hr_column->{table} = $hr_params->{name}; |
95
|
39
|
|
|
|
|
110
|
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
|
|
|
|
|
27
|
for (@{$hr_params->{constraints}}) { |
|
11
|
|
|
|
|
42
|
|
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
|
|
|
|
|
28
|
push @columns, @{$ar_constraints}; |
|
11
|
|
|
|
|
31
|
|
103
|
11
|
100
|
66
|
|
|
51
|
if (scalar @columns == 0 && !$self->converter->can_create_empty_table) { |
104
|
1
|
|
|
|
|
6
|
require Geoffrey::Exception::NotSupportedException; |
105
|
1
|
|
|
|
|
4
|
Geoffrey::Exception::NotSupportedException::throw_empty_table($self->converter, |
106
|
|
|
|
|
|
|
$hr_params); |
107
|
|
|
|
|
|
|
} |
108
|
10
|
|
|
|
|
39
|
$constraint_action->for_table(0); |
109
|
10
|
|
|
|
|
37
|
$column_action->for_table(0); |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
#prepare finaly created table to SQL |
112
|
10
|
|
|
|
|
532
|
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
|
|
|
|
41
|
]); |
118
|
10
|
|
|
|
|
69
|
return $self->do($sql); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub alter { |
122
|
13
|
|
|
13
|
1
|
569
|
my ($self, $hr_params) = @_; |
123
|
13
|
|
|
|
|
84
|
require Ref::Util; |
124
|
13
|
100
|
|
|
|
62
|
if (!Ref::Util::is_hashref($hr_params)) { |
125
|
1
|
|
|
|
|
605
|
require Geoffrey::Exception::General; |
126
|
1
|
|
|
|
|
6
|
Geoffrey::Exception::General::throw_wrong_ref(__PACKAGE__ . '::alter', 'hash'); |
127
|
|
|
|
|
|
|
} |
128
|
12
|
100
|
|
|
|
62
|
if (!$hr_params->{name}) { |
129
|
1
|
|
|
|
|
4
|
require Geoffrey::Exception::General; |
130
|
1
|
|
|
|
|
5
|
Geoffrey::Exception::General::throw_no_table_name('to alter'); |
131
|
|
|
|
|
|
|
} |
132
|
11
|
|
|
|
|
28
|
my @ar_result = (); |
133
|
11
|
|
|
|
|
40
|
require Geoffrey::Utils; |
134
|
11
|
|
|
|
|
33
|
for (@{$hr_params->{alter}}) { |
|
11
|
|
|
|
|
42
|
|
135
|
13
|
|
|
|
|
61
|
my ($s_sub, $s_action) = Geoffrey::Utils::parse_package_sub($_->{action}); |
136
|
13
|
|
|
|
|
59
|
my $obj_action = $self->action($s_action); |
137
|
12
|
100
|
66
|
|
|
163
|
if (!$s_sub || !$obj_action->can($s_sub)) { |
138
|
1
|
|
|
|
|
7
|
require Geoffrey::Exception::RequiredValue; |
139
|
1
|
|
|
|
|
7
|
Geoffrey::Exception::RequiredValue::throw_action_sub($s_action); |
140
|
|
|
|
|
|
|
} |
141
|
11
|
|
|
|
|
39
|
$_->{table} = $hr_params->{name}; |
142
|
11
|
|
|
|
|
64
|
push @ar_result, $obj_action->$s_sub($_); |
143
|
|
|
|
|
|
|
} |
144
|
3
|
|
|
|
|
32
|
return \@ar_result; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub drop { |
148
|
3
|
|
|
3
|
1
|
1650
|
my ($self, $hr_params) = @_; |
149
|
3
|
|
|
|
|
496
|
require Ref::Util; |
150
|
3
|
100
|
|
|
|
1675
|
my $s_name = Ref::Util::is_hashref($hr_params) ? $hr_params->{name} : undef; |
151
|
3
|
100
|
|
|
|
11
|
if (!$s_name) { |
152
|
2
|
|
|
|
|
548
|
require Geoffrey::Exception::General; |
153
|
2
|
|
|
|
|
10
|
Geoffrey::Exception::General::throw_no_table_name('to drop'); |
154
|
|
|
|
|
|
|
} |
155
|
1
|
|
|
|
|
4
|
require Geoffrey::Utils; |
156
|
1
|
|
|
|
|
6
|
return $self->do(Geoffrey::Utils::replace_spare($self->converter->table->drop, [$s_name])); |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub list_from_schema { |
160
|
3
|
|
|
3
|
1
|
20
|
my ($self, $schema) = @_; |
161
|
3
|
|
|
|
|
9
|
return [map { $_->{name} } @{$self->do_arrayref($self->converter->table->list($schema), [])}]; |
|
19
|
|
|
|
|
1328
|
|
|
3
|
|
|
|
|
19
|
|
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
1; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
__END__ |