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