use warnings
[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
da06ac74 32use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
33
11ad2df9 34$VERSION = '1.59';
3c5de62a 35
9371be50 36__PACKAGE__->_attributes( qw/
37 name sql fields schema order
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 ) {
76 next if $unique{ $f };
77 $unique{ $f } = 1;
78 push @unique, $f;
79 }
80
81 $self->{'fields'} = \@unique;
82 }
83
4598b71c 84 return wantarray ? @{ $self->{'fields'} || [] } : ($self->{'fields'} || '');
25a02fa7 85}
86
25a02fa7 87sub is_valid {
88
89=pod
90
91=head2 is_valid
92
93Determine whether the view is valid or not.
94
95 my $ok = $view->is_valid;
96
97=cut
98
99 my $self = shift;
100
101 return $self->error('No name') unless $self->name;
102 return $self->error('No sql') unless $self->sql;
103
104 return 1;
105}
106
3c5de62a 107sub name {
108
109=pod
110
111=head2 name
112
113Get or set the view's name.
114
115 my $name = $view->name('foo');
116
117=cut
118
119 my $self = shift;
120 $self->{'name'} = shift if @_;
121 return $self->{'name'} || '';
122}
123
25a02fa7 124sub order {
3c5de62a 125
126=pod
127
25a02fa7 128=head2 order
3c5de62a 129
25a02fa7 130Get or set the view's order.
3c5de62a 131
25a02fa7 132 my $order = $view->order(3);
3c5de62a 133
134=cut
135
25a02fa7 136 my ( $self, $arg ) = @_;
137
138 if ( defined $arg && $arg =~ /^\d+$/ ) {
139 $self->{'order'} = $arg;
140 }
141
142 return $self->{'order'} || 0;
3c5de62a 143}
144
25a02fa7 145sub sql {
3c5de62a 146
147=pod
148
25a02fa7 149=head2 sql
3c5de62a 150
25a02fa7 151Get or set the view's SQL.
3c5de62a 152
25a02fa7 153 my $sql = $view->sql('select * from foo');
3c5de62a 154
155=cut
156
25a02fa7 157 my $self = shift;
158 $self->{'sql'} = shift if @_;
159 return $self->{'sql'} || '';
160}
161
b039cab8 162sub schema {
163
164=pod
165
166=head2 schema
167
168Get or set the view's schema object.
169
170 $view->schema( $schema );
171 my $schema = $view->schema;
172
173=cut
174
175 my $self = shift;
176 if ( my $arg = shift ) {
177 return $self->error('Not a schema object') unless
178 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
179 $self->{'schema'} = $arg;
180 }
181
182 return $self->{'schema'};
183}
184
abf315bb 185sub equals {
186
187=pod
188
189=head2 equals
190
191Determines if this view is the same as another
192
193 my $isIdentical = $view1->equals( $view2 );
194
195=cut
196
197 my $self = shift;
198 my $other = shift;
6be9534b 199 my $case_insensitive = shift;
d1a895ce 200 my $ignore_sql = shift;
ea93df61 201
abf315bb 202 return 0 unless $self->SUPER::equals($other);
6be9534b 203 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
4598b71c 204 #return 0 unless $self->is_valid eq $other->is_valid;
ea93df61 205
d1a895ce 206 unless ($ignore_sql) {
207 my $selfSql = $self->sql;
208 my $otherSql = $other->sql;
209 # Remove comments
210 $selfSql =~ s/--.*$//mg;
211 $otherSql =~ s/--.*$//mg;
212 # Collapse whitespace to space to avoid whitespace comparison issues
213 $selfSql =~ s/\s+/ /sg;
214 $otherSql =~ s/\s+/ /sg;
215 return 0 unless $selfSql eq $otherSql;
216 }
ea93df61 217
6be9534b 218 my $selfFields = join(":", $self->fields);
219 my $otherFields = join(":", $other->fields);
220 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
4598b71c 221 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
abf315bb 222 return 1;
223}
224
25a02fa7 225sub DESTROY {
3c5de62a 226 my $self = shift;
b039cab8 227 undef $self->{'schema'}; # destroy cyclical reference
3c5de62a 228}
229
2301;
231
3c5de62a 232=pod
233
234=head1 AUTHOR
235
c3b0b535 236Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
3c5de62a 237
238=cut