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);
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;
59 default => sub { [] },
60 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
63 around fields => sub {
66 my $fields = parse_list_arg( @_ );
67 $self->$orig($fields) if @$fields;
69 return wantarray ? @{ $self->$orig } : $self->$orig;
74 Gets and set the tables the SELECT mentions. Accepts a string, list or
75 arrayref; returns an array or array reference. Will unique the table
76 names and keep them in order by the first occurrence of a field name.
79 $view->tables('foo', 'bar');
80 $view->tables( 'foo, bar' );
81 $view->tables( [ 'foo', 'bar' ] );
82 $view->tables( qw[ foo bar ] );
84 my @tables = $view->tables;
90 default => sub { [] },
91 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
94 around tables => sub {
97 my $fields = parse_list_arg( @_ );
98 $self->$orig($fields) if @$fields;
100 return wantarray ? @{ $self->$orig } : $self->$orig;
105 Gets and sets a list of options on the view.
107 $view->options('ALGORITHM=UNDEFINED');
109 my @options = $view->options;
115 default => sub { [] },
116 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
119 around options => sub {
122 my $options = parse_list_arg( @_ );
125 $self->$orig([ @{$self->$orig}, @$options ])
128 return wantarray ? @{ $self->$orig } : $self->$orig;
137 Determine whether the view is valid or not.
139 my $ok = $view->is_valid;
145 return $self->error('No name') unless $self->name;
146 return $self->error('No sql') unless $self->sql;
153 Get or set the view's name.
155 my $name = $view->name('foo');
159 has name => ( is => 'rw', default => sub { '' } );
163 Get or set the view's order.
165 my $order = $view->order(3);
169 has order => ( is => 'rw', default => sub { 0 } );
171 around order => sub {
172 my ( $orig, $self, $arg ) = @_;
174 if ( defined $arg && $arg =~ /^\d+$/ ) {
175 return $self->$orig($arg);
183 Get or set the view's SQL.
185 my $sql = $view->sql('select * from foo');
189 has sql => ( is => 'rw', default => sub { '' } );
193 Get or set the view's schema object.
195 $view->schema( $schema );
196 my $schema = $view->schema;
200 has schema => ( is => 'rw', isa => schema_obj('Schema'), weak_ref => 1 );
202 around schema => \&ex2err;
206 Determines if this view is the same as another
208 my $isIdentical = $view1->equals( $view2 );
212 around equals => sub {
216 my $case_insensitive = shift;
217 my $ignore_sql = shift;
219 return 0 unless $self->$orig($other);
220 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
221 #return 0 unless $self->is_valid eq $other->is_valid;
223 unless ($ignore_sql) {
224 my $selfSql = $self->sql;
225 my $otherSql = $other->sql;
227 $selfSql =~ s/--.*$//mg;
228 $otherSql =~ s/--.*$//mg;
229 # Collapse whitespace to space to avoid whitespace comparison issues
230 $selfSql =~ s/\s+/ /sg;
231 $otherSql =~ s/\s+/ /sg;
232 return 0 unless $selfSql eq $otherSql;
235 my $selfFields = join(":", $self->fields);
236 my $otherFields = join(":", $other->fields);
237 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
238 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
242 # Must come after all 'has' declarations
243 around new => \&ex2err;
251 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.