1 package SQL::Translator::Schema;
3 # ----------------------------------------------------------------------
4 # $Id: Schema.pm,v 1.4 2003-05-09 16:53:21 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 - SQL::Translator schema object
31 use SQL::Translator::Schema;
32 my $schema = SQL::Translator::Schema->new;
33 my $table = $schema->add_table( name => 'foo' );
34 my $view = $schema->add_view( name => 'bar', sql => '...' );
38 C<SQL::Translator::Schema> is the object that accepts, validates, and
39 returns the database structure.
47 use SQL::Translator::Schema::Table;
48 use SQL::Translator::Schema::View;
50 use base 'Class::Base';
51 use vars qw[ $VERSION $TABLE_ORDER $VIEW_ORDER ];
55 # ----------------------------------------------------------------------
64 my $schema = SQL::Translator->new(
71 my ( $self, $config ) = @_;
72 $self->params( $config, qw[ name database ] ) || return undef;
76 # ----------------------------------------------------------------------
83 Add a table object. Returns the new SQL::Translator::Schema::Table object.
84 The "name" parameter is required. If you try to create a table with the
85 same name as an existing table, you will get an error and the table will
88 my $table_foo = $schema->add_table( name => 'foo' ) or die $schema->error;
90 my $table_bar = SQL::Translator::Schema::Table->new( name => 'bar' );
91 $table_bar = $schema->add_table( $table_bar ) or die $schema->error;
96 my $table_class = 'SQL::Translator::Schema::Table';
99 if ( UNIVERSAL::isa( $_[0], $table_class ) ) {
101 $table->schema( $self );
105 $args{'schema'} = $self;
106 $table = $table_class->new( \%args ) or return
107 $self->error( $table_class->error );
110 $table->order( ++$TABLE_ORDER );
111 my $table_name = $table->name or return $self->error('No table name');
113 if ( defined $self->{'tables'}{ $table_name } ) {
114 return $self->error(qq[Can't create table: "$table_name" exists]);
117 $self->{'tables'}{ $table_name } = $table;
123 # ----------------------------------------------------------------------
130 Add a view object. Returns the new SQL::Translator::Schema::View object.
131 The "name" parameter is required. If you try to create a view with the
132 same name as an existing view, you will get an error and the view will
135 my $view_foo = $schema->add_view( name => 'foo' );
137 my $view_bar = SQL::Translator::Schema::View->new( name => 'bar' );
138 $view_bar = $schema->add_view( $view_bar ) or die $schema->error;
143 my $view_class = 'SQL::Translator::Schema::View';
146 if ( UNIVERSAL::isa( $_[0], $view_class ) ) {
151 return $self->error('No view name') unless $args{'name'};
152 $view = $view_class->new( \%args ) or return $view_class->error;
155 $view->order( ++$VIEW_ORDER );
156 my $view_name = $view->name or return $self->error('No view name');
158 if ( defined $self->{'views'}{ $view_name } ) {
159 return $self->error(qq[Can't create view: "$view_name" exists]);
162 $self->{'views'}{ $view_name } = $view;
168 # ----------------------------------------------------------------------
175 Get or set the schema's database. (optional)
177 my $database = $schema->database('PostgreSQL');
182 $self->{'database'} = shift if @_;
183 return $self->{'database'} || '';
186 # ----------------------------------------------------------------------
193 Returns true if all the tables and views are valid.
195 my $ok = $schema->is_valid or die $schema->error;
201 return $self->error('No tables') unless $self->get_tables;
203 for my $object ( $self->get_tables, $self->get_views ) {
204 return $object->error unless $object->is_valid;
210 # ----------------------------------------------------------------------
217 Returns a table by the name provided.
219 my $table = $schema->get_table('foo');
224 my $table_name = shift or return $self->error('No table name');
225 return $self->error( qq[Table "$table_name" does not exist] ) unless
226 exists $self->{'tables'}{ $table_name };
227 return $self->{'tables'}{ $table_name };
230 # ----------------------------------------------------------------------
237 Returns all the tables as an array or array reference.
239 my @tables = $schema->get_tables;
246 sort { $a->[0] <=> $b->[0] }
247 map { [ $_->order, $_ ] }
248 values %{ $self->{'tables'} };
251 return wantarray ? @tables : \@tables;
254 $self->error('No tables');
255 return wantarray ? () : undef;
259 # ----------------------------------------------------------------------
266 Returns a view by the name provided.
268 my $view = $schema->get_view('foo');
273 my $view_name = shift or return $self->error('No view name');
274 return $self->error('View "$view_name" does not exist') unless
275 exists $self->{'views'}{ $view_name };
276 return $self->{'views'}{ $view_name };
279 # ----------------------------------------------------------------------
286 Returns all the views as an array or array reference.
288 my @views = $schema->get_views;
295 sort { $a->[0] <=> $b->[0] }
296 map { [ $_->order, $_ ] }
297 values %{ $self->{'views'} };
300 return wantarray ? @views : \@views;
303 $self->error('No views');
304 return wantarray ? () : undef;
308 # ----------------------------------------------------------------------
315 Get or set the schema's name. (optional)
317 my $schema_name = $schema->name('Foo Database');
322 $self->{'name'} = shift if @_;
323 return $self->{'name'} || '';
326 # ----------------------------------------------------------------------
329 undef $_ for values %{ $self->{'tables'} };
330 undef $_ for values %{ $self->{'views'} };
335 # ----------------------------------------------------------------------
341 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>