line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Geoffrey::Action::Constraint; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
21953
|
use utf8; |
|
6
|
|
|
|
|
26
|
|
|
6
|
|
|
|
|
41
|
|
4
|
6
|
|
|
6
|
|
282
|
use 5.016; |
|
6
|
|
|
|
|
22
|
|
5
|
6
|
|
|
6
|
|
35
|
use strict; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
124
|
|
6
|
6
|
|
|
6
|
|
38
|
use warnings; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
314
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$Geoffrey::Action::Constraint::VERSION = '0.000204'; |
9
|
|
|
|
|
|
|
|
10
|
6
|
|
|
6
|
|
450
|
use parent 'Geoffrey::Role::Action'; |
|
6
|
|
|
|
|
321
|
|
|
6
|
|
|
|
|
38
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
16
|
|
|
16
|
1
|
2274
|
my $class = shift; |
14
|
16
|
|
|
|
|
85
|
my $self = $class->SUPER::new(@_); |
15
|
|
|
|
|
|
|
$self->{constraints} = [ |
16
|
15
|
|
|
|
|
84
|
qw/ |
17
|
|
|
|
|
|
|
foreignkey |
18
|
|
|
|
|
|
|
primarykey |
19
|
|
|
|
|
|
|
check |
20
|
|
|
|
|
|
|
index |
21
|
|
|
|
|
|
|
unique |
22
|
|
|
|
|
|
|
/ |
23
|
|
|
|
|
|
|
]; |
24
|
15
|
|
|
|
|
85
|
return bless $self, $class; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _obj_check { |
28
|
39
|
|
|
39
|
|
82
|
my ($self) = @_; |
29
|
39
|
|
|
|
|
2068
|
require Geoffrey::Action::Constraint::Check; |
30
|
39
|
|
66
|
|
|
152
|
$self->{check} //= Geoffrey::Action::Constraint::Check->new( |
31
|
|
|
|
|
|
|
converter => $self->converter, |
32
|
|
|
|
|
|
|
dbh => $self->dbh, |
33
|
|
|
|
|
|
|
); |
34
|
39
|
|
|
|
|
109
|
return $self->{check}; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub _obj_index { |
38
|
39
|
|
|
39
|
|
70
|
my ($self) = @_; |
39
|
39
|
|
|
|
|
2314
|
require Geoffrey::Action::Constraint::Index; |
40
|
39
|
|
66
|
|
|
182
|
$self->{index} //= Geoffrey::Action::Constraint::Index->new( |
41
|
|
|
|
|
|
|
converter => $self->converter, |
42
|
|
|
|
|
|
|
dbh => $self->dbh, |
43
|
|
|
|
|
|
|
); |
44
|
39
|
|
|
|
|
112
|
return $self->{index}; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _obj_unique { |
48
|
40
|
|
|
40
|
|
72
|
my ($self) = @_; |
49
|
40
|
|
|
|
|
2076
|
require Geoffrey::Action::Constraint::Unique; |
50
|
40
|
|
66
|
|
|
157
|
$self->{unique} //= Geoffrey::Action::Constraint::Unique->new( |
51
|
|
|
|
|
|
|
converter => $self->converter, |
52
|
|
|
|
|
|
|
dbh => $self->dbh, |
53
|
|
|
|
|
|
|
); |
54
|
40
|
|
|
|
|
120
|
return $self->{unique}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _obj_primarykey { |
58
|
40
|
|
|
40
|
|
74
|
my ($self) = @_; |
59
|
40
|
|
|
|
|
2152
|
require Geoffrey::Action::Constraint::PrimaryKey; |
60
|
40
|
|
66
|
|
|
160
|
$self->{primarykey} //= Geoffrey::Action::Constraint::PrimaryKey->new( |
61
|
|
|
|
|
|
|
converter => $self->converter, |
62
|
|
|
|
|
|
|
dbh => $self->dbh, |
63
|
|
|
|
|
|
|
); |
64
|
40
|
|
|
|
|
121
|
return $self->{primarykey}; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _obj_foreignkey { |
68
|
121
|
|
|
121
|
|
245
|
my ($self) = @_; |
69
|
121
|
|
|
|
|
3187
|
require Geoffrey::Action::Constraint::ForeignKey; |
70
|
121
|
|
66
|
|
|
399
|
$self->{foreignkey} //= Geoffrey::Action::Constraint::ForeignKey->new( |
71
|
|
|
|
|
|
|
converter => $self->converter, |
72
|
|
|
|
|
|
|
dbh => $self->dbh, |
73
|
|
|
|
|
|
|
); |
74
|
121
|
|
|
|
|
290
|
return $self->{foreignkey}; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _set_collective_params { |
79
|
39
|
|
|
39
|
|
81
|
my ($self, $s_sub_call, $b_param) = @_; |
80
|
39
|
|
|
|
|
67
|
for (@{$self->{constraints}}) { |
|
39
|
|
|
|
|
97
|
|
81
|
195
|
|
|
|
|
527
|
my $s_obj_sub = '_obj_' . $_; |
82
|
195
|
|
|
|
|
495
|
$self->$s_obj_sub($self)->$s_sub_call($b_param); |
83
|
|
|
|
|
|
|
} |
84
|
39
|
|
|
|
|
81
|
return 1; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub _collective_do { |
88
|
3
|
|
|
3
|
|
11
|
my ($self, $s_sub_call, $s_table_name, $constraints_to_alter) = @_; |
89
|
3
|
|
|
|
|
7
|
my @a_result = (); |
90
|
3
|
|
|
|
|
7
|
for (@{$constraints_to_alter}) { |
|
3
|
|
|
|
|
9
|
|
91
|
3
|
|
|
|
|
20
|
my $s_obj_sub = '_obj_' . delete $_->{constraint}; |
92
|
3
|
50
|
|
|
|
24
|
if (!$self->can($s_obj_sub)) { |
93
|
0
|
|
|
|
|
0
|
require Geoffrey::Exception::General; |
94
|
0
|
|
|
|
|
0
|
return Geoffrey::Exception::General::throw_unknown_action($s_obj_sub); |
95
|
|
|
|
|
|
|
} |
96
|
3
|
|
|
|
|
8
|
$_->{table} = $s_table_name; |
97
|
3
|
|
|
|
|
12
|
push @a_result, $self->do($self->$s_obj_sub()->$s_sub_call($_)); |
98
|
|
|
|
|
|
|
} |
99
|
1
|
50
|
|
|
|
4
|
return if scalar @a_result == 0; |
100
|
1
|
50
|
|
|
|
9
|
return shift @a_result if scalar @a_result == 1; |
101
|
0
|
|
|
|
|
0
|
return \@a_result; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub for_table { |
105
|
122
|
|
|
122
|
1
|
1760
|
my ($self, $b_for_table) = @_; |
106
|
122
|
100
|
|
|
|
461
|
return $self->{for_table} if (!defined $b_for_table); |
107
|
38
|
|
|
|
|
75
|
$self->{for_table} = $b_for_table; |
108
|
38
|
|
|
|
|
104
|
$self->_set_collective_params('for_table', $b_for_table); |
109
|
38
|
|
|
|
|
75
|
return $self; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub dryrun { |
113
|
2
|
|
|
2
|
1
|
10
|
my ($self, $b_dryrun) = @_; |
114
|
2
|
100
|
|
|
|
10
|
return $self->{dryrun} if (!defined $b_dryrun); |
115
|
1
|
|
|
|
|
3
|
$self->{dryrun} = $b_dryrun; |
116
|
1
|
|
|
|
|
4
|
$self->_set_collective_params('dryrun', $b_dryrun); |
117
|
1
|
|
|
|
|
16
|
return $self; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub add { |
121
|
84
|
|
|
84
|
1
|
5022
|
my ($self, $s_table_name, $hr_params, $ar_constraint_params) = @_; |
122
|
84
|
100
|
|
|
|
191
|
return $self->create_table_column($s_table_name, $hr_params, $ar_constraint_params) |
123
|
|
|
|
|
|
|
if $self->for_table; |
124
|
4
|
|
|
|
|
25
|
require Geoffrey::Exception::General; |
125
|
4
|
|
|
|
|
19
|
return Geoffrey::Exception::General::throw_unknown_action('add constraint'); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub alter { |
129
|
3
|
|
|
3
|
1
|
674
|
my ($self, $hr_params) = @_; |
130
|
3
|
|
|
|
|
503
|
require Ref::Util; |
131
|
3
|
100
|
|
|
|
1630
|
if (!Ref::Util::is_hashref($hr_params)) { |
132
|
1
|
|
|
|
|
521
|
require Geoffrey::Exception::General; |
133
|
1
|
|
|
|
|
7
|
Geoffrey::Exception::General::throw_wrong_ref(__PACKAGE__ . '::alter', 'hash'); |
134
|
|
|
|
|
|
|
} |
135
|
2
|
|
|
|
|
10
|
return $self->_collective_do('alter', delete $hr_params->{table}, $hr_params->{constraints}); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub drop { |
139
|
2
|
|
|
2
|
1
|
2505
|
my ($self, $s_table_name, $hr_params) = @_; |
140
|
2
|
100
|
|
|
|
6
|
if (!$s_table_name) { |
141
|
1
|
|
|
|
|
511
|
require Geoffrey::Exception::RequiredValue; |
142
|
1
|
|
|
|
|
4
|
Geoffrey::Exception::RequiredValue::throw_reftable_missing(); |
143
|
|
|
|
|
|
|
} |
144
|
1
|
|
|
|
|
6
|
return $self->_collective_do('drop', $s_table_name, $hr_params->{constraints}); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub create_table_column { |
148
|
80
|
|
|
80
|
1
|
162
|
my ($self, $s_table_name, $hr_column_params, $ar_constraint_params) = @_; |
149
|
80
|
100
|
100
|
|
|
237
|
if ($hr_column_params->{primarykey} && $hr_column_params->{primarykey} != 1) { |
150
|
1
|
|
|
|
|
4
|
return push @{$ar_constraint_params}, |
151
|
1
|
|
|
|
|
2
|
$self->_obj_primarykey->add($s_table_name, $hr_column_params->{primarykey}); |
152
|
|
|
|
|
|
|
} |
153
|
79
|
|
|
|
|
325
|
push @{$ar_constraint_params}, |
154
|
|
|
|
|
|
|
$self->_uniques_to_add({ |
155
|
|
|
|
|
|
|
unique => $hr_column_params->{unique}, |
156
|
|
|
|
|
|
|
table => $s_table_name, |
157
|
|
|
|
|
|
|
column => $hr_column_params->{name}, |
158
|
79
|
|
|
|
|
113
|
}); |
159
|
|
|
|
|
|
|
|
160
|
79
|
|
|
|
|
222
|
my $o_foreign_key = $self->_obj_foreignkey; |
161
|
79
|
100
|
|
|
|
181
|
if ($hr_column_params->{foreignkey}) { |
162
|
17
|
|
|
|
|
40
|
$hr_column_params->{foreignkey}->{column} = $hr_column_params->{name}; |
163
|
17
|
|
|
|
|
36
|
$hr_column_params->{foreignkey}->{table} = $s_table_name; |
164
|
|
|
|
|
|
|
$hr_column_params->{foreignkey}->{schema} = $hr_column_params->{schema} |
165
|
17
|
50
|
|
|
|
41
|
if $hr_column_params->{schema}; |
166
|
17
|
|
|
|
|
47
|
push @{$ar_constraint_params}, |
167
|
17
|
|
|
|
|
29
|
$o_foreign_key->for_table(1)->add($hr_column_params->{foreignkey}); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
79
|
|
|
|
|
193
|
my $consts = $self->converter->constraints; |
171
|
79
|
100
|
|
|
|
216
|
my $not_null = ($hr_column_params->{notnull}) ? $consts->{not_null} : q~~; |
172
|
79
|
100
|
|
|
|
175
|
my $primarykey = (defined $hr_column_params->{primarykey}) ? $consts->{primary_key} : q~~; |
173
|
79
|
|
|
|
|
217
|
$o_foreign_key->for_table(0); |
174
|
79
|
|
|
|
|
326
|
return qq~$not_null $primarykey~; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub _uniques_to_add { |
178
|
79
|
|
|
79
|
|
140
|
my ($self, $hr_unique_params) = @_; |
179
|
79
|
100
|
|
|
|
227
|
return () if !$hr_unique_params->{unique}; |
180
|
1
|
|
|
|
|
5
|
require Ref::Util; |
181
|
|
|
|
|
|
|
my $hr_to_add |
182
|
|
|
|
|
|
|
= Ref::Util::is_hashref($hr_unique_params->{unique}) |
183
|
|
|
|
|
|
|
? $hr_unique_params->{unique} |
184
|
1
|
50
|
|
|
|
9
|
: {columns => [$hr_unique_params->{column}]}; |
185
|
1
|
|
|
|
|
4
|
return $self->_obj_unique->add($hr_unique_params->{table}, $hr_to_add); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
1; |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
__END__ |