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.
28 use SQL::Translator::Utils 'parse_list_arg';
30 use base 'SQL::Translator::Schema::Object';
32 our ( $TABLE_COUNT, $VIEW_COUNT );
34 our $VERSION = '1.59';
36 __PACKAGE__->_attributes( qw/
37 name sql fields schema order
46 my $view = SQL::Translator::Schema::View->new;
56 Gets and set the fields the constraint is on. Accepts a string, list or
57 arrayref; returns an array or array reference. Will unique the field
58 names and keep them in order by the first occurrence of a field name.
61 $view->fields('id', 'name');
62 $view->fields( 'id, name' );
63 $view->fields( [ 'id', 'name' ] );
64 $view->fields( qw[ id name ] );
66 my @fields = $view->fields;
71 my $fields = parse_list_arg( @_ );
74 my ( %unique, @unique );
75 for my $f ( @$fields ) {
76 next if $unique{ $f };
81 $self->{'fields'} = \@unique;
84 return wantarray ? @{ $self->{'fields'} || [] } : ($self->{'fields'} || '');
93 Determine whether the view is valid or not.
95 my $ok = $view->is_valid;
101 return $self->error('No name') unless $self->name;
102 return $self->error('No sql') unless $self->sql;
113 Get or set the view's name.
115 my $name = $view->name('foo');
120 $self->{'name'} = shift if @_;
121 return $self->{'name'} || '';
130 Get or set the view's order.
132 my $order = $view->order(3);
136 my ( $self, $arg ) = @_;
138 if ( defined $arg && $arg =~ /^\d+$/ ) {
139 $self->{'order'} = $arg;
142 return $self->{'order'} || 0;
151 Get or set the view's SQL.
153 my $sql = $view->sql('select * from foo');
158 $self->{'sql'} = shift if @_;
159 return $self->{'sql'} || '';
168 Get or set the view's schema object.
170 $view->schema( $schema );
171 my $schema = $view->schema;
176 if ( my $arg = shift ) {
177 return $self->error('Not a schema object') unless
178 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
179 $self->{'schema'} = $arg;
182 return $self->{'schema'};
191 Determines if this view is the same as another
193 my $isIdentical = $view1->equals( $view2 );
199 my $case_insensitive = shift;
200 my $ignore_sql = shift;
202 return 0 unless $self->SUPER::equals($other);
203 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
204 #return 0 unless $self->is_valid eq $other->is_valid;
206 unless ($ignore_sql) {
207 my $selfSql = $self->sql;
208 my $otherSql = $other->sql;
210 $selfSql =~ s/--.*$//mg;
211 $otherSql =~ s/--.*$//mg;
212 # Collapse whitespace to space to avoid whitespace comparison issues
213 $selfSql =~ s/\s+/ /sg;
214 $otherSql =~ s/\s+/ /sg;
215 return 0 unless $selfSql eq $otherSql;
218 my $selfFields = join(":", $self->fields);
219 my $otherFields = join(":", $other->fields);
220 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
221 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
227 undef $self->{'schema'}; # destroy cyclical reference
236 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.