1 package SQL::Translator::Schema::Table;
3 # ----------------------------------------------------------------------
4 # $Id: Table.pm,v 1.13 2003-08-21 18:10:47 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.13 $ =~ /(\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 );
129 # See if another constraint of the same type
130 # covers the same fields.
132 elsif ( $constraint->type ne CHECK_C ) {
133 my @field_names = $constraint->fields;
135 grep { $_->type eq $constraint->type }
136 $self->get_constraints
138 my %fields = map { $_, 1 } $c->fields;
139 for my $field_name ( @field_names ) {
140 if ( $fields{ $field_name } ) {
151 push @{ $self->{'constraints'} }, $constraint;
157 # ----------------------------------------------------------------------
164 Add an index to the table. Returns the newly created
165 C<SQL::Translator::Schema::Index> object.
167 my $i1 = $table->add_index(
169 fields => [ 'name' ],
173 my $i2 = SQL::Translator::Schema::Index->new( name => 'id' );
174 $i2 = $table->add_index( $index );
179 my $index_class = 'SQL::Translator::Schema::Index';
182 if ( UNIVERSAL::isa( $_[0], $index_class ) ) {
184 $index->table( $self );
188 $args{'table'} = $self;
189 $index = $index_class->new( \%args ) or return
190 $self->error( $index_class->error );
193 push @{ $self->{'indices'} }, $index;
197 # ----------------------------------------------------------------------
204 Add an field to the table. Returns the newly created
205 C<SQL::Translator::Schema::Field> object. The "name" parameter is
206 required. If you try to create a field with the same name as an
207 existing field, you will get an error and the field will not be created.
209 my $f1 = $table->add_field(
211 data_type => 'integer',
215 my $f2 = SQL::Translator::Schema::Field->new(
219 $f2 = $table->add_field( $field2 ) or die $table->error;
224 my $field_class = 'SQL::Translator::Schema::Field';
227 if ( UNIVERSAL::isa( $_[0], $field_class ) ) {
229 $field->table( $self );
233 $args{'table'} = $self;
234 $field = $field_class->new( \%args ) or return
235 $self->error( $field_class->error );
238 $field->order( ++$FIELD_ORDER );
239 my $field_name = $field->name or return $self->error('No name');
241 if ( exists $self->{'fields'}{ $field_name } ) {
242 return $self->error(qq[Can't create field: "$field_name" exists]);
245 $self->{'fields'}{ $field_name } = $field;
251 # ----------------------------------------------------------------------
258 Get or set the comments on a table. May be called several times to
259 set and it will accumulate the comments. Called in an array context,
260 returns each comment individually; called in a scalar context, returns
261 all the comments joined on newlines.
263 $table->comments('foo');
264 $table->comments('bar');
265 print join( ', ', $table->comments ); # prints "foo, bar"
270 my @comments = ref $_[0] ? @{ $_[0] } : @_;
272 for my $arg ( @comments ) {
273 $arg = $arg->[0] if ref $arg;
274 push @{ $self->{'comments'} }, $arg if defined $arg && $arg;
277 if ( @{ $self->{'comments'} || [] } ) {
279 ? @{ $self->{'comments'} }
280 : join( "\n", @{ $self->{'comments'} } )
284 return wantarray ? () : undef;
288 # ----------------------------------------------------------------------
289 sub get_constraints {
293 =head2 get_constraints
295 Returns all the constraint objects as an array or array reference.
297 my @constraints = $table->get_constraints;
303 if ( ref $self->{'constraints'} ) {
305 ? @{ $self->{'constraints'} } : $self->{'constraints'};
308 $self->error('No constraints');
309 return wantarray ? () : undef;
313 # ----------------------------------------------------------------------
320 Returns all the index objects as an array or array reference.
322 my @indices = $table->get_indices;
328 if ( ref $self->{'indices'} ) {
330 ? @{ $self->{'indices'} }
331 : $self->{'indices'};
334 $self->error('No indices');
335 return wantarray ? () : undef;
339 # ----------------------------------------------------------------------
346 Returns a field by the name provided.
348 my $field = $table->get_field('foo');
353 my $field_name = shift or return $self->error('No field name');
354 return $self->error( qq[Field "$field_name" does not exist] ) unless
355 exists $self->{'fields'}{ $field_name };
356 return $self->{'fields'}{ $field_name };
359 # ----------------------------------------------------------------------
366 Returns all the field objects as an array or array reference.
368 my @fields = $table->get_fields;
375 sort { $a->[0] <=> $b->[0] }
376 map { [ $_->order, $_ ] }
377 values %{ $self->{'fields'} || {} };
380 return wantarray ? @fields : \@fields;
383 $self->error('No fields');
384 return wantarray ? () : undef;
388 # ----------------------------------------------------------------------
395 Determine whether the view is valid or not.
397 my $ok = $view->is_valid;
402 return $self->error('No name') unless $self->name;
403 return $self->error('No fields') unless $self->get_fields;
406 $self->get_fields, $self->get_indices, $self->get_constraints
408 return $object->error unless $object->is_valid;
414 # ----------------------------------------------------------------------
421 Get or set the table's name.
423 If provided an argument, checks the schema object for a table of
424 that name and disallows the change if one exists.
426 my $table_name = $table->name('foo');
432 if ( my $arg = shift ) {
433 if ( my $schema = $self->schema ) {
434 return $self->error( qq[Can't use table name "$arg": table exists] )
435 if $schema->get_table( $arg );
437 $self->{'name'} = $arg;
440 return $self->{'name'} || '';
443 # ----------------------------------------------------------------------
450 Get or set the table's schema object.
452 my $schema = $table->schema;
457 if ( my $arg = shift ) {
458 return $self->error('Not a schema object') unless
459 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
460 $self->{'schema'} = $arg;
463 return $self->{'schema'};
466 # ----------------------------------------------------------------------
473 Gets or sets the table's primary key(s). Takes one or more field
474 names (as a string, list or array[ref]) as an argument. If the field
475 names are present, it will create a new PK if none exists, or it will
476 add to the fields of an existing PK (and will unique the field names).
477 Returns the C<SQL::Translator::Schema::Constraint> object representing
482 $table->primary_key('id');
483 $table->primary_key(['name']);
484 $table->primary_key('id','name']);
485 $table->primary_key(['id','name']);
486 $table->primary_key('id,name');
487 $table->primary_key(qw[ id name ]);
489 my $pk = $table->primary_key;
494 my $fields = parse_list_arg( @_ );
498 for my $f ( @$fields ) {
499 return $self->error(qq[Invalid field "$f"]) unless
500 $self->get_field($f);
504 for my $c ( $self->get_constraints ) {
505 if ( $c->type eq PRIMARY_KEY ) {
507 $c->fields( @{ $c->fields }, @$fields );
513 $constraint = $self->add_constraint(
524 for my $c ( $self->get_constraints ) {
525 return $c if $c->type eq PRIMARY_KEY;
532 # ----------------------------------------------------------------------
539 Get or set the table's options (e.g., table types for MySQL). Returns
540 an array or array reference.
542 my @options = $table->options;
547 my $options = parse_list_arg( @_ );
549 push @{ $self->{'options'} }, @$options;
551 if ( ref $self->{'options'} ) {
552 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
555 return wantarray ? () : [];
559 # ----------------------------------------------------------------------
566 Get or set the table's order.
568 my $order = $table->order(3);
572 my ( $self, $arg ) = @_;
574 if ( defined $arg && $arg =~ /^\d+$/ ) {
575 $self->{'order'} = $arg;
578 return $self->{'order'} || 0;
581 # ----------------------------------------------------------------------
584 undef $self->{'schema'}; # destroy cyclical reference
585 undef $_ for @{ $self->{'constraints'} };
586 undef $_ for @{ $self->{'indices'} };
587 undef $_ for values %{ $self->{'fields'} };
592 # ----------------------------------------------------------------------
598 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>