line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Geoffrey::Action::Constraint; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
21938
|
use utf8; |
|
6
|
|
|
|
|
25
|
|
|
6
|
|
|
|
|
34
|
|
4
|
6
|
|
|
6
|
|
263
|
use 5.016; |
|
6
|
|
|
|
|
22
|
|
5
|
6
|
|
|
6
|
|
32
|
use strict; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
125
|
|
6
|
6
|
|
|
6
|
|
27
|
use warnings; |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
276
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$Geoffrey::Action::Constraint::VERSION = '0.000205'; |
9
|
|
|
|
|
|
|
|
10
|
6
|
|
|
6
|
|
480
|
use parent 'Geoffrey::Role::Action'; |
|
6
|
|
|
|
|
311
|
|
|
6
|
|
|
|
|
48
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
16
|
|
|
16
|
1
|
2323
|
my $class = shift; |
14
|
16
|
|
|
|
|
74
|
my $self = $class->SUPER::new(@_); |
15
|
|
|
|
|
|
|
$self->{constraints} = [ |
16
|
15
|
|
|
|
|
70
|
qw/ |
17
|
|
|
|
|
|
|
foreignkey |
18
|
|
|
|
|
|
|
primarykey |
19
|
|
|
|
|
|
|
check |
20
|
|
|
|
|
|
|
index |
21
|
|
|
|
|
|
|
unique |
22
|
|
|
|
|
|
|
/ |
23
|
|
|
|
|
|
|
]; |
24
|
15
|
|
|
|
|
66
|
return bless $self, $class; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub _obj_check { |
28
|
39
|
|
|
39
|
|
66
|
my ($self) = @_; |
29
|
39
|
|
|
|
|
2124
|
require Geoffrey::Action::Constraint::Check; |
30
|
39
|
|
66
|
|
|
134
|
$self->{check} //= Geoffrey::Action::Constraint::Check->new( |
31
|
|
|
|
|
|
|
converter => $self->converter, |
32
|
|
|
|
|
|
|
dbh => $self->dbh, |
33
|
|
|
|
|
|
|
); |
34
|
39
|
|
|
|
|
107
|
return $self->{check}; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub _obj_index { |
38
|
39
|
|
|
39
|
|
65
|
my ($self) = @_; |
39
|
39
|
|
|
|
|
2065
|
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
|
|
|
|
|
105
|
return $self->{index}; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _obj_unique { |
48
|
40
|
|
|
40
|
|
67
|
my ($self) = @_; |
49
|
40
|
|
|
|
|
2070
|
require Geoffrey::Action::Constraint::Unique; |
50
|
40
|
|
66
|
|
|
143
|
$self->{unique} //= Geoffrey::Action::Constraint::Unique->new( |
51
|
|
|
|
|
|
|
converter => $self->converter, |
52
|
|
|
|
|
|
|
dbh => $self->dbh, |
53
|
|
|
|
|
|
|
); |
54
|
40
|
|
|
|
|
117
|
return $self->{unique}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _obj_primarykey { |
58
|
40
|
|
|
40
|
|
63
|
my ($self) = @_; |
59
|
40
|
|
|
|
|
2125
|
require Geoffrey::Action::Constraint::PrimaryKey; |
60
|
40
|
|
66
|
|
|
142
|
$self->{primarykey} //= Geoffrey::Action::Constraint::PrimaryKey->new( |
61
|
|
|
|
|
|
|
converter => $self->converter, |
62
|
|
|
|
|
|
|
dbh => $self->dbh, |
63
|
|
|
|
|
|
|
); |
64
|
40
|
|
|
|
|
109
|
return $self->{primarykey}; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _obj_foreignkey { |
68
|
121
|
|
|
121
|
|
200
|
my ($self) = @_; |
69
|
121
|
|
|
|
|
3148
|
require Geoffrey::Action::Constraint::ForeignKey; |
70
|
121
|
|
66
|
|
|
359
|
$self->{foreignkey} //= Geoffrey::Action::Constraint::ForeignKey->new( |
71
|
|
|
|
|
|
|
converter => $self->converter, |
72
|
|
|
|
|
|
|
dbh => $self->dbh, |
73
|
|
|
|
|
|
|
); |
74
|
121
|
|
|
|
|
296
|
return $self->{foreignkey}; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _set_collective_params { |
79
|
39
|
|
|
39
|
|
79
|
my ($self, $s_sub_call, $b_param) = @_; |
80
|
39
|
|
|
|
|
63
|
for (@{$self->{constraints}}) { |
|
39
|
|
|
|
|
92
|
|
81
|
195
|
|
|
|
|
369
|
my $s_obj_sub = '_obj_' . $_; |
82
|
195
|
|
|
|
|
445
|
$self->$s_obj_sub($self)->$s_sub_call($b_param); |
83
|
|
|
|
|
|
|
} |
84
|
39
|
|
|
|
|
67
|
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
|
|
|
|
|
6
|
my @a_result = (); |
90
|
3
|
|
|
|
|
6
|
for (@{$constraints_to_alter}) { |
|
3
|
|
|
|
|
10
|
|
91
|
3
|
|
|
|
|
9
|
my $s_obj_sub = '_obj_' . delete $_->{constraint}; |
92
|
3
|
50
|
|
|
|
18
|
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
|
|
|
|
|
7
|
$_->{table} = $s_table_name; |
97
|
3
|
|
|
|
|
11
|
push @a_result, $self->do($self->$s_obj_sub()->$s_sub_call($_)); |
98
|
|
|
|
|
|
|
} |
99
|
1
|
50
|
|
|
|
5
|
return if scalar @a_result == 0; |
100
|
1
|
50
|
|
|
|
19
|
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
|
1700
|
my ($self, $b_for_table) = @_; |
106
|
122
|
100
|
|
|
|
425
|
return $self->{for_table} if (!defined $b_for_table); |
107
|
38
|
|
|
|
|
65
|
$self->{for_table} = $b_for_table; |
108
|
38
|
|
|
|
|
101
|
$self->_set_collective_params('for_table', $b_for_table); |
109
|
38
|
|
|
|
|
78
|
return $self; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub dryrun { |
113
|
2
|
|
|
2
|
1
|
8
|
my ($self, $b_dryrun) = @_; |
114
|
2
|
100
|
|
|
|
9
|
return $self->{dryrun} if (!defined $b_dryrun); |
115
|
1
|
|
|
|
|
3
|
$self->{dryrun} = $b_dryrun; |
116
|
1
|
|
|
|
|
3
|
$self->_set_collective_params('dryrun', $b_dryrun); |
117
|
1
|
|
|
|
|
8
|
return $self; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub add { |
121
|
84
|
|
|
84
|
1
|
5026
|
my ($self, $s_table_name, $hr_params, $ar_constraint_params) = @_; |
122
|
84
|
100
|
|
|
|
176
|
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
|
|
|
|
|
14
|
return Geoffrey::Exception::General::throw_unknown_action('add constraint'); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub alter { |
129
|
3
|
|
|
3
|
1
|
534
|
my ($self, $hr_params) = @_; |
130
|
3
|
|
|
|
|
533
|
require Ref::Util; |
131
|
3
|
100
|
|
|
|
1694
|
if (!Ref::Util::is_hashref($hr_params)) { |
132
|
1
|
|
|
|
|
523
|
require Geoffrey::Exception::General; |
133
|
1
|
|
|
|
|
5
|
Geoffrey::Exception::General::throw_wrong_ref(__PACKAGE__ . '::alter', 'hash'); |
134
|
|
|
|
|
|
|
} |
135
|
2
|
|
|
|
|
8
|
return $self->_collective_do('alter', delete $hr_params->{table}, $hr_params->{constraints}); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub drop { |
139
|
2
|
|
|
2
|
1
|
2552
|
my ($self, $s_table_name, $hr_params) = @_; |
140
|
2
|
100
|
|
|
|
6
|
if (!$s_table_name) { |
141
|
1
|
|
|
|
|
503
|
require Geoffrey::Exception::RequiredValue; |
142
|
1
|
|
|
|
|
4
|
Geoffrey::Exception::RequiredValue::throw_reftable_missing(); |
143
|
|
|
|
|
|
|
} |
144
|
1
|
|
|
|
|
4
|
return $self->_collective_do('drop', $s_table_name, $hr_params->{constraints}); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub create_table_column { |
148
|
80
|
|
|
80
|
1
|
156
|
my ($self, $s_table_name, $hr_column_params, $ar_constraint_params) = @_; |
149
|
80
|
100
|
100
|
|
|
205
|
if ($hr_column_params->{primarykey} && $hr_column_params->{primarykey} != 1) { |
150
|
1
|
|
|
|
|
4
|
return push @{$ar_constraint_params}, |
151
|
1
|
|
|
|
|
3
|
$self->_obj_primarykey->add($s_table_name, $hr_column_params->{primarykey}); |
152
|
|
|
|
|
|
|
} |
153
|
79
|
|
|
|
|
291
|
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
|
|
|
|
|
108
|
}); |
159
|
|
|
|
|
|
|
|
160
|
79
|
|
|
|
|
233
|
my $o_foreign_key = $self->_obj_foreignkey; |
161
|
79
|
100
|
|
|
|
174
|
if ($hr_column_params->{foreignkey}) { |
162
|
17
|
|
|
|
|
41
|
$hr_column_params->{foreignkey}->{column} = $hr_column_params->{name}; |
163
|
17
|
|
|
|
|
32
|
$hr_column_params->{foreignkey}->{table} = $s_table_name; |
164
|
|
|
|
|
|
|
$hr_column_params->{foreignkey}->{schema} = $hr_column_params->{schema} |
165
|
17
|
50
|
|
|
|
38
|
if $hr_column_params->{schema}; |
166
|
17
|
|
|
|
|
44
|
push @{$ar_constraint_params}, |
167
|
17
|
|
|
|
|
26
|
$o_foreign_key->for_table(1)->add($hr_column_params->{foreignkey}); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
79
|
|
|
|
|
177
|
my $consts = $self->converter->constraints; |
171
|
79
|
100
|
|
|
|
218
|
my $not_null = ($hr_column_params->{notnull}) ? $consts->{not_null} : q~~; |
172
|
79
|
100
|
|
|
|
152
|
my $primarykey = (defined $hr_column_params->{primarykey}) ? $consts->{primary_key} : q~~; |
173
|
79
|
|
|
|
|
267
|
$o_foreign_key->for_table(0); |
174
|
79
|
|
|
|
|
285
|
return qq~$not_null $primarykey~; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
sub _uniques_to_add { |
178
|
79
|
|
|
79
|
|
142
|
my ($self, $hr_unique_params) = @_; |
179
|
79
|
100
|
|
|
|
237
|
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
|
|
|
|
6
|
: {columns => [$hr_unique_params->{column}]}; |
185
|
1
|
|
|
|
|
14
|
return $self->_obj_unique->add($hr_unique_params->{table}, $hr_to_add); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
1; |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
__END__ |