More changes to getting the schema to a working state.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Table.pm
1 package SQL::Translator::Schema::Table;
2
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>
7 #
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.
11 #
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.
16 #
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
20 # 02111-1307  USA
21 # -------------------------------------------------------------------
22
23 =pod
24
25 =head1 NAME
26
27 SQL::Translator::Schema::Table - SQL::Translator table object
28
29 =head1 SYNOPSIS
30
31   use SQL::Translator::Schema::Table;
32   my $table = SQL::Translator::Schema::Table->new( name => 'foo' );
33
34 =head1 DESCSIPTION
35
36 C<SQL::Translator::Schema::Table> is the table object.
37
38 =head1 METHODS
39
40 =cut
41
42 use strict;
43 use Class::Base;
44 use SQL::Translator::Schema::Constants;
45 use SQL::Translator::Schema::Constraint;
46 use SQL::Translator::Schema::Field;
47 use SQL::Translator::Schema::Index;
48
49 use base 'Class::Base';
50 use vars qw( $VERSION $FIELD_ORDER );
51
52 $VERSION = 1.00;
53
54 # ----------------------------------------------------------------------
55 sub init {
56
57 =pod
58
59 =head2 new
60
61 Object constructor.
62
63   my $schema = SQL::Translator::Schema::Table->new( name => 'foo' );
64
65 =cut
66
67     my ( $self, $config ) = @_;
68     $self->params( $config, qw[ name ] ) || return undef;
69     return $self;
70 }
71
72 # ----------------------------------------------------------------------
73 sub name {
74
75 =pod
76
77 =head2 name
78
79 Get or set the table's name.
80
81   my $table_name = $table->name('foo');
82
83 =cut
84
85     my $self = shift;
86     $self->{'name'} = shift if @_;
87     return $self->{'name'} || '';
88 }
89
90 # ----------------------------------------------------------------------
91 sub add_constraint {
92
93 =pod
94
95 =head2 add_constraint
96
97 Add a constraint to the table.  Returns the newly created 
98 C<SQL::Translator::Schema::Constraint> object.
99
100   my $constraint = $table->add_constraint(
101       name   => 'pk',
102       type      => PRIMARY_KEY,
103       fields => [ 'foo_id' ],
104   );
105
106 =cut
107
108     my $self       = shift;
109     my $constraint = SQL::Translator::Schema::Constraint->new( @_ ) or 
110         return SQL::Translator::Schema::Constraint->error;
111     push @{ $self->{'constraints'} }, $constraint;
112     return $constraint;
113 }
114
115 # ----------------------------------------------------------------------
116 sub add_index {
117
118 =pod
119
120 =head2 add_index
121
122 Add an index to the table.  Returns the newly created
123 C<SQL::Translator::Schema::Index> object.
124
125   my $index  = $table->add_index(
126       name   => 'name',
127       fields => [ 'name' ],
128       type   => 'normal',
129   );
130
131 =cut
132
133     my $self  = shift;
134     my $index = SQL::Translator::Schema::Index->new( @_ ) or return
135                 SQL::Translator::Schema::Index->error;
136     push @{ $self->{'indices'} }, $index;
137     return $index;
138 }
139
140 # ----------------------------------------------------------------------
141 sub add_field {
142
143 =pod
144
145 =head2 add_field
146
147 Add an field to the table.  Returns the newly created 
148 C<SQL::Translator::Schema::Field> object.
149
150   my $field     =  $table->add_field(
151       name      => 'foo_id',
152       data_type => 'integer',
153       size      => 11,
154   );
155
156 =cut
157
158     my $self  = shift;
159     my %args  = @_;
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;
165     return $field;
166 }
167
168 # ----------------------------------------------------------------------
169 sub get_constraints {
170
171 =pod
172
173 =head2 get_constraints
174
175 Returns all the constraint objects as an array or array reference.
176
177   my @constraints = $table->get_constraints;
178
179 =cut
180
181     my $self = shift;
182
183     if ( ref $self->{'constraints'} ) {
184         return wantarray 
185             ? @{ $self->{'constraints'} } : $self->{'constraints'};
186     }
187     else {
188         $self->error('No constraints');
189         return wantarray ? () : undef;
190     }
191 }
192
193 # ----------------------------------------------------------------------
194 sub get_indices {
195
196 =pod
197
198 =head2 get_indices
199
200 Returns all the index objects as an array or array reference.
201
202   my @indices = $table->get_indices;
203
204 =cut
205
206     my $self = shift;
207
208     if ( ref $self->{'indices'} ) {
209         return wantarray 
210             ? @{ $self->{'indices'} } 
211             : $self->{'indices'};
212     }
213     else {
214         $self->error('No indices');
215         return wantarray ? () : undef;
216     }
217 }
218
219 # ----------------------------------------------------------------------
220 sub get_fields {
221
222 =pod
223
224 =head2 get_fields
225
226 Returns all the field objects as an array or array reference.
227
228   my @fields = $table->get_fields;
229
230 =cut
231
232     my $self = shift;
233     my @fields = 
234         sort { $a->{'order'} <=> $b->{'order'} }
235         values %{ $self->{'fields'} || {} };
236
237     if ( @fields ) {
238         return wantarray ? @fields : \@fields;
239     }
240     else {
241         $self->error('No fields');
242         return wantarray ? () : undef;
243     }
244 }
245
246 # ----------------------------------------------------------------------
247 sub is_valid {
248
249 =pod
250
251 =head2 is_valid
252
253 Determine whether the view is valid or not.
254
255   my $ok = $view->is_valid;
256
257 =cut
258
259     my $self = shift;
260     return $self->error('No name') unless $self->name;
261     return $self->error('No fields') unless $self->get_fields;
262
263     for my $object ( 
264         $self->get_fields, $self->get_indices, $self->get_constraints 
265     ) {
266         return $object->error unless $object->is_valid;
267     }
268
269     return 1;
270 }
271
272 1;
273
274 # ----------------------------------------------------------------------
275
276 =pod
277
278 =head1 AUTHOR
279
280 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
281
282 =cut