Added "is_unique" method to determine if a field has a UNIQUE index.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Field.pm
index ae925f2..b40c92a 100644 (file)
@@ -1,7 +1,7 @@
 package SQL::Translator::Schema::Field;
 
 # ----------------------------------------------------------------------
-# $Id: Field.pm,v 1.6 2003-06-06 00:09:25 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>
 #
@@ -70,7 +70,7 @@ Object constructor.
     for my $arg ( 
         qw[ 
             table name data_type size is_primary_key is_nullable
-            is_auto_increment default_value
+            is_auto_increment default_value comments
         ] 
     ) {
         next unless defined $config->{ $arg };
@@ -81,6 +81,37 @@ Object constructor.
 }
 
 # ----------------------------------------------------------------------
+sub comments {
+
+=pod
+
+=head2 comments
+
+Get or set the comments on a field.  May be called several times to 
+set and it will accumulate the comments.  Called in an array context,
+returns each comment individually; called in a scalar context, returns
+all the comments joined on newlines.
+
+  $field->comments('foo');
+  $field->comments('bar');
+  print join( ', ', $field->comments ); # prints "foo, bar"
+
+=cut
+
+    my $self = shift;
+
+    for my $arg ( @_ ) {
+        $arg = $arg->[0] if ref $arg;
+        push @{ $self->{'comments'} }, $arg;
+    }
+
+    return wantarray 
+        ? @{ $self->{'comments'} || [] }
+        : join( "\n", @{ $self->{'comments'} || [] } );
+}
+
+
+# ----------------------------------------------------------------------
 sub data_type {
 
 =pod
@@ -245,7 +276,6 @@ Returns whether or not the field is a foreign key.
     return $self->{'is_foreign_key'} || 0;
 }
 
-
 # ----------------------------------------------------------------------
 sub is_nullable {
 
@@ -313,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
@@ -415,7 +477,7 @@ numbers and returns a string.
     }
 
     return wantarray 
-        ? @{ $self->{'size'} }
+        ? @{ $self->{'size'} || [0] }
         : join( ',', @{ $self->{'size'} || [0] } )
     ;
 }