Commit | Line | Data |
3c5de62a |
1 | package SQL::Translator::Schema::View; |
2 | |
3 | # ---------------------------------------------------------------------- |
6606c4c6 |
4 | # $Id: View.pm,v 1.7 2004-02-09 22:15:15 kycl4rk Exp $ |
3c5de62a |
5 | # ---------------------------------------------------------------------- |
6606c4c6 |
6 | # Copyright (C) 2002-4 SQLFairy Authors |
3c5de62a |
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::View - SQL::Translator view object |
28 | |
29 | =head1 SYNOPSIS |
30 | |
31 | use SQL::Translator::Schema::View; |
b039cab8 |
32 | my $view = SQL::Translator::Schema::View->new( |
33 | name => 'foo', # name, required |
34 | sql => 'select id, name from foo', # SQL for view |
35 | fields => 'id, name', # field names in view |
3c5de62a |
36 | ); |
37 | |
38 | =head1 DESCRIPTION |
39 | |
40 | C<SQL::Translator::Schema::View> is the view object. |
41 | |
42 | =head1 METHODS |
43 | |
44 | =cut |
45 | |
46 | use strict; |
47 | use Class::Base; |
25a02fa7 |
48 | use SQL::Translator::Utils 'parse_list_arg'; |
3c5de62a |
49 | |
50 | use base 'Class::Base'; |
51 | use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); |
52 | |
6606c4c6 |
53 | $VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/; |
3c5de62a |
54 | |
55 | # ---------------------------------------------------------------------- |
56 | sub init { |
57 | |
58 | =pod |
59 | |
60 | =head2 new |
61 | |
62 | Object constructor. |
63 | |
b039cab8 |
64 | my $view = SQL::Translator::Schema::View->new; |
3c5de62a |
65 | |
66 | =cut |
67 | |
68 | my ( $self, $config ) = @_; |
25a02fa7 |
69 | |
b039cab8 |
70 | for my $arg ( qw[ name sql fields schema ] ) { |
25a02fa7 |
71 | next unless $config->{ $arg }; |
72 | $self->$arg( $config->{ $arg } ) or return; |
73 | } |
74 | |
3c5de62a |
75 | return $self; |
76 | } |
77 | |
78 | # ---------------------------------------------------------------------- |
25a02fa7 |
79 | sub fields { |
80 | |
81 | =pod |
82 | |
83 | =head2 fields |
84 | |
85 | Gets and set the fields the constraint is on. Accepts a string, list or |
86 | arrayref; returns an array or array reference. Will unique the field |
87 | names and keep them in order by the first occurrence of a field name. |
88 | |
89 | $view->fields('id'); |
90 | $view->fields('id', 'name'); |
91 | $view->fields( 'id, name' ); |
92 | $view->fields( [ 'id', 'name' ] ); |
93 | $view->fields( qw[ id name ] ); |
94 | |
95 | my @fields = $view->fields; |
96 | |
97 | =cut |
98 | |
99 | my $self = shift; |
100 | my $fields = parse_list_arg( @_ ); |
101 | |
102 | if ( @$fields ) { |
103 | my ( %unique, @unique ); |
104 | for my $f ( @$fields ) { |
105 | next if $unique{ $f }; |
106 | $unique{ $f } = 1; |
107 | push @unique, $f; |
108 | } |
109 | |
110 | $self->{'fields'} = \@unique; |
111 | } |
112 | |
113 | return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'}; |
114 | } |
115 | |
116 | # ---------------------------------------------------------------------- |
117 | sub is_valid { |
118 | |
119 | =pod |
120 | |
121 | =head2 is_valid |
122 | |
123 | Determine whether the view is valid or not. |
124 | |
125 | my $ok = $view->is_valid; |
126 | |
127 | =cut |
128 | |
129 | my $self = shift; |
130 | |
131 | return $self->error('No name') unless $self->name; |
132 | return $self->error('No sql') unless $self->sql; |
133 | |
134 | return 1; |
135 | } |
136 | |
137 | # ---------------------------------------------------------------------- |
3c5de62a |
138 | sub name { |
139 | |
140 | =pod |
141 | |
142 | =head2 name |
143 | |
144 | Get or set the view's name. |
145 | |
146 | my $name = $view->name('foo'); |
147 | |
148 | =cut |
149 | |
150 | my $self = shift; |
151 | $self->{'name'} = shift if @_; |
152 | return $self->{'name'} || ''; |
153 | } |
154 | |
155 | # ---------------------------------------------------------------------- |
25a02fa7 |
156 | sub order { |
3c5de62a |
157 | |
158 | =pod |
159 | |
25a02fa7 |
160 | =head2 order |
3c5de62a |
161 | |
25a02fa7 |
162 | Get or set the view's order. |
3c5de62a |
163 | |
25a02fa7 |
164 | my $order = $view->order(3); |
3c5de62a |
165 | |
166 | =cut |
167 | |
25a02fa7 |
168 | my ( $self, $arg ) = @_; |
169 | |
170 | if ( defined $arg && $arg =~ /^\d+$/ ) { |
171 | $self->{'order'} = $arg; |
172 | } |
173 | |
174 | return $self->{'order'} || 0; |
3c5de62a |
175 | } |
176 | |
177 | # ---------------------------------------------------------------------- |
25a02fa7 |
178 | sub sql { |
3c5de62a |
179 | |
180 | =pod |
181 | |
25a02fa7 |
182 | =head2 sql |
3c5de62a |
183 | |
25a02fa7 |
184 | Get or set the view's SQL. |
3c5de62a |
185 | |
25a02fa7 |
186 | my $sql = $view->sql('select * from foo'); |
3c5de62a |
187 | |
188 | =cut |
189 | |
25a02fa7 |
190 | my $self = shift; |
191 | $self->{'sql'} = shift if @_; |
192 | return $self->{'sql'} || ''; |
193 | } |
194 | |
195 | # ---------------------------------------------------------------------- |
b039cab8 |
196 | sub schema { |
197 | |
198 | =pod |
199 | |
200 | =head2 schema |
201 | |
202 | Get or set the view's schema object. |
203 | |
204 | $view->schema( $schema ); |
205 | my $schema = $view->schema; |
206 | |
207 | =cut |
208 | |
209 | my $self = shift; |
210 | if ( my $arg = shift ) { |
211 | return $self->error('Not a schema object') unless |
212 | UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' ); |
213 | $self->{'schema'} = $arg; |
214 | } |
215 | |
216 | return $self->{'schema'}; |
217 | } |
218 | |
b039cab8 |
219 | # ---------------------------------------------------------------------- |
25a02fa7 |
220 | sub DESTROY { |
3c5de62a |
221 | my $self = shift; |
b039cab8 |
222 | undef $self->{'schema'}; # destroy cyclical reference |
3c5de62a |
223 | } |
224 | |
225 | 1; |
226 | |
227 | # ---------------------------------------------------------------------- |
228 | |
229 | =pod |
230 | |
231 | =head1 AUTHOR |
232 | |
6606c4c6 |
233 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. |
3c5de62a |
234 | |
235 | =cut |