Factor list attributes into variant role
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / View.pm
1 package SQL::Translator::Schema::View;
2
3 =pod
4
5 =head1 NAME
6
7 SQL::Translator::Schema::View - SQL::Translator view object
8
9 =head1 SYNOPSIS
10
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
16   );
17
18 =head1 DESCRIPTION
19
20 C<SQL::Translator::Schema::View> is the view object.
21
22 =head1 METHODS
23
24 =cut
25
26 use Moo;
27 use SQL::Translator::Utils qw(ex2err);
28 use SQL::Translator::Types qw(schema_obj);
29 use SQL::Translator::Role::ListAttr;
30
31 extends 'SQL::Translator::Schema::Object';
32
33 our $VERSION = '1.59';
34
35 =head2 new
36
37 Object constructor.
38
39   my $view = SQL::Translator::Schema::View->new;
40
41 =head2 fields
42
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.
46
47   $view->fields('id');
48   $view->fields('id', 'name');
49   $view->fields( 'id, name' );
50   $view->fields( [ 'id', 'name' ] );
51   $view->fields( qw[ id name ] );
52
53   my @fields = $view->fields;
54
55 =cut
56
57 with ListAttr fields => ( uniq => 1 );
58
59 =head2 tables
60
61 Gets and set the tables the SELECT mentions.  Accepts a string, list or
62 arrayref; returns an array or array reference.  Will unique the table
63 names and keep them in order by the first occurrence of a field name.
64
65   $view->tables('foo');
66   $view->tables('foo', 'bar');
67   $view->tables( 'foo, bar' );
68   $view->tables( [ 'foo', 'bar' ] );
69   $view->tables( qw[ foo bar ] );
70
71   my @tables = $view->tables;
72
73 =cut
74
75 with ListAttr tables => ( uniq => 1 );
76
77 =head2 options
78
79 Gets and sets a list of options on the view.
80
81   $view->options('ALGORITHM=UNDEFINED');
82
83   my @options = $view->options;
84
85 =cut
86
87 with ListAttr options => ( uniq => 1, append => 1 );
88
89 sub is_valid {
90
91 =pod
92
93 =head2 is_valid
94
95 Determine whether the view is valid or not.
96
97   my $ok = $view->is_valid;
98
99 =cut
100
101     my $self = shift;
102
103     return $self->error('No name') unless $self->name;
104     return $self->error('No sql')  unless $self->sql;
105
106     return 1;
107 }
108
109 =head2 name
110
111 Get or set the view's name.
112
113   my $name = $view->name('foo');
114
115 =cut
116
117 has name => ( is => 'rw', default => sub { '' } );
118
119 =head2 order
120
121 Get or set the view's order.
122
123   my $order = $view->order(3);
124
125 =cut
126
127 has order => ( is => 'rw', default => sub { 0 } );
128
129 around order => sub {
130     my ( $orig, $self, $arg ) = @_;
131
132     if ( defined $arg && $arg =~ /^\d+$/ ) {
133         return $self->$orig($arg);
134     }
135
136     return $self->$orig;
137 };
138
139 =head2 sql
140
141 Get or set the view's SQL.
142
143   my $sql = $view->sql('select * from foo');
144
145 =cut
146
147 has sql => ( is => 'rw', default => sub { '' } );
148
149 =head2 schema
150
151 Get or set the view's schema object.
152
153   $view->schema( $schema );
154   my $schema = $view->schema;
155
156 =cut
157
158 has schema => ( is => 'rw', isa => schema_obj('Schema'), weak_ref => 1 );
159
160 around schema => \&ex2err;
161
162 =head2 equals
163
164 Determines if this view is the same as another
165
166   my $isIdentical = $view1->equals( $view2 );
167
168 =cut
169
170 around equals => sub {
171     my $orig = shift;
172     my $self = shift;
173     my $other = shift;
174     my $case_insensitive = shift;
175     my $ignore_sql = shift;
176
177     return 0 unless $self->$orig($other);
178     return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
179     #return 0 unless $self->is_valid eq $other->is_valid;
180
181     unless ($ignore_sql) {
182         my $selfSql = $self->sql;
183         my $otherSql = $other->sql;
184         # Remove comments
185         $selfSql =~ s/--.*$//mg;
186         $otherSql =~ s/--.*$//mg;
187         # Collapse whitespace to space to avoid whitespace comparison issues
188         $selfSql =~ s/\s+/ /sg;
189         $otherSql =~ s/\s+/ /sg;
190         return 0 unless $selfSql eq $otherSql;
191     }
192
193     my $selfFields = join(":", $self->fields);
194     my $otherFields = join(":", $other->fields);
195     return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
196     return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
197     return 1;
198 };
199
200 # Must come after all 'has' declarations
201 around new => \&ex2err;
202
203 1;
204
205 =pod
206
207 =head1 AUTHOR
208
209 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
210
211 =cut