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