package SQL::Translator::Schema::Constraint;
# ----------------------------------------------------------------------
-# $Id: Constraint.pm,v 1.10 2004-02-09 22:15:15 kycl4rk Exp $
+# $Id: Constraint.pm,v 1.11 2004-02-29 16:05:31 grommit Exp $
# ----------------------------------------------------------------------
# Copyright (C) 2002-4 SQLFairy Authors
#
use base 'Class::Base';
use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
-$VERSION = sprintf "%d.%02d", q$Revision: 1.10 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/;
my %VALID_CONSTRAINT_TYPE = (
PRIMARY_KEY, 1,
reference_fields => 'phone_id', # referenced field
reference_table => 'phone', # referenced table
match_type => 'full', # how to match
- on_delete_do => 'cascade', # what to do on deletes
- on_update_do => '', # what to do on updates
+ on_delete => 'cascade', # what to do on deletes
+ on_update => '', # what to do on updates
);
=cut
=head2 deferrable
-Get or set the whether the constraint is deferrable. If not defined,
+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:
$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');
- my $ref_table_name = $self->reference_table or
- return $self->error('No 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'"
- );
+ 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'} }
: $self->{'reference_fields'};
}
else {
=head2 table
-Get or set the field's table object.
+Get or set the constraint's table object.
my $table = $field->table;