1 package SQL::Translator::Schema::Table;
3 # ----------------------------------------------------------------------
4 # $Id: Table.pm,v 1.23 2003-10-04 01:22: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;
51 use base 'Class::Base';
52 use vars qw( $VERSION $FIELD_ORDER );
54 $VERSION = sprintf "%d.%02d", q$Revision: 1.23 $ =~ /(\d+)\.(\d+)/;
56 # ----------------------------------------------------------------------
65 my $table = SQL::Translator::Schema::Table->new(
72 my ( $self, $config ) = @_;
74 for my $arg ( qw[ schema name comments ] ) {
75 next unless defined $config->{ $arg };
76 defined $self->$arg( $config->{ $arg } ) or return;
82 # ----------------------------------------------------------------------
89 Add a constraint to the table. Returns the newly created
90 C<SQL::Translator::Schema::Constraint> object.
92 my $c1 = $table->add_constraint(
95 fields => [ 'foo_id' ],
98 my $c2 = SQL::Translator::Schema::Constraint->new( name => 'uniq' );
99 $c2 = $table->add_constraint( $constraint );
104 my $constraint_class = 'SQL::Translator::Schema::Constraint';
107 if ( UNIVERSAL::isa( $_[0], $constraint_class ) ) {
109 $constraint->table( $self );
113 $args{'table'} = $self;
114 $constraint = $constraint_class->new( \%args ) or
115 return $self->error( $constraint_class->error );
119 # If we're trying to add a PK when one is already defined,
120 # then just add the fields to the existing definition.
123 my $pk = $self->primary_key;
124 if ( $pk && $constraint->type eq PRIMARY_KEY ) {
125 $self->primary_key( $constraint->fields );
129 elsif ( $constraint->type eq PRIMARY_KEY ) {
130 for my $fname ( $constraint->fields ) {
131 if ( my $f = $self->get_field( $fname ) ) {
132 $f->is_primary_key( 1 );
137 # See if another constraint of the same type
138 # covers the same fields. -- This doesn't work! ky
140 # elsif ( $constraint->type ne CHECK_C ) {
141 # my @field_names = $constraint->fields;
143 # grep { $_->type eq $constraint->type }
144 # $self->get_constraints
146 # my %fields = map { $_, 1 } $c->fields;
147 # for my $field_name ( @field_names ) {
148 # if ( $fields{ $field_name } ) {
159 push @{ $self->{'constraints'} }, $constraint;
165 # ----------------------------------------------------------------------
172 Add an index to the table. Returns the newly created
173 C<SQL::Translator::Schema::Index> object.
175 my $i1 = $table->add_index(
177 fields => [ 'name' ],
181 my $i2 = SQL::Translator::Schema::Index->new( name => 'id' );
182 $i2 = $table->add_index( $index );
187 my $index_class = 'SQL::Translator::Schema::Index';
190 if ( UNIVERSAL::isa( $_[0], $index_class ) ) {
192 $index->table( $self );
196 $args{'table'} = $self;
197 $index = $index_class->new( \%args ) or return
198 $self->error( $index_class->error );
201 push @{ $self->{'indices'} }, $index;
205 # ----------------------------------------------------------------------
212 Add an field to the table. Returns the newly created
213 C<SQL::Translator::Schema::Field> object. The "name" parameter is
214 required. If you try to create a field with the same name as an
215 existing field, you will get an error and the field will not be created.
217 my $f1 = $table->add_field(
219 data_type => 'integer',
223 my $f2 = SQL::Translator::Schema::Field->new(
227 $f2 = $table->add_field( $field2 ) or die $table->error;
232 my $field_class = 'SQL::Translator::Schema::Field';
235 if ( UNIVERSAL::isa( $_[0], $field_class ) ) {
237 $field->table( $self );
241 $args{'table'} = $self;
242 $field = $field_class->new( \%args ) or return
243 $self->error( $field_class->error );
246 $field->order( ++$FIELD_ORDER );
247 my $field_name = $field->name or return $self->error('No name');
249 if ( exists $self->{'fields'}{ $field_name } ) {
250 return $self->error(qq[Can't create field: "$field_name" exists]);
253 $self->{'fields'}{ $field_name } = $field;
259 # ----------------------------------------------------------------------
266 Get or set the comments on a table. May be called several times to
267 set and it will accumulate the comments. Called in an array context,
268 returns each comment individually; called in a scalar context, returns
269 all the comments joined on newlines.
271 $table->comments('foo');
272 $table->comments('bar');
273 print join( ', ', $table->comments ); # prints "foo, bar"
278 my @comments = ref $_[0] ? @{ $_[0] } : @_;
280 for my $arg ( @comments ) {
281 $arg = $arg->[0] if ref $arg;
282 push @{ $self->{'comments'} }, $arg if defined $arg && $arg;
285 if ( @{ $self->{'comments'} || [] } ) {
287 ? @{ $self->{'comments'} }
288 : join( "\n", @{ $self->{'comments'} } )
292 return wantarray ? () : undef;
296 # ----------------------------------------------------------------------
297 sub get_constraints {
301 =head2 get_constraints
303 Returns all the constraint objects as an array or array reference.
305 my @constraints = $table->get_constraints;
311 if ( ref $self->{'constraints'} ) {
313 ? @{ $self->{'constraints'} } : $self->{'constraints'};
316 $self->error('No constraints');
317 return wantarray ? () : undef;
321 # ----------------------------------------------------------------------
328 Returns all the index objects as an array or array reference.
330 my @indices = $table->get_indices;
336 if ( ref $self->{'indices'} ) {
338 ? @{ $self->{'indices'} }
339 : $self->{'indices'};
342 $self->error('No indices');
343 return wantarray ? () : undef;
347 # ----------------------------------------------------------------------
354 Returns a field by the name provided.
356 my $field = $table->get_field('foo');
361 my $field_name = shift or return $self->error('No field name');
362 return $self->error( qq[Field "$field_name" does not exist] ) unless
363 exists $self->{'fields'}{ $field_name };
364 return $self->{'fields'}{ $field_name };
367 # ----------------------------------------------------------------------
374 Returns all the field objects as an array or array reference.
376 my @fields = $table->get_fields;
383 sort { $a->[0] <=> $b->[0] }
384 map { [ $_->order, $_ ] }
385 values %{ $self->{'fields'} || {} };
388 return wantarray ? @fields : \@fields;
391 $self->error('No fields');
392 return wantarray ? () : undef;
396 # ----------------------------------------------------------------------
403 Determine whether the view is valid or not.
405 my $ok = $view->is_valid;
410 return $self->error('No name') unless $self->name;
411 return $self->error('No fields') unless $self->get_fields;
414 $self->get_fields, $self->get_indices, $self->get_constraints
416 return $object->error unless $object->is_valid;
422 # ----------------------------------------------------------------------
423 sub is_trivial_link {
432 return 0 if $self->is_data;
433 return $self->{'is_trivial_link'} if defined $self->{'is_trivial_link'};
435 $self->{'is_trivial_link'} = 1;
439 foreach my $field ( $self->get_fields ) {
440 next unless $field->is_foreign_key;
441 $fk{$field->foreign_key_reference->reference_table}++;
444 foreach my $referenced (keys %fk){
445 if($fk{$referenced} > 1){
446 $self->{'is_trivial_link'} = 0;
451 return $self->{'is_trivial_link'};
464 return $self->{'is_data'} if defined $self->{'is_data'};
466 $self->{'is_data'} = 0;
468 foreach my $field ( $self->get_fields ) {
469 if ( !$field->is_primary_key and !$field->is_foreign_key ) {
470 $self->{'is_data'} = 1;
471 return $self->{'is_data'};
475 return $self->{'is_data'};
478 # ----------------------------------------------------------------------
485 Determine whether the table can link two arg tables via many-to-many.
487 my $ok = $table->can_link($table1,$table2);
491 my ( $self, $table1, $table2 ) = @_;
493 return $self->{'can_link'}{ $table1->name }{ $table2->name }
494 if defined $self->{'can_link'}{ $table1->name }{ $table2->name };
496 if ( $self->is_data == 1 ) {
497 $self->{'can_link'}{ $table1->name }{ $table2->name } = [0];
498 $self->{'can_link'}{ $table2->name }{ $table1->name } = [0];
499 return $self->{'can_link'}{ $table1->name }{ $table2->name };
504 foreach my $field ( $self->get_fields ) {
505 if ( $field->is_foreign_key ) {
506 push @{ $fk{ $field->foreign_key_reference->reference_table } },
507 $field->foreign_key_reference;
511 if ( !defined( $fk{ $table1->name } ) or !defined( $fk{ $table2->name } ) )
513 $self->{'can_link'}{ $table1->name }{ $table2->name } = [0];
514 $self->{'can_link'}{ $table2->name }{ $table1->name } = [0];
515 return $self->{'can_link'}{ $table1->name }{ $table2->name };
518 # trivial traversal, only one way to link the two tables
519 if ( scalar( @{ $fk{ $table1->name } } == 1 )
520 and scalar( @{ $fk{ $table2->name } } == 1 ) )
522 $self->{'can_link'}{ $table1->name }{ $table2->name } =
523 [ 'one2one', $fk{ $table1->name }, $fk{ $table2->name } ];
524 $self->{'can_link'}{ $table1->name }{ $table2->name } =
525 [ 'one2one', $fk{ $table2->name }, $fk{ $table1->name } ];
527 # non-trivial traversal. one way to link table2,
528 # many ways to link table1
530 elsif ( scalar( @{ $fk{ $table1->name } } > 1 )
531 and scalar( @{ $fk{ $table2->name } } == 1 ) )
533 $self->{'can_link'}{ $table1->name }{ $table2->name } =
534 [ 'many2one', $fk{ $table1->name }, $fk{ $table2->name } ];
535 $self->{'can_link'}{ $table2->name }{ $table1->name } =
536 [ 'one2many', $fk{ $table2->name }, $fk{ $table1->name } ];
538 # non-trivial traversal. one way to link table1,
539 # many ways to link table2
541 elsif ( scalar( @{ $fk{ $table1->name } } == 1 )
542 and scalar( @{ $fk{ $table2->name } } > 1 ) )
544 $self->{'can_link'}{ $table1->name }{ $table2->name } =
545 [ 'one2many', $fk{ $table1->name }, $fk{ $table2->name } ];
546 $self->{'can_link'}{ $table2->name }{ $table1->name } =
547 [ 'many2one', $fk{ $table2->name }, $fk{ $table1->name } ];
549 # non-trivial traversal. many ways to link table1 and table2
551 elsif ( scalar( @{ $fk{ $table1->name } } > 1 )
552 and scalar( @{ $fk{ $table2->name } } > 1 ) )
554 $self->{'can_link'}{ $table1->name }{ $table2->name } =
555 [ 'many2many', $fk{ $table1->name }, $fk{ $table2->name } ];
556 $self->{'can_link'}{ $table2->name }{ $table1->name } =
557 [ 'many2many', $fk{ $table2->name }, $fk{ $table1->name } ];
559 # one of the tables didn't export a key
560 # to this table, no linking possible
563 $self->{'can_link'}{ $table1->name }{ $table2->name } = [0];
564 $self->{'can_link'}{ $table2->name }{ $table1->name } = [0];
567 return $self->{'can_link'}{ $table1->name }{ $table2->name };
570 # ----------------------------------------------------------------------
577 Get or set the table's name.
579 If provided an argument, checks the schema object for a table of
580 that name and disallows the change if one exists.
582 my $table_name = $table->name('foo');
588 if ( my $arg = shift ) {
589 if ( my $schema = $self->schema ) {
590 return $self->error( qq[Can't use table name "$arg": table exists] )
591 if $schema->get_table( $arg );
593 $self->{'name'} = $arg;
596 return $self->{'name'} || '';
599 # ----------------------------------------------------------------------
606 Get or set the table's schema object.
608 my $schema = $table->schema;
613 if ( my $arg = shift ) {
614 return $self->error('Not a schema object') unless
615 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
616 $self->{'schema'} = $arg;
619 return $self->{'schema'};
622 # ----------------------------------------------------------------------
629 Gets or sets the table's primary key(s). Takes one or more field
630 names (as a string, list or array[ref]) as an argument. If the field
631 names are present, it will create a new PK if none exists, or it will
632 add to the fields of an existing PK (and will unique the field names).
633 Returns the C<SQL::Translator::Schema::Constraint> object representing
638 $table->primary_key('id');
639 $table->primary_key(['name']);
640 $table->primary_key('id','name']);
641 $table->primary_key(['id','name']);
642 $table->primary_key('id,name');
643 $table->primary_key(qw[ id name ]);
645 my $pk = $table->primary_key;
650 my $fields = parse_list_arg( @_ );
654 for my $f ( @$fields ) {
655 return $self->error(qq[Invalid field "$f"]) unless
656 $self->get_field($f);
660 for my $c ( $self->get_constraints ) {
661 if ( $c->type eq PRIMARY_KEY ) {
663 $c->fields( @{ $c->fields }, @$fields );
669 $constraint = $self->add_constraint(
680 for my $c ( $self->get_constraints ) {
681 return $c if $c->type eq PRIMARY_KEY;
688 # ----------------------------------------------------------------------
695 Get or set the table's options (e.g., table types for MySQL). Returns
696 an array or array reference.
698 my @options = $table->options;
703 my $options = parse_list_arg( @_ );
705 push @{ $self->{'options'} }, @$options;
707 if ( ref $self->{'options'} ) {
708 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
711 return wantarray ? () : [];
715 # ----------------------------------------------------------------------
722 Get or set the table's order.
724 my $order = $table->order(3);
728 my ( $self, $arg ) = @_;
730 if ( defined $arg && $arg =~ /^\d+$/ ) {
731 $self->{'order'} = $arg;
734 return $self->{'order'} || 0;
737 # ----------------------------------------------------------------------
740 undef $self->{'schema'}; # destroy cyclical reference
741 undef $_ for @{ $self->{'constraints'} };
742 undef $_ for @{ $self->{'indices'} };
743 undef $_ for values %{ $self->{'fields'} };
748 # ----------------------------------------------------------------------
754 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>,
755 Allen Day E<lt>allenday@ucla.eduE<gt>.