Remove copyright headers from individual scripts
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / View.pm
1 package SQL::Translator::Schema::View;
2
3 =pod
4
5 =head1 NAME
6
7 SQL::Translator::Schema::View - SQL::Translator view object
8
9 =head1 SYNOPSIS
10
11   use SQL::Translator::Schema::View;
12   my $view   = SQL::Translator::Schema::View->new(
13       name   => 'foo',                      # name, required
14       sql    => 'select id, name from foo', # SQL for view
15       fields => 'id, name',                 # field names in view
16   );
17
18 =head1 DESCRIPTION
19
20 C<SQL::Translator::Schema::View> is the view object.
21
22 =head1 METHODS
23
24 =cut
25
26 use strict;
27 use SQL::Translator::Utils 'parse_list_arg';
28
29 use base 'SQL::Translator::Schema::Object';
30
31 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
32
33 $VERSION = '1.59';
34
35 # ----------------------------------------------------------------------
36
37 __PACKAGE__->_attributes( qw/
38     name sql fields schema order
39 /);
40
41 =pod
42
43 =head2 new
44
45 Object constructor.
46
47   my $view = SQL::Translator::Schema::View->new;
48
49 =cut
50
51 # ----------------------------------------------------------------------
52 sub fields {
53
54 =pod
55
56 =head2 fields
57
58 Gets and set the fields the constraint is on.  Accepts a string, list or
59 arrayref; returns an array or array reference.  Will unique the field
60 names and keep them in order by the first occurrence of a field name.
61
62   $view->fields('id');
63   $view->fields('id', 'name');
64   $view->fields( 'id, name' );
65   $view->fields( [ 'id', 'name' ] );
66   $view->fields( qw[ id name ] );
67
68   my @fields = $view->fields;
69
70 =cut
71
72     my $self   = shift;
73     my $fields = parse_list_arg( @_ );
74
75     if ( @$fields ) {
76         my ( %unique, @unique );
77         for my $f ( @$fields ) {
78             next if $unique{ $f };
79             $unique{ $f } = 1;
80             push @unique, $f;
81         }
82
83         $self->{'fields'} = \@unique;
84     }
85
86     return wantarray ? @{ $self->{'fields'} || [] } : ($self->{'fields'} || '');
87 }
88
89 # ----------------------------------------------------------------------
90 sub is_valid {
91
92 =pod
93
94 =head2 is_valid
95
96 Determine whether the view is valid or not.
97
98   my $ok = $view->is_valid;
99
100 =cut
101
102     my $self = shift;
103
104     return $self->error('No name') unless $self->name;
105     return $self->error('No sql')  unless $self->sql;
106
107     return 1;
108 }
109
110 # ----------------------------------------------------------------------
111 sub name {
112
113 =pod
114
115 =head2 name
116
117 Get or set the view's name.
118
119   my $name = $view->name('foo');
120
121 =cut
122
123     my $self        = shift;
124     $self->{'name'} = shift if @_;
125     return $self->{'name'} || '';
126 }
127
128 # ----------------------------------------------------------------------
129 sub order {
130
131 =pod
132
133 =head2 order
134
135 Get or set the view's order.
136
137   my $order = $view->order(3);
138
139 =cut
140
141     my ( $self, $arg ) = @_;
142
143     if ( defined $arg && $arg =~ /^\d+$/ ) {
144         $self->{'order'} = $arg;
145     }
146
147     return $self->{'order'} || 0;
148 }
149
150 # ----------------------------------------------------------------------
151 sub sql {
152
153 =pod
154
155 =head2 sql
156
157 Get or set the view's SQL.
158
159   my $sql = $view->sql('select * from foo');
160
161 =cut
162
163     my $self       = shift;
164     $self->{'sql'} = shift if @_;
165     return $self->{'sql'} || '';
166 }
167
168 # ----------------------------------------------------------------------
169 sub schema {
170
171 =pod
172
173 =head2 schema
174
175 Get or set the view's schema object.
176
177   $view->schema( $schema );
178   my $schema = $view->schema;
179
180 =cut
181
182     my $self = shift;
183     if ( my $arg = shift ) {
184         return $self->error('Not a schema object') unless
185             UNIVERSAL::isa( $arg, 'SQL::Translator::Schema' );
186         $self->{'schema'} = $arg;
187     }
188
189     return $self->{'schema'};
190 }
191
192 # ----------------------------------------------------------------------
193 sub equals {
194
195 =pod
196
197 =head2 equals
198
199 Determines if this view is the same as another
200
201   my $isIdentical = $view1->equals( $view2 );
202
203 =cut
204
205     my $self = shift;
206     my $other = shift;
207     my $case_insensitive = shift;
208     my $ignore_sql = shift;
209     
210     return 0 unless $self->SUPER::equals($other);
211     return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
212     #return 0 unless $self->is_valid eq $other->is_valid;
213     
214     unless ($ignore_sql) {
215         my $selfSql = $self->sql;
216         my $otherSql = $other->sql;
217         # Remove comments
218         $selfSql =~ s/--.*$//mg;
219         $otherSql =~ s/--.*$//mg;
220         # Collapse whitespace to space to avoid whitespace comparison issues
221         $selfSql =~ s/\s+/ /sg;
222         $otherSql =~ s/\s+/ /sg;
223         return 0 unless $selfSql eq $otherSql;
224     }
225     
226     my $selfFields = join(":", $self->fields);
227     my $otherFields = join(":", $other->fields);
228     return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
229     return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
230     return 1;
231 }
232
233 # ----------------------------------------------------------------------
234 sub DESTROY {
235     my $self = shift;
236     undef $self->{'schema'}; # destroy cyclical reference
237 }
238
239 1;
240
241 # ----------------------------------------------------------------------
242
243 =pod
244
245 =head1 AUTHOR
246
247 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
248
249 =cut