DDL/Field.pm

filter: Statementstmt 58.6% 44 / 75 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 80.0% 12 / 15 Podpod 100% 2 / 2 totaltotal 37.9% 58 / 153 CC 21 SCAR 54.8
CC
21
Cov
36%
to_sqlt
420.0
type_name
6.0
__ANON__
2.0
CRAP 240.7
↑ Summary DDL/Table.pm »
1 package CXC::DB::DDL::Field;
2  
3 # ABSTRACT: DDL Representation of a field
4  
5 7 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Ref::Util qw( is_coderef is_ref );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use CXC::DB::DDL::Failure;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use CXC::DB::DDL::Constants -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Types::Standard qw( ArrayRef Bool CodeRef Dict Enum HashRef Int Optional ScalarRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Types::Common::String qw( NonEmptyStr );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use experimental 'signatures', 'postderef', 'declared_refs', 'refaliasing';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 our $VERSION = '0.21';
18  
19 7 ▶use namespace::clean -except => [ 'has', '_tag_list', '_tags' ];
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 7 ▶use MooX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # need _tag_list for CloneClear (MooX::TaggedAttributes)
24 # need new for MooX::StrictConstructor
25  
26 7 with 'CXC::DB::DDL::CloneClear';
27  
28  
29  
30  
31  
32  
33  
34 7 has name => (
35 is => 'ro',
36 isa => NonEmptyStr,
37 required => 1,
38 );
39  
40  
41  
42  
43  
44  
45  
46 has data_type => (
47 is => 'ro',
48 126 ▶ isa => ArrayRef->of( Enum [SQL_TYPE_CONSTANTS] )->plus_coercions( Int, sub { [$_] } ),
Subroutine __ANON__: called CC=1 SCAR=0.0
49 coerce => 1,
50 required => 1,
51 );
52  
53  
54  
55  
56  
57  
58  
59 7 has is_nullable => (
60 is => 'ro',
61 isa => Bool,
62 default => 0,
63 );
64  
65  
66  
67  
68  
69  
70  
71 7 has is_primary_key => (
72 is => 'ro',
73 isa => Bool,
74 default => 0,
75 );
76  
77  
78  
79  
80  
81  
82  
83 7 has is_auto_increment => (
84 is => 'ro',
85 isa => Bool,
86 clearer => 1,
87 cloneclear => 1,
88 default => 0,
89 );
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109 7 has foreign_key => (
110 is => 'ro',
111 isa => NonEmptyStr | Dict [
112 table => NonEmptyStr,
113 field => Optional [NonEmptyStr],
114 on_delete => Optional [NonEmptyStr],
115 on_update => Optional [NonEmptyStr],
116 ],
117 clearer => 1,
118 cloneclear => 1,
119 predicate => 1,
120 );
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134 7 has check => (
135 is => 'ro',
136 isa => NonEmptyStr,
137 clearer => 1,
138 cloneclear => 1,
139 predicate => 1,
140 );
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152 7 has default_value => (
153 is => 'ro',
154 isa => NonEmptyStr | ScalarRef | CodeRef,
155 clearer => 1,
156 cloneclear => 1,
157 predicate => 1,
158 );
159  
160  
161  
162  
163  
164 0 ▶sub type_name ( $self, $dbh ) {
Subroutine type_name: not called CC=2 SCAR=17.9
Pod documented
165 return ( $dbh->type_info( $self->data_type )
166 0 ▶ // $dbh->type_info( [ SQL_VARCHAR, SQL_LONGVARCHAR ] ) )->{TYPE_NAME};
Condition0%0/3
$dbh->type_info($self->data_type) // $dbh->type_info([12, -1])
l!l&&r!l&&!r
000
MC/DC0%0/2
$dbh->type_info($self->data_type) // $dbh->type_info([12, -1])
$dbh->type_info($self->data_type) $dbh->type_info([12, -1])
Truth table0%0/3
A // B
A= $dbh->type_info($self->data_type)B= $dbh->type_info([12, -1])
ABresult
1X1
011
000
167 }
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184 0 ▶sub to_sqlt ( $self, $dbh, $table ) {
Subroutine to_sqlt: not called CC=20 SCAR=60.4
Pod documented
185  
186 0 require SQL::Translator::Schema::Field;
187  
188 0 ▶ my %attr = (
Branch: 2 branches
BranchTrueFalseTotal
$self->has_default_value ? : 0 0 0
Ref::Util::XS::is_coderef($self->default_value) ? : 0 0 0
189 name => $self->name,
190 table => $table,
191 is_nullable => $self->is_nullable,
192 data_type => $self->type_name( $dbh ),
193 is_auto_increment => $self->is_auto_increment,
194 (
195 $self->has_default_value
196 ? (
197 default_value => is_coderef( $self->default_value )
198 ? $self->default_value->( $dbh )
199 : $self->default_value,
200 )
201 : ()
202 ),
203 );
204  
205  
206 0 ▶ if ( $self->is_auto_increment && $dbh->{Driver}->{Name} eq DBD_SQLITE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_auto_increment and $dbh->{"Driver"}{"Name"} eq "SQLite") 0 0 0
Condition0%0/3
$self->is_auto_increment and $dbh->{"Driver"}{"Name"} eq "SQLite"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->is_auto_increment and $dbh->{"Driver"}{"Name"} eq "SQLite"
$self->is_auto_increment $dbh->{"Driver"}{"Name"} eq "SQLite"
Truth table0%0/3
A and B
A= $self->is_auto_incrementB= $dbh->{"Driver"}{"Name"} eq "SQLite"
ABresult
0X0
100
111
207 0 ▶ $self->is_primary_key
Branch: 1 branch
BranchTrueFalseTotal
$self->is_primary_key or "CXC::DB::DDL::Failure::ddl"->throw(join(".", $table->name, $self->name) . ': SQLite requires an autoincrementing column to be a primary key.') 0 0 0
208 or CXC::DB::DDL::Failure::ddl->throw(
209 join( q{.}, $table->name, $self->name )
210 . ': SQLite requires an autoincrementing column to be a primary key.', );
211  
212 CXC::DB::DDL::Failure::ddl->throw(
213 join( q{.}, $table->name, $self->name )
214 . ': SQLite requires an autoincrementing column to be of INTEGER type.', )
215 0 ▶ if $attr{data_type} ne 'INTEGER';
Branch: 1 branch
BranchTrueFalseTotal
if $attr{"data_type"} ne "INTEGER" 0 0 0
216  
217 # this magic comes from reading the source of
218 # SQL::Translator::Generator::DDL::SQLite::field_autoinc()
219 0 $attr{extra} = { auto_increment_type => 'monotonic' };
220 }
221  
222  
223 0 ▶ if ( $self->has_foreign_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_foreign_key) 0 0 0
224 0 require SQL::Translator::Schema::Constraint;
225  
226 0 ▶ my \%fk
Branch: 1 branch
BranchTrueFalseTotal
!Ref::Util::XS::is_ref($self->foreign_key) ? : 0 0 0
227 = !is_ref( $self->foreign_key )
228 ? { table => $self->foreign_key }
229 : $self->foreign_key;
230  
231 0 ▶ $fk{field} //= $self->name;
Condition0%0/3
$fk{"field"} //= $self->name
l!l&&r!l&&!r
000
MC/DC0%0/2
$fk{"field"} //= $self->name
$fk{"field"} $self->name
Truth table0%0/3
A //= B
A= $fk{"field"}B= $self->name
ABresult
1X1
011
000
232  
233 0 ▶ my $reference_table = first { $fk{table} eq $_->name } $table->schema->get_tables;
Subroutine __ANON__: not called CC=1 SCAR=6.9
234  
235 0 ▶ $reference_table
Branch: 1 branch
BranchTrueFalseTotal
$reference_table or "CXC::DB::DDL::Failure::ddl"->throw(join(".", $table->name, $self->name) . qq[: unknown table in foreign key constraint: $fk{"table"}]) 0 0 0
236 or CXC::DB::DDL::Failure::ddl->throw(
237 join( q{.}, $table->name, $self->name ) . ": unknown table in foreign key constraint: $fk{table}" );
238  
239 $attr{foreign_key_reference} = SQL::Translator::Schema::Constraint->new(
240 table => $table,
241 type => FOREIGN_KEY,
242 fields => $self->name,
243 reference_fields => $fk{field},
244 ( defined $fk{on_delete} ? %fk{on_delete} : () ),
245 0 ▶ ( defined $fk{on_update} ? %fk{on_update} : () ),
Branch: 2 branches
BranchTrueFalseTotal
defined $fk{'on_delete'} ? : 0 0 0
defined $fk{'on_update'} ? : 0 0 0
246 reference_table => $reference_table,
247 );
248 }
249  
250 # create this before adding table check constraint on this field
251 # in case table object needs the field object.
252 0 ▶ my $field = $table->add_field( %attr )
Condition0%0/3
$table->add_field(%attr) // "CXC::DB::DDL::Failure::ddl"->throw("error adding field ${\$self->name;} to table ${\$table->name;}")
l!l&&r!l&&!r
000
MC/DC0%0/2
$table->add_field(%attr) // "CXC::DB::DDL::Failure::ddl"->throw("error adding field ${\$self->name;} to table ${\$table->name;}")
$table->add_field(%attr) "CXC::DB::DDL::Failure::ddl"->throw("error adding field ${\$self->name;} to table ${\$table->name;}")
Truth table0%0/3
A // B
A= $table->add_field(%attr)B= "CXC::DB::DDL::Failure::ddl"->throw("error adding field ${\$self->name;} to table ${\$table->name;}")
ABresult
1X1
011
000
253 // CXC::DB::DDL::Failure::ddl->throw(
254 0 "error adding field ${ \$self->name } to table ${ \$table->name }" );
255  
256 0 ▶ if ( $self->has_check ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_check) 0 0 0
257 0 ▶ $table->add_constraint(
Condition0%0/3
$table->add_constraint("type", "CHECK", "fields", $self->name, "expression", $self->check) // "CXC::DB::DDL::Failure::ddl"->throw("error adding check constraint ( ${\$self->check;} ) on ${\$self->name;} : ${\$table->error;}")
l!l&&r!l&&!r
000
MC/DC0%0/2
$table->add_constraint("type", "CHECK", "fields", $self->name, "expression", $self->check) // "CXC::DB::DDL::Failure::ddl"->throw("error adding check constraint ( ${\$self->check;} ) on ${\$self->name;} : ${\$table->error;}")
$table->add_constraint("type", "CHECK", "fields", $self->name, "expression", $self->check) "CXC::DB::DDL::Failure::ddl"->throw("error adding check constraint ( ${\$self->check;} ) on ${\$self->name;} : ${\$table->error;}")
Truth table0%0/3
A // B
A= $table->add_constraint("type", "CHECK", "fields", $self->name, "expression", $self->check)B= "CXC::DB::DDL::Failure::ddl"->throw("error adding check constraint ( ${\$self->check;} ) on ${\$self->name;} : ${\$table->error;}")
ABresult
1X1
011
000
258 type => CHECK_C,
259 fields => $self->name,
260 expression => $self->check,
261 )
262 // CXC::DB::DDL::Failure::ddl->throw(
263 0 "error adding check constraint ( ${ \$self->check } ) on ${ \$self->name } : ${ \$table->error }",
264 );
265 }
266  
267 # SQL::Translator::Schema::Field has a settable is_primary_key
268 # attribute which does nothing. Need to use a table constraint.
269 0 ▶ if ( $self->is_primary_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_primary_key) 0 0 0
270 0 ▶ $table->add_constraint(
Condition0%0/3
$table->add_constraint("type", "PRIMARY KEY", "fields", $self->name) // "CXC::DB::DDL::Failure::ddl"->throw("error adding PRIMARY constraint on ${\$self->name;}: ${\$table->error;}")
l!l&&r!l&&!r
000
MC/DC0%0/2
$table->add_constraint("type", "PRIMARY KEY", "fields", $self->name) // "CXC::DB::DDL::Failure::ddl"->throw("error adding PRIMARY constraint on ${\$self->name;}: ${\$table->error;}")
$table->add_constraint("type", "PRIMARY KEY", "fields", $self->name) "CXC::DB::DDL::Failure::ddl"->throw("error adding PRIMARY constraint on ${\$self->name;}: ${\$table->error;}")
Truth table0%0/3
A // B
A= $table->add_constraint("type", "PRIMARY KEY", "fields", $self->name)B= "CXC::DB::DDL::Failure::ddl"->throw("error adding PRIMARY constraint on ${\$self->name;}: ${\$table->error;}")
ABresult
1X1
011
000
271 type => PRIMARY_KEY,
272 fields => $self->name,
273 )
274 // CXC::DB::DDL::Failure::ddl->throw(
275 0 "error adding PRIMARY constraint on ${ \$self->name }: ${ \$table->error }", );
276 }
277  
278 0 ▶ if ( $self->has_foreign_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_foreign_key) 0 0 0
279 $table->add_constraint( $attr{foreign_key_reference} )
280 0 ▶ // CXC::DB::DDL::Failure::ddl->throw(
Condition0%0/3
$table->add_constraint($attr{"foreign_key_reference"}) // "CXC::DB::DDL::Failure::ddl"->throw(qq[error adding FOREIGN KEY constraint on ${\$self->name;}: $attr{"foreign_key_reference"}])
l!l&&r!l&&!r
000
MC/DC0%0/2
$table->add_constraint($attr{"foreign_key_reference"}) // "CXC::DB::DDL::Failure::ddl"->throw(qq[error adding FOREIGN KEY constraint on ${\$self->name;}: $attr{"foreign_key_reference"}])
$table->add_constraint($attr{"foreign_key_reference"}) "CXC::DB::DDL::Failure::ddl"->throw(qq[error adding FOREIGN KEY constraint on ${\$self->name;}: $attr{"foreign_key_reference"}])
Truth table0%0/3
A // B
A= $table->add_constraint($attr{"foreign_key_reference"})B= "CXC::DB::DDL::Failure::ddl"->throw(qq[error adding FOREIGN KEY constraint on ${\$self->name;}: $attr{"foreign_key_reference"}])
ABresult
1X1
011
000
281 0 "error adding FOREIGN KEY constraint on ${ \$self->name }: $attr{foreign_key_reference}", );
282 }
283  
284 0 return $field;
285 }
286  
287 #
288 # This file is part of CXC-DB-DDL
289 #
290 # This software is Copyright (c) 2022 by Smithsonian Astrophysical Observatory.
291 #
292 # This is free software, licensed under:
293 #
294 # The GNU General Public License, Version 3, June 2007
295 #
296  
297 7 1;
298  
299 __END__
↑ Summary DDL/Table.pm »