1 package SQL::Translator::Schema;
3 # ----------------------------------------------------------------------
4 # $Id: Schema.pm,v 1.2 2003-05-03 04:07:38 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;
68 my ( $self, $config ) = @_;
73 # ----------------------------------------------------------------------
80 Add a table object. Returns the new SQL::Translator::Schema::Table object.
82 my $table = $schema->add_table( name => 'foo' );
88 return $self->error('No table name') unless $args{'name'};
89 my $table = SQL::Translator::Schema::Table->new( \%args ) or return
90 SQL::Translator::Schema::Table->error;
92 $self->{'tables'}{ $table->name } = $table;
93 $self->{'tables'}{ $table->name }{'order'} = ++$TABLE_ORDER;
98 # ----------------------------------------------------------------------
105 Add a view object. Returns the new SQL::Translator::Schema::View object.
107 my $view = $schema->add_view( name => 'foo' );
113 return $self->error('No view name') unless $args{'name'};
114 my $view = SQL::Translator::Schema::View->new( @_ ) or return
115 SQL::Translator::Schema::View->error;
117 $self->{'views'}{ $view->name } = $view;
118 $self->{'views'}{ $view->name }{'order'} = ++$VIEW_ORDER;
123 # ----------------------------------------------------------------------
130 Returns true if all the tables and views are valid.
132 my $ok = $schema->is_valid or die $schema->error;
138 return $self->error('No tables') unless $self->get_tables;
140 for my $object ( $self->get_tables, $self->get_views ) {
141 return $object->error unless $object->is_valid;
147 # ----------------------------------------------------------------------
154 Returns a table by the name provided.
156 my $table = $schema->get_table('foo');
161 my $table_name = shift or return $self->error('No table name');
162 return $self->error('Table "$table_name" does not exist') unless
163 exists $self->{'tables'}{ $table_name };
164 return $self->{'tables'}{ $table_name };
167 # ----------------------------------------------------------------------
174 Returns all the tables as an array or array reference.
176 my @tables = $schema->get_tables;
182 sort { $a->{'order'} <=> $b->{'order'} }
183 values %{ $self->{'tables'} };
186 return wantarray ? @tables : \@tables;
189 $self->error('No tables');
190 return wantarray ? () : undef;
194 # ----------------------------------------------------------------------
201 Returns a view by the name provided.
203 my $view = $schema->get_view('foo');
208 my $view_name = shift or return $self->error('No view name');
209 return $self->error('View "$view_name" does not exist') unless
210 exists $self->{'views'}{ $view_name };
211 return $self->{'views'}{ $view_name };
214 # ----------------------------------------------------------------------
221 Returns all the views as an array or array reference.
223 my @views = $schema->get_views;
229 sort { $a->{'order'} <=> $b->{'order'} } values %{ $self->{'views'} };
232 return wantarray ? @views : \@views;
235 $self->error('No views');
236 return wantarray ? () : undef;
242 # ----------------------------------------------------------------------
248 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>