69ad4f39e73e4cf1a23624d1502d9b4631672b05
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Table.pm
1 package SQL::Translator::Schema::Table;
2
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>
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 $foo_table    = SQL::Translator::Schema::Table->new('foo');
33
34   $foo_table->add_field( 
35       name           => 'foo_id', 
36       data_type      => 'integer', 
37       size           => 11,
38       is_primary_key => 1,
39   );
40
41   $foo_table->add_field(
42       name           => 'foo_name', 
43       data_type      => 'char', 
44       size           => 10,
45   );
46
47   $foo_table->add_index(
48       name           => '',
49       fields         => [ 'foo_name' ],
50   );
51
52 =head1 DESCSIPTION
53
54 C<SQL::Translator::Schema::Table> is the table object.
55
56 =head1 METHODS
57
58 =cut
59
60 use strict;
61 use Class::Base;
62 use SQL::Translator::Schema::Constraint;
63 use SQL::Translator::Schema::Field;
64 use SQL::Translator::Schema::Index;
65
66 use base 'Class::Base';
67 use vars qw($VERSION);
68
69 $VERSION = 1.00;
70
71 # ----------------------------------------------------------------------
72 sub init {
73
74 =pod
75
76 =head2 new
77
78 Object constructor.
79
80   my $schema = SQL::Translator::Schema::Table->new( name => 'foo' );
81
82 =cut
83
84     my ( $self, $config ) = @_;
85     $self->params( $config, qw[ name ] ) || return undef;
86     return $self;
87 }
88
89 # ----------------------------------------------------------------------
90 sub name {
91
92 =pod
93
94 =head2 name
95
96 Get or set the table's name.
97
98   my $table_name = $table->name('foo');
99
100 =cut
101
102     my $self = shift;
103     $self->{'name'} = shift if @_;
104     return $self->{'name'} || '';
105 }
106
107 # ----------------------------------------------------------------------
108 sub add_constraint {
109
110 =pod
111
112 =head2 add_constraint
113
114 Add a constraint to the table.
115
116   $table->add_constraint(
117       name   => 'pk',
118       fields => [ 'foo_id' ],
119       type   => 'primary_key',
120   );
121
122 =cut
123
124     my $self       = shift;
125     my $constraint = SQL::Translator::Schema::Constraint->new( @_ ) or 
126         return SQL::Translator::Schema::Constraint->error;
127     push @{ $self->{'constraints'} }, $constraint;
128     return $constraint;
129 }
130
131 # ----------------------------------------------------------------------
132 sub add_index {
133
134 =pod
135
136 =head2 add_index
137
138 Add an index to the table.
139
140   $table->add_index(
141       name   => 'name',
142       fields => [ 'name' ],
143       type   => 'normal',
144   );
145
146 =cut
147
148     my $self  = shift;
149     my $index = SQL::Translator::Schema::Index->new( @_ ) or return
150                 SQL::Translator::Schema::Index->error;
151     push @{ $self->{'indices'} }, $index;
152     return $index;
153 }
154
155 # ----------------------------------------------------------------------
156 sub add_field {
157
158 =pod
159
160 =head2 add_field
161
162 Add an field to the table.  Returns the SQL::Translator::Schema::Field 
163 object.
164
165   my $field          =  $table->add_field(
166       name           => 'foo_id',
167       data_type      => 'integer',
168       size           => 11,
169       is_primary_key => 1,
170   );
171
172 =cut
173
174     my $self  = shift;
175     my $field = SQL::Translator::Schema::Field->new( @_ ) or return;
176                 SQL::Translator::Schema::Field->error;
177     $self->{'fields'}{ $field->name } = $field;
178     return $field;
179 }
180
181 # ----------------------------------------------------------------------
182 sub fields {
183
184 =pod
185
186 =head2 fields
187
188 Returns all the fields.
189
190   my @fields = $table->fields;
191
192 =cut
193
194     my $self = shift;
195     return wantarray ? %{ $self->{'fields'} || {} } : $self->{'fields'};
196 }
197
198 # ----------------------------------------------------------------------
199 sub is_valid {
200
201 =pod
202
203 =head2 is_valid
204
205 Determine whether the view is valid or not.
206
207   my $ok = $view->is_valid;
208
209 =cut
210
211     my $self = shift;
212     return ( $self->name && $self->fields ) ? 1 : 0;
213 }
214
215 1;
216
217 # ----------------------------------------------------------------------
218
219 =pod
220
221 =head1 AUTHOR
222
223 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
224
225 =cut