Fixed spacing.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / View.pm
1 package SQL::Translator::Schema::View;
2
3 # ----------------------------------------------------------------------
4 # $Id: View.pm,v 1.6 2003-10-08 17:34:23 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::View - SQL::Translator view object
28
29 =head1 SYNOPSIS
30
31   use SQL::Translator::Schema::View;
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
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;
48 use SQL::Translator::Utils 'parse_list_arg';
49
50 use base 'Class::Base';
51 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
52
53 $VERSION = sprintf "%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/;
54
55 # ----------------------------------------------------------------------
56 sub init {
57
58 =pod
59
60 =head2 new
61
62 Object constructor.
63
64   my $view = SQL::Translator::Schema::View->new;
65
66 =cut
67
68     my ( $self, $config ) = @_;
69
70     for my $arg ( qw[ name sql fields schema ] ) {
71         next unless $config->{ $arg };
72         $self->$arg( $config->{ $arg } ) or return;
73     }
74
75     return $self;
76 }
77
78 # ----------------------------------------------------------------------
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 # ----------------------------------------------------------------------
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 # ----------------------------------------------------------------------
156 sub order {
157
158 =pod
159
160 =head2 order
161
162 Get or set the view's order.
163
164   my $order = $view->order(3);
165
166 =cut
167
168     my ( $self, $arg ) = @_;
169
170     if ( defined $arg && $arg =~ /^\d+$/ ) {
171         $self->{'order'} = $arg;
172     }
173
174     return $self->{'order'} || 0;
175 }
176
177 # ----------------------------------------------------------------------
178 sub sql {
179
180 =pod
181
182 =head2 sql
183
184 Get or set the view's SQL.
185
186   my $sql = $view->sql('select * from foo');
187
188 =cut
189
190     my $self       = shift;
191     $self->{'sql'} = shift if @_;
192     return $self->{'sql'} || '';
193 }
194
195 # ----------------------------------------------------------------------
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
219 # ----------------------------------------------------------------------
220 sub DESTROY {
221     my $self = shift;
222     undef $self->{'schema'}; # destroy cyclical reference
223 }
224
225 1;
226
227 # ----------------------------------------------------------------------
228
229 =pod
230
231 =head1 AUTHOR
232
233 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>
234
235 =cut