DDL/Table.pm

filter: Statementstmt 58.9% 82 / 139 Branchbran 19.0% 8 / 42 Conditioncond 17.3% 4 / 23 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 63.6% 21 / 33 Podpod 85.7% 6 / 7 totaltotal 47.2% 122 / 258 CC 37 SCAR 50.6
CC
37
Cov
46%
to_sqlt
506.0
__ANON__
12.1
exists
6.0
CRAP 157.4
« DDL/Field.pm ↑ Summary DDL/Util.pm »
1 package CXC::DB::DDL::Table;
2  
3 # ABSTRACT: A Table
4  
5 6 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our $VERSION = '0.21';
10  
11 6 ▶use List::Util qw( any );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Ref::Util qw( is_ref is_arrayref is_coderef is_plain_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use CXC::DB::DDL::Failure;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use CXC::DB::DDL::Constants -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use CXC::DB::DDL::Types -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Type::Params qw( signature_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Types::Standard qw( ArrayRef Bool Dict Enum HashRef InstanceOf Object Optional Str Undef );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use Types::Common::String qw( NonEmptyStr );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶use experimental 'signatures', 'postderef', 'declared_refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 6 ▶use namespace::clean -except => 'has';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 6 with 'CXC::DB::DDL::CloneClear';
27  
28 # use after namespace::clean to avoid cleaning out important bits.
29 6 ▶use MooX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31  
32  
33  
34  
35  
36  
37 6 ▶use constant FIELD_CLASS => 'CXC::DB::DDL::Field';
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 0 ▶my sub croak {
Subroutine croak: not called CC=1 SCAR=6.9
40 0 require Carp;
41 0 goto \&Carp::croak;
42 }
43  
44 6 has _name => (
45 is => 'ro',
46 init_arg => 'name',
47 isa => NonEmptyStr,
48 required => 1,
49 );
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63 6 has schema => (
64 is => 'ro',
65 isa => NonEmptyStr,
66 predicate => 1,
67 );
68  
69  
70  
71  
72  
73  
74  
75  
76 has name => (
77 is => 'lazy',
78 init_arg => undef,
79 8 ▶ builder => sub ( $self ) {
Subroutine _build_name: called CC=2 SCAR=7.6
80 8 ▶ return $self->has_schema
Branch: 1 branch
BranchTrueFalseTotal
$self->has_schema ? : 0 8 8
81 ? join( q{.}, $self->schema, $self->_name )
82 : $self->_name;
83 },
84 6 );
85  
86  
87  
88  
89  
90  
91  
92 6 has temporary => (
93 is => 'ro',
94 isa => Bool,
95 default => !!0,
96 );
97  
98  
99  
100  
101  
102  
103  
104 has indexes => (
105 is => 'lazy',
106 isa => Indexes,
107 0 ▶ builder => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine _build_indexes: not called CC=1 SCAR=6.9
108 6 coerce => 1,
109 clearer => 1,
110 cloneclear => 1,
111 );
112  
113  
114  
115  
116  
117  
118  
119 has constraints => (
120 is => 'lazy',
121 isa => ArrayRef->of( Constraint )->plus_coercions( Constraint, q{ [$_] } ),
122 coerce => 1,
123 clearer => 1,
124 cloneclear => 1,
125 0 ▶ builder => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 6 );
127  
128  
129  
130  
131  
132  
133  
134  
135  
136 has checks => (
137 is => 'lazy',
138 isa => ArrayRef [ NonEmptyStr | Dict [ name => NonEmptyStr, expr => NonEmptyStr ], ],
139 clearer => 1,
140 cloneclear => 1,
141 0 ▶ builder => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 6 );
143  
144  
145  
146  
147  
148  
149  
150  
151 has fields => (
152 is => 'lazy',
153 isa => ArrayRef( [ InstanceOf [FIELD_CLASS] ] ),
154 clearer => 1,
155 cloneclear => 1,
156 0 ▶ builder => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
157 6 );
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
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  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245 9 ▶around BUILDARGS => sub ( $orig, $class, @args ) {
Subroutine __ANON__: called CC=8 SCAR=24.9
246  
247 9 my \%args = $class->$orig( @args );
248  
249 9 ▶ if ( defined $args{name} && !defined $args{schema} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"name"} and not defined $args{"schema"}) 9 0 9
Condition33%1/3
defined $args{"name"} and not defined $args{"schema"}
!ll&&!rl&&r
009
MC/DC0%0/2
defined $args{"name"} and not defined $args{"schema"}
defined $args{"name"} not defined $args{"schema"}
Truth table33%1/3
A and not(B)
A= defined $args{"name"}B= not defined $args{"schema"}
ABresult
0X0
100
111
250  
251 9 my $pos = index( $args{name}, q{.} );
252  
253 9 ▶ if ( $pos == -1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pos == -1) { } 9 0 9
elsif ($pos > 0) { } 0 0 0
254 9 delete $args{schema};
255 }
256 elsif ( $pos > 0 ) {
257 0 $args{schema} = substr( $args{name}, 0, $pos );
258 0 $args{name} = substr( $args{name}, $pos + 1 );
259 }
260 else {
261 0 parameter_constraint->throw( "illegal table name: $args{name}" );
262 }
263 }
264  
265 9 ▶ if ( defined $args{fields} && is_arrayref( $args{fields} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"fields"} and Ref::Util::XS::is_arrayref($args{"fields"})) 9 0 9
Condition33%1/3
defined $args{"fields"} and Ref::Util::XS::is_arrayref($args{"fields"})
!ll&&!rl&&r
009
MC/DC0%0/2
defined $args{"fields"} and Ref::Util::XS::is_arrayref($args{"fields"})
defined $args{"fields"} Ref::Util::XS::is_arrayref($args{"fields"})
Truth table33%1/3
A and B
A= defined $args{"fields"}B= Ref::Util::XS::is_arrayref($args{"fields"})
ABresult
0X0
100
111
266 9 require Module::Load;
267 9 Module::Load::load( $class->field_class );
268  
269 9 my @fields = $args{fields}->@*;
270  
271 9 $_ = $class->field_class->new( $_ ) for grep { is_plain_hashref( $_ ) } @fields;
272  
273 9 $args{fields} = \@fields;
274 }
275  
276 9 return \%args;
277 6 };
278  
279 9 ▶sub BUILD ( $self, $args ) {
Subroutine BUILD: called CC=5 SCAR=16.1
Pod undocumented
280  
281 # convert constraints' fields into array refs if required; handle
282 # a scalar of '-all' as well. ATM, constraints are hashes which
283 # pass the CXC::DB::DDL::Types::Constraint constraint.
284 9 for my $constraint ( $self->constraints->@* ) {
285  
286 18 my $fields = $constraint->{fields};
287  
288 # only worry about scalars
289 18 ▶ next if !defined( $fields ) || ref $fields;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $fields or ref $fields 14 4 18
Condition66%2/3
not defined $fields or ref $fields
l!l&&r!l&&!r
0144
MC/DC50%1/2
not defined $fields or ref $fields
not defined $fields ref $fields
Truth table66%2/3
not(A) or B
A= not defined $fieldsB= ref $fields
ABresult
1X1
011
000
290  
291 $constraint->{fields} =
292 # replace fields => '-all' with all of the fields (->field_names() returns a copy)
293 4 ▶ $fields eq '-all'
Branch: 1 branch
BranchTrueFalseTotal
$fields eq '-all' ? : 3 1 4
294 ? $self->field_names
295 # otherwise turn it into an array
296 : [$fields];
297 }
298  
299 }
300  
301  
302  
303  
304  
305  
306  
307  
308  
309 15 ▶sub field_class { FIELD_CLASS }
Subroutine field_class: called CC=1 SCAR=0.0
Pod documented
310  
311  
312  
313  
314  
315  
316  
317  
318  
319  
320  
321  
322 0 ▶sub to_sqlt ( $self, $dbh, $schema ) {
Subroutine to_sqlt: not called CC=22 SCAR=62.3
Pod documented
323  
324 0 my %extra;
325  
326 0 ▶ if ( $self->temporary ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->temporary) 0 0 0
327  
328 # this works for SQL::Translator::Producer::PostgreSQL, but not for
329 # SQL::Translator::Producer::SQLite; that gets monkey-patched in CXC::DB::DDL
330 0 $extra{temporary} = $self->temporary;
331  
332 0 my $dbd = $dbh->{Driver}->{Name};
333  
334 # SYNC THIS CODE WITH THE OVERRIDE FUNCTION IN CXC::DB::DDL.
335 0 ▶ croak(
Branch: 1 branch
BranchTrueFalseTotal
unless $dbd eq "SQLite" or $dbd eq "Pg" 0 0 0
Condition0%0/3
$dbd eq "SQLite" or $dbd eq "Pg"
l!l&&r!l&&!r
000
MC/DC0%0/2
$dbd eq "SQLite" or $dbd eq "Pg"
$dbd eq "SQLite" $dbd eq "Pg"
Truth table0%0/3
A or B
A= $dbd eq "SQLite"B= $dbd eq "Pg"
ABresult
1X1
011
000
336 "SQL::Translator either doesn't support temp tables for DBD::$dbd, or we don't know how to make it do so",
337 ) unless $dbd eq DBD_SQLITE || $dbd eq DBD_POSTGRESQL;
338 }
339  
340  
341 0 my $sqlt_table = $schema->add_table( name => $self->name, extra => \%extra );
342  
343 0 for my $field ( $self->fields->@* ) {
344 0 ▶ $field->to_sqlt( $dbh, $sqlt_table )
Condition0%0/3
$field->to_sqlt($dbh, $sqlt_table) // "CXC::DB::DDL::Failure::ddl"->throw(sprintf("Error adding field %s to table %s: %s", $field->name, $self->name, $sqlt_table->error))
l!l&&r!l&&!r
000
MC/DC0%0/2
$field->to_sqlt($dbh, $sqlt_table) // "CXC::DB::DDL::Failure::ddl"->throw(sprintf("Error adding field %s to table %s: %s", $field->name, $self->name, $sqlt_table->error))
$field->to_sqlt($dbh, $sqlt_table) "CXC::DB::DDL::Failure::ddl"->throw(sprintf("Error adding field %s to table %s: %s", $field->name, $self->name, $sqlt_table->error))
Truth table0%0/3
A // B
A= $field->to_sqlt($dbh, $sqlt_table)B= "CXC::DB::DDL::Failure::ddl"->throw(sprintf("Error adding field %s to table %s: %s", $field->name, $self->name, $sqlt_table->error))
ABresult
1X1
011
000
345 // CXC::DB::DDL::Failure::ddl->throw(
346 sprintf( 'Error adding field %s to table %s: %s', $field->name, $self->name, $sqlt_table->error ),
347 );
348 }
349  
350 0 for my $index ( $self->indexes->@* ) {
351  
352 # if $index is a string, it's a normal index on that field
353 0 ▶ if ( !is_ref( $index ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Ref::Util::XS::is_ref($index)) 0 0 0
354 0 $index = { fields => $index };
355 }
356  
357 0 my %attr = $index->%*;
358 0 ▶ $attr{type} //= NORMAL;
Condition0%0/2
$attr{"type"} //= "NORMAL"
l!l
00
MC/DC0%0/1
$attr{"type"} //= "NORMAL"
$attr{"type"}
Truth table0%0/2
A
A= $attr{"type"}
Aresult
11
00
359  
360 # if name is not specified, create one from the field names and index type.
361  
362 $attr{name} //= join( '_',
363 $self->_name, 'idx',
364 ( $attr{type} ne NORMAL ? lc( $attr{type} =~ s/\s+/_/gr ) : () ),
365 0 ▶ ( is_arrayref( $attr{fields} ) ? $attr{fields}->@* : $attr{fields} ),
Branch: 2 branches
BranchTrueFalseTotal
$attr{'type'} ne 'NORMAL' ? : 0 0 0
Ref::Util::XS::is_arrayref($attr{'fields'}) ? : 0 0 0
Condition0%0/2
$attr{"name"} //= join("_", $self->_name, "idx", $attr{"type"} ne "NORMAL" ? lc $attr{"type"} =~ s/\s+/_/gru : (), Ref::Util::XS::is_arrayref($attr{"fields"}) ? @{$attr{"fields"}} : $attr{"fields"})
l!l
00
MC/DC0%0/1
$attr{"name"} //= join("_", $self->_name, "idx", $attr{"type"} ne "NORMAL" ? lc $attr{"type"} =~ s/\s+/_/gru : (), Ref::Util::XS::is_arrayref($attr{"fields"}) ? @{$attr{"fields"}} : $attr{"fields"})
$attr{"name"}
Truth table0%0/2
A
A= $attr{"name"}
Aresult
11
00
366 );
367  
368 $sqlt_table->add_index( %attr )
369 or CXC::DB::DDL::Failure::ddl->throw(
370 0 ▶ sprintf( 'error adding index %s: %s', $attr{name}, $sqlt_table->error, ) );
Branch: 1 branch
BranchTrueFalseTotal
$sqlt_table->add_index(%attr) or "CXC::DB::DDL::Failure::ddl"->throw(sprintf("error adding index %s: %s", $attr{"name"}, $sqlt_table->error)) 0 0 0
371 }
372  
373 0 my $cstr = 'cstr000';
374 0 for my $constraint ( $self->constraints->@* ) {
375  
376 0 my %attr = $constraint->%*;
377  
378 # if name is not specified, create one from the field names
379 $attr{name} //= join(
380 '_',
381 $self->_name, # name without schema
382 ++$cstr,
383 lc( $attr{type} =~ s/\s+/_/gr ),
384 defined( $attr{fields} )
385 ? (
386 $attr{fields}->@*
387 )
388 0 ▶ : (),
Branch: 1 branch
BranchTrueFalseTotal
defined $attr{'fields'} ? : 0 0 0
Condition0%0/2
$attr{"name"} //= join("_", $self->_name, ++$cstr, lc $attr{"type"} =~ s/\s+/_/gru, defined $attr{"fields"} ? @{$attr{"fields"}} : ())
l!l
00
MC/DC0%0/1
$attr{"name"} //= join("_", $self->_name, ++$cstr, lc $attr{"type"} =~ s/\s+/_/gru, defined $attr{"fields"} ? @{$attr{"fields"}} : ())
$attr{"name"}
Truth table0%0/2
A
A= $attr{"name"}
Aresult
11
00
389 );
390  
391 0 ▶ if ( is_coderef( my $expr = $attr{expression} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_coderef(my $expr = $attr{"expression"})) 0 0 0
392 0 $attr{expression} = $expr->( $dbh, $schema->translator, $constraint );
393 }
394  
395 $sqlt_table->add_constraint( %attr )
396 or CXC::DB::DDL::Failure::ddl->throw(
397 0 ▶ sprintf( 'error adding constraint %s: %s', $attr{name}, $sqlt_table->error, ) );
Branch: 1 branch
BranchTrueFalseTotal
$sqlt_table->add_constraint(%attr) or "CXC::DB::DDL::Failure::ddl"->throw(sprintf("error adding constraint %s: %s", $attr{"name"}, $sqlt_table->error)) 0 0 0
398 }
399  
400 0 my $check_num = '00';
401 0 for my $check ( $self->checks->@* ) {
402  
403 0 ▶ my %attr = is_ref( $check ) ? $check->%* : ( expr => $check );
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_ref($check) ? : 0 0 0
404  
405 $attr{name}
406 0 ▶ = join( '_', $self->_name, 'check', $attr{name} // ++$check_num );
Condition0%0/2
$attr{"name"} // ++$check_num
l!l
00
MC/DC0%0/1
$attr{"name"} // ++$check_num
$attr{"name"}
Truth table0%0/2
A
A= $attr{"name"}
Aresult
11
00
407 0 $attr{expression} = delete $attr{expr};
408  
409 $sqlt_table->add_constraint( type => CHECK_C, %attr )
410 or CXC::DB::DDL::Failure::ddl->throw(
411 0 ▶ sprintf( 'error adding check %s: %s', $attr{expression}, $sqlt_table->error, ) );
Branch: 1 branch
BranchTrueFalseTotal
$sqlt_table->add_constraint("type", "CHECK", %attr) or "CXC::DB::DDL::Failure::ddl"->throw(sprintf("error adding check %s: %s", $attr{"expression"}, $sqlt_table->error)) 0 0 0
412 }
413  
414 0 return $sqlt_table;
415 }
416  
417  
418  
419  
420  
421  
422  
423  
424  
425 0 ▶sub exists ( $self, $dbh ) { ## no critic(Subroutines::ProhibitBuiltinHomonyms)
Subroutine exists: not called CC=2 SCAR=17.9
Pod documented
426  
427 0 ▶ my $schema = $self->has_schema ? $self->schema : q{%};
Branch: 1 branch
BranchTrueFalseTotal
$self->has_schema ? : 0 0 0
428  
429 # make sure that we agree on case for key names
430 ## no critic( Variables::ProhibitLocalVars )
431 0 local $dbh->{FetchHashKeyName} = 'NAME_lc';
432  
433 0 my $sth = $dbh->table_info( q{%}, $schema, $self->_name, 'TABLE' );
434  
435 # Not all DBD drivers can filter properly
436 0 ▶ return any { $_->{table_name} eq $self->_name } $sth->fetchall_arrayref( {} )->@*;
Subroutine __ANON__: not called CC=1 SCAR=6.9
437 }
438  
439  
440  
441  
442  
443  
444  
445  
446  
447 3 ▶sub field_names ( $self ) {
Subroutine field_names: called CC=1 SCAR=0.0
Pod documented
448 3 [ map { $_->name } $self->fields->@* ]
449 }
450  
451  
452  
453  
454  
455  
456  
457  
458  
459 0 ▶sub clear ( $self, $dbh ) {
Subroutine clear: not called CC=2 SCAR=17.9
Pod documented
460 0 ▶ return 0 unless $self->exists( $dbh );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists($dbh) 0 0 0
461 0 $dbh->do( "DELETE FROM ${ \$self->name }" );
462 0 return 1;
463 }
464  
465  
466  
467  
468  
469  
470  
471  
472  
473 0 ▶sub drop ( $self, $dbh ) {
Subroutine drop: not called CC=2 SCAR=17.9
Pod documented
474 0 ▶ return 0 unless $self->exists( $dbh );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists($dbh) 0 0 0
475 0 $dbh->do( "DROP TABLE ${ \$self->name }" );
476 0 return 1;
477 }
478  
479  
480  
481  
482  
483  
484  
485  
486  
487 6 signature_for add_to_schema => (
488 method => 1,
489 positional => [ Object, InstanceOf ['SQL::Translator::Schema'], ] );
490  
491 sub add_to_schema ( $self, $dbh, $schema ) {
492  
493 $self->to_sqlt( $dbh, $schema );
494 }
495  
496 0 ▶around 'clone_simple' => sub ( $orig, $self, @args ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
497 0 my $fields = $self->fields;
498 0 my $clone = $self->$orig( @args );
499 0 $clone->fields->@* = map { $_->clone_simple } $fields->@*;
500 0 return $clone;
501 6 };
502  
503 #
504 # This file is part of CXC-DB-DDL
505 #
506 # This software is Copyright (c) 2022 by Smithsonian Astrophysical Observatory.
507 #
508 # This is free software, licensed under:
509 #
510 # The GNU General Public License, Version 3, June 2007
511 #
512  
513 6 1;
514  
515 __END__
« DDL/Field.pm ↑ Summary DDL/Util.pm »