1 package SQL::Translator::Schema::View;
3 # ----------------------------------------------------------------------
4 # $Id: View.pm,v 1.9 2004-11-05 13:19:31 grommit Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2002-4 SQLFairy Authors
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.
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.
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
21 # -------------------------------------------------------------------
27 SQL::Translator::Schema::View - SQL::Translator view object
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
40 C<SQL::Translator::Schema::View> is the view object.
47 use SQL::Translator::Utils 'parse_list_arg';
49 use base 'SQL::Translator::Schema::Object';
51 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
53 $VERSION = sprintf "%d.%02d", q$Revision: 1.9 $ =~ /(\d+)\.(\d+)/;
55 # ----------------------------------------------------------------------
57 __PACKAGE__->_attributes( qw/
58 name sql fields schema order
67 my $view = SQL::Translator::Schema::View->new;
71 # ----------------------------------------------------------------------
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.
83 $view->fields('id', 'name');
84 $view->fields( 'id, name' );
85 $view->fields( [ 'id', 'name' ] );
86 $view->fields( qw[ id name ] );
88 my @fields = $view->fields;
93 my $fields = parse_list_arg( @_ );
96 my ( %unique, @unique );
97 for my $f ( @$fields ) {
98 next if $unique{ $f };
103 $self->{'fields'} = \@unique;
106 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
109 # ----------------------------------------------------------------------
116 Determine whether the view is valid or not.
118 my $ok = $view->is_valid;
124 return $self->error('No name') unless $self->name;
125 return $self->error('No sql') unless $self->sql;
130 # ----------------------------------------------------------------------
137 Get or set the view's name.
139 my $name = $view->name('foo');
144 $self->{'name'} = shift if @_;
145 return $self->{'name'} || '';
148 # ----------------------------------------------------------------------
155 Get or set the view's order.
157 my $order = $view->order(3);
161 my ( $self, $arg ) = @_;
163 if ( defined $arg && $arg =~ /^\d+$/ ) {
164 $self->{'order'} = $arg;
167 return $self->{'order'} || 0;
170 # ----------------------------------------------------------------------
177 Get or set the view's SQL.
179 my $sql = $view->sql('select * from foo');
184 $self->{'sql'} = shift if @_;
185 return $self->{'sql'} || '';
188 # ----------------------------------------------------------------------
195 Get or set the view's schema object.
197 $view->schema( $schema );
198 my $schema = $view->schema;
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;
209 return $self->{'schema'};
212 # ----------------------------------------------------------------------
215 undef $self->{'schema'}; # destroy cyclical reference
220 # ----------------------------------------------------------------------
226 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.