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;
30 use Sub::Quote qw(quote_sub);
32 extends 'SQL::Translator::Schema::Object';
34 our $VERSION = '1.59';
40 my $view = SQL::Translator::Schema::View->new;
44 Gets and set the fields the constraint is on. Accepts a string, list or
45 arrayref; returns an array or array reference. Will unique the field
46 names and keep them in order by the first occurrence of a field name.
49 $view->fields('id', 'name');
50 $view->fields( 'id, name' );
51 $view->fields( [ 'id', 'name' ] );
52 $view->fields( qw[ id name ] );
54 my @fields = $view->fields;
58 with ListAttr fields => ( uniq => 1 );
62 Gets and set the tables the SELECT mentions. Accepts a string, list or
63 arrayref; returns an array or array reference. Will unique the table
64 names and keep them in order by the first occurrence of a field name.
67 $view->tables('foo', 'bar');
68 $view->tables( 'foo, bar' );
69 $view->tables( [ 'foo', 'bar' ] );
70 $view->tables( qw[ foo bar ] );
72 my @tables = $view->tables;
76 with ListAttr tables => ( uniq => 1 );
80 Gets or appends a list of options on the view.
82 $view->options('ALGORITHM=UNDEFINED');
84 my @options = $view->options;
88 with ListAttr options => ( uniq => 1, append => 1 );
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;
112 Get or set the view's name.
114 my $name = $view->name('foo');
118 has name => ( is => 'rw', default => quote_sub(q{ '' }) );
122 Get or set the view's order.
124 my $order = $view->order(3);
128 has order => ( is => 'rw', default => quote_sub(q{ 0 }) );
130 around order => sub {
131 my ( $orig, $self, $arg ) = @_;
133 if ( defined $arg && $arg =~ /^\d+$/ ) {
134 return $self->$orig($arg);
142 Get or set the view's SQL.
144 my $sql = $view->sql('select * from foo');
148 has sql => ( is => 'rw', default => quote_sub(q{ '' }) );
152 Get or set the view's schema object.
154 $view->schema( $schema );
155 my $schema = $view->schema;
159 has schema => ( is => 'rw', isa => schema_obj('Schema'), weak_ref => 1 );
161 around schema => \&ex2err;
165 Determines if this view is the same as another
167 my $isIdentical = $view1->equals( $view2 );
171 around equals => sub {
175 my $case_insensitive = shift;
176 my $ignore_sql = shift;
178 return 0 unless $self->$orig($other);
179 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
180 #return 0 unless $self->is_valid eq $other->is_valid;
182 unless ($ignore_sql) {
183 my $selfSql = $self->sql;
184 my $otherSql = $other->sql;
186 $selfSql =~ s/--.*$//mg;
187 $otherSql =~ s/--.*$//mg;
188 # Collapse whitespace to space to avoid whitespace comparison issues
189 $selfSql =~ s/\s+/ /sg;
190 $otherSql =~ s/\s+/ /sg;
191 return 0 unless $selfSql eq $otherSql;
194 my $selfFields = join(":", $self->fields);
195 my $otherFields = join(":", $other->fields);
196 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
197 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
201 # Must come after all 'has' declarations
202 around new => \&ex2err;
210 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.