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 tables options
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 }++;
80 $self->{'fields'} = \@unique;
83 my @flds = @{ $self->{'fields'} || [] };
85 return wantarray ? @flds : \@flds;
94 Gets and set the tables the SELECT mentions. Accepts a string, list or
95 arrayref; returns an array or array reference. Will unique the table
96 names and keep them in order by the first occurrence of a field name.
99 $view->tables('foo', 'bar');
100 $view->tables( 'foo, bar' );
101 $view->tables( [ 'foo', 'bar' ] );
102 $view->tables( qw[ foo bar ] );
104 my @tables = $view->tables;
109 my $tables = parse_list_arg( @_ );
112 my ( %unique, @unique );
113 for my $t ( @$tables ) {
114 next if $unique{ $t }++;
118 $self->{'tables'} = \@unique;
121 my @tbls = @{ $self->{'tables'} || [] };
123 return wantarray ? @tbls : \@tbls;
132 Gets and sets a list of options on the view.
134 $view->options('ALGORITHM=UNDEFINED');
136 my @options = $view->options;
141 my $options = parse_list_arg( @_ );
144 my ( %unique, @unique );
145 for my $o ( @$options, @{ $self->{'options'} || [] } ) {
146 next if $unique{ $o }++;
150 $self->{'options'} = \@unique;
153 my @opts = @{ $self->{'options'} || [] };
155 return wantarray ? @opts : \@opts;
164 Determine whether the view is valid or not.
166 my $ok = $view->is_valid;
172 return $self->error('No name') unless $self->name;
173 return $self->error('No sql') unless $self->sql;
184 Get or set the view's name.
186 my $name = $view->name('foo');
191 $self->{'name'} = shift if @_;
192 return $self->{'name'} || '';
201 Get or set the view's order.
203 my $order = $view->order(3);
207 my ( $self, $arg ) = @_;
209 if ( defined $arg && $arg =~ /^\d+$/ ) {
210 $self->{'order'} = $arg;
213 return $self->{'order'} || 0;
222 Get or set the view's SQL.
224 my $sql = $view->sql('select * from foo');
229 $self->{'sql'} = shift if @_;
230 return $self->{'sql'} || '';
239 Get or set the view's schema object.
241 $view->schema( $schema );
242 my $schema = $view->schema;
247 if ( my $arg = shift ) {
248 return $self->error('Not a schema object') unless
249 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
250 $self->{'schema'} = $arg;
253 return $self->{'schema'};
262 Determines if this view is the same as another
264 my $isIdentical = $view1->equals( $view2 );
270 my $case_insensitive = shift;
271 my $ignore_sql = shift;
273 return 0 unless $self->SUPER::equals($other);
274 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
275 #return 0 unless $self->is_valid eq $other->is_valid;
277 unless ($ignore_sql) {
278 my $selfSql = $self->sql;
279 my $otherSql = $other->sql;
281 $selfSql =~ s/--.*$//mg;
282 $otherSql =~ s/--.*$//mg;
283 # Collapse whitespace to space to avoid whitespace comparison issues
284 $selfSql =~ s/\s+/ /sg;
285 $otherSql =~ s/\s+/ /sg;
286 return 0 unless $selfSql eq $otherSql;
289 my $selfFields = join(":", $self->fields);
290 my $otherFields = join(":", $other->fields);
291 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
292 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
298 undef $self->{'schema'}; # destroy cyclical reference
307 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.