| 1 | |||||||||||||||||||||
| 2 | package SQL::Admin::Driver::Base::Decompose; | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | our $VERSION = v0.5.0; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | ###################################################################### | ||||||||||||||||||||
| 10 | ###################################################################### | ||||||||||||||||||||
| 11 | 0 | ▶ | sub new { # ; | ||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 12 | 0 | my ($class, %param) = @_; | |||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 0 | ▶ | bless \ %param, ref $class || $class; | ||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 15 | } | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | ###################################################################### | ||||||||||||||||||||
| 19 | ###################################################################### | ||||||||||||||||||||
| 20 | 0 | ▶ | sub decompose { # ; | ||||||||||||||||||
|
Subroutine
decompose: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||
| 21 | 0 | my ($self, $catalog) = @_; | |||||||||||||||||||
| 22 | 0 | my (@retval, %map); | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 0 | local $self->{log} = {}; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 0 | ▶ | my $sorter = sub { $a->fullname cmp $b->fullname }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | ################################################################## | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | for my $entity (qw( schema sequence table index column primary_key unique foreign_key )) { | |||||||||||||||||||
| 31 | 0 | ▶ | my $log = $self->{log}{$entity} ||= {}; | ||||||||||||||||||
|
Condition0%0/2
$self->{"log"}{$entity} ||= {}
MC/DC0%0/1
$self->{"log"}{$entity} ||= {}
$self->{"log"}{$entity}
Truth table0%0/2
A
A= $self->{"log"}{$entity}
| |||||||||||||||||||||
| 32 | 0 | my $method = 'create_' . $entity; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 0 | for my $obj (sort $sorter values %{ $catalog->list ($entity) }) { | |||||||||||||||||||
| 35 | 0 | ▶ | next if exists $log->{$obj->fullname}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 0 | push @retval, $self->$method ($catalog, $obj); | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | ################################################################## | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 0 | for my $obj (sort $sorter values %{ $catalog->list ('table') }) { | |||||||||||||||||||
| 43 | 0 | push @retval, $self->table_row ($obj); | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | ################################################################## | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | \ @retval; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | ###################################################################### | ||||||||||||||||||||
| 52 | ###################################################################### | ||||||||||||||||||||
| 53 | 0 | ▶ | sub create_schema { # ; | ||||||||||||||||||
|
Subroutine
create_schema: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 54 | 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 | 0 | ▶ | sub create_table { # ; | ||||||||||||||||||
|
Subroutine
create_table: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 67 | 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 | 0 | ▶ | sub create_table_column { # ; | ||||||||||||||||||
|
Subroutine
create_table_column: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||
| 83 | 0 | my ($self, $catalog, $column) = @_; | |||||||||||||||||||
| 84 | 0 | my $type = { %{ $column->type } }; | |||||||||||||||||||
| 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 | ▶ | +{ column_definition => { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 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 | 0 | ▶ | sub create_index { # ; | ||||||||||||||||||
|
Subroutine
create_index: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 116 | 0 | my ($self, $catalog, $index) = @_; | |||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | +{ 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 | ▶ | }, @{ $index->column_list } | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 127 | ] | ||||||||||||||||||||
| 128 | }}; | ||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | ###################################################################### | ||||||||||||||||||||
| 133 | ###################################################################### | ||||||||||||||||||||
| 134 | 0 | ▶ | sub create_primary_key { # ; | ||||||||||||||||||
|
Subroutine
create_primary_key: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 135 | 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 | 0 | ▶ | sub create_unique { # ; | ||||||||||||||||||
|
Subroutine
create_unique: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 151 | 0 | my ($self, $catalog, $obj) = @_; | |||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | +{ 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 | 0 | ▶ | sub create_foreign_key { # ; | ||||||||||||||||||
|
Subroutine
create_foreign_key: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 168 | 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 | 0 | ▶ | sub drop_schema { # ; | ||||||||||||||||||
|
Subroutine
drop_schema: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 192 | 0 | my ($self, $catalog, $schema) = @_; | |||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 0 | +{ drop_schema => { | |||||||||||||||||||
| 195 | schema_identifier => $schema->name, | ||||||||||||||||||||
| 196 | }}; | ||||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | ###################################################################### | ||||||||||||||||||||
| 201 | ###################################################################### | ||||||||||||||||||||
| 202 | 0 | ▶ | sub add_column { # ; | ||||||||||||||||||
|
Subroutine
add_column: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 203 | 0 | my ($self, $catalog, $column) = @_; | |||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 0 | my $data_type = { %{ $column->type } }; | |||||||||||||||||||
| 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 | 0 | ▶ | sub alter_column { # ; | ||||||||||||||||||
|
Subroutine
alter_column: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 224 | 0 | my ($self, $catalog, $column, $action, $value) = @_; | |||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | 0 | my $data_type = { %{ $column->type } }; | |||||||||||||||||||
| 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 | 0 | ▶ | sub table_row { # ; | ||||||||||||||||||
|
Subroutine
table_row: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 245 | 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 }) { | |||||||||||||||||||
| 250 | push @retval, +{ statement_insert => { | ||||||||||||||||||||
| 251 | table_name => $table_name, | ||||||||||||||||||||
| 252 | (map +(column_list => $_), grep $_, $row->{columns}), | ||||||||||||||||||||
| 253 | insert_value_list => $row->{values}, | ||||||||||||||||||||
| 254 | 0 | }}; | |||||||||||||||||||
| 255 | } | ||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | 0 | @retval; | |||||||||||||||||||
| 259 | } | ||||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | ###################################################################### | ||||||||||||||||||||
| 263 | ###################################################################### | ||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | package SQL::Admin::Driver::Base::Decompose; | ||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | 1 | 1; | |||||||||||||||||||