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 qw(ex2err);
28 use SQL::Translator::Types qw(schema_obj);
29 use SQL::Translator::Role::ListAttr;
31 extends 'SQL::Translator::Schema::Object';
33 our $VERSION = '1.59';
39 my $view = SQL::Translator::Schema::View->new;
43 Gets and set the fields the constraint is on. Accepts a string, list or
44 arrayref; returns an array or array reference. Will unique the field
45 names and keep them in order by the first occurrence of a field name.
48 $view->fields('id', 'name');
49 $view->fields( 'id, name' );
50 $view->fields( [ 'id', 'name' ] );
51 $view->fields( qw[ id name ] );
53 my @fields = $view->fields;
57 with ListAttr fields => ( uniq => 1 );
61 Gets and set the tables the SELECT mentions. Accepts a string, list or
62 arrayref; returns an array or array reference. Will unique the table
63 names and keep them in order by the first occurrence of a field name.
66 $view->tables('foo', 'bar');
67 $view->tables( 'foo, bar' );
68 $view->tables( [ 'foo', 'bar' ] );
69 $view->tables( qw[ foo bar ] );
71 my @tables = $view->tables;
75 with ListAttr tables => ( uniq => 1 );
79 Gets and sets a list of options on the view.
81 $view->options('ALGORITHM=UNDEFINED');
83 my @options = $view->options;
87 with ListAttr options => ( uniq => 1, append => 1 );
95 Determine whether the view is valid or not.
97 my $ok = $view->is_valid;
103 return $self->error('No name') unless $self->name;
104 return $self->error('No sql') unless $self->sql;
111 Get or set the view's name.
113 my $name = $view->name('foo');
117 has name => ( is => 'rw', default => sub { '' } );
121 Get or set the view's order.
123 my $order = $view->order(3);
127 has order => ( is => 'rw', default => sub { 0 } );
129 around order => sub {
130 my ( $orig, $self, $arg ) = @_;
132 if ( defined $arg && $arg =~ /^\d+$/ ) {
133 return $self->$orig($arg);
141 Get or set the view's SQL.
143 my $sql = $view->sql('select * from foo');
147 has sql => ( is => 'rw', default => sub { '' } );
151 Get or set the view's schema object.
153 $view->schema( $schema );
154 my $schema = $view->schema;
158 has schema => ( is => 'rw', isa => schema_obj('Schema'), weak_ref => 1 );
160 around schema => \&ex2err;
164 Determines if this view is the same as another
166 my $isIdentical = $view1->equals( $view2 );
170 around equals => sub {
174 my $case_insensitive = shift;
175 my $ignore_sql = shift;
177 return 0 unless $self->$orig($other);
178 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
179 #return 0 unless $self->is_valid eq $other->is_valid;
181 unless ($ignore_sql) {
182 my $selfSql = $self->sql;
183 my $otherSql = $other->sql;
185 $selfSql =~ s/--.*$//mg;
186 $otherSql =~ s/--.*$//mg;
187 # Collapse whitespace to space to avoid whitespace comparison issues
188 $selfSql =~ s/\s+/ /sg;
189 $otherSql =~ s/\s+/ /sg;
190 return 0 unless $selfSql eq $otherSql;
193 my $selfFields = join(":", $self->fields);
194 my $otherFields = join(":", $other->fields);
195 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
196 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
200 # Must come after all 'has' declarations
201 around new => \&ex2err;
209 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.