Added "is_unique" method to determine if a field has a UNIQUE index.
Ken Youens-Clark [Mon, 9 Jun 2003 04:11:57 +0000 (04:11 +0000)]
lib/SQL/Translator/Schema/Field.pm

index b7b2529..b40c92a 100644 (file)
@@ -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 <kclark@cpan.org>
 #
@@ -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