X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FSchema%2FField.pm;h=c2b4fa6ef1ffd0ef959534b8db4985bda2d4d07f;hb=d6d2873a78cf19f60f3cd7b5c5eb964b9104ec84;hp=ae925f269ef96b1d178b7b5fdbef9bf2cd1460bd;hpb=9966eebc4550a535bac2f8dd4ae9d22ff8be1840;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Schema/Field.pm b/lib/SQL/Translator/Schema/Field.pm index ae925f2..c2b4fa6 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.6 2003-06-06 00:09:25 kycl4rk Exp $ +# $Id: Field.pm,v 1.12 2003-08-12 22:03:59 kycl4rk Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2003 Ken Y. Clark # @@ -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.12 $ =~ /(\d+)\.(\d+)/; # ---------------------------------------------------------------------- sub init { @@ -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,42 @@ 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 if $arg; + } + + if ( @{ $self->{'comments'} || [] } ) { + return wantarray + ? @{ $self->{'comments'} || [] } + : join( "\n", @{ $self->{'comments'} || [] } ); + } + else { + return wantarray ? () : ''; + } +} + + +# ---------------------------------------------------------------------- sub data_type { =pod @@ -245,7 +281,6 @@ Returns whether or not the field is a foreign key. return $self->{'is_foreign_key'} || 0; } - # ---------------------------------------------------------------------- sub is_nullable { @@ -274,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; } @@ -313,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 @@ -415,7 +490,7 @@ numbers and returns a string. } return wantarray - ? @{ $self->{'size'} } + ? @{ $self->{'size'} || [0] } : join( ',', @{ $self->{'size'} || [0] } ) ; }