From: Ken Youens-Clark Date: Mon, 9 Jun 2003 04:11:57 +0000 (+0000) Subject: Added "is_unique" method to determine if a field has a UNIQUE index. X-Git-Tag: v0.02~47 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ee2766f418cac89023fafdc455404441618df8e9;hp=226d3a81f39ee43d30b651721734e7b090e7024d;p=dbsrgits%2FSQL-Translator.git Added "is_unique" method to determine if a field has a UNIQUE index. --- diff --git a/lib/SQL/Translator/Schema/Field.pm b/lib/SQL/Translator/Schema/Field.pm index b7b2529..b40c92a 100644 --- a/lib/SQL/Translator/Schema/Field.pm +++ b/lib/SQL/Translator/Schema/Field.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::Field; # ---------------------------------------------------------------------- -# $Id: Field.pm,v 1.8 2003-06-09 02:10:59 kycl4rk Exp $ +# $Id: Field.pm,v 1.9 2003-06-09 04:11:57 kycl4rk Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark # @@ -276,7 +276,6 @@ Returns whether or not the field is a foreign key. return $self->{'is_foreign_key'} || 0; } - # ---------------------------------------------------------------------- sub is_nullable { @@ -344,6 +343,38 @@ a table constraint (should it?). } # ---------------------------------------------------------------------- +sub is_unique { + +=pod + +=head2 is_unique + +Determine whether the field has a UNIQUE constraint or not. + + my $is_unique = $field->is_unique; + +=cut + + my $self = shift; + + unless ( defined $self->{'is_unique'} ) { + if ( my $table = $self->table ) { + for my $c ( $table->get_constraints ) { + if ( $c->type eq UNIQUE ) { + my %fields = map { $_, 1 } $c->fields; + if ( $fields{ $self->name } ) { + $self->{'is_unique'} = 1; + last; + } + } + } + } + } + + return $self->{'is_unique'} || 0; +} + +# ---------------------------------------------------------------------- sub is_valid { =pod