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