1 package SQL::Translator::Schema::Table;
3 # ----------------------------------------------------------------------
4 # $Id: Table.pm,v 1.14 2003-08-21 20:27:04 kycl4rk Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
27 SQL::Translator::Schema::Table - SQL::Translator table object
31 use SQL::Translator::Schema::Table;
32 my $table = SQL::Translator::Schema::Table->new( name => 'foo' );
36 C<SQL::Translator::Schema::Table> is the table object.
44 use SQL::Translator::Utils 'parse_list_arg';
45 use SQL::Translator::Schema::Constants;
46 use SQL::Translator::Schema::Constraint;
47 use SQL::Translator::Schema::Field;
48 use SQL::Translator::Schema::Index;
50 use base 'Class::Base';
51 use vars qw( $VERSION $FIELD_ORDER );
53 $VERSION = sprintf "%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/;
55 # ----------------------------------------------------------------------
64 my $table = SQL::Translator::Schema::Table->new(
71 my ( $self, $config ) = @_;
73 for my $arg ( qw[ schema name comments ] ) {
74 next unless defined $config->{ $arg };
75 defined $self->$arg( $config->{ $arg } ) or return;
81 # ----------------------------------------------------------------------
88 Add a constraint to the table. Returns the newly created
89 C<SQL::Translator::Schema::Constraint> object.
91 my $c1 = $table->add_constraint(
94 fields => [ 'foo_id' ],
97 my $c2 = SQL::Translator::Schema::Constraint->new( name => 'uniq' );
98 $c2 = $table->add_constraint( $constraint );
103 my $constraint_class = 'SQL::Translator::Schema::Constraint';
106 if ( UNIVERSAL::isa( $_[0], $constraint_class ) ) {
108 $constraint->table( $self );
112 $args{'table'} = $self;
113 $constraint = $constraint_class->new( \%args ) or
114 return $self->error( $constraint_class->error );
118 # If we're trying to add a PK when one is already defined,
119 # then just add the fields to the existing definition.
122 my $pk = $self->primary_key;
123 if ( $pk && $constraint->type eq PRIMARY_KEY ) {
124 $self->primary_key( $constraint->fields );
128 elsif ( $constraint->type eq PRIMARY_KEY ) {
129 for my $fname ( $constraint->fields ) {
130 if ( my $f = $self->get_field( $fname ) ) {
131 $f->is_primary_key( 1 );
136 # See if another constraint of the same type
137 # covers the same fields.
139 elsif ( $constraint->type ne CHECK_C ) {
140 my @field_names = $constraint->fields;
142 grep { $_->type eq $constraint->type }
143 $self->get_constraints
145 my %fields = map { $_, 1 } $c->fields;
146 for my $field_name ( @field_names ) {
147 if ( $fields{ $field_name } ) {
158 push @{ $self->{'constraints'} }, $constraint;
164 # ----------------------------------------------------------------------
171 Add an index to the table. Returns the newly created
172 C<SQL::Translator::Schema::Index> object.
174 my $i1 = $table->add_index(
176 fields => [ 'name' ],
180 my $i2 = SQL::Translator::Schema::Index->new( name => 'id' );
181 $i2 = $table->add_index( $index );
186 my $index_class = 'SQL::Translator::Schema::Index';
189 if ( UNIVERSAL::isa( $_[0], $index_class ) ) {
191 $index->table( $self );
195 $args{'table'} = $self;
196 $index = $index_class->new( \%args ) or return
197 $self->error( $index_class->error );
200 push @{ $self->{'indices'} }, $index;
204 # ----------------------------------------------------------------------
211 Add an field to the table. Returns the newly created
212 C<SQL::Translator::Schema::Field> object. The "name" parameter is
213 required. If you try to create a field with the same name as an
214 existing field, you will get an error and the field will not be created.
216 my $f1 = $table->add_field(
218 data_type => 'integer',
222 my $f2 = SQL::Translator::Schema::Field->new(
226 $f2 = $table->add_field( $field2 ) or die $table->error;
231 my $field_class = 'SQL::Translator::Schema::Field';
234 if ( UNIVERSAL::isa( $_[0], $field_class ) ) {
236 $field->table( $self );
240 $args{'table'} = $self;
241 $field = $field_class->new( \%args ) or return
242 $self->error( $field_class->error );
245 $field->order( ++$FIELD_ORDER );
246 my $field_name = $field->name or return $self->error('No name');
248 if ( exists $self->{'fields'}{ $field_name } ) {
249 return $self->error(qq[Can't create field: "$field_name" exists]);
252 $self->{'fields'}{ $field_name } = $field;
258 # ----------------------------------------------------------------------
265 Get or set the comments on a table. May be called several times to
266 set and it will accumulate the comments. Called in an array context,
267 returns each comment individually; called in a scalar context, returns
268 all the comments joined on newlines.
270 $table->comments('foo');
271 $table->comments('bar');
272 print join( ', ', $table->comments ); # prints "foo, bar"
277 my @comments = ref $_[0] ? @{ $_[0] } : @_;
279 for my $arg ( @comments ) {
280 $arg = $arg->[0] if ref $arg;
281 push @{ $self->{'comments'} }, $arg if defined $arg && $arg;
284 if ( @{ $self->{'comments'} || [] } ) {
286 ? @{ $self->{'comments'} }
287 : join( "\n", @{ $self->{'comments'} } )
291 return wantarray ? () : undef;
295 # ----------------------------------------------------------------------
296 sub get_constraints {
300 =head2 get_constraints
302 Returns all the constraint objects as an array or array reference.
304 my @constraints = $table->get_constraints;
310 if ( ref $self->{'constraints'} ) {
312 ? @{ $self->{'constraints'} } : $self->{'constraints'};
315 $self->error('No constraints');
316 return wantarray ? () : undef;
320 # ----------------------------------------------------------------------
327 Returns all the index objects as an array or array reference.
329 my @indices = $table->get_indices;
335 if ( ref $self->{'indices'} ) {
337 ? @{ $self->{'indices'} }
338 : $self->{'indices'};
341 $self->error('No indices');
342 return wantarray ? () : undef;
346 # ----------------------------------------------------------------------
353 Returns a field by the name provided.
355 my $field = $table->get_field('foo');
360 my $field_name = shift or return $self->error('No field name');
361 return $self->error( qq[Field "$field_name" does not exist] ) unless
362 exists $self->{'fields'}{ $field_name };
363 return $self->{'fields'}{ $field_name };
366 # ----------------------------------------------------------------------
373 Returns all the field objects as an array or array reference.
375 my @fields = $table->get_fields;
382 sort { $a->[0] <=> $b->[0] }
383 map { [ $_->order, $_ ] }
384 values %{ $self->{'fields'} || {} };
387 return wantarray ? @fields : \@fields;
390 $self->error('No fields');
391 return wantarray ? () : undef;
395 # ----------------------------------------------------------------------
402 Determine whether the view is valid or not.
404 my $ok = $view->is_valid;
409 return $self->error('No name') unless $self->name;
410 return $self->error('No fields') unless $self->get_fields;
413 $self->get_fields, $self->get_indices, $self->get_constraints
415 return $object->error unless $object->is_valid;
421 # ----------------------------------------------------------------------
428 Get or set the table's name.
430 If provided an argument, checks the schema object for a table of
431 that name and disallows the change if one exists.
433 my $table_name = $table->name('foo');
439 if ( my $arg = shift ) {
440 if ( my $schema = $self->schema ) {
441 return $self->error( qq[Can't use table name "$arg": table exists] )
442 if $schema->get_table( $arg );
444 $self->{'name'} = $arg;
447 return $self->{'name'} || '';
450 # ----------------------------------------------------------------------
457 Get or set the table's schema object.
459 my $schema = $table->schema;
464 if ( my $arg = shift ) {
465 return $self->error('Not a schema object') unless
466 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
467 $self->{'schema'} = $arg;
470 return $self->{'schema'};
473 # ----------------------------------------------------------------------
480 Gets or sets the table's primary key(s). Takes one or more field
481 names (as a string, list or array[ref]) as an argument. If the field
482 names are present, it will create a new PK if none exists, or it will
483 add to the fields of an existing PK (and will unique the field names).
484 Returns the C<SQL::Translator::Schema::Constraint> object representing
489 $table->primary_key('id');
490 $table->primary_key(['name']);
491 $table->primary_key('id','name']);
492 $table->primary_key(['id','name']);
493 $table->primary_key('id,name');
494 $table->primary_key(qw[ id name ]);
496 my $pk = $table->primary_key;
501 my $fields = parse_list_arg( @_ );
505 for my $f ( @$fields ) {
506 return $self->error(qq[Invalid field "$f"]) unless
507 $self->get_field($f);
511 for my $c ( $self->get_constraints ) {
512 if ( $c->type eq PRIMARY_KEY ) {
514 $c->fields( @{ $c->fields }, @$fields );
520 $constraint = $self->add_constraint(
531 for my $c ( $self->get_constraints ) {
532 return $c if $c->type eq PRIMARY_KEY;
539 # ----------------------------------------------------------------------
546 Get or set the table's options (e.g., table types for MySQL). Returns
547 an array or array reference.
549 my @options = $table->options;
554 my $options = parse_list_arg( @_ );
556 push @{ $self->{'options'} }, @$options;
558 if ( ref $self->{'options'} ) {
559 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
562 return wantarray ? () : [];
566 # ----------------------------------------------------------------------
573 Get or set the table's order.
575 my $order = $table->order(3);
579 my ( $self, $arg ) = @_;
581 if ( defined $arg && $arg =~ /^\d+$/ ) {
582 $self->{'order'} = $arg;
585 return $self->{'order'} || 0;
588 # ----------------------------------------------------------------------
591 undef $self->{'schema'}; # destroy cyclical reference
592 undef $_ for @{ $self->{'constraints'} };
593 undef $_ for @{ $self->{'indices'} };
594 undef $_ for values %{ $self->{'fields'} };
599 # ----------------------------------------------------------------------
605 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>