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 __PACKAGE__->_attributes( qw/
54 name sql fields schema order
63 my $view = SQL::Translator::Schema::View->new;
73 Gets and set the fields the constraint is on. Accepts a string, list or
74 arrayref; returns an array or array reference. Will unique the field
75 names and keep them in order by the first occurrence of a field name.
78 $view->fields('id', 'name');
79 $view->fields( 'id, name' );
80 $view->fields( [ 'id', 'name' ] );
81 $view->fields( qw[ id name ] );
83 my @fields = $view->fields;
88 my $fields = parse_list_arg( @_ );
91 my ( %unique, @unique );
92 for my $f ( @$fields ) {
93 next if $unique{ $f };
98 $self->{'fields'} = \@unique;
101 return wantarray ? @{ $self->{'fields'} || [] } : ($self->{'fields'} || '');
110 Determine whether the view is valid or not.
112 my $ok = $view->is_valid;
118 return $self->error('No name') unless $self->name;
119 return $self->error('No sql') unless $self->sql;
130 Get or set the view's name.
132 my $name = $view->name('foo');
137 $self->{'name'} = shift if @_;
138 return $self->{'name'} || '';
147 Get or set the view's order.
149 my $order = $view->order(3);
153 my ( $self, $arg ) = @_;
155 if ( defined $arg && $arg =~ /^\d+$/ ) {
156 $self->{'order'} = $arg;
159 return $self->{'order'} || 0;
168 Get or set the view's SQL.
170 my $sql = $view->sql('select * from foo');
175 $self->{'sql'} = shift if @_;
176 return $self->{'sql'} || '';
185 Get or set the view's schema object.
187 $view->schema( $schema );
188 my $schema = $view->schema;
193 if ( my $arg = shift ) {
194 return $self->error('Not a schema object') unless
195 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
196 $self->{'schema'} = $arg;
199 return $self->{'schema'};
208 Determines if this view is the same as another
210 my $isIdentical = $view1->equals( $view2 );
216 my $case_insensitive = shift;
217 my $ignore_sql = shift;
219 return 0 unless $self->SUPER::equals($other);
220 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
221 #return 0 unless $self->is_valid eq $other->is_valid;
223 unless ($ignore_sql) {
224 my $selfSql = $self->sql;
225 my $otherSql = $other->sql;
227 $selfSql =~ s/--.*$//mg;
228 $otherSql =~ s/--.*$//mg;
229 # Collapse whitespace to space to avoid whitespace comparison issues
230 $selfSql =~ s/\s+/ /sg;
231 $otherSql =~ s/\s+/ /sg;
232 return 0 unless $selfSql eq $otherSql;
235 my $selfFields = join(":", $self->fields);
236 my $otherFields = join(":", $other->fields);
237 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
238 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
244 undef $self->{'schema'}; # destroy cyclical reference
253 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.