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