Whitespace
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / View.pm
1 package SQL::Translator::Schema::View;
2
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
21 =pod
22
23 =head1 NAME
24
25 SQL::Translator::Schema::View - SQL::Translator view object
26
27 =head1 SYNOPSIS
28
29   use SQL::Translator::Schema::View;
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
34   );
35
36 =head1 DESCRIPTION
37
38 C<SQL::Translator::Schema::View> is the view object.
39
40 =head1 METHODS
41
42 =cut
43
44 use strict;
45 use SQL::Translator::Utils 'parse_list_arg';
46
47 use base 'SQL::Translator::Schema::Object';
48
49 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
50
51 $VERSION = '1.59';
52
53 # ----------------------------------------------------------------------
54
55 __PACKAGE__->_attributes( qw/
56     name sql fields schema order
57 /);
58
59 =pod
60
61 =head2 new
62
63 Object constructor.
64
65   my $view = SQL::Translator::Schema::View->new;
66
67 =cut
68
69 # ----------------------------------------------------------------------
70 sub fields {
71
72 =pod
73
74 =head2 fields
75
76 Gets and set the fields the constraint is on.  Accepts a string, list or
77 arrayref; returns an array or array reference.  Will unique the field
78 names 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
104     return wantarray ? @{ $self->{'fields'} || [] } : ($self->{'fields'} || '');
105 }
106
107 # ----------------------------------------------------------------------
108 sub is_valid {
109
110 =pod
111
112 =head2 is_valid
113
114 Determine 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 # ----------------------------------------------------------------------
129 sub name {
130
131 =pod
132
133 =head2 name
134
135 Get 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 # ----------------------------------------------------------------------
147 sub order {
148
149 =pod
150
151 =head2 order
152
153 Get or set the view's order.
154
155   my $order = $view->order(3);
156
157 =cut
158
159     my ( $self, $arg ) = @_;
160
161     if ( defined $arg && $arg =~ /^\d+$/ ) {
162         $self->{'order'} = $arg;
163     }
164
165     return $self->{'order'} || 0;
166 }
167
168 # ----------------------------------------------------------------------
169 sub sql {
170
171 =pod
172
173 =head2 sql
174
175 Get or set the view's SQL.
176
177   my $sql = $view->sql('select * from foo');
178
179 =cut
180
181     my $self       = shift;
182     $self->{'sql'} = shift if @_;
183     return $self->{'sql'} || '';
184 }
185
186 # ----------------------------------------------------------------------
187 sub schema {
188
189 =pod
190
191 =head2 schema
192
193 Get 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
210 # ----------------------------------------------------------------------
211 sub equals {
212
213 =pod
214
215 =head2 equals
216
217 Determines 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;
225     my $case_insensitive = shift;
226     my $ignore_sql = shift;
227
228     return 0 unless $self->SUPER::equals($other);
229     return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
230     #return 0 unless $self->is_valid eq $other->is_valid;
231
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     }
243
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;
247     return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
248     return 1;
249 }
250
251 # ----------------------------------------------------------------------
252 sub DESTROY {
253     my $self = shift;
254     undef $self->{'schema'}; # destroy cyclical reference
255 }
256
257 1;
258
259 # ----------------------------------------------------------------------
260
261 =pod
262
263 =head1 AUTHOR
264
265 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
266
267 =cut