1 package SQL::Translator::Schema::View;
7 SQL::Translator::Schema::View - SQL::Translator view object
11 use SQL::Translator::Schema::View;
12 my $view = SQL::Translator::Schema::View->new(
13 name => 'foo', # name, required
14 sql => 'select id, name from foo', # SQL for view
15 fields => 'id, name', # field names in view
20 C<SQL::Translator::Schema::View> is the view object.
27 use SQL::Translator::Utils 'parse_list_arg';
29 use base 'SQL::Translator::Schema::Object';
31 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
35 __PACKAGE__->_attributes( qw/
36 name sql fields schema order
45 my $view = SQL::Translator::Schema::View->new;
55 Gets and set the fields the constraint is on. Accepts a string, list or
56 arrayref; returns an array or array reference. Will unique the field
57 names and keep them in order by the first occurrence of a field name.
60 $view->fields('id', 'name');
61 $view->fields( 'id, name' );
62 $view->fields( [ 'id', 'name' ] );
63 $view->fields( qw[ id name ] );
65 my @fields = $view->fields;
70 my $fields = parse_list_arg( @_ );
73 my ( %unique, @unique );
74 for my $f ( @$fields ) {
75 next if $unique{ $f };
80 $self->{'fields'} = \@unique;
83 return wantarray ? @{ $self->{'fields'} || [] } : ($self->{'fields'} || '');
92 Determine whether the view is valid or not.
94 my $ok = $view->is_valid;
100 return $self->error('No name') unless $self->name;
101 return $self->error('No sql') unless $self->sql;
112 Get or set the view's name.
114 my $name = $view->name('foo');
119 $self->{'name'} = shift if @_;
120 return $self->{'name'} || '';
129 Get or set the view's order.
131 my $order = $view->order(3);
135 my ( $self, $arg ) = @_;
137 if ( defined $arg && $arg =~ /^\d+$/ ) {
138 $self->{'order'} = $arg;
141 return $self->{'order'} || 0;
150 Get or set the view's SQL.
152 my $sql = $view->sql('select * from foo');
157 $self->{'sql'} = shift if @_;
158 return $self->{'sql'} || '';
167 Get or set the view's schema object.
169 $view->schema( $schema );
170 my $schema = $view->schema;
175 if ( my $arg = shift ) {
176 return $self->error('Not a schema object') unless
177 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
178 $self->{'schema'} = $arg;
181 return $self->{'schema'};
190 Determines if this view is the same as another
192 my $isIdentical = $view1->equals( $view2 );
198 my $case_insensitive = shift;
199 my $ignore_sql = shift;
201 return 0 unless $self->SUPER::equals($other);
202 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
203 #return 0 unless $self->is_valid eq $other->is_valid;
205 unless ($ignore_sql) {
206 my $selfSql = $self->sql;
207 my $otherSql = $other->sql;
209 $selfSql =~ s/--.*$//mg;
210 $otherSql =~ s/--.*$//mg;
211 # Collapse whitespace to space to avoid whitespace comparison issues
212 $selfSql =~ s/\s+/ /sg;
213 $otherSql =~ s/\s+/ /sg;
214 return 0 unless $selfSql eq $otherSql;
217 my $selfFields = join(":", $self->fields);
218 my $otherFields = join(":", $other->fields);
219 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
220 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
226 undef $self->{'schema'}; # destroy cyclical reference
235 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.