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(parse_list_arg ex2err);
28 use SQL::Translator::Types qw(schema_obj);
29 use List::MoreUtils qw(uniq);
32 SQL::Translator::Schema::Role::BuildArgs
33 SQL::Translator::Schema::Role::Extra
34 SQL::Translator::Schema::Role::Error
35 SQL::Translator::Schema::Role::Compare
38 our ( $TABLE_COUNT, $VIEW_COUNT );
40 our $VERSION = '1.59';
46 my $view = SQL::Translator::Schema::View->new;
50 Gets and set the fields the constraint is on. Accepts a string, list or
51 arrayref; returns an array or array reference. Will unique the field
52 names and keep them in order by the first occurrence of a field name.
55 $view->fields('id', 'name');
56 $view->fields( 'id, name' );
57 $view->fields( [ 'id', 'name' ] );
58 $view->fields( qw[ id name ] );
60 my @fields = $view->fields;
66 default => sub { [] },
67 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
70 around fields => sub {
73 my $fields = parse_list_arg( @_ );
74 $self->$orig($fields) if @$fields;
76 return wantarray ? @{ $self->$orig } : $self->$orig;
81 Gets and set the tables the SELECT mentions. Accepts a string, list or
82 arrayref; returns an array or array reference. Will unique the table
83 names and keep them in order by the first occurrence of a field name.
86 $view->tables('foo', 'bar');
87 $view->tables( 'foo, bar' );
88 $view->tables( [ 'foo', 'bar' ] );
89 $view->tables( qw[ foo bar ] );
91 my @tables = $view->tables;
97 default => sub { [] },
98 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
101 around tables => sub {
104 my $fields = parse_list_arg( @_ );
105 $self->$orig($fields) if @$fields;
107 return wantarray ? @{ $self->$orig } : $self->$orig;
112 Gets and sets a list of options on the view.
114 $view->options('ALGORITHM=UNDEFINED');
116 my @options = $view->options;
122 default => sub { [] },
123 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
126 around options => sub {
129 my $options = parse_list_arg( @_ );
132 $self->$orig([ @{$self->$orig}, @$options ])
135 return wantarray ? @{ $self->$orig } : $self->$orig;
144 Determine whether the view is valid or not.
146 my $ok = $view->is_valid;
152 return $self->error('No name') unless $self->name;
153 return $self->error('No sql') unless $self->sql;
160 Get or set the view's name.
162 my $name = $view->name('foo');
166 has name => ( is => 'rw', default => sub { '' } );
170 Get or set the view's order.
172 my $order = $view->order(3);
176 has order => ( is => 'rw', default => sub { 0 } );
178 around order => sub {
179 my ( $orig, $self, $arg ) = @_;
181 if ( defined $arg && $arg =~ /^\d+$/ ) {
182 return $self->$orig($arg);
190 Get or set the view's SQL.
192 my $sql = $view->sql('select * from foo');
196 has sql => ( is => 'rw', default => sub { '' } );
200 Get or set the view's schema object.
202 $view->schema( $schema );
203 my $schema = $view->schema;
207 has schema => ( is => 'rw', isa => schema_obj('Schema') );
209 around schema => \&ex2err;
213 Determines if this view is the same as another
215 my $isIdentical = $view1->equals( $view2 );
219 around equals => sub {
223 my $case_insensitive = shift;
224 my $ignore_sql = shift;
226 return 0 unless $self->$orig($other);
227 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
228 #return 0 unless $self->is_valid eq $other->is_valid;
230 unless ($ignore_sql) {
231 my $selfSql = $self->sql;
232 my $otherSql = $other->sql;
234 $selfSql =~ s/--.*$//mg;
235 $otherSql =~ s/--.*$//mg;
236 # Collapse whitespace to space to avoid whitespace comparison issues
237 $selfSql =~ s/\s+/ /sg;
238 $otherSql =~ s/\s+/ /sg;
239 return 0 unless $selfSql eq $otherSql;
242 my $selfFields = join(":", $self->fields);
243 my $otherFields = join(":", $other->fields);
244 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
245 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
251 undef $self->{'schema'}; # destroy cyclical reference
254 # Must come after all 'has' declarations
255 around new => \&ex2err;
263 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.