1 package SQL::Translator::Schema::View;
3 # ----------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
25 SQL::Translator::Schema::View - SQL::Translator view object
29 use SQL::Translator::Schema::View;
30 my $view = SQL::Translator::Schema::View->new(
31 name => 'foo', # name, required
32 sql => 'select id, name from foo', # SQL for view
33 fields => 'id, name', # field names in view
38 C<SQL::Translator::Schema::View> is the view object.
45 use SQL::Translator::Utils 'parse_list_arg';
47 use base 'SQL::Translator::Schema::Object';
49 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
53 # ----------------------------------------------------------------------
55 __PACKAGE__->_attributes( qw/
56 name sql fields schema order
65 my $view = SQL::Translator::Schema::View->new;
69 # ----------------------------------------------------------------------
76 Gets and set the fields the constraint is on. Accepts a string, list or
77 arrayref; returns an array or array reference. Will unique the field
78 names and keep them in order by the first occurrence of a field name.
81 $view->fields('id', 'name');
82 $view->fields( 'id, name' );
83 $view->fields( [ 'id', 'name' ] );
84 $view->fields( qw[ id name ] );
86 my @fields = $view->fields;
91 my $fields = parse_list_arg( @_ );
94 my ( %unique, @unique );
95 for my $f ( @$fields ) {
96 next if $unique{ $f };
101 $self->{'fields'} = \@unique;
104 return wantarray ? @{ $self->{'fields'} || [] } : ($self->{'fields'} || '');
107 # ----------------------------------------------------------------------
114 Determine whether the view is valid or not.
116 my $ok = $view->is_valid;
122 return $self->error('No name') unless $self->name;
123 return $self->error('No sql') unless $self->sql;
128 # ----------------------------------------------------------------------
135 Get or set the view's name.
137 my $name = $view->name('foo');
142 $self->{'name'} = shift if @_;
143 return $self->{'name'} || '';
146 # ----------------------------------------------------------------------
153 Get or set the view's order.
155 my $order = $view->order(3);
159 my ( $self, $arg ) = @_;
161 if ( defined $arg && $arg =~ /^\d+$/ ) {
162 $self->{'order'} = $arg;
165 return $self->{'order'} || 0;
168 # ----------------------------------------------------------------------
175 Get or set the view's SQL.
177 my $sql = $view->sql('select * from foo');
182 $self->{'sql'} = shift if @_;
183 return $self->{'sql'} || '';
186 # ----------------------------------------------------------------------
193 Get or set the view's schema object.
195 $view->schema( $schema );
196 my $schema = $view->schema;
201 if ( my $arg = shift ) {
202 return $self->error('Not a schema object') unless
203 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
204 $self->{'schema'} = $arg;
207 return $self->{'schema'};
210 # ----------------------------------------------------------------------
217 Determines if this view is the same as another
219 my $isIdentical = $view1->equals( $view2 );
225 my $case_insensitive = shift;
226 my $ignore_sql = shift;
228 return 0 unless $self->SUPER::equals($other);
229 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
230 #return 0 unless $self->is_valid eq $other->is_valid;
232 unless ($ignore_sql) {
233 my $selfSql = $self->sql;
234 my $otherSql = $other->sql;
236 $selfSql =~ s/--.*$//mg;
237 $otherSql =~ s/--.*$//mg;
238 # Collapse whitespace to space to avoid whitespace comparison issues
239 $selfSql =~ s/\s+/ /sg;
240 $otherSql =~ s/\s+/ /sg;
241 return 0 unless $selfSql eq $otherSql;
244 my $selfFields = join(":", $self->fields);
245 my $otherFields = join(":", $other->fields);
246 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
247 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
251 # ----------------------------------------------------------------------
254 undef $self->{'schema'}; # destroy cyclical reference
259 # ----------------------------------------------------------------------
265 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.