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

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