Added "tables" and "options" methods to Schema::View
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / View.pm
CommitLineData
3c5de62a 1package SQL::Translator::Schema::View;
2
3c5de62a 3=pod
4
5=head1 NAME
6
7SQL::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
20C<SQL::Translator::Schema::View> is the view object.
21
22=head1 METHODS
23
24=cut
25
26use strict;
f27f9229 27use warnings;
25a02fa7 28use SQL::Translator::Utils 'parse_list_arg';
3c5de62a 29
b6a880d1 30use base 'SQL::Translator::Schema::Object';
31
0c04c5a2 32our ( $TABLE_COUNT, $VIEW_COUNT );
da06ac74 33
0c04c5a2 34our $VERSION = '1.59';
3c5de62a 35
9371be50 36__PACKAGE__->_attributes( qw/
e6c5fb6e 37 name sql fields schema order tables options
9371be50 38/);
3c5de62a 39
40=pod
41
42=head2 new
43
44Object constructor.
45
b039cab8 46 my $view = SQL::Translator::Schema::View->new;
3c5de62a 47
48=cut
49
25a02fa7 50sub fields {
51
52=pod
53
54=head2 fields
55
56Gets and set the fields the constraint is on. Accepts a string, list or
57arrayref; returns an array or array reference. Will unique the field
58names and keep them in order by the first occurrence of a field name.
59
60 $view->fields('id');
61 $view->fields('id', 'name');
62 $view->fields( 'id, name' );
63 $view->fields( [ 'id', 'name' ] );
64 $view->fields( qw[ id name ] );
65
66 my @fields = $view->fields;
67
68=cut
69
70 my $self = shift;
71 my $fields = parse_list_arg( @_ );
72
73 if ( @$fields ) {
74 my ( %unique, @unique );
75 for my $f ( @$fields ) {
f9a2a1d9 76 next if $unique{ $f }++;
25a02fa7 77 push @unique, $f;
78 }
79
80 $self->{'fields'} = \@unique;
81 }
82
f9a2a1d9 83 my @flds = @{ $self->{'fields'} || [] };
84
85 return wantarray ? @flds : \@flds;
25a02fa7 86}
87
e6c5fb6e 88sub tables {
89
90=pod
91
92=head2 tables
93
94Gets and set the tables the SELECT mentions. Accepts a string, list or
95arrayref; returns an array or array reference. Will unique the table
96names and keep them in order by the first occurrence of a field name.
97
98 $view->tables('foo');
99 $view->tables('foo', 'bar');
100 $view->tables( 'foo, bar' );
101 $view->tables( [ 'foo', 'bar' ] );
102 $view->tables( qw[ foo bar ] );
103
104 my @tables = $view->tables;
105
106=cut
107
108 my $self = shift;
109 my $tables = parse_list_arg( @_ );
110
111 if ( @$tables ) {
112 my ( %unique, @unique );
113 for my $t ( @$tables ) {
114 next if $unique{ $t }++;
115 push @unique, $t;
116 }
117
118 $self->{'tables'} = \@unique;
119 }
120
121 my @tbls = @{ $self->{'tables'} || [] };
122
123 return wantarray ? @tbls : \@tbls;
124}
125
126sub options {
127
128=pod
129
130=head2 options
131
132Gets and sets a list of options on the view.
133
134 $view->options('ALGORITHM=UNDEFINED');
135
136 my @options = $view->options;
137
138=cut
139
140 my $self = shift;
141 my $options = parse_list_arg( @_ );
142
143 if ( @$options ) {
144 my ( %unique, @unique );
145 for my $o ( @$options, @{ $self->{'options'} || [] } ) {
146 next if $unique{ $o }++;
147 push @unique, $o;
148 }
149
150 $self->{'options'} = \@unique;
151 }
152
153 my @opts = @{ $self->{'options'} || [] };
154
155 return wantarray ? @opts : \@opts;
156}
157
25a02fa7 158sub is_valid {
159
160=pod
161
162=head2 is_valid
163
164Determine whether the view is valid or not.
165
166 my $ok = $view->is_valid;
167
168=cut
169
170 my $self = shift;
171
172 return $self->error('No name') unless $self->name;
173 return $self->error('No sql') unless $self->sql;
174
175 return 1;
176}
177
3c5de62a 178sub name {
179
180=pod
181
182=head2 name
183
184Get or set the view's name.
185
186 my $name = $view->name('foo');
187
188=cut
189
190 my $self = shift;
191 $self->{'name'} = shift if @_;
192 return $self->{'name'} || '';
193}
194
25a02fa7 195sub order {
3c5de62a 196
197=pod
198
25a02fa7 199=head2 order
3c5de62a 200
25a02fa7 201Get or set the view's order.
3c5de62a 202
25a02fa7 203 my $order = $view->order(3);
3c5de62a 204
205=cut
206
25a02fa7 207 my ( $self, $arg ) = @_;
208
209 if ( defined $arg && $arg =~ /^\d+$/ ) {
210 $self->{'order'} = $arg;
211 }
212
213 return $self->{'order'} || 0;
3c5de62a 214}
215
25a02fa7 216sub sql {
3c5de62a 217
218=pod
219
25a02fa7 220=head2 sql
3c5de62a 221
25a02fa7 222Get or set the view's SQL.
3c5de62a 223
25a02fa7 224 my $sql = $view->sql('select * from foo');
3c5de62a 225
226=cut
227
25a02fa7 228 my $self = shift;
229 $self->{'sql'} = shift if @_;
230 return $self->{'sql'} || '';
231}
232
b039cab8 233sub schema {
234
235=pod
236
237=head2 schema
238
239Get or set the view's schema object.
240
241 $view->schema( $schema );
242 my $schema = $view->schema;
243
244=cut
245
246 my $self = shift;
247 if ( my $arg = shift ) {
248 return $self->error('Not a schema object') unless
249 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
250 $self->{'schema'} = $arg;
251 }
252
253 return $self->{'schema'};
254}
255
abf315bb 256sub equals {
257
258=pod
259
260=head2 equals
261
262Determines if this view is the same as another
263
264 my $isIdentical = $view1->equals( $view2 );
265
266=cut
267
268 my $self = shift;
269 my $other = shift;
6be9534b 270 my $case_insensitive = shift;
d1a895ce 271 my $ignore_sql = shift;
ea93df61 272
abf315bb 273 return 0 unless $self->SUPER::equals($other);
6be9534b 274 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
4598b71c 275 #return 0 unless $self->is_valid eq $other->is_valid;
ea93df61 276
d1a895ce 277 unless ($ignore_sql) {
278 my $selfSql = $self->sql;
279 my $otherSql = $other->sql;
280 # Remove comments
281 $selfSql =~ s/--.*$//mg;
282 $otherSql =~ s/--.*$//mg;
283 # Collapse whitespace to space to avoid whitespace comparison issues
284 $selfSql =~ s/\s+/ /sg;
285 $otherSql =~ s/\s+/ /sg;
286 return 0 unless $selfSql eq $otherSql;
287 }
ea93df61 288
6be9534b 289 my $selfFields = join(":", $self->fields);
290 my $otherFields = join(":", $other->fields);
291 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
4598b71c 292 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
abf315bb 293 return 1;
294}
295
25a02fa7 296sub DESTROY {
3c5de62a 297 my $self = shift;
b039cab8 298 undef $self->{'schema'}; # destroy cyclical reference
3c5de62a 299}
300
3011;
302
3c5de62a 303=pod
304
305=head1 AUTHOR
306
c3b0b535 307Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
3c5de62a 308
309=cut