| 1 | package CXC::DB::DDL; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use v5.26; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = '0.21'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | # ABSTRACT: DDL for table creation, based on SQL::Translator::Schema | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use DBI::Const::GetInfoType; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use List::Util qw( first notall all ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Hash::Ordered; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use SQL::Translator::Schema; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use SQL::Translator::Schema::Table; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use Ref::Util qw( is_arrayref is_plain_hashref); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use CXC::DB::DDL::Util 'sqlt_entity_map', 'db_version'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use CXC::DB::DDL::Failure; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use CXC::DB::DDL::Table; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | ▶ | use CXC::DB::DDL::Constants '-all'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use Type::Params qw( signature_for ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | ▶ | use Types::Standard qw( ArrayRef Bool Dict Enum HashRef InstanceOf Object Optional Str Slurpy ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 4 | ▶ | use Feature::Compat::Try; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | ▶ | use experimental 'signatures', 'postderef', 'declared_refs', 'refaliasing'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 4 | ▶ | use constant AutoCommit => 'AutoCommit'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | # use after namespace::clean to avoid cleaning out important bits. | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | ▶ | use MooX::StrictConstructor; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | ▶ | use constant TABLE_CLASS => 'CXC::DB::DDL::Table'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | my sub wrap_producers; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 8 | ▶ | my sub coerce_table_arrayref ( $class, $array ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
coerce_table_arrayref: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 8 | require Module::Load; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 8 | Module::Load::load( $class->table_class ); | |||||||||||||||||||||||||||||||||||||||||
| 44 | Hash::Ordered->new( | ||||||||||||||||||||||||||||||||||||||||||
| 45 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 8 | my $table = $class->table_class->new( $_ ); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 8 | $table->name, $table | |||||||||||||||||||||||||||||||||||||||||
| 48 | } $array->@*, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | has _tables => ( | ||||||||||||||||||||||||||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 54 | init_arg => 'tables', | ||||||||||||||||||||||||||||||||||||||||||
| 55 | isa => InstanceOf( ['Hash::Ordered'] ), | ||||||||||||||||||||||||||||||||||||||||||
| 56 | coerce => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | default => sub { Hash::Ordered->new }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 4 | handles => [qw( get set exists )], | |||||||||||||||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 8 | ▶ | around BUILDARGS => sub ( $orig, $class, @args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 8 | ▶ | if ( @args == 1 && !( is_plain_hashref( $args[0] ) && exists $args[0]{tables} ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
@args == 1 and not Ref::Util::XS::is_plain_hashref($args[0]) && exists $args[0]{"tables"}
Ref::Util::XS::is_plain_hashref($args[0]) && exists $args[0]{'tables'}
MC/DC100%3/3
@args == 1 and not Ref::Util::XS::is_plain_hashref($args[0]) && exists $args[0]{"tables"}
@args == 1
Ref::Util::XS::is_plain_hashref($args[0])
exists $args[0]{'tables'}
Truth table100%4/4
A and not(B && C)
A= @args == 1B= Ref::Util::XS::is_plain_hashref($args[0])C= exists $args[0]{'tables'}
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 4 | unshift @args, 'tables'; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 8 | my \%args = $class->$orig( @args ); | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 8 | ▶ | return \%args unless exists $args{tables}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 8 | my $tables = $args{tables}; | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 8 | ▶ | $tables = [$tables] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | unless is_arrayref $tables; | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 8 | $args{tables} = coerce_table_arrayref( $class, $tables ); | |||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 8 | return \%args; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 12 | ▶ | sub table_class { TABLE_CLASS } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
table_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 4 | signature_for table => ( | |||||||||||||||||||||||||||||||||||||||||
| 143 | method => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 144 | positional => [ Optional [Str] ], | ||||||||||||||||||||||||||||||||||||||||||
| 145 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 8 | ▶ | sub table ( $self, @table_name ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
table: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 8 | ▶ | if ( @table_name ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | $self->get( $table_name[0] ); | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | elsif ( $self->_tables->values == 1 ) { | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 8 | ( $self->_tables->values )[0]; | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | sub tables ( $self ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tables: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | [ $self->_tables->values ]; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 4 | signature_for add_table => ( | |||||||||||||||||||||||||||||||||||||||||
| 178 | method => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 179 | positional => [ InstanceOf [TABLE_CLASS] ] ); | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | sub add_table ( $self, $table ) { | ||||||||||||||||||||||||||||||||||||||||||
| 182 | CXC::DB::DDL::Failure::parameter_constraint->throw( | ||||||||||||||||||||||||||||||||||||||||||
| 183 | "attempt to add existing table: ${ \$table->name } " ) | ||||||||||||||||||||||||||||||||||||||||||
| 184 | if $self->exists( $table->name ); | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | $self->set( $table->name, $table ); | ||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 4 | signature_for sql => ( | |||||||||||||||||||||||||||||||||||||||||
| 224 | method => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 225 | head => [Object], # $dbh | ||||||||||||||||||||||||||||||||||||||||||
| 226 | named => [ ( | ||||||||||||||||||||||||||||||||||||||||||
| 227 | create => Optional [ Enum [CREATE_CONSTANTS] ], | ||||||||||||||||||||||||||||||||||||||||||
| 228 | { default => CREATE_ONCE }, | ||||||||||||||||||||||||||||||||||||||||||
| 229 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 230 | ( sqlt_comments => Optional [Bool], { default => !!1 } ), | ||||||||||||||||||||||||||||||||||||||||||
| 231 | ( sqlt_quote_identifiers => Optional [Bool], ), | ||||||||||||||||||||||||||||||||||||||||||
| 232 | ( sqlt_debug => Optional [Bool], { default => !!0 } ), | ||||||||||||||||||||||||||||||||||||||||||
| 233 | ( sqlt_trace => Optional [Bool], { default => !!0 } ), | ||||||||||||||||||||||||||||||||||||||||||
| 234 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 235 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | sub sql ( $self, $dbh, $opt ) { | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | my @tables = $self->tables->@*; | ||||||||||||||||||||||||||||||||||||||||||
| 240 | my @missing = grep { !$_->exists( $dbh ) } @tables; | ||||||||||||||||||||||||||||||||||||||||||
| 241 | my @existing = grep { $_->exists( $dbh ) } @tables; | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | my $create = $opt->create; | ||||||||||||||||||||||||||||||||||||||||||
| 244 | my $no_comments = !$opt->sqlt_comments; | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | my $add_drop_table = !!0; | ||||||||||||||||||||||||||||||||||||||||||
| 248 | # drop and create | ||||||||||||||||||||||||||||||||||||||||||
| 249 | if ( $create == CREATE_ALWAYS ) { | ||||||||||||||||||||||||||||||||||||||||||
| 250 | # if none of the tables exist, don't bother dropping them. | ||||||||||||||||||||||||||||||||||||||||||
| 251 | $add_drop_table = @existing; | ||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | # only create the missing tables | ||||||||||||||||||||||||||||||||||||||||||
| 255 | elsif ( $create == CREATE_IF_NOT_EXISTS ) { | ||||||||||||||||||||||||||||||||||||||||||
| 256 | return () if !@missing; | ||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | # try to create and die if already created. | ||||||||||||||||||||||||||||||||||||||||||
| 260 | elsif ( $create == CREATE_ONCE && @existing ) { | ||||||||||||||||||||||||||||||||||||||||||
| 261 | CXC::DB::DDL::Failure::create->throw( | ||||||||||||||||||||||||||||||||||||||||||
| 262 | 'attempt to create tables which already exist: ' . join( q{, }, map { $_->name } @existing ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | require SQL::Translator; | ||||||||||||||||||||||||||||||||||||||||||
| 266 | my $tr = SQL::Translator->new( | ||||||||||||||||||||||||||||||||||||||||||
| 267 | add_drop_table => $add_drop_table, | ||||||||||||||||||||||||||||||||||||||||||
| 268 | no_comments => $no_comments, | ||||||||||||||||||||||||||||||||||||||||||
| 269 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 270 | $opt->has_sqlt_quote_identifiers | ||||||||||||||||||||||||||||||||||||||||||
| 271 | ? ( quote_identifiers => $opt->sqlt_quote_identifiers ) | ||||||||||||||||||||||||||||||||||||||||||
| 272 | : () | ||||||||||||||||||||||||||||||||||||||||||
| 273 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 274 | debug => $opt->sqlt_debug, | ||||||||||||||||||||||||||||||||||||||||||
| 275 | trace => $opt->sqlt_trace, | ||||||||||||||||||||||||||||||||||||||||||
| 276 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 277 | my $schema = $tr->schema; | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | # create entire schema, as this ensures that all foreign key | ||||||||||||||||||||||||||||||||||||||||||
| 280 | # dependencies are met | ||||||||||||||||||||||||||||||||||||||||||
| 281 | $_->add_to_schema( $dbh, $schema ) for @tables; | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | # unless CREATE_ALWAYS, remove existing tables from the | ||||||||||||||||||||||||||||||||||||||||||
| 284 | # schema, so the DDL for them isn't created. | ||||||||||||||||||||||||||||||||||||||||||
| 285 | $schema->drop_table( $_->name ) for $create == CREATE_ALWAYS ? () : @existing; | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | my $dbd = $dbh->{Driver}->{Name}; | ||||||||||||||||||||||||||||||||||||||||||
| 288 | my $guard = wrap_producers( $dbd ); ## no critic(Variables::ProhibitUnusedVarsStricter) | ||||||||||||||||||||||||||||||||||||||||||
| 289 | my $producer = sqlt_entity_map( $dbd, 'producer' ); | ||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | my %producer_args = ( | ||||||||||||||||||||||||||||||||||||||||||
| 292 | no_transaction => 1, # we handle this ourselves. | ||||||||||||||||||||||||||||||||||||||||||
| 293 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | if ( defined( my $db_version_arg = sqlt_entity_map( $dbd, 'db_version' ) ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 296 | $producer_args{$db_version_arg} = db_version( $dbh ); | ||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | $tr->producer_args( \%producer_args ); | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | # SQL::Translator::translate() is calling context aware, so we need to | ||||||||||||||||||||||||||||||||||||||||||
| 302 | # do the same. | ||||||||||||||||||||||||||||||||||||||||||
| 303 | ## no critic (Community::Wantarray) | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | if ( wantarray ) { | ||||||||||||||||||||||||||||||||||||||||||
| 306 | my @res = $tr->translate( to => $producer ); | ||||||||||||||||||||||||||||||||||||||||||
| 307 | return @res if defined $res[0]; | ||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 310 | my $res = $tr->translate( to => $producer ); | ||||||||||||||||||||||||||||||||||||||||||
| 311 | return $res if defined $res; | ||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | CXC::DB::DDL::Failure::create->throw( $tr->error ); | ||||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | 4 | signature_for create => ( | |||||||||||||||||||||||||||||||||||||||||
| 354 | method => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 355 | positional => [ Object, Slurpy [HashRef] ] ); | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | sub create ( $self, $dbh, $opt ) { | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | my @sql = $self->sql( $dbh, $opt->%* ); | ||||||||||||||||||||||||||||||||||||||||||
| 360 | return unless @sql; | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | my $in_txn = !$dbh->{AutoCommit}; | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | my $statement; | ||||||||||||||||||||||||||||||||||||||||||
| 365 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 366 | $dbh->begin_work unless $in_txn; | ||||||||||||||||||||||||||||||||||||||||||
| 367 | while ( $statement = shift @sql ) { | ||||||||||||||||||||||||||||||||||||||||||
| 368 | next if all { /\s*--/ } split( /\n+/, $statement ); | ||||||||||||||||||||||||||||||||||||||||||
| 369 | die( "error running $statement" ) | ||||||||||||||||||||||||||||||||||||||||||
| 370 | if !defined $dbh->do( $statement ); | ||||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||
| 372 | $dbh->commit unless $in_txn; | ||||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||
| 374 | catch ( $e ) { | ||||||||||||||||||||||||||||||||||||||||||
| 375 | $dbh->rollback unless $in_txn; | ||||||||||||||||||||||||||||||||||||||||||
| 376 | CXC::DB::DDL::Failure::create->throw( "$e: " . ( $statement // q{} ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 377 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | ▶ | sub clear ( $self, $dbh ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | return map { $_->name } grep { !$_->clear( $dbh ) } $self->tables->@*; | |||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | ▶ | sub drop ( $self, $dbh ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
drop: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | 0 | $_->drop( $dbh ) for reverse $self->tables->@*; | |||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | ▶ | sub clone_simple ( $self ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clone_simple: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | return blessed( $self )->new( tables => [ map $_->clone_simple, $self->tables->@* ] ); | |||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | my %WrapProducer = ( | ||||||||||||||||||||||||||||||||||||||||||
| 419 | +( DBD_SQLITE ) => { | ||||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | ▶ | 'SQL::Translator::Producer::SQLite::create_table' => sub ( $orig, $table, $options ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | 0 | $options->{temporary_table} = $table->extra->{temporary}; | |||||||||||||||||||||||||||||||||||||||||
| 422 | 0 | $orig->( $table, $options ); | |||||||||||||||||||||||||||||||||||||||||
| 423 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 424 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 425 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | 0 | ▶ | sub wrap_producers ( $dbd ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wrap_producers: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | ▶ | my $wrapper = $WrapProducer{$dbd} // return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$WrapProducer{$dbd} // (return undef)
MC/DC0%0/1
$WrapProducer{$dbd} // (return undef)
$WrapProducer{$dbd}
Truth table0%0/2
A
A= $WrapProducer{$dbd}
| |||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||
| 431 | 0 | require Module::Runtime; | |||||||||||||||||||||||||||||||||||||||||
| 432 | 0 | require Sub::Override; | |||||||||||||||||||||||||||||||||||||||||
| 433 | 0 | my $override = Sub::Override->new; | |||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | 0 | for my $name ( keys %{$wrapper} ) { | |||||||||||||||||||||||||||||||||||||||||
| 436 | 0 | Module::Runtime::require_module( $name =~ s/::[^:]+$//r ); | |||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | $override->wrap( $name => $wrapper->{$name} ); | |||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | 0 | return $override; | |||||||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | # | ||||||||||||||||||||||||||||||||||||||||||
| 446 | # This file is part of CXC-DB-DDL | ||||||||||||||||||||||||||||||||||||||||||
| 447 | # | ||||||||||||||||||||||||||||||||||||||||||
| 448 | # This software is Copyright (c) 2022 by Smithsonian Astrophysical Observatory. | ||||||||||||||||||||||||||||||||||||||||||
| 449 | # | ||||||||||||||||||||||||||||||||||||||||||
| 450 | # This is free software, licensed under: | ||||||||||||||||||||||||||||||||||||||||||
| 451 | # | ||||||||||||||||||||||||||||||||||||||||||
| 452 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||||||||||||||||
| 453 | # | ||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | __END__ | ||||||||||||||||||||||||||||||||||||||||||