1 package SQL::Translator::Schema::View;
3 # ----------------------------------------------------------------------
4 # $Id: View.pm,v 1.7 2004-02-09 22:15:15 kycl4rk 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.
48 use SQL::Translator::Utils 'parse_list_arg';
50 use base 'Class::Base';
51 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
53 $VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/;
55 # ----------------------------------------------------------------------
64 my $view = SQL::Translator::Schema::View->new;
68 my ( $self, $config ) = @_;
70 for my $arg ( qw[ name sql fields schema ] ) {
71 next unless $config->{ $arg };
72 $self->$arg( $config->{ $arg } ) or return;
78 # ----------------------------------------------------------------------
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.
90 $view->fields('id', 'name');
91 $view->fields( 'id, name' );
92 $view->fields( [ 'id', 'name' ] );
93 $view->fields( qw[ id name ] );
95 my @fields = $view->fields;
100 my $fields = parse_list_arg( @_ );
103 my ( %unique, @unique );
104 for my $f ( @$fields ) {
105 next if $unique{ $f };
110 $self->{'fields'} = \@unique;
113 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
116 # ----------------------------------------------------------------------
123 Determine whether the view is valid or not.
125 my $ok = $view->is_valid;
131 return $self->error('No name') unless $self->name;
132 return $self->error('No sql') unless $self->sql;
137 # ----------------------------------------------------------------------
144 Get or set the view's name.
146 my $name = $view->name('foo');
151 $self->{'name'} = shift if @_;
152 return $self->{'name'} || '';
155 # ----------------------------------------------------------------------
162 Get or set the view's order.
164 my $order = $view->order(3);
168 my ( $self, $arg ) = @_;
170 if ( defined $arg && $arg =~ /^\d+$/ ) {
171 $self->{'order'} = $arg;
174 return $self->{'order'} || 0;
177 # ----------------------------------------------------------------------
184 Get or set the view's SQL.
186 my $sql = $view->sql('select * from foo');
191 $self->{'sql'} = shift if @_;
192 return $self->{'sql'} || '';
195 # ----------------------------------------------------------------------
202 Get or set the view's schema object.
204 $view->schema( $schema );
205 my $schema = $view->schema;
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;
216 return $self->{'schema'};
219 # ----------------------------------------------------------------------
222 undef $self->{'schema'}; # destroy cyclical reference
227 # ----------------------------------------------------------------------
233 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.