1 package SQL::Translator::Schema::View;
3 # ----------------------------------------------------------------------
4 # $Id: View.pm,v 1.4 2003-06-27 16:47:40 kycl4rk Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
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(
34 sql => 'select * from foo',
39 C<SQL::Translator::Schema::View> is the view object.
47 use SQL::Translator::Utils 'parse_list_arg';
49 use base 'Class::Base';
50 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
52 $VERSION = sprintf "%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/;
54 # ----------------------------------------------------------------------
63 my $schema = SQL::Translator::Schema::View->new;
67 my ( $self, $config ) = @_;
69 for my $arg ( qw[ name sql fields ] ) {
70 next unless $config->{ $arg };
71 $self->$arg( $config->{ $arg } ) or return;
77 # ----------------------------------------------------------------------
84 Gets and set the fields the constraint is on. Accepts a string, list or
85 arrayref; returns an array or array reference. Will unique the field
86 names and keep them in order by the first occurrence of a field name.
89 $view->fields('id', 'name');
90 $view->fields( 'id, name' );
91 $view->fields( [ 'id', 'name' ] );
92 $view->fields( qw[ id name ] );
94 my @fields = $view->fields;
99 my $fields = parse_list_arg( @_ );
102 my ( %unique, @unique );
103 for my $f ( @$fields ) {
104 next if $unique{ $f };
109 $self->{'fields'} = \@unique;
112 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
115 # ----------------------------------------------------------------------
122 Determine whether the view is valid or not.
124 my $ok = $view->is_valid;
130 return $self->error('No name') unless $self->name;
131 return $self->error('No sql') unless $self->sql;
136 # ----------------------------------------------------------------------
143 Get or set the view's name.
145 my $name = $view->name('foo');
150 $self->{'name'} = shift if @_;
151 return $self->{'name'} || '';
154 # ----------------------------------------------------------------------
161 Get or set the view's order.
163 my $order = $view->order(3);
167 my ( $self, $arg ) = @_;
169 if ( defined $arg && $arg =~ /^\d+$/ ) {
170 $self->{'order'} = $arg;
173 return $self->{'order'} || 0;
176 # ----------------------------------------------------------------------
183 Get or set the view's SQL.
185 my $sql = $view->sql('select * from foo');
190 $self->{'sql'} = shift if @_;
191 return $self->{'sql'} || '';
194 # ----------------------------------------------------------------------
197 undef $self->{'table'}; # destroy cyclical reference
202 # ----------------------------------------------------------------------
208 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>