Fixed copyrights.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Field.pm
index b7b2529..2e76f89 100644 (file)
@@ -1,9 +1,9 @@
 package SQL::Translator::Schema::Field;
 
 # ----------------------------------------------------------------------
-# $Id: Field.pm,v 1.8 2003-06-09 02:10:59 kycl4rk Exp $
+# $Id: Field.pm,v 1.13 2004-02-09 22:15:15 kycl4rk Exp $
 # ----------------------------------------------------------------------
-# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
+# Copyright (C) 2002-4 SQLFairy Authors
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License as
@@ -50,7 +50,7 @@ use SQL::Translator::Utils 'parse_list_arg';
 use base 'Class::Base';
 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
 
-$VERSION = 1.00;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/;
 
 # ----------------------------------------------------------------------
 sub init {
@@ -102,12 +102,17 @@ all the comments joined on newlines.
 
     for my $arg ( @_ ) {
         $arg = $arg->[0] if ref $arg;
-        push @{ $self->{'comments'} }, $arg;
+        push @{ $self->{'comments'} }, $arg if $arg;
     }
 
-    return wantarray 
-        ? @{ $self->{'comments'} || [] }
-        : join( "\n", @{ $self->{'comments'} || [] } );
+    if ( @{ $self->{'comments'} || [] } ) {
+        return wantarray 
+            ? @{ $self->{'comments'} || [] }
+            : join( "\n", @{ $self->{'comments'} || [] } );
+    }
+    else {
+        return wantarray ? () : '';
+    }
 }
 
 
@@ -276,7 +281,6 @@ Returns whether or not the field is a foreign key.
     return $self->{'is_foreign_key'} || 0;
 }
 
-
 # ----------------------------------------------------------------------
 sub is_nullable {
 
@@ -305,6 +309,14 @@ foreign keys; checks) are represented as table constraints.
         $self->{'is_nullable'} = $arg ? 1 : 0;
     }
 
+    if ( 
+        defined $self->{'is_nullable'} && 
+        $self->{'is_nullable'} == 1    &&
+        $self->is_primary_key
+    ) {
+        $self->{'is_nullable'} = 0;
+    }
+
     return defined $self->{'is_nullable'} ? $self->{'is_nullable'} : 1;
 }
 
@@ -344,6 +356,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
@@ -492,6 +536,6 @@ sub DESTROY {
 
 =head1 AUTHOR
 
-Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
+Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
 
 =cut