Commit | Line | Data |
3c5de62a |
1 | package SQL::Translator::Schema; |
2 | |
3 | # ---------------------------------------------------------------------- |
76dce619 |
4 | # $Id: Schema.pm,v 1.2 2003-05-03 04:07:38 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 | |
64 | my $schema = SQL::Translator->new; |
65 | |
66 | =cut |
67 | |
68 | my ( $self, $config ) = @_; |
69 | # empty for now |
70 | return $self; |
71 | } |
72 | |
73 | # ---------------------------------------------------------------------- |
76dce619 |
74 | sub add_table { |
3c5de62a |
75 | |
76 | =pod |
77 | |
76dce619 |
78 | =head2 add_table |
3c5de62a |
79 | |
76dce619 |
80 | Add a table object. Returns the new SQL::Translator::Schema::Table object. |
3c5de62a |
81 | |
76dce619 |
82 | my $table = $schema->add_table( name => 'foo' ); |
3c5de62a |
83 | |
84 | =cut |
85 | |
86 | my $self = shift; |
76dce619 |
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; |
3c5de62a |
91 | |
92 | $self->{'tables'}{ $table->name } = $table; |
76dce619 |
93 | $self->{'tables'}{ $table->name }{'order'} = ++$TABLE_ORDER; |
3c5de62a |
94 | |
95 | return $table; |
96 | } |
97 | |
98 | # ---------------------------------------------------------------------- |
76dce619 |
99 | sub add_view { |
3c5de62a |
100 | |
101 | =pod |
102 | |
76dce619 |
103 | =head2 add_view |
3c5de62a |
104 | |
76dce619 |
105 | Add a view object. Returns the new SQL::Translator::Schema::View object. |
3c5de62a |
106 | |
76dce619 |
107 | my $view = $schema->add_view( name => 'foo' ); |
3c5de62a |
108 | |
109 | =cut |
110 | |
76dce619 |
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; |
3c5de62a |
116 | |
76dce619 |
117 | $self->{'views'}{ $view->name } = $view; |
118 | $self->{'views'}{ $view->name }{'order'} = ++$VIEW_ORDER; |
3c5de62a |
119 | |
76dce619 |
120 | return $view; |
3c5de62a |
121 | } |
122 | |
123 | # ---------------------------------------------------------------------- |
76dce619 |
124 | sub is_valid { |
3c5de62a |
125 | |
126 | =pod |
127 | |
76dce619 |
128 | =head2 is_valid |
3c5de62a |
129 | |
76dce619 |
130 | Returns true if all the tables and views are valid. |
3c5de62a |
131 | |
76dce619 |
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'); |
3c5de62a |
204 | |
205 | =cut |
206 | |
207 | my $self = shift; |
76dce619 |
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 | } |
3c5de62a |
213 | |
76dce619 |
214 | # ---------------------------------------------------------------------- |
215 | sub get_views { |
3c5de62a |
216 | |
76dce619 |
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 | } |
3c5de62a |
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 |