take out duplicate docs
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / View.pm
CommitLineData
3c5de62a 1package SQL::Translator::Schema::View;
2
44659089 3# ----------------------------------------------------------------------
4# Copyright (C) 2002-2009 SQLFairy Authors
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
3c5de62a 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
9371be50 53__PACKAGE__->_attributes( qw/
54 name sql fields schema order
55/);
3c5de62a 56
57=pod
58
59=head2 new
60
61Object constructor.
62
b039cab8 63 my $view = SQL::Translator::Schema::View->new;
3c5de62a 64
65=cut
66
25a02fa7 67sub fields {
68
69=pod
70
71=head2 fields
72
73Gets and set the fields the constraint is on. Accepts a string, list or
74arrayref; returns an array or array reference. Will unique the field
75names and keep them in order by the first occurrence of a field name.
76
77 $view->fields('id');
78 $view->fields('id', 'name');
79 $view->fields( 'id, name' );
80 $view->fields( [ 'id', 'name' ] );
81 $view->fields( qw[ id name ] );
82
83 my @fields = $view->fields;
84
85=cut
86
87 my $self = shift;
88 my $fields = parse_list_arg( @_ );
89
90 if ( @$fields ) {
91 my ( %unique, @unique );
92 for my $f ( @$fields ) {
93 next if $unique{ $f };
94 $unique{ $f } = 1;
95 push @unique, $f;
96 }
97
98 $self->{'fields'} = \@unique;
99 }
100
4598b71c 101 return wantarray ? @{ $self->{'fields'} || [] } : ($self->{'fields'} || '');
25a02fa7 102}
103
25a02fa7 104sub is_valid {
105
106=pod
107
108=head2 is_valid
109
110Determine whether the view is valid or not.
111
112 my $ok = $view->is_valid;
113
114=cut
115
116 my $self = shift;
117
118 return $self->error('No name') unless $self->name;
119 return $self->error('No sql') unless $self->sql;
120
121 return 1;
122}
123
3c5de62a 124sub name {
125
126=pod
127
128=head2 name
129
130Get or set the view's name.
131
132 my $name = $view->name('foo');
133
134=cut
135
136 my $self = shift;
137 $self->{'name'} = shift if @_;
138 return $self->{'name'} || '';
139}
140
25a02fa7 141sub order {
3c5de62a 142
143=pod
144
25a02fa7 145=head2 order
3c5de62a 146
25a02fa7 147Get or set the view's order.
3c5de62a 148
25a02fa7 149 my $order = $view->order(3);
3c5de62a 150
151=cut
152
25a02fa7 153 my ( $self, $arg ) = @_;
154
155 if ( defined $arg && $arg =~ /^\d+$/ ) {
156 $self->{'order'} = $arg;
157 }
158
159 return $self->{'order'} || 0;
3c5de62a 160}
161
25a02fa7 162sub sql {
3c5de62a 163
164=pod
165
25a02fa7 166=head2 sql
3c5de62a 167
25a02fa7 168Get or set the view's SQL.
3c5de62a 169
25a02fa7 170 my $sql = $view->sql('select * from foo');
3c5de62a 171
172=cut
173
25a02fa7 174 my $self = shift;
175 $self->{'sql'} = shift if @_;
176 return $self->{'sql'} || '';
177}
178
b039cab8 179sub schema {
180
181=pod
182
183=head2 schema
184
185Get or set the view's schema object.
186
187 $view->schema( $schema );
188 my $schema = $view->schema;
189
190=cut
191
192 my $self = shift;
193 if ( my $arg = shift ) {
194 return $self->error('Not a schema object') unless
195 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
196 $self->{'schema'} = $arg;
197 }
198
199 return $self->{'schema'};
200}
201
abf315bb 202sub equals {
203
204=pod
205
206=head2 equals
207
208Determines if this view is the same as another
209
210 my $isIdentical = $view1->equals( $view2 );
211
212=cut
213
214 my $self = shift;
215 my $other = shift;
6be9534b 216 my $case_insensitive = shift;
d1a895ce 217 my $ignore_sql = shift;
ea93df61 218
abf315bb 219 return 0 unless $self->SUPER::equals($other);
6be9534b 220 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
4598b71c 221 #return 0 unless $self->is_valid eq $other->is_valid;
ea93df61 222
d1a895ce 223 unless ($ignore_sql) {
224 my $selfSql = $self->sql;
225 my $otherSql = $other->sql;
226 # Remove comments
227 $selfSql =~ s/--.*$//mg;
228 $otherSql =~ s/--.*$//mg;
229 # Collapse whitespace to space to avoid whitespace comparison issues
230 $selfSql =~ s/\s+/ /sg;
231 $otherSql =~ s/\s+/ /sg;
232 return 0 unless $selfSql eq $otherSql;
233 }
ea93df61 234
6be9534b 235 my $selfFields = join(":", $self->fields);
236 my $otherFields = join(":", $other->fields);
237 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
4598b71c 238 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
abf315bb 239 return 1;
240}
241
25a02fa7 242sub DESTROY {
3c5de62a 243 my $self = shift;
b039cab8 244 undef $self->{'schema'}; # destroy cyclical reference
3c5de62a 245}
246
2471;
248
3c5de62a 249=pod
250
251=head1 AUTHOR
252
c3b0b535 253Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
3c5de62a 254
255=cut