1 package SQL::Translator::Schema::Table;
3 # ----------------------------------------------------------------------
4 # $Id: Table.pm,v 1.2 2003-05-03 04:07:09 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::Schema::Constants;
45 use SQL::Translator::Schema::Constraint;
46 use SQL::Translator::Schema::Field;
47 use SQL::Translator::Schema::Index;
49 use base 'Class::Base';
50 use vars qw( $VERSION $FIELD_ORDER );
54 # ----------------------------------------------------------------------
63 my $schema = SQL::Translator::Schema::Table->new( name => 'foo' );
67 my ( $self, $config ) = @_;
68 $self->params( $config, qw[ name ] ) || return undef;
72 # ----------------------------------------------------------------------
79 Get or set the table's name.
81 my $table_name = $table->name('foo');
86 $self->{'name'} = shift if @_;
87 return $self->{'name'} || '';
90 # ----------------------------------------------------------------------
97 Add a constraint to the table. Returns the newly created
98 C<SQL::Translator::Schema::Constraint> object.
100 my $constraint = $table->add_constraint(
103 fields => [ 'foo_id' ],
109 my $constraint = SQL::Translator::Schema::Constraint->new( @_ ) or
110 return SQL::Translator::Schema::Constraint->error;
111 push @{ $self->{'constraints'} }, $constraint;
115 # ----------------------------------------------------------------------
122 Add an index to the table. Returns the newly created
123 C<SQL::Translator::Schema::Index> object.
125 my $index = $table->add_index(
127 fields => [ 'name' ],
134 my $index = SQL::Translator::Schema::Index->new( @_ ) or return
135 SQL::Translator::Schema::Index->error;
136 push @{ $self->{'indices'} }, $index;
140 # ----------------------------------------------------------------------
147 Add an field to the table. Returns the newly created
148 C<SQL::Translator::Schema::Field> object.
150 my $field = $table->add_field(
152 data_type => 'integer',
160 return $self->error('No name') unless $args{'name'};
161 my $field = SQL::Translator::Schema::Field->new( \%args ) or return;
162 SQL::Translator::Schema::Field->error;
163 $self->{'fields'}{ $field->name } = $field;
164 $self->{'fields'}{ $field->name }{'order'} = ++$FIELD_ORDER;
168 # ----------------------------------------------------------------------
169 sub get_constraints {
173 =head2 get_constraints
175 Returns all the constraint objects as an array or array reference.
177 my @constraints = $table->get_constraints;
183 if ( ref $self->{'constraints'} ) {
185 ? @{ $self->{'constraints'} } : $self->{'constraints'};
188 $self->error('No constraints');
189 return wantarray ? () : undef;
193 # ----------------------------------------------------------------------
200 Returns all the index objects as an array or array reference.
202 my @indices = $table->get_indices;
208 if ( ref $self->{'indices'} ) {
210 ? @{ $self->{'indices'} }
211 : $self->{'indices'};
214 $self->error('No indices');
215 return wantarray ? () : undef;
219 # ----------------------------------------------------------------------
226 Returns all the field objects as an array or array reference.
228 my @fields = $table->get_fields;
234 sort { $a->{'order'} <=> $b->{'order'} }
235 values %{ $self->{'fields'} || {} };
238 return wantarray ? @fields : \@fields;
241 $self->error('No fields');
242 return wantarray ? () : undef;
246 # ----------------------------------------------------------------------
253 Determine whether the view is valid or not.
255 my $ok = $view->is_valid;
260 return $self->error('No name') unless $self->name;
261 return $self->error('No fields') unless $self->get_fields;
264 $self->get_fields, $self->get_indices, $self->get_constraints
266 return $object->error unless $object->is_valid;
274 # ----------------------------------------------------------------------
280 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>