Fixed up some POD.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema.pm
CommitLineData
3c5de62a 1package SQL::Translator::Schema;
2
3# ----------------------------------------------------------------------
68e8e2e1 4# $Id: Schema.pm,v 1.5 2003-06-06 00:11:06 kycl4rk Exp $
3c5de62a 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
27SQL::Translator::Schema - SQL::Translator schema object
28
29=head1 SYNOPSIS
30
31 use SQL::Translator::Schema;
76dce619 32 my $schema = SQL::Translator::Schema->new;
33 my $table = $schema->add_table( name => 'foo' );
34 my $view = $schema->add_view( name => 'bar', sql => '...' );
3c5de62a 35
36=head1 DESCSIPTION
37
38C<SQL::Translator::Schema> is the object that accepts, validates, and
39returns the database structure.
40
41=head1 METHODS
42
43=cut
44
45use strict;
46use Class::Base;
47use SQL::Translator::Schema::Table;
48use SQL::Translator::Schema::View;
49
50use base 'Class::Base';
76dce619 51use vars qw[ $VERSION $TABLE_ORDER $VIEW_ORDER ];
3c5de62a 52
53$VERSION = 1.00;
54
55# ----------------------------------------------------------------------
56sub init {
57
58=pod
59
60=head2 new
61
62Object constructor.
63
99248301 64 my $schema = SQL::Translator->new(
65 name => 'Foo',
66 database => 'MySQL',
67 );
3c5de62a 68
69=cut
70
71 my ( $self, $config ) = @_;
99248301 72 $self->params( $config, qw[ name database ] ) || return undef;
3c5de62a 73 return $self;
74}
75
76# ----------------------------------------------------------------------
76dce619 77sub add_table {
3c5de62a 78
79=pod
80
76dce619 81=head2 add_table
3c5de62a 82
76dce619 83Add a table object. Returns the new SQL::Translator::Schema::Table object.
99248301 84The "name" parameter is required. If you try to create a table with the
85same name as an existing table, you will get an error and the table will
86not be created.
3c5de62a 87
68e8e2e1 88 my $t1 = $schema->add_table( name => 'foo' ) or die $schema->error;
89 my $t2 = SQL::Translator::Schema::Table->new( name => 'bar' );
90 $t2 = $schema->add_table( $table_bar ) or die $schema->error;
3c5de62a 91
92=cut
93
99248301 94 my $self = shift;
95 my $table_class = 'SQL::Translator::Schema::Table';
96 my $table;
97
98 if ( UNIVERSAL::isa( $_[0], $table_class ) ) {
99 $table = shift;
100 $table->schema( $self );
101 }
102 else {
103 my %args = @_;
104 $args{'schema'} = $self;
105 $table = $table_class->new( \%args ) or return
106 $self->error( $table_class->error );
107 }
3c5de62a 108
d0b43695 109 $table->order( ++$TABLE_ORDER );
99248301 110 my $table_name = $table->name or return $self->error('No table name');
111
112 if ( defined $self->{'tables'}{ $table_name } ) {
113 return $self->error(qq[Can't create table: "$table_name" exists]);
114 }
115 else {
116 $self->{'tables'}{ $table_name } = $table;
99248301 117 }
3c5de62a 118
119 return $table;
120}
121
122# ----------------------------------------------------------------------
76dce619 123sub add_view {
3c5de62a 124
125=pod
126
76dce619 127=head2 add_view
3c5de62a 128
76dce619 129Add a view object. Returns the new SQL::Translator::Schema::View object.
99248301 130The "name" parameter is required. If you try to create a view with the
131same name as an existing view, you will get an error and the view will
132not be created.
133
68e8e2e1 134 my $v1 = $schema->add_view( name => 'foo' );
135 my $v2 = SQL::Translator::Schema::View->new( name => 'bar' );
136 $v2 = $schema->add_view( $view_bar ) or die $schema->error;
3c5de62a 137
138=cut
139
99248301 140 my $self = shift;
141 my $view_class = 'SQL::Translator::Schema::View';
142 my $view;
143
144 if ( UNIVERSAL::isa( $_[0], $view_class ) ) {
145 $view = shift;
146 }
147 else {
148 my %args = @_;
149 return $self->error('No view name') unless $args{'name'};
150 $view = $view_class->new( \%args ) or return $view_class->error;
151 }
3c5de62a 152
d0b43695 153 $view->order( ++$VIEW_ORDER );
99248301 154 my $view_name = $view->name or return $self->error('No view name');
155
156 if ( defined $self->{'views'}{ $view_name } ) {
157 return $self->error(qq[Can't create view: "$view_name" exists]);
158 }
159 else {
160 $self->{'views'}{ $view_name } = $view;
99248301 161 }
3c5de62a 162
76dce619 163 return $view;
3c5de62a 164}
165
166# ----------------------------------------------------------------------
99248301 167sub database {
168
169=pod
170
171=head2 database
172
173Get or set the schema's database. (optional)
174
175 my $database = $schema->database('PostgreSQL');
176
177=cut
178
179 my $self = shift;
180 $self->{'database'} = shift if @_;
181 return $self->{'database'} || '';
182}
183
184# ----------------------------------------------------------------------
76dce619 185sub is_valid {
3c5de62a 186
187=pod
188
76dce619 189=head2 is_valid
3c5de62a 190
76dce619 191Returns true if all the tables and views are valid.
3c5de62a 192
76dce619 193 my $ok = $schema->is_valid or die $schema->error;
194
195=cut
196
197 my $self = shift;
198
199 return $self->error('No tables') unless $self->get_tables;
200
201 for my $object ( $self->get_tables, $self->get_views ) {
202 return $object->error unless $object->is_valid;
203 }
204
205 return 1;
206}
207
208# ----------------------------------------------------------------------
209sub get_table {
210
211=pod
212
213=head2 get_table
214
215Returns a table by the name provided.
216
217 my $table = $schema->get_table('foo');
218
219=cut
220
221 my $self = shift;
222 my $table_name = shift or return $self->error('No table name');
99248301 223 return $self->error( qq[Table "$table_name" does not exist] ) unless
76dce619 224 exists $self->{'tables'}{ $table_name };
225 return $self->{'tables'}{ $table_name };
226}
227
228# ----------------------------------------------------------------------
229sub get_tables {
230
231=pod
232
233=head2 get_tables
234
235Returns all the tables as an array or array reference.
236
237 my @tables = $schema->get_tables;
238
239=cut
240
241 my $self = shift;
d0b43695 242 my @tables =
243 map { $_->[1] }
244 sort { $a->[0] <=> $b->[0] }
245 map { [ $_->order, $_ ] }
76dce619 246 values %{ $self->{'tables'} };
247
248 if ( @tables ) {
249 return wantarray ? @tables : \@tables;
250 }
251 else {
252 $self->error('No tables');
253 return wantarray ? () : undef;
254 }
255}
256
257# ----------------------------------------------------------------------
258sub get_view {
259
260=pod
261
262=head2 get_view
263
264Returns a view by the name provided.
265
266 my $view = $schema->get_view('foo');
3c5de62a 267
268=cut
269
270 my $self = shift;
76dce619 271 my $view_name = shift or return $self->error('No view name');
272 return $self->error('View "$view_name" does not exist') unless
273 exists $self->{'views'}{ $view_name };
274 return $self->{'views'}{ $view_name };
275}
3c5de62a 276
76dce619 277# ----------------------------------------------------------------------
278sub get_views {
3c5de62a 279
76dce619 280=pod
281
282=head2 get_views
283
284Returns all the views as an array or array reference.
285
286 my @views = $schema->get_views;
287
288=cut
289
290 my $self = shift;
d0b43695 291 my @views =
292 map { $_->[1] }
293 sort { $a->[0] <=> $b->[0] }
294 map { [ $_->order, $_ ] }
99248301 295 values %{ $self->{'views'} };
76dce619 296
297 if ( @views ) {
298 return wantarray ? @views : \@views;
299 }
300 else {
301 $self->error('No views');
302 return wantarray ? () : undef;
303 }
3c5de62a 304}
305
99248301 306# ----------------------------------------------------------------------
307sub name {
308
309=pod
310
311=head2 name
312
313Get or set the schema's name. (optional)
314
315 my $schema_name = $schema->name('Foo Database');
316
317=cut
318
319 my $self = shift;
320 $self->{'name'} = shift if @_;
321 return $self->{'name'} || '';
322}
323
d0b43695 324# ----------------------------------------------------------------------
325sub DESTROY {
326 my $self = shift;
327 undef $_ for values %{ $self->{'tables'} };
328 undef $_ for values %{ $self->{'views'} };
329}
330
3c5de62a 3311;
332
333# ----------------------------------------------------------------------
334
335=pod
336
337=head1 AUTHOR
338
339Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
340
341=cut