1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
package SQL::Translator::Schema::Constraint;
=pod
=head1 NAME
SQL::Translator::Schema::Constraint - SQL::Translator constraint object
=head1 SYNOPSIS
use SQL::Translator::Schema::Constraint;
my $constraint = SQL::Translator::Schema::Constraint->new(
name => 'foo',
fields => [ id ],
type => PRIMARY_KEY,
);
=head1 DESCRIPTION
C<SQL::Translator::Schema::Constraint> is the constraint object.
=head1 METHODS
=cut
use strict;
use warnings;
use SQL::Translator::Schema::Constants;
use SQL::Translator::Utils 'parse_list_arg';
use base 'SQL::Translator::Schema::Object';
our ( $TABLE_COUNT, $VIEW_COUNT );
our $VERSION = '1.59';
my %VALID_CONSTRAINT_TYPE = (
PRIMARY_KEY, 1,
UNIQUE, 1,
CHECK_C, 1,
FOREIGN_KEY, 1,
NOT_NULL, 1,
);
__PACKAGE__->_attributes( qw/
table name type fields reference_fields reference_table
match_type on_delete on_update expression deferrable
/);
# Override to remove empty arrays from args.
# t/14postgres-parser breaks without this.
sub init {
=pod
=head2 new
Object constructor.
my $schema = SQL::Translator::Schema::Constraint->new(
table => $table, # table to which it belongs
type => 'foreign_key', # type of table constraint
name => 'fk_phone_id', # name of the constraint
fields => 'phone_id', # field in the referring table
reference_fields => 'phone_id', # referenced field
reference_table => 'phone', # referenced table
match_type => 'full', # how to match
on_delete => 'cascade', # what to do on deletes
on_update => '', # what to do on updates
);
=cut
my $self = shift;
foreach ( values %{$_[0]} ) { $_ = undef if ref($_) eq "ARRAY" && ! @$_; }
$self->SUPER::init(@_);
}
sub deferrable {
=pod
=head2 deferrable
Get or set whether the constraint is deferrable. If not defined,
then returns "1." The argument is evaluated by Perl for True or
False, so the following are eqivalent:
$deferrable = $field->deferrable(0);
$deferrable = $field->deferrable('');
$deferrable = $field->deferrable('0');
=cut
my ( $self, $arg ) = @_;
if ( defined $arg ) {
$self->{'deferrable'} = $arg ? 1 : 0;
}
return defined $self->{'deferrable'} ? $self->{'deferrable'} : 1;
}
sub expression {
=pod
=head2 expression
Gets and set the expression used in a CHECK constraint.
my $expression = $constraint->expression('...');
=cut
my $self = shift;
if ( my $arg = shift ) {
# check arg here?
$self->{'expression'} = $arg;
}
return $self->{'expression'} || '';
}
sub is_valid {
=pod
=head2 is_valid
Determine whether the constraint is valid or not.
my $ok = $constraint->is_valid;
=cut
my $self = shift;
my $type = $self->type or return $self->error('No type');
my $table = $self->table or return $self->error('No table');
my @fields = $self->fields or return $self->error('No fields');
my $table_name = $table->name or return $self->error('No table name');
for my $f ( @fields ) {
next if $table->get_field( $f );
return $self->error(
"Constraint references non-existent field '$f' ",
"in table '$table_name'"
);
}
my $schema = $table->schema or return $self->error(
'Table ', $table->name, ' has no schema object'
);
if ( $type eq FOREIGN_KEY ) {
return $self->error('Only one field allowed for foreign key')
if scalar @fields > 1;
my $ref_table_name = $self->reference_table or
return $self->error('No reference table');
my $ref_table = $schema->get_table( $ref_table_name ) or
return $self->error("No table named '$ref_table_name' in schema");
my @ref_fields = $self->reference_fields or return;
return $self->error('Only one field allowed for foreign key reference')
if scalar @ref_fields > 1;
for my $ref_field ( @ref_fields ) {
next if $ref_table->get_field( $ref_field );
return $self->error(
"Constraint from field(s) ",
join(', ', map {qq['$table_name.$_']} @fields),
" to non-existent field '$ref_table_name.$ref_field'"
);
}
}
elsif ( $type eq CHECK_C ) {
return $self->error('No expression for CHECK') unless
$self->expression;
}
return 1;
}
sub fields {
=pod
=head2 fields
Gets and set the fields the constraint is on. Accepts a string, list or
arrayref; returns an array or array reference. Will unique the field
names and keep them in order by the first occurrence of a field name.
The fields are returned as Field objects if they exist or as plain
names if not. (If you just want the names and want to avoid the Field's overload
magic use L<field_names>).
Returns undef or an empty list if the constraint has no fields set.
$constraint->fields('id');
$constraint->fields('id', 'name');
$constraint->fields( 'id, name' );
$constraint->fields( [ 'id', 'name' ] );
$constraint->fields( qw[ id name ] );
my @fields = $constraint->fields;
=cut
my $self = shift;
my $fields = parse_list_arg( @_ );
if ( @$fields ) {
my ( %unique, @unique );
for my $f ( @$fields ) {
next if $unique{ $f };
$unique{ $f } = 1;
push @unique, $f;
}
$self->{'fields'} = \@unique;
}
if ( @{ $self->{'fields'} || [] } ) {
# We have to return fields that don't exist on the table as names in
# case those fields havn't been created yet.
my @ret = map {
$self->table->get_field($_) || $_ } @{ $self->{'fields'} };
return wantarray ? @ret : \@ret;
}
else {
return wantarray ? () : undef;
}
}
sub field_names {
=head2 field_names
Read-only method to return a list or array ref of the field names. Returns undef
or an empty list if the constraint has no fields set. Useful if you want to
avoid the overload magic of the Field objects returned by the fields method.
my @names = $constraint->field_names;
=cut
my $self = shift;
return wantarray ? @{ $self->{'fields'} || [] } : ($self->{'fields'} || '');
}
sub match_type {
=pod
=head2 match_type
Get or set the constraint's match_type. Only valid values are "full"
"partial" and "simple"
my $match_type = $constraint->match_type('FULL');
=cut
my ( $self, $arg ) = @_;
if ( $arg ) {
$arg = lc $arg;
return $self->error("Invalid match type: $arg")
unless $arg eq 'full' || $arg eq 'partial' || $arg eq 'simple';
$self->{'match_type'} = $arg;
}
return $self->{'match_type'} || '';
}
sub name {
=pod
=head2 name
Get or set the constraint's name.
my $name = $constraint->name('foo');
=cut
my $self = shift;
my $arg = shift || '';
$self->{'name'} = $arg if $arg;
return $self->{'name'} || '';
}
sub options {
=pod
=head2 options
Gets or adds to the constraints's options (e.g., "INITIALLY IMMEDIATE").
Returns an array or array reference.
$constraint->options('NORELY');
my @options = $constraint->options;
=cut
my $self = shift;
my $options = parse_list_arg( @_ );
push @{ $self->{'options'} }, @$options;
if ( ref $self->{'options'} ) {
return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
}
else {
return wantarray ? () : [];
}
}
sub on_delete {
=pod
=head2 on_delete
Get or set the constraint's "on delete" action.
my $action = $constraint->on_delete('cascade');
=cut
my $self = shift;
if ( my $arg = shift ) {
# validate $arg?
$self->{'on_delete'} = $arg;
}
return $self->{'on_delete'} || '';
}
sub on_update {
=pod
=head2 on_update
Get or set the constraint's "on update" action.
my $action = $constraint->on_update('no action');
=cut
my $self = shift;
if ( my $arg = shift ) {
# validate $arg?
$self->{'on_update'} = $arg;
}
return $self->{'on_update'} || '';
}
sub reference_fields {
=pod
=head2 reference_fields
Gets and set the fields in the referred table. Accepts a string, list or
arrayref; returns an array or array reference.
$constraint->reference_fields('id');
$constraint->reference_fields('id', 'name');
$constraint->reference_fields( 'id, name' );
$constraint->reference_fields( [ 'id', 'name' ] );
$constraint->reference_fields( qw[ id name ] );
my @reference_fields = $constraint->reference_fields;
=cut
my $self = shift;
my $fields = parse_list_arg( @_ );
if ( @$fields ) {
$self->{'reference_fields'} = $fields;
}
# Nothing set so try and derive it from the other constraint data
unless ( ref $self->{'reference_fields'} ) {
my $table = $self->table or return $self->error('No table');
my $schema = $table->schema or return $self->error('No schema');
if ( my $ref_table_name = $self->reference_table ) {
my $ref_table = $schema->get_table( $ref_table_name ) or
return $self->error("Can't find table '$ref_table_name'");
if ( my $constraint = $ref_table->primary_key ) {
$self->{'reference_fields'} = [ $constraint->fields ];
}
else {
$self->error(
'No reference fields defined and cannot find primary key in ',
"reference table '$ref_table_name'"
);
}
}
# No ref table so we are not that sort of constraint, hence no ref
# fields. So we let the return below return an empty list.
}
if ( ref $self->{'reference_fields'} ) {
return wantarray
? @{ $self->{'reference_fields'} }
: $self->{'reference_fields'};
}
else {
return wantarray ? () : [];
}
}
sub reference_table {
=pod
=head2 reference_table
Get or set the table referred to by the constraint.
my $reference_table = $constraint->reference_table('foo');
=cut
my $self = shift;
$self->{'reference_table'} = shift if @_;
return $self->{'reference_table'} || '';
}
sub table {
=pod
=head2 table
Get or set the constraint's table object.
my $table = $field->table;
=cut
my $self = shift;
if ( my $arg = shift ) {
return $self->error('Not a table object') unless
UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
$self->{'table'} = $arg;
}
return $self->{'table'};
}
sub type {
=pod
=head2 type
Get or set the constraint's type.
my $type = $constraint->type( PRIMARY_KEY );
=cut
my ( $self, $type ) = @_;
if ( $type ) {
$type = uc $type;
$type =~ s/_/ /g;
return $self->error("Invalid constraint type: $type")
unless $VALID_CONSTRAINT_TYPE{ $type };
$self->{'type'} = $type;
}
return $self->{'type'} || '';
}
sub equals {
=pod
=head2 equals
Determines if this constraint is the same as another
my $isIdentical = $constraint1->equals( $constraint2 );
=cut
my $self = shift;
my $other = shift;
my $case_insensitive = shift;
my $ignore_constraint_names = shift;
return 0 unless $self->SUPER::equals($other);
return 0 unless $self->type eq $other->type;
unless ($ignore_constraint_names) {
return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
}
return 0 unless $self->deferrable eq $other->deferrable;
#return 0 unless $self->is_valid eq $other->is_valid;
return 0 unless $case_insensitive ? uc($self->table->name) eq uc($other->table->name)
: $self->table->name eq $other->table->name;
return 0 unless $self->expression eq $other->expression;
# Check fields, regardless of order
my %otherFields = (); # create a hash of the other fields
foreach my $otherField ($other->fields) {
$otherField = uc($otherField) if $case_insensitive;
$otherFields{$otherField} = 1;
}
foreach my $selfField ($self->fields) { # check for self fields in hash
$selfField = uc($selfField) if $case_insensitive;
return 0 unless $otherFields{$selfField};
delete $otherFields{$selfField};
}
# Check all other fields were accounted for
return 0 unless keys %otherFields == 0;
# Check reference fields, regardless of order
my %otherRefFields = (); # create a hash of the other reference fields
foreach my $otherRefField ($other->reference_fields) {
$otherRefField = uc($otherRefField) if $case_insensitive;
$otherRefFields{$otherRefField} = 1;
}
foreach my $selfRefField ($self->reference_fields) { # check for self reference fields in hash
$selfRefField = uc($selfRefField) if $case_insensitive;
return 0 unless $otherRefFields{$selfRefField};
delete $otherRefFields{$selfRefField};
}
# Check all other reference fields were accounted for
return 0 unless keys %otherRefFields == 0;
return 0 unless $case_insensitive ? uc($self->reference_table) eq uc($other->reference_table) : $self->reference_table eq $other->reference_table;
return 0 unless $self->match_type eq $other->match_type;
return 0 unless $self->on_delete eq $other->on_delete;
return 0 unless $self->on_update eq $other->on_update;
return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
return 1;
}
sub DESTROY {
my $self = shift;
undef $self->{'table'}; # destroy cyclical reference
}
1;
=pod
=head1 AUTHOR
Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
=cut
|