Force everything to 1.99, hopefully will work
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / View.pm
1 package SQL::Translator::Schema::View;
2
3 # ----------------------------------------------------------------------
4 # $Id: View.pm 1440 2009-01-17 16:31:57Z jawnsy $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2002-2009 SQLFairy Authors
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
27 SQL::Translator::Schema::View - SQL::Translator view object
28
29 =head1 SYNOPSIS
30
31   use SQL::Translator::Schema::View;
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
36   );
37
38 =head1 DESCRIPTION
39
40 C<SQL::Translator::Schema::View> is the view object.
41
42 =head1 METHODS
43
44 =cut
45
46 use strict;
47 use SQL::Translator::Utils 'parse_list_arg';
48
49 use base 'SQL::Translator::Schema::Object';
50
51 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
52
53 $VERSION = '1.99';
54
55 # ----------------------------------------------------------------------
56
57 __PACKAGE__->_attributes( qw/
58     name sql fields schema order
59 /);
60
61 =pod
62
63 =head2 new
64
65 Object constructor.
66
67   my $view = SQL::Translator::Schema::View->new;
68
69 =cut
70
71 # ----------------------------------------------------------------------
72 sub fields {
73
74 =pod
75
76 =head2 fields
77
78 Gets and set the fields the constraint is on.  Accepts a string, list or
79 arrayref; returns an array or array reference.  Will unique the field
80 names 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
106     return wantarray ? @{ $self->{'fields'} || [] } : ($self->{'fields'} || '');
107 }
108
109 # ----------------------------------------------------------------------
110 sub is_valid {
111
112 =pod
113
114 =head2 is_valid
115
116 Determine 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 # ----------------------------------------------------------------------
131 sub name {
132
133 =pod
134
135 =head2 name
136
137 Get 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 # ----------------------------------------------------------------------
149 sub order {
150
151 =pod
152
153 =head2 order
154
155 Get or set the view's order.
156
157   my $order = $view->order(3);
158
159 =cut
160
161     my ( $self, $arg ) = @_;
162
163     if ( defined $arg && $arg =~ /^\d+$/ ) {
164         $self->{'order'} = $arg;
165     }
166
167     return $self->{'order'} || 0;
168 }
169
170 # ----------------------------------------------------------------------
171 sub sql {
172
173 =pod
174
175 =head2 sql
176
177 Get or set the view's SQL.
178
179   my $sql = $view->sql('select * from foo');
180
181 =cut
182
183     my $self       = shift;
184     $self->{'sql'} = shift if @_;
185     return $self->{'sql'} || '';
186 }
187
188 # ----------------------------------------------------------------------
189 sub schema {
190
191 =pod
192
193 =head2 schema
194
195 Get 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
212 # ----------------------------------------------------------------------
213 sub equals {
214
215 =pod
216
217 =head2 equals
218
219 Determines 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     my $case_insensitive = shift;
228     my $ignore_sql = shift;
229     
230     return 0 unless $self->SUPER::equals($other);
231     return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
232     #return 0 unless $self->is_valid eq $other->is_valid;
233     
234     unless ($ignore_sql) {
235         my $selfSql = $self->sql;
236         my $otherSql = $other->sql;
237         # Remove comments
238         $selfSql =~ s/--.*$//mg;
239         $otherSql =~ s/--.*$//mg;
240         # Collapse whitespace to space to avoid whitespace comparison issues
241         $selfSql =~ s/\s+/ /sg;
242         $otherSql =~ s/\s+/ /sg;
243         return 0 unless $selfSql eq $otherSql;
244     }
245     
246     my $selfFields = join(":", $self->fields);
247     my $otherFields = join(":", $other->fields);
248     return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
249     return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
250     return 1;
251 }
252
253 # ----------------------------------------------------------------------
254 sub DESTROY {
255     my $self = shift;
256     undef $self->{'schema'}; # destroy cyclical reference
257 }
258
259 1;
260
261 # ----------------------------------------------------------------------
262
263 =pod
264
265 =head1 AUTHOR
266
267 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
268
269 =cut