Not much to say ... just trying to get this working.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema.pm
1 package SQL::Translator::Schema;
2
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>
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 - SQL::Translator schema object
28
29 =head1 SYNOPSIS
30
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 => '...' );
35
36 =head1 DESCSIPTION
37
38 C<SQL::Translator::Schema> is the object that accepts, validates, and
39 returns the database structure.
40
41 =head1 METHODS
42
43 =cut
44
45 use strict;
46 use Class::Base;
47 use SQL::Translator::Schema::Table;
48 use SQL::Translator::Schema::View;
49
50 use base 'Class::Base';
51 use vars qw[ $VERSION $TABLE_ORDER $VIEW_ORDER ];
52
53 $VERSION = 1.00;
54
55 # ----------------------------------------------------------------------
56 sub init {
57
58 =pod
59
60 =head2 new
61
62 Object constructor.
63
64   my $schema = SQL::Translator->new;
65
66 =cut
67
68     my ( $self, $config ) = @_;
69     # empty for now
70     return $self;
71 }
72
73 # ----------------------------------------------------------------------
74 sub add_table {
75
76 =pod
77
78 =head2 add_table
79
80 Add a table object.  Returns the new SQL::Translator::Schema::Table object.
81
82   my $table = $schema->add_table( name => 'foo' );
83
84 =cut
85
86     my $self  = shift;
87     my %args  = @_;
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;
91
92     $self->{'tables'}{ $table->name } = $table;
93     $self->{'tables'}{ $table->name }{'order'} = ++$TABLE_ORDER;
94
95     return $table;
96 }
97
98 # ----------------------------------------------------------------------
99 sub add_view {
100
101 =pod
102
103 =head2 add_view
104
105 Add a view object.  Returns the new SQL::Translator::Schema::View object.
106
107   my $view = $schema->add_view( name => 'foo' );
108
109 =cut
110
111     my $self = shift;
112     my %args = @_;
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;
116
117     $self->{'views'}{ $view->name } = $view;
118     $self->{'views'}{ $view->name }{'order'} = ++$VIEW_ORDER;
119
120     return $view;
121 }
122
123 # ----------------------------------------------------------------------
124 sub is_valid {
125
126 =pod
127
128 =head2 is_valid
129
130 Returns true if all the tables and views are valid.
131
132   my $ok = $schema->is_valid or die $schema->error;
133
134 =cut
135
136     my $self = shift;
137
138     return $self->error('No tables') unless $self->get_tables;
139
140     for my $object ( $self->get_tables, $self->get_views ) {
141         return $object->error unless $object->is_valid;
142     }
143
144     return 1;
145 }
146
147 # ----------------------------------------------------------------------
148 sub get_table {
149
150 =pod
151
152 =head2 get_table
153
154 Returns a table by the name provided.
155
156   my $table = $schema->get_table('foo');
157
158 =cut
159
160     my $self       = shift;
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 };
165 }
166
167 # ----------------------------------------------------------------------
168 sub get_tables {
169
170 =pod
171
172 =head2 get_tables
173
174 Returns all the tables as an array or array reference.
175
176   my @tables = $schema->get_tables;
177
178 =cut
179
180     my $self   = shift;
181     my @tables = 
182         sort { $a->{'order'} <=> $b->{'order'} } 
183         values %{ $self->{'tables'} };
184
185     if ( @tables ) {
186         return wantarray ? @tables : \@tables;
187     }
188     else {
189         $self->error('No tables');
190         return wantarray ? () : undef;
191     }
192 }
193
194 # ----------------------------------------------------------------------
195 sub get_view {
196
197 =pod
198
199 =head2 get_view
200
201 Returns a view by the name provided.
202
203   my $view = $schema->get_view('foo');
204
205 =cut
206
207     my $self      = shift;
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 };
212 }
213
214 # ----------------------------------------------------------------------
215 sub get_views {
216
217 =pod
218
219 =head2 get_views
220
221 Returns all the views as an array or array reference.
222
223   my @views = $schema->get_views;
224
225 =cut
226
227     my $self  = shift;
228     my @views = 
229         sort { $a->{'order'} <=> $b->{'order'} } values %{ $self->{'views'} };
230
231     if ( @views ) {
232         return wantarray ? @views : \@views;
233     }
234     else {
235         $self->error('No views');
236         return wantarray ? () : undef;
237     }
238 }
239
240 1;
241
242 # ----------------------------------------------------------------------
243
244 =pod
245
246 =head1 AUTHOR
247
248 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
249
250 =cut