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 # ----------------------------------------------------------------------
37 __PACKAGE__->_attributes( qw/
38 name sql fields schema order
47 my $view = SQL::Translator::Schema::View->new;
51 # ----------------------------------------------------------------------
58 Gets and set the fields the constraint is on. Accepts a string, list or
59 arrayref; returns an array or array reference. Will unique the field
60 names and keep them in order by the first occurrence of a field name.
63 $view->fields('id', 'name');
64 $view->fields( 'id, name' );
65 $view->fields( [ 'id', 'name' ] );
66 $view->fields( qw[ id name ] );
68 my @fields = $view->fields;
73 my $fields = parse_list_arg( @_ );
76 my ( %unique, @unique );
77 for my $f ( @$fields ) {
78 next if $unique{ $f };
83 $self->{'fields'} = \@unique;
86 return wantarray ? @{ $self->{'fields'} || [] } : ($self->{'fields'} || '');
89 # ----------------------------------------------------------------------
96 Determine whether the view is valid or not.
98 my $ok = $view->is_valid;
104 return $self->error('No name') unless $self->name;
105 return $self->error('No sql') unless $self->sql;
110 # ----------------------------------------------------------------------
117 Get or set the view's name.
119 my $name = $view->name('foo');
124 $self->{'name'} = shift if @_;
125 return $self->{'name'} || '';
128 # ----------------------------------------------------------------------
135 Get or set the view's order.
137 my $order = $view->order(3);
141 my ( $self, $arg ) = @_;
143 if ( defined $arg && $arg =~ /^\d+$/ ) {
144 $self->{'order'} = $arg;
147 return $self->{'order'} || 0;
150 # ----------------------------------------------------------------------
157 Get or set the view's SQL.
159 my $sql = $view->sql('select * from foo');
164 $self->{'sql'} = shift if @_;
165 return $self->{'sql'} || '';
168 # ----------------------------------------------------------------------
175 Get or set the view's schema object.
177 $view->schema( $schema );
178 my $schema = $view->schema;
183 if ( my $arg = shift ) {
184 return $self->error('Not a schema object') unless
185 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
186 $self->{'schema'} = $arg;
189 return $self->{'schema'};
192 # ----------------------------------------------------------------------
199 Determines if this view is the same as another
201 my $isIdentical = $view1->equals( $view2 );
207 my $case_insensitive = shift;
208 my $ignore_sql = shift;
210 return 0 unless $self->SUPER::equals($other);
211 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
212 #return 0 unless $self->is_valid eq $other->is_valid;
214 unless ($ignore_sql) {
215 my $selfSql = $self->sql;
216 my $otherSql = $other->sql;
218 $selfSql =~ s/--.*$//mg;
219 $otherSql =~ s/--.*$//mg;
220 # Collapse whitespace to space to avoid whitespace comparison issues
221 $selfSql =~ s/\s+/ /sg;
222 $otherSql =~ s/\s+/ /sg;
223 return 0 unless $selfSql eq $otherSql;
226 my $selfFields = join(":", $self->fields);
227 my $otherFields = join(":", $other->fields);
228 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
229 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
233 # ----------------------------------------------------------------------
236 undef $self->{'schema'}; # destroy cyclical reference
241 # ----------------------------------------------------------------------
247 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.