package SQL::Translator::Schema::Field;
-# ----------------------------------------------------------------------
-# $Id$
-# ----------------------------------------------------------------------
-# Copyright (C) 2002-2009 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
-# published by the Free Software Foundation; version 2.
-#
-# This program is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-# 02111-1307 USA
-# -------------------------------------------------------------------
-
=pod
=head1 NAME
=cut
use strict;
+use warnings;
use SQL::Translator::Schema::Constants;
use SQL::Translator::Utils 'parse_list_arg';
use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
-$VERSION = '1.99';
+$VERSION = '1.59';
# Stringify to our name, being careful not to pass any args through so we don't
# accidentally set it to undef. We also have to tweak bool so the object is
text => SQL_LONGVARCHAR
);
-# ----------------------------------------------------------------------
__PACKAGE__->_attributes( qw/
table name data_type size is_primary_key is_nullable
=cut
-# ----------------------------------------------------------------------
sub comments {
=pod
=head2 comments
-Get or set the comments on a field. May be called several times to
+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.
}
if ( @{ $self->{'comments'} || [] } ) {
- return wantarray
+ return wantarray
? @{ $self->{'comments'} || [] }
: join( "\n", @{ $self->{'comments'} || [] } );
}
}
-# ----------------------------------------------------------------------
sub data_type {
=pod
}
-# ----------------------------------------------------------------------
sub default_value {
=pod
=head2 default_value
Get or set the field's default value. Will return undef if not defined
-and could return the empty string (it's a valid default value), so don't
+and could return the empty string (it's a valid default value), so don't
assume an error like other methods.
my $default = $field->default_value('foo');
=cut
- my ( $self, $arg ) = @_;
- $self->{'default_value'} = $arg if defined $arg;
+ my $self = shift;
+ $self->{'default_value'} = shift if @_;
return $self->{'default_value'};
}
-# ----------------------------------------------------------------------
=pod
=head2 extra
=cut
-
-# ----------------------------------------------------------------------
sub foreign_key_reference {
=pod
return $self->{'foreign_key_reference'};
}
-# ----------------------------------------------------------------------
sub is_auto_increment {
=pod
unless ( defined $self->{'is_auto_increment'} ) {
if ( my $table = $self->table ) {
if ( my $schema = $table->schema ) {
- if (
+ if (
$schema->database eq 'PostgreSQL' &&
$self->data_type eq 'serial'
) {
return $self->{'is_auto_increment'} || 0;
}
-# ----------------------------------------------------------------------
sub is_foreign_key {
=pod
return $self->{'is_foreign_key'} || 0;
}
-# ----------------------------------------------------------------------
sub is_nullable {
=pod
=head2 is_nullable
-Get or set whether the field can be null. If not defined, then
+Get or set whether the field can be null. If not defined, then
returns "1" (assumes the field can be null). The argument is evaluated
by Perl for True or False, so the following are eqivalent:
$self->{'is_nullable'} = $arg ? 1 : 0;
}
- if (
- defined $self->{'is_nullable'} &&
+ if (
+ defined $self->{'is_nullable'} &&
$self->{'is_nullable'} == 1 &&
$self->is_primary_key
) {
return defined $self->{'is_nullable'} ? $self->{'is_nullable'} : 1;
}
-# ----------------------------------------------------------------------
sub is_primary_key {
=pod
return $self->{'is_primary_key'} || 0;
}
-# ----------------------------------------------------------------------
sub is_unique {
=pod
=cut
my $self = shift;
-
+
unless ( defined $self->{'is_unique'} ) {
if ( my $table = $self->table ) {
for my $c ( $table->get_constraints ) {
return $self->{'is_unique'} || 0;
}
-# ----------------------------------------------------------------------
sub is_valid {
=pod
return 1;
}
-# ----------------------------------------------------------------------
sub name {
=pod
return $self->table.".".$self->name;
}
-# ----------------------------------------------------------------------
sub order {
=pod
return $self->{'order'} || 0;
}
-# ----------------------------------------------------------------------
sub schema {
-=head2 schema
+=head2 schema
Shortcut to get the fields schema ($field->table->schema) or undef if it
doesn't have one.
return undef;
}
-# ----------------------------------------------------------------------
sub size {
=pod
$self->{'size'} = \@new if @new; # only set if all OK
}
- return wantarray
+ return wantarray
? @{ $self->{'size'} || [0] }
: join( ',', @{ $self->{'size'} || [0] } )
;
}
-# ----------------------------------------------------------------------
sub table {
=pod
sub parsed_field {
-=head2
+=head2
Returns the field exactly as the parser found it
return $self->{parsed_field} || $self;
}
-# ----------------------------------------------------------------------
sub equals {
=pod
my $self = shift;
my $other = shift;
my $case_insensitive = shift;
-
+
return 0 unless $self->SUPER::equals($other);
return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
return 1;
}
-# ----------------------------------------------------------------------
sub DESTROY {
#
# Destroy cyclical references.
1;
-# ----------------------------------------------------------------------
-
=pod
=head1 AUTHOR
-Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
+Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
=cut