Bumping version to 1.62
[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
90097ddd 26use Moo;
0fb58589 27use SQL::Translator::Utils qw(ex2err);
52068c95 28use SQL::Translator::Types qw(schema_obj);
0fb58589 29use SQL::Translator::Role::ListAttr;
68d75205 30use Sub::Quote qw(quote_sub);
3c5de62a 31
954ed12e 32extends 'SQL::Translator::Schema::Object';
b6a880d1 33
f769b7e8 34our $VERSION = '1.62';
3c5de62a 35
3c5de62a 36=head2 new
37
38Object constructor.
39
b039cab8 40 my $view = SQL::Translator::Schema::View->new;
3c5de62a 41
25a02fa7 42=head2 fields
43
44Gets and set the fields the constraint is on. Accepts a string, list or
45arrayref; returns an array or array reference. Will unique the field
46names and keep them in order by the first occurrence of a field name.
47
48 $view->fields('id');
49 $view->fields('id', 'name');
50 $view->fields( 'id, name' );
51 $view->fields( [ 'id', 'name' ] );
52 $view->fields( qw[ id name ] );
53
54 my @fields = $view->fields;
55
56=cut
57
0fb58589 58with ListAttr fields => ( uniq => 1 );
e6c5fb6e 59
60=head2 tables
61
62Gets and set the tables the SELECT mentions. Accepts a string, list or
63arrayref; returns an array or array reference. Will unique the table
64names and keep them in order by the first occurrence of a field name.
65
66 $view->tables('foo');
67 $view->tables('foo', 'bar');
68 $view->tables( 'foo, bar' );
69 $view->tables( [ 'foo', 'bar' ] );
70 $view->tables( qw[ foo bar ] );
71
72 my @tables = $view->tables;
73
74=cut
75
0fb58589 76with ListAttr tables => ( uniq => 1 );
e6c5fb6e 77
78=head2 options
79
6d060264 80Gets or appends a list of options on the view.
e6c5fb6e 81
82 $view->options('ALGORITHM=UNDEFINED');
83
84 my @options = $view->options;
85
86=cut
87
0fb58589 88with ListAttr options => ( uniq => 1, append => 1 );
e6c5fb6e 89
25a02fa7 90sub is_valid {
91
92=pod
93
94=head2 is_valid
95
96Determine 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
3c5de62a 110=head2 name
111
112Get or set the view's name.
113
114 my $name = $view->name('foo');
115
116=cut
117
68d75205 118has name => ( is => 'rw', default => quote_sub(q{ '' }) );
3c5de62a 119
25a02fa7 120=head2 order
3c5de62a 121
25a02fa7 122Get or set the view's order.
3c5de62a 123
25a02fa7 124 my $order = $view->order(3);
3c5de62a 125
126=cut
127
68d75205 128has order => ( is => 'rw', default => quote_sub(q{ 0 }) );
52068c95 129
130around order => sub {
131 my ( $orig, $self, $arg ) = @_;
25a02fa7 132
133 if ( defined $arg && $arg =~ /^\d+$/ ) {
52068c95 134 return $self->$orig($arg);
25a02fa7 135 }
136
52068c95 137 return $self->$orig;
138};
3c5de62a 139
25a02fa7 140=head2 sql
3c5de62a 141
25a02fa7 142Get or set the view's SQL.
3c5de62a 143
25a02fa7 144 my $sql = $view->sql('select * from foo');
3c5de62a 145
146=cut
147
68d75205 148has sql => ( is => 'rw', default => quote_sub(q{ '' }) );
b039cab8 149
150=head2 schema
151
152Get or set the view's schema object.
153
154 $view->schema( $schema );
155 my $schema = $view->schema;
156
157=cut
158
a5bfeba8 159has schema => ( is => 'rw', isa => schema_obj('Schema'), weak_ref => 1 );
b039cab8 160
52068c95 161around schema => \&ex2err;
abf315bb 162
163=head2 equals
164
165Determines if this view is the same as another
166
167 my $isIdentical = $view1->equals( $view2 );
168
169=cut
170
52068c95 171around equals => sub {
172 my $orig = shift;
abf315bb 173 my $self = shift;
174 my $other = shift;
6be9534b 175 my $case_insensitive = shift;
d1a895ce 176 my $ignore_sql = shift;
ea93df61 177
52068c95 178 return 0 unless $self->$orig($other);
6be9534b 179 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
4598b71c 180 #return 0 unless $self->is_valid eq $other->is_valid;
ea93df61 181
d1a895ce 182 unless ($ignore_sql) {
183 my $selfSql = $self->sql;
184 my $otherSql = $other->sql;
185 # Remove comments
186 $selfSql =~ s/--.*$//mg;
187 $otherSql =~ s/--.*$//mg;
188 # Collapse whitespace to space to avoid whitespace comparison issues
189 $selfSql =~ s/\s+/ /sg;
190 $otherSql =~ s/\s+/ /sg;
191 return 0 unless $selfSql eq $otherSql;
192 }
ea93df61 193
6be9534b 194 my $selfFields = join(":", $self->fields);
195 my $otherFields = join(":", $other->fields);
196 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
4598b71c 197 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
abf315bb 198 return 1;
52068c95 199};
abf315bb 200
52068c95 201# Must come after all 'has' declarations
202around new => \&ex2err;
203
3c5de62a 2041;
205
3c5de62a 206=pod
207
208=head1 AUTHOR
209
c3b0b535 210Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
3c5de62a 211
212=cut