1 package SQL::Translator::Schema::Table;
3 # ----------------------------------------------------------------------
4 # $Id: Table.pm,v 1.5 2003-05-09 17:11:00 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 );
55 # ----------------------------------------------------------------------
64 my $table = SQL::Translator::Schema::Table->new(
71 my ( $self, $config ) = @_;
73 for my $arg ( qw[ schema name ] ) {
74 next unless defined $config->{ $arg };
75 $self->$arg( $config->{ $arg } ) or return;
81 # ----------------------------------------------------------------------
88 Get or set the table's name.
90 If provided an argument, checks the schema object for a table of
91 that name and disallows the change if one exists.
93 my $table_name = $table->name('foo');
99 if ( my $arg = shift ) {
100 if ( my $schema = $self->schema ) {
101 return $self->error( qq[Can't use table name "$arg": table exists] )
102 if $schema->get_table( $arg );
104 $self->{'name'} = $arg;
107 return $self->{'name'} || '';
110 # ----------------------------------------------------------------------
115 =head2 add_constraint
117 Add a constraint to the table. Returns the newly created
118 C<SQL::Translator::Schema::Constraint> object.
120 my $constraint1 = $table->add_constraint(
123 fields => [ 'foo_id' ],
126 my $constraint2 = SQL::Translator::Schema::Constraint->new( name => 'uniq' );
127 $constraint2 = $table->add_constraint( $constraint );
132 my $constraint_class = 'SQL::Translator::Schema::Constraint';
135 if ( UNIVERSAL::isa( $_[0], $constraint_class ) ) {
137 $constraint->table( $self );
141 $args{'table'} = $self;
142 $constraint = $constraint_class->new( \%args ) or
143 return $self->error( $constraint_class->error );
146 push @{ $self->{'constraints'} }, $constraint;
150 # ----------------------------------------------------------------------
157 Add an index to the table. Returns the newly created
158 C<SQL::Translator::Schema::Index> object.
160 my $index1 = $table->add_index(
162 fields => [ 'name' ],
166 my $index2 = SQL::Translator::Schema::Index->new( name => 'id' );
167 $index2 = $table->add_index( $index );
172 my $index_class = 'SQL::Translator::Schema::Index';
175 if ( UNIVERSAL::isa( $_[0], $index_class ) ) {
177 $index->table( $self );
181 $args{'table'} = $self;
182 $index = $index_class->new( \%args ) or return
183 $self->error( $index_class->error );
186 push @{ $self->{'indices'} }, $index;
190 # ----------------------------------------------------------------------
197 Add an field to the table. Returns the newly created
198 C<SQL::Translator::Schema::Field> object. The "name" parameter is
199 required. If you try to create a field with the same name as an
200 existing field, you will get an error and the field will not be created.
202 my $field1 = $table->add_field(
204 data_type => 'integer',
208 my $field2 = SQL::Translator::Schema::Field->new(
212 $field2 = $table->add_field( $field2 ) or die $table->error;
217 my $field_class = 'SQL::Translator::Schema::Field';
220 if ( UNIVERSAL::isa( $_[0], $field_class ) ) {
222 $field->table( $self );
226 $args{'table'} = $self;
227 $field = $field_class->new( \%args ) or return
228 $self->error( $field_class->error );
231 $field->order( ++$FIELD_ORDER );
232 my $field_name = $field->name or return $self->error('No name');
234 if ( exists $self->{'fields'}{ $field_name } ) {
235 return $self->error(qq[Can't create field: "$field_name" exists]);
238 $self->{'fields'}{ $field_name } = $field;
244 # ----------------------------------------------------------------------
245 sub get_constraints {
249 =head2 get_constraints
251 Returns all the constraint objects as an array or array reference.
253 my @constraints = $table->get_constraints;
259 if ( ref $self->{'constraints'} ) {
261 ? @{ $self->{'constraints'} } : $self->{'constraints'};
264 $self->error('No constraints');
265 return wantarray ? () : undef;
269 # ----------------------------------------------------------------------
276 Returns all the index objects as an array or array reference.
278 my @indices = $table->get_indices;
284 if ( ref $self->{'indices'} ) {
286 ? @{ $self->{'indices'} }
287 : $self->{'indices'};
290 $self->error('No indices');
291 return wantarray ? () : undef;
295 # ----------------------------------------------------------------------
302 Returns a field by the name provided.
304 my $field = $table->get_field('foo');
309 my $field_name = shift or return $self->error('No field name');
310 return $self->error( qq[Field "$field_name" does not exist] ) unless
311 exists $self->{'fields'}{ $field_name };
312 return $self->{'fields'}{ $field_name };
315 # ----------------------------------------------------------------------
322 Returns all the field objects as an array or array reference.
324 my @fields = $table->get_fields;
331 sort { $a->[0] <=> $b->[0] }
332 map { [ $_->order, $_ ] }
333 values %{ $self->{'fields'} || {} };
336 return wantarray ? @fields : \@fields;
339 $self->error('No fields');
340 return wantarray ? () : undef;
344 # ----------------------------------------------------------------------
351 Determine whether the view is valid or not.
353 my $ok = $view->is_valid;
358 return $self->error('No name') unless $self->name;
359 return $self->error('No fields') unless $self->get_fields;
362 $self->get_fields, $self->get_indices, $self->get_constraints
364 return $object->error unless $object->is_valid;
370 # ----------------------------------------------------------------------
377 Get or set the table's schema object.
379 my $schema = $table->schema;
384 if ( my $arg = shift ) {
385 return $self->error('Not a schema object') unless
386 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
387 $self->{'schema'} = $arg;
390 return $self->{'schema'};
393 # ----------------------------------------------------------------------
400 Gets or sets the table's primary key(s). Takes one or more field
401 names (as a string, list or array[ref]) as an argument. If the field
402 names are present, it will create a new PK if none exists, or it will
403 add to the fields of an existing PK (and will unique the field names).
404 Returns the C<SQL::Translator::Schema::Constraint> object representing
409 $table->primary_key('id');
410 $table->primary_key(['name']);
411 $table->primary_key('id','name']);
412 $table->primary_key(['id','name']);
413 $table->primary_key('id,name');
414 $table->primary_key(qw[ id name ]);
416 my $pk = $table->primary_key;
421 my $fields = parse_list_arg( @_ );
425 for my $f ( @$fields ) {
426 return $self->error(qq[Invalid field "$f"]) unless
427 $self->get_field($f);
431 for my $c ( $self->get_constraints ) {
432 if ( $c->type eq PRIMARY_KEY ) {
434 $c->fields( @{ $c->fields }, @$fields );
440 $constraint = $self->add_constraint(
451 for my $c ( $self->get_constraints ) {
452 return $c if $c->type eq PRIMARY_KEY;
456 return $self->error('No primary key');
459 # ----------------------------------------------------------------------
466 Get or set the table's options (e.g., table types for MySQL). Returns
467 an array or array reference.
469 my @options = $table->options;
474 my $options = parse_list_arg( @_ );
476 push @{ $self->{'options'} }, @$options;
478 if ( ref $self->{'options'} ) {
479 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
482 return wantarray ? () : [];
486 # ----------------------------------------------------------------------
493 Get or set the table's order.
495 my $order = $table->order(3);
499 my ( $self, $arg ) = @_;
501 if ( defined $arg && $arg =~ /^\d+$/ ) {
502 $self->{'order'} = $arg;
505 return $self->{'order'} || 0;
508 # ----------------------------------------------------------------------
511 undef $self->{'schema'}; # destroy cyclical reference
512 undef $_ for @{ $self->{'constraints'} };
513 undef $_ for @{ $self->{'indices'} };
514 undef $_ for values %{ $self->{'fields'} };
519 # ----------------------------------------------------------------------
525 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>