line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package SQL::Admin::Driver::Base::Decompose; |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
1378
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
39
|
|
5
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
2051
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = v0.5.0; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
###################################################################### |
10
|
|
|
|
|
|
|
###################################################################### |
11
|
|
|
|
|
|
|
sub new { # ; |
12
|
0
|
|
|
0
|
0
|
|
my ($class, %param) = @_; |
13
|
|
|
|
|
|
|
|
14
|
0
|
|
0
|
|
|
|
bless \ %param, ref $class || $class; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
###################################################################### |
19
|
|
|
|
|
|
|
###################################################################### |
20
|
|
|
|
|
|
|
sub decompose { # ; |
21
|
0
|
|
|
0
|
0
|
|
my ($self, $catalog) = @_; |
22
|
0
|
|
|
|
|
|
my (@retval, %map); |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
|
local $self->{log} = {}; |
25
|
|
|
|
|
|
|
|
26
|
0
|
|
|
0
|
|
|
my $sorter = sub { $a->fullname cmp $b->fullname }; |
|
0
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
################################################################## |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
for my $entity (qw( schema sequence table index column primary_key unique foreign_key )) { |
31
|
0
|
|
0
|
|
|
|
my $log = $self->{log}{$entity} ||= {}; |
32
|
0
|
|
|
|
|
|
my $method = 'create_' . $entity; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
for my $obj (sort $sorter values %{ $catalog->list ($entity) }) { |
|
0
|
|
|
|
|
|
|
35
|
0
|
0
|
|
|
|
|
next if exists $log->{$obj->fullname}; |
36
|
0
|
|
|
|
|
|
push @retval, $self->$method ($catalog, $obj); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
################################################################## |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
for my $obj (sort $sorter values %{ $catalog->list ('table') }) { |
|
0
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
push @retval, $self->table_row ($obj); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
################################################################## |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
\ @retval; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
###################################################################### |
52
|
|
|
|
|
|
|
###################################################################### |
53
|
|
|
|
|
|
|
sub create_schema { # ; |
54
|
0
|
|
|
0
|
0
|
|
my ($self, $catalog, $schema) = @_; |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
$self->{log}{schema}{ $schema->fullname } = 1; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
+{ create_schema => { |
59
|
|
|
|
|
|
|
schema_identifier => $schema->name, |
60
|
|
|
|
|
|
|
}}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
###################################################################### |
65
|
|
|
|
|
|
|
###################################################################### |
66
|
|
|
|
|
|
|
sub create_table { # ; |
67
|
0
|
|
|
0
|
0
|
|
my ($self, $catalog, $table) = @_; |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
$self->{log}{table}{ $table->fullname } = 1; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
+{ create_table => { |
72
|
|
|
|
|
|
|
table_name => { name => $table->name, schema => $table->schema }, |
73
|
|
|
|
|
|
|
table_content => [ |
74
|
|
|
|
|
|
|
( map $self->create_table_column ($catalog, $table->column ($_)), $table->columns ), |
75
|
|
|
|
|
|
|
], |
76
|
|
|
|
|
|
|
}}; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
###################################################################### |
81
|
|
|
|
|
|
|
###################################################################### |
82
|
|
|
|
|
|
|
sub create_table_column { # ; |
83
|
0
|
|
|
0
|
0
|
|
my ($self, $catalog, $column) = @_; |
84
|
0
|
|
|
|
|
|
my $type = { %{ $column->type } }; |
|
0
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
$type->{data_type} = delete $type->{type}; |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
my @param = ( |
88
|
|
|
|
|
|
|
table => $column->table, |
89
|
|
|
|
|
|
|
column_list => [ $column->name ], |
90
|
|
|
|
|
|
|
); |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
my $primary_key = $catalog->exists (primary_key => @param); |
93
|
0
|
|
|
|
|
|
my $unique = $catalog->exists (unique => @param); |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
$self->{log}{column}{ $column->fullname } = 1; |
96
|
0
|
|
|
|
|
|
$self->{log}{primary_key}{ $primary_key } = 1; |
97
|
0
|
|
|
|
|
|
$self->{log}{unique}{ $unique } = 1; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
################################################################## |
100
|
|
|
|
|
|
|
|
101
|
0
|
0
|
|
|
|
|
+{ column_definition => { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
102
|
|
|
|
|
|
|
column_name => $column->name, |
103
|
|
|
|
|
|
|
%$type, |
104
|
|
|
|
|
|
|
($primary_key ? (column_primary_key => 1) : ()), |
105
|
|
|
|
|
|
|
($unique ? (column_unique => 1) : ()), |
106
|
|
|
|
|
|
|
(map +(column_not_null => 1), grep $_, $column->not_null), |
107
|
|
|
|
|
|
|
(map +(default_clause => $_), grep $_, $column->default), |
108
|
|
|
|
|
|
|
($column->autoincrement ? (autoincrement => $column->autoincrement_hint) : ()), |
109
|
|
|
|
|
|
|
}}; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
###################################################################### |
114
|
|
|
|
|
|
|
###################################################################### |
115
|
|
|
|
|
|
|
sub create_index { # ; |
116
|
0
|
|
|
0
|
0
|
|
my ($self, $catalog, $index) = @_; |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
+{ create_index => { |
119
|
|
|
|
|
|
|
index_name => { name => $index->name, schema => $index->schema }, |
120
|
|
|
|
|
|
|
table_name => { name => $index->table->name, schema => $index->table->schema }, |
121
|
|
|
|
|
|
|
($index->unique ? (index_unique => 1) : ()), |
122
|
|
|
|
|
|
|
index_column_list => [ |
123
|
|
|
|
|
|
|
map +{ |
124
|
|
|
|
|
|
|
column_name => $_->[0], |
125
|
|
|
|
|
|
|
($_->[1] ? (column_order => $_->[1]) : ()), |
126
|
0
|
0
|
|
|
|
|
}, @{ $index->column_list } |
|
|
0
|
|
|
|
|
|
127
|
|
|
|
|
|
|
] |
128
|
|
|
|
|
|
|
}}; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
###################################################################### |
133
|
|
|
|
|
|
|
###################################################################### |
134
|
|
|
|
|
|
|
sub create_primary_key { # ; |
135
|
0
|
|
|
0
|
0
|
|
my ($self, $catalog, $obj) = @_; |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
+{ alter_table => { |
138
|
|
|
|
|
|
|
table_name => { name => $obj->table->name, schema => $obj->table->schema }, |
139
|
|
|
|
|
|
|
alter_table_actions => [ { |
140
|
|
|
|
|
|
|
add_constraint => { primary_key_constraint => { |
141
|
|
|
|
|
|
|
column_list => $obj->column_list, |
142
|
|
|
|
|
|
|
}} |
143
|
|
|
|
|
|
|
}] |
144
|
|
|
|
|
|
|
}}; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
###################################################################### |
149
|
|
|
|
|
|
|
###################################################################### |
150
|
|
|
|
|
|
|
sub create_unique { # ; |
151
|
0
|
|
|
0
|
0
|
|
my ($self, $catalog, $obj) = @_; |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
+{ alter_table => { |
154
|
|
|
|
|
|
|
table_name => { name => $obj->table->name, schema => $obj->table->schema }, |
155
|
|
|
|
|
|
|
alter_table_actions => [ { |
156
|
|
|
|
|
|
|
add_constraint => { unique_constraint => { |
157
|
0
|
|
|
|
|
|
(map {(constraint_name => $_)} grep $_, $obj->name), |
158
|
|
|
|
|
|
|
column_list => $obj->column_list, |
159
|
|
|
|
|
|
|
}} |
160
|
|
|
|
|
|
|
}] |
161
|
|
|
|
|
|
|
}}; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
###################################################################### |
166
|
|
|
|
|
|
|
###################################################################### |
167
|
|
|
|
|
|
|
sub create_foreign_key { # ; |
168
|
0
|
|
|
0
|
0
|
|
my ($self, $catalog, $obj) = @_; |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
+{ alter_table => { |
171
|
|
|
|
|
|
|
table_name => { name => $obj->table->name, schema => $obj->table->schema }, |
172
|
|
|
|
|
|
|
alter_table_actions => [ { |
173
|
|
|
|
|
|
|
add_constraint => { foreign_key_constraint => { |
174
|
|
|
|
|
|
|
(map +(constraint_name => $_), grep $_, $obj->name), |
175
|
|
|
|
|
|
|
referencing_column_list => $obj->referencing_column_list, |
176
|
|
|
|
|
|
|
referenced_column_list => $obj->referenced_column_list, |
177
|
|
|
|
|
|
|
referenced_table => { |
178
|
|
|
|
|
|
|
name => $obj->referenced_table->name, |
179
|
|
|
|
|
|
|
schema => $obj->referenced_table->schema, |
180
|
|
|
|
|
|
|
}, |
181
|
|
|
|
|
|
|
(map +(update_rule => $_), grep $_, $obj->update_rule), |
182
|
|
|
|
|
|
|
(map +(delete_rule => $_), grep $_, $obj->delete_rule), |
183
|
|
|
|
|
|
|
}} |
184
|
|
|
|
|
|
|
}] |
185
|
|
|
|
|
|
|
}}; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
###################################################################### |
190
|
|
|
|
|
|
|
###################################################################### |
191
|
|
|
|
|
|
|
sub drop_schema { # ; |
192
|
0
|
|
|
0
|
0
|
|
my ($self, $catalog, $schema) = @_; |
193
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
|
+{ drop_schema => { |
195
|
|
|
|
|
|
|
schema_identifier => $schema->name, |
196
|
|
|
|
|
|
|
}}; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
###################################################################### |
201
|
|
|
|
|
|
|
###################################################################### |
202
|
|
|
|
|
|
|
sub add_column { # ; |
203
|
0
|
|
|
0
|
0
|
|
my ($self, $catalog, $column) = @_; |
204
|
|
|
|
|
|
|
|
205
|
0
|
|
|
|
|
|
my $data_type = { %{ $column->type } }; |
|
0
|
|
|
|
|
|
|
206
|
0
|
|
|
|
|
|
$data_type->{data_type} = delete $data_type->{type}; |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# print Data::Dumper::Dumper ($column->default); |
209
|
0
|
|
|
|
|
|
+{ alter_table => { |
210
|
|
|
|
|
|
|
table_name => { name => $column->table->name, schema => $column->table->schema }, |
211
|
|
|
|
|
|
|
alter_table_actions => [ { add_column => [ { column_definition => { |
212
|
|
|
|
|
|
|
column_name => $column->name, |
213
|
|
|
|
|
|
|
%$data_type, |
214
|
|
|
|
|
|
|
not_null => $column->not_null, |
215
|
|
|
|
|
|
|
default_clause => $column->default, |
216
|
|
|
|
|
|
|
} } ] } ], |
217
|
|
|
|
|
|
|
}}; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
###################################################################### |
222
|
|
|
|
|
|
|
###################################################################### |
223
|
|
|
|
|
|
|
sub alter_column { # ; |
224
|
0
|
|
|
0
|
0
|
|
my ($self, $catalog, $column, $action, $value) = @_; |
225
|
|
|
|
|
|
|
|
226
|
0
|
|
|
|
|
|
my $data_type = { %{ $column->type } }; |
|
0
|
|
|
|
|
|
|
227
|
0
|
|
|
|
|
|
$data_type->{data_type} = delete $data_type->{type}; |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# print Data::Dumper::Dumper ($column->default); |
230
|
0
|
|
|
|
|
|
+{ alter_table => { |
231
|
|
|
|
|
|
|
table_name => { name => $column->table->name, schema => $column->table->schema }, |
232
|
|
|
|
|
|
|
alter_table_actions => [ { add_column => [ { column_definition => { |
233
|
|
|
|
|
|
|
column_name => $column->name, |
234
|
|
|
|
|
|
|
%$data_type, |
235
|
|
|
|
|
|
|
not_null => $column->not_null, |
236
|
|
|
|
|
|
|
default_clause => $column->default, |
237
|
|
|
|
|
|
|
} } ] } ], |
238
|
|
|
|
|
|
|
}}; |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
###################################################################### |
243
|
|
|
|
|
|
|
###################################################################### |
244
|
|
|
|
|
|
|
sub table_row { # ; |
245
|
0
|
|
|
0
|
0
|
|
my ($self, $table) = @_; |
246
|
0
|
|
|
|
|
|
my @retval; |
247
|
0
|
|
|
|
|
|
my $table_name = { name => $table->name, schema => $table->schema }; |
248
|
|
|
|
|
|
|
|
249
|
0
|
|
|
|
|
|
for my $row (@{ $table->table_row }) { |
|
0
|
|
|
|
|
|
|
250
|
0
|
|
|
|
|
|
push @retval, +{ statement_insert => { |
251
|
|
|
|
|
|
|
table_name => $table_name, |
252
|
|
|
|
|
|
|
(map +(column_list => $_), grep $_, $row->{columns}), |
253
|
|
|
|
|
|
|
insert_value_list => $row->{values}, |
254
|
|
|
|
|
|
|
}}; |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
|
258
|
0
|
|
|
|
|
|
@retval; |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
###################################################################### |
263
|
|
|
|
|
|
|
###################################################################### |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
package SQL::Admin::Driver::Base::Decompose; |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
1; |