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