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 $VERSION = '1.59';
44 my $view = SQL::Translator::Schema::View->new;
48 Gets and set the fields the constraint is on. Accepts a string, list or
49 arrayref; returns an array or array reference. Will unique the field
50 names and keep them in order by the first occurrence of a field name.
53 $view->fields('id', 'name');
54 $view->fields( 'id, name' );
55 $view->fields( [ 'id', 'name' ] );
56 $view->fields( qw[ id name ] );
58 my @fields = $view->fields;
64 default => sub { [] },
65 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
68 around fields => sub {
71 my $fields = parse_list_arg( @_ );
72 $self->$orig($fields) if @$fields;
74 return wantarray ? @{ $self->$orig } : $self->$orig;
79 Gets and set the tables the SELECT mentions. Accepts a string, list or
80 arrayref; returns an array or array reference. Will unique the table
81 names and keep them in order by the first occurrence of a field name.
84 $view->tables('foo', 'bar');
85 $view->tables( 'foo, bar' );
86 $view->tables( [ 'foo', 'bar' ] );
87 $view->tables( qw[ foo bar ] );
89 my @tables = $view->tables;
95 default => sub { [] },
96 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
99 around tables => sub {
102 my $fields = parse_list_arg( @_ );
103 $self->$orig($fields) if @$fields;
105 return wantarray ? @{ $self->$orig } : $self->$orig;
110 Gets and sets a list of options on the view.
112 $view->options('ALGORITHM=UNDEFINED');
114 my @options = $view->options;
120 default => sub { [] },
121 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
124 around options => sub {
127 my $options = parse_list_arg( @_ );
130 $self->$orig([ @{$self->$orig}, @$options ])
133 return wantarray ? @{ $self->$orig } : $self->$orig;
142 Determine whether the view is valid or not.
144 my $ok = $view->is_valid;
150 return $self->error('No name') unless $self->name;
151 return $self->error('No sql') unless $self->sql;
158 Get or set the view's name.
160 my $name = $view->name('foo');
164 has name => ( is => 'rw', default => sub { '' } );
168 Get or set the view's order.
170 my $order = $view->order(3);
174 has order => ( is => 'rw', default => sub { 0 } );
176 around order => sub {
177 my ( $orig, $self, $arg ) = @_;
179 if ( defined $arg && $arg =~ /^\d+$/ ) {
180 return $self->$orig($arg);
188 Get or set the view's SQL.
190 my $sql = $view->sql('select * from foo');
194 has sql => ( is => 'rw', default => sub { '' } );
198 Get or set the view's schema object.
200 $view->schema( $schema );
201 my $schema = $view->schema;
205 has schema => ( is => 'rw', isa => schema_obj('Schema'), weak_ref => 1 );
207 around schema => \&ex2err;
211 Determines if this view is the same as another
213 my $isIdentical = $view1->equals( $view2 );
217 around equals => sub {
221 my $case_insensitive = shift;
222 my $ignore_sql = shift;
224 return 0 unless $self->$orig($other);
225 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
226 #return 0 unless $self->is_valid eq $other->is_valid;
228 unless ($ignore_sql) {
229 my $selfSql = $self->sql;
230 my $otherSql = $other->sql;
232 $selfSql =~ s/--.*$//mg;
233 $otherSql =~ s/--.*$//mg;
234 # Collapse whitespace to space to avoid whitespace comparison issues
235 $selfSql =~ s/\s+/ /sg;
236 $otherSql =~ s/\s+/ /sg;
237 return 0 unless $selfSql eq $otherSql;
240 my $selfFields = join(":", $self->fields);
241 my $otherFields = join(":", $other->fields);
242 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
243 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
247 # Must come after all 'has' declarations
248 around new => \&ex2err;
256 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.