Changes + Reverts for 0.11000, see Changes file for info
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / View.pm
CommitLineData
3c5de62a 1package SQL::Translator::Schema::View;
2
3# ----------------------------------------------------------------------
478f608d 4# Copyright (C) 2002-2009 SQLFairy Authors
3c5de62a 5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License as
8# published by the Free Software Foundation; version 2.
9#
10# This program is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18# 02111-1307 USA
19# -------------------------------------------------------------------
20
21=pod
22
23=head1 NAME
24
25SQL::Translator::Schema::View - SQL::Translator view object
26
27=head1 SYNOPSIS
28
29 use SQL::Translator::Schema::View;
b039cab8 30 my $view = SQL::Translator::Schema::View->new(
31 name => 'foo', # name, required
32 sql => 'select id, name from foo', # SQL for view
33 fields => 'id, name', # field names in view
3c5de62a 34 );
35
36=head1 DESCRIPTION
37
38C<SQL::Translator::Schema::View> is the view object.
39
40=head1 METHODS
41
42=cut
43
44use strict;
25a02fa7 45use SQL::Translator::Utils 'parse_list_arg';
3c5de62a 46
b6a880d1 47use base 'SQL::Translator::Schema::Object';
48
da06ac74 49use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
50
11ad2df9 51$VERSION = '1.59';
3c5de62a 52
53# ----------------------------------------------------------------------
9371be50 54
55__PACKAGE__->_attributes( qw/
56 name sql fields schema order
57/);
3c5de62a 58
59=pod
60
61=head2 new
62
63Object constructor.
64
b039cab8 65 my $view = SQL::Translator::Schema::View->new;
3c5de62a 66
67=cut
68
3c5de62a 69# ----------------------------------------------------------------------
25a02fa7 70sub fields {
71
72=pod
73
74=head2 fields
75
76Gets and set the fields the constraint is on. Accepts a string, list or
77arrayref; returns an array or array reference. Will unique the field
78names and keep them in order by the first occurrence of a field name.
79
80 $view->fields('id');
81 $view->fields('id', 'name');
82 $view->fields( 'id, name' );
83 $view->fields( [ 'id', 'name' ] );
84 $view->fields( qw[ id name ] );
85
86 my @fields = $view->fields;
87
88=cut
89
90 my $self = shift;
91 my $fields = parse_list_arg( @_ );
92
93 if ( @$fields ) {
94 my ( %unique, @unique );
95 for my $f ( @$fields ) {
96 next if $unique{ $f };
97 $unique{ $f } = 1;
98 push @unique, $f;
99 }
100
101 $self->{'fields'} = \@unique;
102 }
103
4598b71c 104 return wantarray ? @{ $self->{'fields'} || [] } : ($self->{'fields'} || '');
25a02fa7 105}
106
107# ----------------------------------------------------------------------
108sub is_valid {
109
110=pod
111
112=head2 is_valid
113
114Determine whether the view is valid or not.
115
116 my $ok = $view->is_valid;
117
118=cut
119
120 my $self = shift;
121
122 return $self->error('No name') unless $self->name;
123 return $self->error('No sql') unless $self->sql;
124
125 return 1;
126}
127
128# ----------------------------------------------------------------------
3c5de62a 129sub name {
130
131=pod
132
133=head2 name
134
135Get or set the view's name.
136
137 my $name = $view->name('foo');
138
139=cut
140
141 my $self = shift;
142 $self->{'name'} = shift if @_;
143 return $self->{'name'} || '';
144}
145
146# ----------------------------------------------------------------------
25a02fa7 147sub order {
3c5de62a 148
149=pod
150
25a02fa7 151=head2 order
3c5de62a 152
25a02fa7 153Get or set the view's order.
3c5de62a 154
25a02fa7 155 my $order = $view->order(3);
3c5de62a 156
157=cut
158
25a02fa7 159 my ( $self, $arg ) = @_;
160
161 if ( defined $arg && $arg =~ /^\d+$/ ) {
162 $self->{'order'} = $arg;
163 }
164
165 return $self->{'order'} || 0;
3c5de62a 166}
167
168# ----------------------------------------------------------------------
25a02fa7 169sub sql {
3c5de62a 170
171=pod
172
25a02fa7 173=head2 sql
3c5de62a 174
25a02fa7 175Get or set the view's SQL.
3c5de62a 176
25a02fa7 177 my $sql = $view->sql('select * from foo');
3c5de62a 178
179=cut
180
25a02fa7 181 my $self = shift;
182 $self->{'sql'} = shift if @_;
183 return $self->{'sql'} || '';
184}
185
186# ----------------------------------------------------------------------
b039cab8 187sub schema {
188
189=pod
190
191=head2 schema
192
193Get or set the view's schema object.
194
195 $view->schema( $schema );
196 my $schema = $view->schema;
197
198=cut
199
200 my $self = shift;
201 if ( my $arg = shift ) {
202 return $self->error('Not a schema object') unless
203 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
204 $self->{'schema'} = $arg;
205 }
206
207 return $self->{'schema'};
208}
209
b039cab8 210# ----------------------------------------------------------------------
abf315bb 211sub equals {
212
213=pod
214
215=head2 equals
216
217Determines if this view is the same as another
218
219 my $isIdentical = $view1->equals( $view2 );
220
221=cut
222
223 my $self = shift;
224 my $other = shift;
6be9534b 225 my $case_insensitive = shift;
d1a895ce 226 my $ignore_sql = shift;
abf315bb 227
228 return 0 unless $self->SUPER::equals($other);
6be9534b 229 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
4598b71c 230 #return 0 unless $self->is_valid eq $other->is_valid;
deee3ae8 231
d1a895ce 232 unless ($ignore_sql) {
233 my $selfSql = $self->sql;
234 my $otherSql = $other->sql;
235 # Remove comments
236 $selfSql =~ s/--.*$//mg;
237 $otherSql =~ s/--.*$//mg;
238 # Collapse whitespace to space to avoid whitespace comparison issues
239 $selfSql =~ s/\s+/ /sg;
240 $otherSql =~ s/\s+/ /sg;
241 return 0 unless $selfSql eq $otherSql;
242 }
deee3ae8 243
6be9534b 244 my $selfFields = join(":", $self->fields);
245 my $otherFields = join(":", $other->fields);
246 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
4598b71c 247 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
abf315bb 248 return 1;
249}
250
251# ----------------------------------------------------------------------
25a02fa7 252sub DESTROY {
3c5de62a 253 my $self = shift;
b039cab8 254 undef $self->{'schema'}; # destroy cyclical reference
3c5de62a 255}
256
2571;
258
259# ----------------------------------------------------------------------
260
261=pod
262
263=head1 AUTHOR
264
c3b0b535 265Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
3c5de62a 266
267=cut