package SQL::Translator::Schema::Table;
# ----------------------------------------------------------------------
-# $Id: Table.pm,v 1.6 2003-06-06 00:10:32 kycl4rk Exp $
+# $Id: Table.pm,v 1.14 2003-08-21 20:27:04 kycl4rk Exp $
# ----------------------------------------------------------------------
# Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
#
use base 'Class::Base';
use vars qw( $VERSION $FIELD_ORDER );
-$VERSION = 1.00;
+$VERSION = sprintf "%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/;
# ----------------------------------------------------------------------
sub init {
my ( $self, $config ) = @_;
- for my $arg ( qw[ schema name ] ) {
+ for my $arg ( qw[ schema name comments ] ) {
next unless defined $config->{ $arg };
- $self->$arg( $config->{ $arg } ) or return;
+ defined $self->$arg( $config->{ $arg } ) or return;
}
return $self;
# If we're trying to add a PK when one is already defined,
# then just add the fields to the existing definition.
#
- my $ok = 0;
+ my $ok = 1;
my $pk = $self->primary_key;
if ( $pk && $constraint->type eq PRIMARY_KEY ) {
$self->primary_key( $constraint->fields );
$constraint = $pk;
+ $ok = 0;
}
- else {
+ elsif ( $constraint->type eq PRIMARY_KEY ) {
+ for my $fname ( $constraint->fields ) {
+ if ( my $f = $self->get_field( $fname ) ) {
+ $f->is_primary_key( 1 );
+ }
+ }
+ }
+ #
+ # See if another constraint of the same type
+ # covers the same fields.
+ #
+ elsif ( $constraint->type ne CHECK_C ) {
my @field_names = $constraint->fields;
- $ok = 1;
-
for my $c (
grep { $_->type eq $constraint->type }
$self->get_constraints
}
# ----------------------------------------------------------------------
+sub comments {
+
+=pod
+
+=head2 comments
+
+Get or set the comments on a table. 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.
+
+ $table->comments('foo');
+ $table->comments('bar');
+ print join( ', ', $table->comments ); # prints "foo, bar"
+
+=cut
+
+ my $self = shift;
+ my @comments = ref $_[0] ? @{ $_[0] } : @_;
+
+ for my $arg ( @comments ) {
+ $arg = $arg->[0] if ref $arg;
+ push @{ $self->{'comments'} }, $arg if defined $arg && $arg;
+ }
+
+ if ( @{ $self->{'comments'} || [] } ) {
+ return wantarray
+ ? @{ $self->{'comments'} }
+ : join( "\n", @{ $self->{'comments'} } )
+ ;
+ }
+ else {
+ return wantarray ? () : undef;
+ }
+}
+
+# ----------------------------------------------------------------------
sub get_constraints {
=pod
$constraint = $self->add_constraint(
type => PRIMARY_KEY,
fields => $fields,
- );
+ ) or return;
}
}