take out duplicate docs
[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 __PACKAGE__->_attributes( qw/
54     name sql fields schema order
55 /);
56
57 =pod
58
59 =head2 new
60
61 Object constructor.
62
63   my $view = SQL::Translator::Schema::View->new;
64
65 =cut
66
67 sub fields {
68
69 =pod
70
71 =head2 fields
72
73 Gets and set the fields the constraint is on.  Accepts a string, list or
74 arrayref; returns an array or array reference.  Will unique the field
75 names 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
101     return wantarray ? @{ $self->{'fields'} || [] } : ($self->{'fields'} || '');
102 }
103
104 sub is_valid {
105
106 =pod
107
108 =head2 is_valid
109
110 Determine 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
124 sub name {
125
126 =pod
127
128 =head2 name
129
130 Get 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
141 sub order {
142
143 =pod
144
145 =head2 order
146
147 Get or set the view's order.
148
149   my $order = $view->order(3);
150
151 =cut
152
153     my ( $self, $arg ) = @_;
154
155     if ( defined $arg && $arg =~ /^\d+$/ ) {
156         $self->{'order'} = $arg;
157     }
158
159     return $self->{'order'} || 0;
160 }
161
162 sub sql {
163
164 =pod
165
166 =head2 sql
167
168 Get or set the view's SQL.
169
170   my $sql = $view->sql('select * from foo');
171
172 =cut
173
174     my $self       = shift;
175     $self->{'sql'} = shift if @_;
176     return $self->{'sql'} || '';
177 }
178
179 sub schema {
180
181 =pod
182
183 =head2 schema
184
185 Get 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
202 sub equals {
203
204 =pod
205
206 =head2 equals
207
208 Determines 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;
216     my $case_insensitive = shift;
217     my $ignore_sql = shift;
218
219     return 0 unless $self->SUPER::equals($other);
220     return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
221     #return 0 unless $self->is_valid eq $other->is_valid;
222
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     }
234
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;
238     return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
239     return 1;
240 }
241
242 sub DESTROY {
243     my $self = shift;
244     undef $self->{'schema'}; # destroy cyclical reference
245 }
246
247 1;
248
249 =pod
250
251 =head1 AUTHOR
252
253 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
254
255 =cut