Commit | Line | Data |
3c5de62a |
1 | package SQL::Translator::Schema::View; |
2 | |
3c5de62a |
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; |
b039cab8 |
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 |
3c5de62a |
16 | ); |
17 | |
18 | =head1 DESCRIPTION |
19 | |
20 | C<SQL::Translator::Schema::View> is the view object. |
21 | |
22 | =head1 METHODS |
23 | |
24 | =cut |
25 | |
52068c95 |
26 | use Moo; |
27 | use SQL::Translator::Utils qw(parse_list_arg ex2err); |
28 | use SQL::Translator::Types qw(schema_obj); |
29 | use List::MoreUtils qw(uniq); |
3c5de62a |
30 | |
52068c95 |
31 | with qw( |
32 | SQL::Translator::Schema::Role::Extra |
33 | SQL::Translator::Schema::Role::Error |
34 | SQL::Translator::Schema::Role::Compare |
35 | ); |
b6a880d1 |
36 | |
0c04c5a2 |
37 | our ( $TABLE_COUNT, $VIEW_COUNT ); |
da06ac74 |
38 | |
0c04c5a2 |
39 | our $VERSION = '1.59'; |
3c5de62a |
40 | |
3c5de62a |
41 | =head2 new |
42 | |
43 | Object constructor. |
44 | |
b039cab8 |
45 | my $view = SQL::Translator::Schema::View->new; |
3c5de62a |
46 | |
25a02fa7 |
47 | =head2 fields |
48 | |
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. |
52 | |
53 | $view->fields('id'); |
54 | $view->fields('id', 'name'); |
55 | $view->fields( 'id, name' ); |
56 | $view->fields( [ 'id', 'name' ] ); |
57 | $view->fields( qw[ id name ] ); |
58 | |
59 | my @fields = $view->fields; |
60 | |
61 | =cut |
62 | |
52068c95 |
63 | has fields => ( |
64 | is => 'rw', |
65 | default => sub { [] }, |
66 | coerce => sub { [uniq @{parse_list_arg($_[0])}] }, |
67 | ); |
68 | |
69 | around fields => sub { |
70 | my $orig = shift; |
25a02fa7 |
71 | my $self = shift; |
72 | my $fields = parse_list_arg( @_ ); |
52068c95 |
73 | $self->$orig($fields) if @$fields; |
25a02fa7 |
74 | |
52068c95 |
75 | return wantarray ? @{ $self->$orig } : $self->$orig; |
76 | }; |
e6c5fb6e |
77 | |
78 | =head2 tables |
79 | |
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. |
83 | |
84 | $view->tables('foo'); |
85 | $view->tables('foo', 'bar'); |
86 | $view->tables( 'foo, bar' ); |
87 | $view->tables( [ 'foo', 'bar' ] ); |
88 | $view->tables( qw[ foo bar ] ); |
89 | |
90 | my @tables = $view->tables; |
91 | |
92 | =cut |
93 | |
52068c95 |
94 | has tables => ( |
95 | is => 'rw', |
96 | default => sub { [] }, |
97 | coerce => sub { [uniq @{parse_list_arg($_[0])}] }, |
98 | ); |
e6c5fb6e |
99 | |
52068c95 |
100 | around tables => sub { |
101 | my $orig = shift; |
102 | my $self = shift; |
103 | my $fields = parse_list_arg( @_ ); |
104 | $self->$orig($fields) if @$fields; |
e6c5fb6e |
105 | |
52068c95 |
106 | return wantarray ? @{ $self->$orig } : $self->$orig; |
107 | }; |
e6c5fb6e |
108 | |
109 | =head2 options |
110 | |
111 | Gets and sets a list of options on the view. |
112 | |
113 | $view->options('ALGORITHM=UNDEFINED'); |
114 | |
115 | my @options = $view->options; |
116 | |
117 | =cut |
118 | |
52068c95 |
119 | has options => ( |
120 | is => 'rw', |
121 | default => sub { [] }, |
122 | coerce => sub { [uniq @{parse_list_arg($_[0])}] }, |
123 | ); |
124 | |
125 | around options => sub { |
126 | my $orig = shift; |
e6c5fb6e |
127 | my $self = shift; |
128 | my $options = parse_list_arg( @_ ); |
129 | |
130 | if ( @$options ) { |
52068c95 |
131 | $self->$orig([ @{$self->$orig}, @$options ]) |
e6c5fb6e |
132 | } |
133 | |
52068c95 |
134 | return wantarray ? @{ $self->$orig } : $self->$orig; |
135 | }; |
e6c5fb6e |
136 | |
25a02fa7 |
137 | sub is_valid { |
138 | |
139 | =pod |
140 | |
141 | =head2 is_valid |
142 | |
143 | Determine whether the view is valid or not. |
144 | |
145 | my $ok = $view->is_valid; |
146 | |
147 | =cut |
148 | |
149 | my $self = shift; |
150 | |
151 | return $self->error('No name') unless $self->name; |
152 | return $self->error('No sql') unless $self->sql; |
153 | |
154 | return 1; |
155 | } |
156 | |
3c5de62a |
157 | =head2 name |
158 | |
159 | Get or set the view's name. |
160 | |
161 | my $name = $view->name('foo'); |
162 | |
163 | =cut |
164 | |
52068c95 |
165 | has name => ( is => 'rw', default => sub { '' } ); |
3c5de62a |
166 | |
25a02fa7 |
167 | =head2 order |
3c5de62a |
168 | |
25a02fa7 |
169 | Get or set the view's order. |
3c5de62a |
170 | |
25a02fa7 |
171 | my $order = $view->order(3); |
3c5de62a |
172 | |
173 | =cut |
174 | |
52068c95 |
175 | has order => ( is => 'rw', default => sub { 0 } ); |
176 | |
177 | around order => sub { |
178 | my ( $orig, $self, $arg ) = @_; |
25a02fa7 |
179 | |
180 | if ( defined $arg && $arg =~ /^\d+$/ ) { |
52068c95 |
181 | return $self->$orig($arg); |
25a02fa7 |
182 | } |
183 | |
52068c95 |
184 | return $self->$orig; |
185 | }; |
3c5de62a |
186 | |
25a02fa7 |
187 | =head2 sql |
3c5de62a |
188 | |
25a02fa7 |
189 | Get or set the view's SQL. |
3c5de62a |
190 | |
25a02fa7 |
191 | my $sql = $view->sql('select * from foo'); |
3c5de62a |
192 | |
193 | =cut |
194 | |
52068c95 |
195 | has sql => ( is => 'rw', default => sub { '' } ); |
b039cab8 |
196 | |
197 | =head2 schema |
198 | |
199 | Get or set the view's schema object. |
200 | |
201 | $view->schema( $schema ); |
202 | my $schema = $view->schema; |
203 | |
204 | =cut |
205 | |
52068c95 |
206 | has schema => ( is => 'rw', isa => schema_obj('Schema') ); |
b039cab8 |
207 | |
52068c95 |
208 | around schema => \&ex2err; |
abf315bb |
209 | |
210 | =head2 equals |
211 | |
212 | Determines if this view is the same as another |
213 | |
214 | my $isIdentical = $view1->equals( $view2 ); |
215 | |
216 | =cut |
217 | |
52068c95 |
218 | around equals => sub { |
219 | my $orig = shift; |
abf315bb |
220 | my $self = shift; |
221 | my $other = shift; |
6be9534b |
222 | my $case_insensitive = shift; |
d1a895ce |
223 | my $ignore_sql = shift; |
ea93df61 |
224 | |
52068c95 |
225 | return 0 unless $self->$orig($other); |
6be9534b |
226 | return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name; |
4598b71c |
227 | #return 0 unless $self->is_valid eq $other->is_valid; |
ea93df61 |
228 | |
d1a895ce |
229 | unless ($ignore_sql) { |
230 | my $selfSql = $self->sql; |
231 | my $otherSql = $other->sql; |
232 | # Remove comments |
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; |
239 | } |
ea93df61 |
240 | |
6be9534b |
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; |
4598b71c |
244 | return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra); |
abf315bb |
245 | return 1; |
52068c95 |
246 | }; |
abf315bb |
247 | |
25a02fa7 |
248 | sub DESTROY { |
3c5de62a |
249 | my $self = shift; |
b039cab8 |
250 | undef $self->{'schema'}; # destroy cyclical reference |
3c5de62a |
251 | } |
252 | |
52068c95 |
253 | # Must come after all 'has' declarations |
254 | around new => \&ex2err; |
255 | |
3c5de62a |
256 | 1; |
257 | |
3c5de62a |
258 | =pod |
259 | |
260 | =head1 AUTHOR |
261 | |
c3b0b535 |
262 | Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>. |
3c5de62a |
263 | |
264 | =cut |