1 package SQL::Translator::Schema::Table;
3 # ----------------------------------------------------------------------
4 # $Id: Table.pm,v 1.1 2003-05-01 04:25: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 $foo_table = SQL::Translator::Schema::Table->new('foo');
34 $foo_table->add_field(
36 data_type => 'integer',
41 $foo_table->add_field(
47 $foo_table->add_index(
49 fields => [ 'foo_name' ],
54 C<SQL::Translator::Schema::Table> is the table object.
62 use SQL::Translator::Schema::Constraint;
63 use SQL::Translator::Schema::Field;
64 use SQL::Translator::Schema::Index;
66 use base 'Class::Base';
67 use vars qw($VERSION);
71 # ----------------------------------------------------------------------
80 my $schema = SQL::Translator::Schema::Table->new( name => 'foo' );
84 my ( $self, $config ) = @_;
85 $self->params( $config, qw[ name ] ) || return undef;
89 # ----------------------------------------------------------------------
96 Get or set the table's name.
98 my $table_name = $table->name('foo');
103 $self->{'name'} = shift if @_;
104 return $self->{'name'} || '';
107 # ----------------------------------------------------------------------
112 =head2 add_constraint
114 Add a constraint to the table.
116 $table->add_constraint(
118 fields => [ 'foo_id' ],
119 type => 'primary_key',
125 my $constraint = SQL::Translator::Schema::Constraint->new( @_ ) or
126 return SQL::Translator::Schema::Constraint->error;
127 push @{ $self->{'constraints'} }, $constraint;
131 # ----------------------------------------------------------------------
138 Add an index to the table.
142 fields => [ 'name' ],
149 my $index = SQL::Translator::Schema::Index->new( @_ ) or return
150 SQL::Translator::Schema::Index->error;
151 push @{ $self->{'indices'} }, $index;
155 # ----------------------------------------------------------------------
162 Add an field to the table. Returns the SQL::Translator::Schema::Field
165 my $field = $table->add_field(
167 data_type => 'integer',
175 my $field = SQL::Translator::Schema::Field->new( @_ ) or return;
176 SQL::Translator::Schema::Field->error;
177 $self->{'fields'}{ $field->name } = $field;
181 # ----------------------------------------------------------------------
188 Returns all the fields.
190 my @fields = $table->fields;
195 return wantarray ? %{ $self->{'fields'} || {} } : $self->{'fields'};
198 # ----------------------------------------------------------------------
205 Determine whether the view is valid or not.
207 my $ok = $view->is_valid;
212 return ( $self->name && $self->fields ) ? 1 : 0;
217 # ----------------------------------------------------------------------
223 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>