line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Geoffrey::Action::Table; |
2
|
|
|
|
|
|
|
|
3
|
7
|
|
|
7
|
|
20516
|
use utf8; |
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
45
|
|
4
|
7
|
|
|
7
|
|
314
|
use 5.016; |
|
7
|
|
|
|
|
28
|
|
5
|
7
|
|
|
7
|
|
38
|
use strict; |
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
224
|
|
6
|
7
|
|
|
7
|
|
41
|
use warnings; |
|
7
|
|
|
|
|
26
|
|
|
7
|
|
|
|
|
457
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$Geoffrey::Action::Table::VERSION = '0.000203'; |
9
|
|
|
|
|
|
|
|
10
|
7
|
|
|
7
|
|
1896
|
use parent 'Geoffrey::Role::Action'; |
|
7
|
|
|
|
|
1162
|
|
|
7
|
|
|
|
|
58
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub _hr_merge_templates { |
13
|
8
|
|
|
8
|
|
23
|
my ($self, $s_template, $s_table_name) = @_; |
14
|
8
|
100
|
66
|
|
|
81
|
if (($self->{template} && !$self->{template}->template($s_template)) || !$self->{template}) { |
|
|
|
66
|
|
|
|
|
15
|
1
|
|
|
|
|
690
|
require Geoffrey::Exception::Template; |
16
|
1
|
|
|
|
|
6
|
Geoffrey::Exception::Template::throw_template_not_found($s_template); |
17
|
|
|
|
|
|
|
} |
18
|
7
|
|
|
|
|
17
|
my $ar_template_columns = []; |
19
|
7
|
|
|
|
|
14
|
my $ar_template_constraints = []; |
20
|
7
|
|
|
|
|
19
|
$self->column_action->for_table(1); |
21
|
7
|
|
|
|
|
19
|
$self->constraint_action->for_table(1); |
22
|
7
|
|
|
|
|
13
|
for (@{$self->{template}->template($s_template)}) { |
|
7
|
|
|
|
|
27
|
|
23
|
38
|
|
|
|
|
91
|
$_->{table} = $s_table_name; |
24
|
38
|
|
|
|
|
50
|
push @{$ar_template_columns}, |
|
38
|
|
|
|
|
91
|
|
25
|
|
|
|
|
|
|
$self->column_action->add($_, |
26
|
|
|
|
|
|
|
$self->constraint_action->add($s_table_name, $_, $ar_template_constraints)); |
27
|
|
|
|
|
|
|
} |
28
|
7
|
|
|
|
|
21
|
$self->column_action->for_table(0); |
29
|
7
|
|
|
|
|
23
|
$self->constraint_action->for_table(0); |
30
|
7
|
|
|
|
|
32
|
return {columns => $ar_template_columns, constraints => $ar_template_constraints}; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub postfix { |
34
|
12
|
100
|
100
|
12
|
1
|
122
|
return $_[0]->{postfix} // q~~ if !defined $_[1]; |
35
|
2
|
|
|
|
|
5
|
$_[0]->{postfix} = $_[1]; |
36
|
2
|
|
|
|
|
8
|
return $_[0]->{postfix}; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub prefix { |
40
|
12
|
100
|
100
|
12
|
1
|
1356
|
return $_[0]->{prefix} // q~~ if !defined $_[1]; |
41
|
2
|
|
|
|
|
9
|
$_[0]->{prefix} = $_[1]; |
42
|
2
|
|
|
|
|
15
|
return $_[0]->{prefix}; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub constraint_action { |
46
|
64
|
|
|
64
|
1
|
95
|
my $self = shift; |
47
|
64
|
100
|
|
|
|
244
|
return $self->{constraint_action} if ($self->{constraint_action}); |
48
|
10
|
|
|
|
|
1620
|
require Geoffrey::Action::Constraint; |
49
|
|
|
|
|
|
|
$self->{constraint_action} |
50
|
10
|
|
|
|
|
63
|
= 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
|
107
|
my $self = shift; |
56
|
64
|
100
|
|
|
|
218
|
return $self->{column_action} if ($self->{column_action}); |
57
|
10
|
|
|
|
|
1443
|
require Geoffrey::Action::Column; |
58
|
|
|
|
|
|
|
$self->{column_action} |
59
|
10
|
|
|
|
|
47
|
= Geoffrey::Action::Column->new(converter => $self->converter, dbh => $self->dbh,); |
60
|
10
|
|
|
|
|
26
|
return $self->{column_action}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub action { |
64
|
13
|
|
|
13
|
1
|
32
|
my ($self, $s_action) = @_; |
65
|
13
|
|
|
|
|
41
|
$s_action = join q//, map {ucfirst} split /_/, $s_action; |
|
12
|
|
|
|
|
34
|
|
66
|
13
|
|
|
|
|
65
|
require Geoffrey::Utils; |
67
|
13
|
|
|
|
|
51
|
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
|
237
|
my ($self, $hr_params) = @_; |
77
|
13
|
100
|
66
|
|
|
89
|
if (!$hr_params || !$hr_params->{name}) { |
78
|
1
|
|
|
|
|
596
|
require Geoffrey::Exception::RequiredValue; |
79
|
1
|
|
|
|
|
4
|
Geoffrey::Exception::RequiredValue::throw_table_name(__PACKAGE__); |
80
|
|
|
|
|
|
|
} |
81
|
12
|
|
|
|
|
42
|
my @columns = (); |
82
|
12
|
|
|
|
|
27
|
my $ar_constraints = []; |
83
|
12
|
|
|
|
|
47
|
my $constraint_action = $self->constraint_action; |
84
|
12
|
|
|
|
|
40
|
my $column_action = $self->column_action; |
85
|
12
|
100
|
|
|
|
44
|
if ($hr_params->{template}) { |
86
|
8
|
|
|
|
|
43
|
my $templates = $self->_hr_merge_templates($hr_params->{template}, $hr_params->{name}); |
87
|
7
|
|
|
|
|
17
|
push @columns, @{$templates->{columns}}; |
|
7
|
|
|
|
|
22
|
|
88
|
7
|
|
|
|
|
15
|
push @{$ar_constraints}, @{$templates->{constraints}}; |
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
25
|
|
89
|
|
|
|
|
|
|
} |
90
|
11
|
|
|
|
|
57
|
$constraint_action->for_table(1); |
91
|
11
|
|
|
|
|
45
|
$column_action->for_table(1); |
92
|
11
|
|
|
|
|
19
|
for my $hr_column (@{$hr_params->{columns}}) { |
|
11
|
|
|
|
|
40
|
|
93
|
39
|
50
|
|
|
|
100
|
$hr_column->{schema} = $hr_params->{schema} if exists $hr_params->{schema}; |
94
|
39
|
|
|
|
|
88
|
$hr_column->{table} = $hr_params->{name}; |
95
|
39
|
|
|
|
|
111
|
my $const = $constraint_action->add($hr_params->{name}, $hr_column, $ar_constraints); |
96
|
39
|
|
|
|
|
121
|
push @columns, $column_action->add($hr_column, $const); |
97
|
|
|
|
|
|
|
} |
98
|
11
|
|
|
|
|
24
|
for (@{$hr_params->{constraints}}) { |
|
11
|
|
|
|
|
40
|
|
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
|
|
|
|
|
25
|
push @columns, @{$ar_constraints}; |
|
11
|
|
|
|
|
26
|
|
103
|
11
|
100
|
66
|
|
|
48
|
if (scalar @columns == 0 && !$self->converter->can_create_empty_table) { |
104
|
1
|
|
|
|
|
7
|
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
|
|
|
|
|
34
|
$column_action->for_table(0); |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
#prepare finaly created table to SQL |
112
|
10
|
|
|
|
|
616
|
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
|
|
|
|
|
63
|
return $self->do($sql); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub alter { |
122
|
13
|
|
|
13
|
1
|
505
|
my ($self, $hr_params) = @_; |
123
|
13
|
|
|
|
|
90
|
require Ref::Util; |
124
|
13
|
100
|
|
|
|
58
|
if (!Ref::Util::is_hashref($hr_params)) { |
125
|
1
|
|
|
|
|
623
|
require Geoffrey::Exception::General; |
126
|
1
|
|
|
|
|
7
|
Geoffrey::Exception::General::throw_wrong_ref(__PACKAGE__ . '::alter', 'hash'); |
127
|
|
|
|
|
|
|
} |
128
|
12
|
100
|
|
|
|
38
|
if (!$hr_params->{name}) { |
129
|
1
|
|
|
|
|
6
|
require Geoffrey::Exception::General; |
130
|
1
|
|
|
|
|
25
|
Geoffrey::Exception::General::throw_no_table_name('to alter'); |
131
|
|
|
|
|
|
|
} |
132
|
11
|
|
|
|
|
27
|
my @ar_result = (); |
133
|
11
|
|
|
|
|
39
|
require Geoffrey::Utils; |
134
|
11
|
|
|
|
|
24
|
for (@{$hr_params->{alter}}) { |
|
11
|
|
|
|
|
34
|
|
135
|
13
|
|
|
|
|
57
|
my ($s_sub, $s_action) = Geoffrey::Utils::parse_package_sub($_->{action}); |
136
|
13
|
|
|
|
|
58
|
my $obj_action = $self->action($s_action); |
137
|
12
|
100
|
66
|
|
|
149
|
if (!$s_sub || !$obj_action->can($s_sub)) { |
138
|
1
|
|
|
|
|
5
|
require Geoffrey::Exception::RequiredValue; |
139
|
1
|
|
|
|
|
7
|
Geoffrey::Exception::RequiredValue::throw_action_sub($s_action); |
140
|
|
|
|
|
|
|
} |
141
|
11
|
|
|
|
|
43
|
$_->{table} = $hr_params->{name}; |
142
|
11
|
|
|
|
|
60
|
push @ar_result, $obj_action->$s_sub($_); |
143
|
|
|
|
|
|
|
} |
144
|
3
|
|
|
|
|
29
|
return \@ar_result; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub drop { |
148
|
3
|
|
|
3
|
1
|
1535
|
my ($self, $hr_params) = @_; |
149
|
3
|
|
|
|
|
691
|
require Ref::Util; |
150
|
3
|
100
|
|
|
|
1892
|
my $s_name = Ref::Util::is_hashref($hr_params) ? $hr_params->{name} : undef; |
151
|
3
|
100
|
|
|
|
11
|
if (!$s_name) { |
152
|
2
|
|
|
|
|
638
|
require Geoffrey::Exception::General; |
153
|
2
|
|
|
|
|
10
|
Geoffrey::Exception::General::throw_no_table_name('to drop'); |
154
|
|
|
|
|
|
|
} |
155
|
1
|
|
|
|
|
5
|
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
|
18
|
my ($self, $schema) = @_; |
161
|
3
|
|
|
|
|
9
|
return [map { $_->{name} } @{$self->do_arrayref($self->converter->table->list($schema), [])}]; |
|
19
|
|
|
|
|
1321
|
|
|
3
|
|
|
|
|
19
|
|
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
1; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
__END__ |