From: Mark Addison Date: Sun, 29 Feb 2004 16:05:31 +0000 (+0000) Subject: reference_fields now returns an empty list (or array ref) for constraints that X-Git-Tag: v0.06~170 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2d034ab486b874d2d22225626a6d1aaeefcc1626;p=dbsrgits%2FSQL-Translator.git reference_fields now returns an empty list (or array ref) for constraints that don't have reference fields e.g. UNIQUE. Was returning an undef before. Doc tweaks. --- diff --git a/lib/SQL/Translator/Schema/Constraint.pm b/lib/SQL/Translator/Schema/Constraint.pm index 9124035..e8ae2ab 100644 --- a/lib/SQL/Translator/Schema/Constraint.pm +++ b/lib/SQL/Translator/Schema/Constraint.pm @@ -1,7 +1,7 @@ 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 # @@ -51,7 +51,7 @@ use SQL::Translator::Utils 'parse_list_arg'; 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, @@ -78,8 +78,8 @@ Object constructor. 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 @@ -106,7 +106,7 @@ sub deferrable { =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: @@ -400,28 +400,31 @@ arrayref; returns an array or array reference. $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 { @@ -454,7 +457,7 @@ sub table { =head2 table -Get or set the field's table object. +Get or set the constraint's table object. my $table = $field->table;