blib/lib/SQL/Translator/Schema/Constraint.pm

filter: Statementstmt 99.2% 133 / 134 Branchbran 75.0% 72 / 96 Conditioncond 65.0% 13 / 20 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 100% 15 / 15 Podpod 100% 2 / 2 totaltotal 85.0% 238 / 280 CC 63 SCAR 29.2
CC
63
Cov
87%
__ANON__
33.6
is_valid
19.3
_build_reference_fields
6.0
CRAP 18.6
« blib/lib/SQL/Translator/Producer/Dumper.pm ↑ Summary blib/lib/SQL/Translator/Parser/Access.pm »
1 package SQL::Translator::Schema::Constraint;
2  
3 =pod
4  
5 =head1 NAME
6  
7 SQL::Translator::Schema::Constraint - SQL::Translator constraint object
8  
9 =head1 SYNOPSIS
10  
11 use SQL::Translator::Schema::Constraint;
12 my $constraint = SQL::Translator::Schema::Constraint->new(
13 name => 'foo',
14 fields => [ id ],
15 type => PRIMARY_KEY,
16 );
17  
18 =head1 DESCRIPTION
19  
20 C<SQL::Translator::Schema::Constraint> is the constraint object.
21  
22 =head1 METHODS
23  
24 =cut
25  
26 76 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 76 ▶use SQL::Translator::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 76 ▶use SQL::Translator::Utils qw(ex2err throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 76 ▶use SQL::Translator::Role::ListAttr;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 76 ▶use SQL::Translator::Types qw(schema_obj enum);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 76 ▶use Sub::Quote qw(quote_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 76 extends 'SQL::Translator::Schema::Object';
34  
35 76 our $VERSION = '1.66';
36  
37 76 my %VALID_CONSTRAINT_TYPE = (PRIMARY_KEY, 1, UNIQUE, 1, CHECK_C, 1, FOREIGN_KEY, 1, NOT_NULL, 1, EXCLUDE, 1,);
38  
39 =head2 new
40  
41 Object constructor.
42  
43 my $schema = SQL::Translator::Schema::Constraint->new(
44 table => $table, # table to which it belongs
45 type => 'foreign_key', # type of table constraint
46 name => 'fk_phone_id', # name of the constraint
47 fields => 'phone_id', # field in the referring table
48 reference_fields => 'phone_id', # referenced field
49 reference_table => 'phone', # referenced table
50 match_type => 'full', # how to match
51 on_delete => 'cascade', # what to do on deletes
52 on_update => '', # what to do on updates
53 );
54  
55 =cut
56  
57 # Override to remove empty arrays from args.
58 # t/14postgres-parser breaks without this.
59 643 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
60 643 my $orig = shift;
61 643 my $self = shift;
62 643 my $args = $self->$orig(@_);
63  
64 643 foreach my $arg (keys %{$args}) {
65 delete $args->{$arg}
66 3746 ▶ if ref($args->{$arg}) eq "ARRAY" && !@{ $args->{$arg} };
Branch: 1 branch
BranchTrueFalseTotal
if ref $args->{$arg} eq "ARRAY" and not @{$args->{$arg};} 188 3558 3746
Condition100%3/3
ref $args->{$arg} eq "ARRAY" and not @{$args->{$arg};}
!ll&&!rl&&r
2961597188
MC/DC100%2/2
ref $args->{$arg} eq "ARRAY" and not @{$args->{$arg};}
ref $args->{$arg} eq "ARRAY" not @{$args->{$arg};}
Truth table100%3/3
A and not(B)
A= ref $args->{$arg} eq "ARRAY"B= not @{$args->{$arg};}
ABresult
0X0
100
111
67 }
68 643 ▶ if (exists $args->{fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"fields"}) 592 51 643
69 592 $args->{field_names} = delete $args->{fields};
70 }
71 643 return $args;
72 76 };
73  
74 =head2 deferrable
75  
76 Get or set whether the constraint is deferrable. If not defined,
77 then returns "1." The argument is evaluated by Perl for True or
78 False, so the following are equivalent:
79  
80 $deferrable = $field->deferrable(0);
81 $deferrable = $field->deferrable('');
82 $deferrable = $field->deferrable('0');
83  
84 =cut
85  
86 76 has deferrable => (
87 is => 'rw',
88 coerce => quote_sub(q{ $_[0] ? 1 : 0 }),
89 default => quote_sub(q{ 1 }),
90 );
91  
92 =head2 expression
93  
94 Gets and set the expression used in a CHECK constraint.
95  
96 my $expression = $constraint->expression('...');
97  
98 =cut
99  
100 76 has expression => (is => 'rw', default => quote_sub(q{ '' }));
101  
102 470 ▶around expression => sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
103 470 my ($orig, $self, $arg) = @_;
104 470 ▶ $self->$orig($arg || ());
Condition33%1/3
$arg || ()
l!l&&r!l&&!r
04700
MC/DC0%0/2
$arg || ()
$arg ()
Truth table33%1/3
A || B
A= $argB= ()
ABresult
1X1
011
000
105 76 };
106  
107 43 ▶sub is_valid {
Subroutine is_valid: called CC=18 SCAR=29.6
Pod documented
108  
109 =pod
110  
111 =head2 is_valid
112  
113 Determine whether the constraint is valid or not.
114  
115 my $ok = $constraint->is_valid;
116  
117 =cut
118  
119 43 my $self = shift;
120 43 ▶ my $type = $self->type or return $self->error('No type');
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->type or return $self->error("No type") 42 1 43
121 42 ▶ my $table = $self->table or return $self->error('No table');
Branch: 1 branch
BranchTrueFalseTotal
my $table = $self->table or return $self->error("No table") 42 0 42
122 42 ▶ my @fields = $self->fields or return $self->error('No fields');
Branch: 1 branch
BranchTrueFalseTotal
my(@fields) = $self->fields or return $self->error("No fields") 41 1 42
123 41 ▶ my $table_name = $table->name or return $self->error('No table name');
Branch: 1 branch
BranchTrueFalseTotal
my $table_name = $table->name or return $self->error("No table name") 41 0 41
124  
125 41 for my $f (@fields) {
126 46 ▶ next if $table->get_field($f);
Branch: 1 branch
BranchTrueFalseTotal
if $table->get_field($f) 45 1 46
127 1 return $self->error("Constraint references non-existent field '$f' ", "in table '$table_name'");
128 }
129  
130 40 ▶ my $schema = $table->schema
Branch: 1 branch
BranchTrueFalseTotal
my $schema = $table->schema or return $self->error("Table ", $table->name, " has no schema object") 40 0 40
131 or return $self->error('Table ', $table->name, ' has no schema object');
132  
133 40 ▶ if ($type eq FOREIGN_KEY) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'FOREIGN KEY') { } 13 27 40
elsif ($type eq 'CHECK') { } 0 27 27
134 13 ▶ return $self->error('Only one field allowed for foreign key')
Branch: 1 branch
BranchTrueFalseTotal
if scalar @fields > 1 1 12 13
135 if scalar @fields > 1;
136  
137 12 ▶ my $ref_table_name = $self->reference_table
Branch: 1 branch
BranchTrueFalseTotal
my $ref_table_name = $self->reference_table or return $self->error("No reference table") 11 1 12
138 or return $self->error('No reference table');
139  
140 11 ▶ my $ref_table = $schema->get_table($ref_table_name)
Branch: 1 branch
BranchTrueFalseTotal
my $ref_table = $schema->get_table($ref_table_name) or return $self->error("No table named '${ref_table_name}' in schema") 8 3 11
141 or return $self->error("No table named '$ref_table_name' in schema");
142  
143 8 ▶ my @ref_fields = $self->reference_fields or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@ref_fields) = $self->reference_fields or return 7 1 8
144  
145 7 ▶ return $self->error('Only one field allowed for foreign key reference')
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ref_fields > 1 0 7 7
146 if scalar @ref_fields > 1;
147  
148 7 for my $ref_field (@ref_fields) {
149 7 ▶ next if $ref_table->get_field($ref_field);
Branch: 1 branch
BranchTrueFalseTotal
if $ref_table->get_field($ref_field) 6 1 7
150 return $self->error("Constraint from field(s) "
151 1 . join(', ', map {qq['$table_name.$_']} @fields)
152 . " to non-existent field '$ref_table_name.$ref_field'");
153 }
154 } elsif ($type eq CHECK_C) {
155 0 ▶ return $self->error('No expression for CHECK')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->expression 0 0 0
156 unless $self->expression;
157 }
158  
159 33 return 1;
160 }
161  
162 =head2 fields
163  
164 Gets and set the fields the constraint is on. Accepts a string, list or
165 arrayref; returns an array or array reference. Will unique the field
166 names and keep them in order by the first occurrence of a field name.
167  
168 The fields are returned as Field objects if they exist or as plain
169 names if not. (If you just want the names and want to avoid the Field's overload
170 magic use L</field_names>).
171  
172 Returns undef or an empty list if the constraint has no fields set.
173  
174 $constraint->fields('id');
175 $constraint->fields('id', 'name');
176 $constraint->fields( 'id, name' );
177 $constraint->fields( [ 'id', 'name' ] );
178 $constraint->fields( qw[ id name ] );
179  
180 my @fields = $constraint->fields;
181  
182 =cut
183  
184 2387 ▶sub fields {
Subroutine fields: called CC=5 SCAR=16.2
Pod documented
185 2387 my $self = shift;
186 2387 my $table = $self->table;
187 2387 ▶ my @fields = map { $table->get_field($_) || $_ } @{ $self->field_names(@_) || [] };
Condition80%4/5
$table->get_field($_) || $_
l!l&&r!l&&!r
2592300
$self->field_names(@_) || []
l!l
235730
MC/DC0%0/2
$table->get_field($_) || $_
$table->get_field($_) $_
MC/DC100%1/1
$self->field_names(@_) || []
$self->field_names(@_)
Truth table66%2/3
A || B
A= $table->get_field($_)B= $_
ABresult
1X1
011
000
Truth table100%2/2
A
A= $self->field_names(@_)
Aresult
11
00
188 return
189 wantarray ? @fields
190 2387 ▶ : @fields ? \@fields
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 2145 242 2387
@fields ? : 242 0 242
191 : undef;
192 }
193  
194 =head2 field_names
195  
196 Read-only method to return a list or array ref of the field names. Returns undef
197 or an empty list if the constraint has no fields set. Useful if you want to
198 avoid the overload magic of the Field objects returned by the fields method.
199  
200 my @names = $constraint->field_names;
201  
202 =cut
203  
204 76 with ListAttr field_names => (uniq => 1, undef_if_empty => 1);
205  
206 =head2 match_type
207  
208 Get or set the constraint's match_type. Only valid values are "full"
209 "partial" and "simple"
210  
211 my $match_type = $constraint->match_type('FULL');
212  
213 =cut
214  
215 76 has match_type => (
216 is => 'rw',
217 default => quote_sub(q{ '' }),
218 coerce => quote_sub(q{ lc $_[0] }),
219 isa => enum(
220 [qw(full partial simple)],
221 {
222 msg => "Invalid match type: %s",
223 allow_false => 1,
224 }
225 ),
226 );
227  
228 76 around match_type => \&ex2err;
229  
230 =head2 name
231  
232 Get or set the constraint's name.
233  
234 my $name = $constraint->name('foo');
235  
236 =cut
237  
238 76 has name => (is => 'rw', default => quote_sub(q{ '' }));
239  
240 1166 ▶around name => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
241 1166 my ($orig, $self, $arg) = @_;
242 1166 ▶ $self->$orig($arg || ());
Condition66%2/3
$arg || ()
l!l&&r!l&&!r
9010760
MC/DC0%0/2
$arg || ()
$arg ()
Truth table66%2/3
A || B
A= $argB= ()
ABresult
1X1
011
000
243 76 };
244  
245 =head2 options
246  
247 Gets or adds to the constraints's options (e.g., "INITIALLY IMMEDIATE").
248 Returns an array or array reference.
249  
250 $constraint->options('NORELY');
251 my @options = $constraint->options;
252  
253 =cut
254  
255 76 with ListAttr options => ();
256  
257 =head2 on_delete
258  
259 Get or set the constraint's "on delete" action.
260  
261 my $action = $constraint->on_delete('cascade');
262  
263 =cut
264  
265 76 has on_delete => (is => 'rw', default => quote_sub(q{ '' }));
266  
267 448 ▶around on_delete => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
268 448 my ($orig, $self, $arg) = @_;
269 448 ▶ $self->$orig($arg || ());
Condition66%2/3
$arg || ()
l!l&&r!l&&!r
14470
MC/DC0%0/2
$arg || ()
$arg ()
Truth table66%2/3
A || B
A= $argB= ()
ABresult
1X1
011
000
270 76 };
271  
272 =head2 on_update
273  
274 Get or set the constraint's "on update" action.
275  
276 my $action = $constraint->on_update('no action');
277  
278 =cut
279  
280 76 has on_update => (is => 'rw', default => quote_sub(q{ '' }));
281  
282 429 ▶around on_update => sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
283 429 my ($orig, $self, $arg) = @_;
284 429 ▶ $self->$orig($arg || ());
Condition33%1/3
$arg || ()
l!l&&r!l&&!r
04290
MC/DC0%0/2
$arg || ()
$arg ()
Truth table33%1/3
A || B
A= $argB= ()
ABresult
1X1
011
000
285 76 };
286  
287 =head2 reference_fields
288  
289 Gets and set the fields in the referred table. Accepts a string, list or
290 arrayref; returns an array or array reference.
291  
292 $constraint->reference_fields('id');
293 $constraint->reference_fields('id', 'name');
294 $constraint->reference_fields( 'id, name' );
295 $constraint->reference_fields( [ 'id', 'name' ] );
296 $constraint->reference_fields( qw[ id name ] );
297  
298 my @reference_fields = $constraint->reference_fields;
299  
300 =cut
301  
302 76 with ListAttr reference_fields => (
303 may_throw => 1,
304 builder => 1,
305 lazy => 1,
306 );
307  
308 247 ▶sub _build_reference_fields {
Subroutine _build_reference_fields: called CC=6 SCAR=17.9
309 247 my ($self) = @_;
310  
311 247 ▶ my $table = $self->table or throw('No table');
Branch: 1 branch
BranchTrueFalseTotal
my $table = $self->table or throw("No table") 247 0 247
312 247 ▶ my $schema = $table->schema or throw('No schema');
Branch: 1 branch
BranchTrueFalseTotal
my $schema = $table->schema or throw("No schema") 237 10 247
313 237 ▶ if (my $ref_table_name = $self->reference_table) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref_table_name = $self->reference_table) 21 216 237
314 21 ▶ my $ref_table = $schema->get_table($ref_table_name)
Branch: 1 branch
BranchTrueFalseTotal
my $ref_table = $schema->get_table($ref_table_name) or throw("Can't find table '${ref_table_name}'") 18 3 21
315 or throw("Can't find table '$ref_table_name'");
316  
317 18 ▶ if (my $constraint = $ref_table->primary_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $constraint = $ref_table->primary_key) { } 17 1 18
318 17 return [ $constraint->fields ];
319 } else {
320 1 throw('No reference fields defined and cannot find primary key in ', "reference table '$ref_table_name'");
321 }
322 }
323 }
324  
325 =head2 reference_table
326  
327 Get or set the table referred to by the constraint.
328  
329 my $reference_table = $constraint->reference_table('foo');
330  
331 =cut
332  
333 76 has reference_table => (is => 'rw', default => quote_sub(q{ '' }));
334  
335 =head2 table
336  
337 Get or set the constraint's table object.
338  
339 my $table = $field->table;
340  
341 =cut
342  
343 76 has table => (is => 'rw', isa => schema_obj('Table'), weak_ref => 1);
344  
345 76 around table => \&ex2err;
346  
347 =head2 type
348  
349 Get or set the constraint's type.
350  
351 my $type = $constraint->type( PRIMARY_KEY );
352  
353 =cut
354  
355 76 has type => (
356 is => 'rw',
357 default => quote_sub(q{ '' }),
358 coerce => quote_sub(q{ (my $t = $_[0]) =~ s/_/ /g; uc $t }),
359 isa => enum(
360 [ keys %VALID_CONSTRAINT_TYPE ],
361 {
362 msg => "Invalid constraint type: %s",
363 allow_false => 1,
364 }
365 ),
366 );
367  
368 76 around type => \&ex2err;
369  
370 =head2 equals
371  
372 Determines if this constraint is the same as another
373  
374 my $isIdentical = $constraint1->equals( $constraint2 );
375  
376 =cut
377  
378 382 ▶around equals => sub {
Subroutine __ANON__: called CC=29 SCAR=35.2
379 382 my $orig = shift;
380 382 my $self = shift;
381 382 my $other = shift;
382 382 my $case_insensitive = shift;
383 382 my $ignore_constraint_names = shift;
384  
385 382 ▶ return 0 unless $self->$orig($other);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$orig($other) 0 382 382
386 382 ▶ return 0 unless $self->type eq $other->type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type eq $other->type 157 225 382
387 225 ▶ unless ($ignore_constraint_names) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignore_constraint_names) 177 48 225
388 177 ▶ return 0
Branch: 2 branches
BranchTrueFalseTotal
unless $case_insensitive ? uc $self->name eq uc $other->name : $self->name eq $other->name 51 126 177
$case_insensitive ? : 0 177 177
389 unless $case_insensitive
390 ? uc($self->name) eq uc($other->name)
391 : $self->name eq $other->name;
392 }
393 174 ▶ return 0 unless $self->deferrable eq $other->deferrable;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->deferrable eq $other->deferrable 0 174 174
394  
395 #return 0 unless $self->is_valid eq $other->is_valid;
396 174 ▶ return 0
Branch: 2 branches
BranchTrueFalseTotal
unless $case_insensitive ? uc $self->table->name eq uc $other->table->name : $self->table->name eq $other->table->name 0 174 174
$case_insensitive ? : 0 174 174
397 unless $case_insensitive
398 ? uc($self->table->name) eq uc($other->table->name)
399 : $self->table->name eq $other->table->name;
400 174 ▶ return 0 unless $self->expression eq $other->expression;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->expression eq $other->expression 8 166 174
401  
402 # Check fields, regardless of order
403 166 my %otherFields = (); # create a hash of the other fields
404 166 foreach my $otherField ($other->fields) {
405 205 ▶ $otherField = uc($otherField) if $case_insensitive;
Branch: 1 branch
BranchTrueFalseTotal
if $case_insensitive 0 205 205
406 205 $otherFields{$otherField} = 1;
407 }
408 166 foreach my $selfField ($self->fields) { # check for self fields in hash
409 205 ▶ $selfField = uc($selfField) if $case_insensitive;
Branch: 1 branch
BranchTrueFalseTotal
if $case_insensitive 0 205 205
410 205 ▶ return 0 unless $otherFields{$selfField};
Branch: 1 branch
BranchTrueFalseTotal
unless $otherFields{$selfField} 22 183 205
411 183 delete $otherFields{$selfField};
412 }
413  
414 # Check all other fields were accounted for
415 144 ▶ return 0 unless keys %otherFields == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %otherFields == 0 12 132 144
416  
417 # Check reference fields, regardless of order
418 132 my %otherRefFields = (); # create a hash of the other reference fields
419 132 foreach my $otherRefField ($other->reference_fields) {
420 34 ▶ $otherRefField = uc($otherRefField) if $case_insensitive;
Branch: 1 branch
BranchTrueFalseTotal
if $case_insensitive 0 34 34
421 34 $otherRefFields{$otherRefField} = 1;
422 }
423 132 foreach my $selfRefField ($self->reference_fields) { # check for self reference fields in hash
424 34 ▶ $selfRefField = uc($selfRefField) if $case_insensitive;
Branch: 1 branch
BranchTrueFalseTotal
if $case_insensitive 0 34 34
425 34 ▶ return 0 unless $otherRefFields{$selfRefField};
Branch: 1 branch
BranchTrueFalseTotal
unless $otherRefFields{$selfRefField} 4 30 34
426 30 delete $otherRefFields{$selfRefField};
427 }
428  
429 # Check all other reference fields were accounted for
430 128 ▶ return 0 unless keys %otherRefFields == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %otherRefFields == 0 0 128 128
431  
432 128 ▶ return 0
Branch: 2 branches
BranchTrueFalseTotal
unless $case_insensitive ? uc $self->reference_table eq uc $other->reference_table : $self->reference_table eq $other->reference_table 2 126 128
$case_insensitive ? : 0 128 128
433 unless $case_insensitive
434 ? uc($self->reference_table) eq uc($other->reference_table)
435 : $self->reference_table eq $other->reference_table;
436 126 ▶ return 0 unless $self->match_type eq $other->match_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->match_type eq $other->match_type 0 126 126
437 126 ▶ return 0 unless $self->on_delete eq $other->on_delete;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_delete eq $other->on_delete 2 124 126
438 124 ▶ return 0 unless $self->on_update eq $other->on_update;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_update eq $other->on_update 0 124 124
439 124 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_compare_objects(scalar $self->options, scalar $other->options) 0 124 124
440 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
441 124 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_compare_objects(scalar $self->extra, scalar $other->extra) 0 124 124
442 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
443 124 return 1;
444 76 };
445  
446 # Must come after all 'has' declarations
447 76 around new => \&ex2err;
448  
449 76 1;
450  
451 =pod
452  
453 =head1 AUTHOR
454  
455 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
456  
457 =cut
« blib/lib/SQL/Translator/Producer/Dumper.pm ↑ Summary blib/lib/SQL/Translator/Parser/Access.pm »