Commit | Line | Data |
3c5de62a |
1 | package SQL::Translator::Schema; |
2 | |
3 | # ---------------------------------------------------------------------- |
99248301 |
4 | # $Id: Schema.pm,v 1.3 2003-05-05 04:33:22 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 | |
27 | SQL::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 | |
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'; |
76dce619 |
51 | use vars qw[ $VERSION $TABLE_ORDER $VIEW_ORDER ]; |
3c5de62a |
52 | |
53 | $VERSION = 1.00; |
54 | |
55 | # ---------------------------------------------------------------------- |
56 | sub init { |
57 | |
58 | =pod |
59 | |
60 | =head2 new |
61 | |
62 | Object 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 |
77 | sub add_table { |
3c5de62a |
78 | |
79 | =pod |
80 | |
76dce619 |
81 | =head2 add_table |
3c5de62a |
82 | |
76dce619 |
83 | Add a table object. Returns the new SQL::Translator::Schema::Table object. |
99248301 |
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 |
86 | not be created. |
3c5de62a |
87 | |
99248301 |
88 | my $table_foo = $schema->add_table( name => 'foo' ) or die $schema->error; |
89 | |
90 | my $table_bar = SQL::Translator::Schema::Table->new( name => 'bar' ); |
91 | $table_bar = $schema->add_table( $table_bar ) or die $schema->error; |
3c5de62a |
92 | |
93 | =cut |
94 | |
99248301 |
95 | my $self = shift; |
96 | my $table_class = 'SQL::Translator::Schema::Table'; |
97 | my $table; |
98 | |
99 | if ( UNIVERSAL::isa( $_[0], $table_class ) ) { |
100 | $table = shift; |
101 | $table->schema( $self ); |
102 | } |
103 | else { |
104 | my %args = @_; |
105 | $args{'schema'} = $self; |
106 | $table = $table_class->new( \%args ) or return |
107 | $self->error( $table_class->error ); |
108 | } |
3c5de62a |
109 | |
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; |
117 | $self->{'tables'}{ $table_name }{'order'} = ++$TABLE_ORDER; |
118 | } |
3c5de62a |
119 | |
120 | return $table; |
121 | } |
122 | |
123 | # ---------------------------------------------------------------------- |
76dce619 |
124 | sub add_view { |
3c5de62a |
125 | |
126 | =pod |
127 | |
76dce619 |
128 | =head2 add_view |
3c5de62a |
129 | |
76dce619 |
130 | Add a view object. Returns the new SQL::Translator::Schema::View object. |
99248301 |
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 |
133 | not be created. |
134 | |
135 | my $view_foo = $schema->add_view( name => 'foo' ); |
3c5de62a |
136 | |
99248301 |
137 | my $view_bar = SQL::Translator::Schema::View->new( name => 'bar' ); |
138 | $view_bar = $schema->add_view( $view_bar ) or die $schema->error; |
3c5de62a |
139 | |
140 | =cut |
141 | |
99248301 |
142 | my $self = shift; |
143 | my $view_class = 'SQL::Translator::Schema::View'; |
144 | my $view; |
145 | |
146 | if ( UNIVERSAL::isa( $_[0], $view_class ) ) { |
147 | $view = shift; |
148 | } |
149 | else { |
150 | my %args = @_; |
151 | return $self->error('No view name') unless $args{'name'}; |
152 | $view = $view_class->new( \%args ) or return $view_class->error; |
153 | } |
3c5de62a |
154 | |
99248301 |
155 | my $view_name = $view->name or return $self->error('No view name'); |
156 | |
157 | if ( defined $self->{'views'}{ $view_name } ) { |
158 | return $self->error(qq[Can't create view: "$view_name" exists]); |
159 | } |
160 | else { |
161 | $self->{'views'}{ $view_name } = $view; |
162 | $self->{'views'}{ $view_name }{'order'} = ++$VIEW_ORDER; |
163 | } |
3c5de62a |
164 | |
76dce619 |
165 | return $view; |
3c5de62a |
166 | } |
167 | |
168 | # ---------------------------------------------------------------------- |
99248301 |
169 | sub database { |
170 | |
171 | =pod |
172 | |
173 | =head2 database |
174 | |
175 | Get or set the schema's database. (optional) |
176 | |
177 | my $database = $schema->database('PostgreSQL'); |
178 | |
179 | =cut |
180 | |
181 | my $self = shift; |
182 | $self->{'database'} = shift if @_; |
183 | return $self->{'database'} || ''; |
184 | } |
185 | |
186 | # ---------------------------------------------------------------------- |
76dce619 |
187 | sub is_valid { |
3c5de62a |
188 | |
189 | =pod |
190 | |
76dce619 |
191 | =head2 is_valid |
3c5de62a |
192 | |
76dce619 |
193 | Returns true if all the tables and views are valid. |
3c5de62a |
194 | |
76dce619 |
195 | my $ok = $schema->is_valid or die $schema->error; |
196 | |
197 | =cut |
198 | |
199 | my $self = shift; |
200 | |
201 | return $self->error('No tables') unless $self->get_tables; |
202 | |
203 | for my $object ( $self->get_tables, $self->get_views ) { |
204 | return $object->error unless $object->is_valid; |
205 | } |
206 | |
207 | return 1; |
208 | } |
209 | |
210 | # ---------------------------------------------------------------------- |
211 | sub get_table { |
212 | |
213 | =pod |
214 | |
215 | =head2 get_table |
216 | |
217 | Returns a table by the name provided. |
218 | |
219 | my $table = $schema->get_table('foo'); |
220 | |
221 | =cut |
222 | |
223 | my $self = shift; |
224 | my $table_name = shift or return $self->error('No table name'); |
99248301 |
225 | return $self->error( qq[Table "$table_name" does not exist] ) unless |
76dce619 |
226 | exists $self->{'tables'}{ $table_name }; |
227 | return $self->{'tables'}{ $table_name }; |
228 | } |
229 | |
230 | # ---------------------------------------------------------------------- |
231 | sub get_tables { |
232 | |
233 | =pod |
234 | |
235 | =head2 get_tables |
236 | |
237 | Returns all the tables as an array or array reference. |
238 | |
239 | my @tables = $schema->get_tables; |
240 | |
241 | =cut |
242 | |
243 | my $self = shift; |
99248301 |
244 | my @tables = sort { $a->{'order'} <=> $b->{'order'} } |
76dce619 |
245 | values %{ $self->{'tables'} }; |
246 | |
247 | if ( @tables ) { |
248 | return wantarray ? @tables : \@tables; |
249 | } |
250 | else { |
251 | $self->error('No tables'); |
252 | return wantarray ? () : undef; |
253 | } |
254 | } |
255 | |
256 | # ---------------------------------------------------------------------- |
257 | sub get_view { |
258 | |
259 | =pod |
260 | |
261 | =head2 get_view |
262 | |
263 | Returns a view by the name provided. |
264 | |
265 | my $view = $schema->get_view('foo'); |
3c5de62a |
266 | |
267 | =cut |
268 | |
269 | my $self = shift; |
76dce619 |
270 | my $view_name = shift or return $self->error('No view name'); |
271 | return $self->error('View "$view_name" does not exist') unless |
272 | exists $self->{'views'}{ $view_name }; |
273 | return $self->{'views'}{ $view_name }; |
274 | } |
3c5de62a |
275 | |
76dce619 |
276 | # ---------------------------------------------------------------------- |
277 | sub get_views { |
3c5de62a |
278 | |
76dce619 |
279 | =pod |
280 | |
281 | =head2 get_views |
282 | |
283 | Returns all the views as an array or array reference. |
284 | |
285 | my @views = $schema->get_views; |
286 | |
287 | =cut |
288 | |
289 | my $self = shift; |
99248301 |
290 | my @views = sort { $a->{'order'} <=> $b->{'order'} } |
291 | values %{ $self->{'views'} }; |
76dce619 |
292 | |
293 | if ( @views ) { |
294 | return wantarray ? @views : \@views; |
295 | } |
296 | else { |
297 | $self->error('No views'); |
298 | return wantarray ? () : undef; |
299 | } |
3c5de62a |
300 | } |
301 | |
99248301 |
302 | # ---------------------------------------------------------------------- |
303 | sub name { |
304 | |
305 | =pod |
306 | |
307 | =head2 name |
308 | |
309 | Get or set the schema's name. (optional) |
310 | |
311 | my $schema_name = $schema->name('Foo Database'); |
312 | |
313 | =cut |
314 | |
315 | my $self = shift; |
316 | $self->{'name'} = shift if @_; |
317 | return $self->{'name'} || ''; |
318 | } |
319 | |
3c5de62a |
320 | 1; |
321 | |
322 | # ---------------------------------------------------------------------- |
323 | |
324 | =pod |
325 | |
326 | =head1 AUTHOR |
327 | |
328 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt> |
329 | |
330 | =cut |