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