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::Extra
33 SQL::Translator::Schema::Role::Error
34 SQL::Translator::Schema::Role::Compare
37 our ( $TABLE_COUNT, $VIEW_COUNT );
39 our $VERSION = '1.59';
45 my $view = SQL::Translator::Schema::View->new;
49 Gets and set the fields the constraint is on. Accepts a string, list or
50 arrayref; returns an array or array reference. Will unique the field
51 names and keep them in order by the first occurrence of a field name.
54 $view->fields('id', 'name');
55 $view->fields( 'id, name' );
56 $view->fields( [ 'id', 'name' ] );
57 $view->fields( qw[ id name ] );
59 my @fields = $view->fields;
65 default => sub { [] },
66 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
69 around fields => sub {
72 my $fields = parse_list_arg( @_ );
73 $self->$orig($fields) if @$fields;
75 return wantarray ? @{ $self->$orig } : $self->$orig;
80 Gets and set the tables the SELECT mentions. Accepts a string, list or
81 arrayref; returns an array or array reference. Will unique the table
82 names and keep them in order by the first occurrence of a field name.
85 $view->tables('foo', 'bar');
86 $view->tables( 'foo, bar' );
87 $view->tables( [ 'foo', 'bar' ] );
88 $view->tables( qw[ foo bar ] );
90 my @tables = $view->tables;
96 default => sub { [] },
97 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
100 around tables => sub {
103 my $fields = parse_list_arg( @_ );
104 $self->$orig($fields) if @$fields;
106 return wantarray ? @{ $self->$orig } : $self->$orig;
111 Gets and sets a list of options on the view.
113 $view->options('ALGORITHM=UNDEFINED');
115 my @options = $view->options;
121 default => sub { [] },
122 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
125 around options => sub {
128 my $options = parse_list_arg( @_ );
131 $self->$orig([ @{$self->$orig}, @$options ])
134 return wantarray ? @{ $self->$orig } : $self->$orig;
143 Determine whether the view is valid or not.
145 my $ok = $view->is_valid;
151 return $self->error('No name') unless $self->name;
152 return $self->error('No sql') unless $self->sql;
159 Get or set the view's name.
161 my $name = $view->name('foo');
165 has name => ( is => 'rw', default => sub { '' } );
169 Get or set the view's order.
171 my $order = $view->order(3);
175 has order => ( is => 'rw', default => sub { 0 } );
177 around order => sub {
178 my ( $orig, $self, $arg ) = @_;
180 if ( defined $arg && $arg =~ /^\d+$/ ) {
181 return $self->$orig($arg);
189 Get or set the view's SQL.
191 my $sql = $view->sql('select * from foo');
195 has sql => ( is => 'rw', default => sub { '' } );
199 Get or set the view's schema object.
201 $view->schema( $schema );
202 my $schema = $view->schema;
206 has schema => ( is => 'rw', isa => schema_obj('Schema') );
208 around schema => \&ex2err;
212 Determines if this view is the same as another
214 my $isIdentical = $view1->equals( $view2 );
218 around equals => sub {
222 my $case_insensitive = shift;
223 my $ignore_sql = shift;
225 return 0 unless $self->$orig($other);
226 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
227 #return 0 unless $self->is_valid eq $other->is_valid;
229 unless ($ignore_sql) {
230 my $selfSql = $self->sql;
231 my $otherSql = $other->sql;
233 $selfSql =~ s/--.*$//mg;
234 $otherSql =~ s/--.*$//mg;
235 # Collapse whitespace to space to avoid whitespace comparison issues
236 $selfSql =~ s/\s+/ /sg;
237 $otherSql =~ s/\s+/ /sg;
238 return 0 unless $selfSql eq $otherSql;
241 my $selfFields = join(":", $self->fields);
242 my $otherFields = join(":", $other->fields);
243 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
244 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
250 undef $self->{'schema'}; # destroy cyclical reference
253 # Must come after all 'has' declarations
254 around new => \&ex2err;
262 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.