Commit | Line | Data |
3c5de62a |
1 | package SQL::Translator::Schema::Index; |
2 | |
3 | # ---------------------------------------------------------------------- |
478f608d |
4 | # Copyright (C) 2002-2009 SQLFairy Authors |
3c5de62a |
5 | # |
6 | # This program is free software; you can redistribute it and/or |
7 | # modify it under the terms of the GNU General Public License as |
8 | # published by the Free Software Foundation; version 2. |
9 | # |
10 | # This program is distributed in the hope that it will be useful, but |
11 | # WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | # General Public License for more details. |
14 | # |
15 | # You should have received a copy of the GNU General Public License |
16 | # along with this program; if not, write to the Free Software |
17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
18 | # 02111-1307 USA |
19 | # ------------------------------------------------------------------- |
20 | |
21 | =pod |
22 | |
23 | =head1 NAME |
24 | |
25 | SQL::Translator::Schema::Index - SQL::Translator index object |
26 | |
27 | =head1 SYNOPSIS |
28 | |
29 | use SQL::Translator::Schema::Index; |
30 | my $index = SQL::Translator::Schema::Index->new( |
31 | name => 'foo', |
32 | fields => [ id ], |
33 | type => 'unique', |
34 | ); |
35 | |
36 | =head1 DESCRIPTION |
37 | |
38 | C<SQL::Translator::Schema::Index> is the index object. |
39 | |
b54199ae |
40 | Primary and unique keys are table constraints, not indices. |
3c5de62a |
41 | |
42 | =head1 METHODS |
43 | |
44 | =cut |
45 | |
46 | use strict; |
25868dc9 |
47 | use SQL::Translator::Schema::Constants; |
48 | use SQL::Translator::Utils 'parse_list_arg'; |
3c5de62a |
49 | |
b6a880d1 |
50 | use base 'SQL::Translator::Schema::Object'; |
51 | |
da06ac74 |
52 | use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); |
53 | |
4ab3763d |
54 | $VERSION = '1.59'; |
3c5de62a |
55 | |
c8efc003 |
56 | my %VALID_INDEX_TYPE = ( |
19ad0cee |
57 | UNIQUE => 1, |
58 | NORMAL => 1, |
ea09ae82 |
59 | FULLTEXT => 1, # MySQL only (?) |
19ad0cee |
60 | FULL_TEXT => 1, # MySQL only (?) |
61 | SPATIAL => 1, # MySQL only (?) |
c8efc003 |
62 | ); |
3c5de62a |
63 | |
64 | # ---------------------------------------------------------------------- |
9371be50 |
65 | |
66 | __PACKAGE__->_attributes( qw/ |
bdd8e79a |
67 | name type fields table options |
9371be50 |
68 | /); |
3c5de62a |
69 | |
70 | =pod |
71 | |
72 | =head2 new |
73 | |
74 | Object constructor. |
75 | |
76 | my $schema = SQL::Translator::Schema::Index->new; |
77 | |
78 | =cut |
79 | |
3c5de62a |
80 | # ---------------------------------------------------------------------- |
81 | sub fields { |
82 | |
83 | =pod |
84 | |
85 | =head2 fields |
86 | |
b54199ae |
87 | Gets and set the fields the index is on. Accepts a string, list or |
25868dc9 |
88 | arrayref; returns an array or array reference. Will unique the field |
89 | names and keep them in order by the first occurrence of a field name. |
3c5de62a |
90 | |
b54199ae |
91 | $index->fields('id'); |
92 | $index->fields('id', 'name'); |
93 | $index->fields( 'id, name' ); |
94 | $index->fields( [ 'id', 'name' ] ); |
95 | $index->fields( qw[ id name ] ); |
25868dc9 |
96 | |
b54199ae |
97 | my @fields = $index->fields; |
3c5de62a |
98 | |
99 | =cut |
100 | |
101 | my $self = shift; |
25868dc9 |
102 | my $fields = parse_list_arg( @_ ); |
3c5de62a |
103 | |
104 | if ( @$fields ) { |
25868dc9 |
105 | my ( %unique, @unique ); |
106 | for my $f ( @$fields ) { |
107 | next if $unique{ $f }; |
108 | $unique{ $f } = 1; |
109 | push @unique, $f; |
110 | } |
111 | |
112 | $self->{'fields'} = \@unique; |
3c5de62a |
113 | } |
114 | |
115 | return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'}; |
116 | } |
117 | |
118 | # ---------------------------------------------------------------------- |
b54199ae |
119 | sub is_valid { |
120 | |
121 | =pod |
122 | |
123 | =head2 is_valid |
124 | |
125 | Determine whether the index is valid or not. |
126 | |
127 | my $ok = $index->is_valid; |
128 | |
129 | =cut |
130 | |
131 | my $self = shift; |
132 | my $table = $self->table or return $self->error('No table'); |
133 | my @fields = $self->fields or return $self->error('No fields'); |
134 | |
135 | for my $field ( @fields ) { |
136 | return $self->error( |
137 | "Field '$field' does not exist in table '", $table->name, "'" |
138 | ) unless $table->get_field( $field ); |
139 | } |
140 | |
141 | return 1; |
142 | } |
143 | |
144 | # ---------------------------------------------------------------------- |
3c5de62a |
145 | sub name { |
146 | |
147 | =pod |
148 | |
149 | =head2 name |
150 | |
151 | Get or set the index's name. |
152 | |
153 | my $name = $index->name('foo'); |
154 | |
155 | =cut |
156 | |
157 | my $self = shift; |
158 | $self->{'name'} = shift if @_; |
159 | return $self->{'name'} || ''; |
160 | } |
161 | |
162 | # ---------------------------------------------------------------------- |
25868dc9 |
163 | sub options { |
164 | |
165 | =pod |
166 | |
167 | =head2 options |
168 | |
169 | Get or set the index's options (e.g., "using" or "where" for PG). Returns |
170 | an array or array reference. |
171 | |
172 | my @options = $index->options; |
173 | |
174 | =cut |
175 | |
176 | my $self = shift; |
177 | my $options = parse_list_arg( @_ ); |
178 | |
179 | push @{ $self->{'options'} }, @$options; |
180 | |
181 | if ( ref $self->{'options'} ) { |
182 | return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'}; |
183 | } |
184 | else { |
185 | return wantarray ? () : []; |
186 | } |
187 | } |
188 | |
189 | # ---------------------------------------------------------------------- |
43b9dc7a |
190 | sub table { |
191 | |
192 | =pod |
193 | |
194 | =head2 table |
195 | |
196 | Get or set the index's table object. |
197 | |
198 | my $table = $index->table; |
199 | |
200 | =cut |
201 | |
202 | my $self = shift; |
203 | if ( my $arg = shift ) { |
204 | return $self->error('Not a table object') unless |
205 | UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' ); |
206 | $self->{'table'} = $arg; |
207 | } |
208 | |
209 | return $self->{'table'}; |
210 | } |
211 | |
212 | # ---------------------------------------------------------------------- |
3c5de62a |
213 | sub type { |
214 | |
215 | =pod |
216 | |
217 | =head2 type |
218 | |
219 | Get or set the index's type. |
220 | |
221 | my $type = $index->type('unique'); |
222 | |
19ad0cee |
223 | Get or set the index's options (e.g., "using" or "where" for PG). Returns |
224 | |
225 | Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT, |
226 | and SPATIAL. The latter two might be MySQL-specific. While both lowercase |
227 | and uppercase types are acceptable input, this method returns the type in |
228 | uppercase. |
229 | |
3c5de62a |
230 | =cut |
231 | |
232 | my $self = shift; |
233 | |
19ad0cee |
234 | if ( my $type = uc shift ) { |
3c5de62a |
235 | return $self->error("Invalid index type: $type") |
c8efc003 |
236 | unless $VALID_INDEX_TYPE{ $type }; |
3c5de62a |
237 | $self->{'type'} = $type; |
238 | } |
239 | |
19ad0cee |
240 | return $self->{'type'} || 'NORMAL'; |
3c5de62a |
241 | } |
242 | |
abf315bb |
243 | # ---------------------------------------------------------------------- |
244 | sub equals { |
245 | |
246 | =pod |
247 | |
248 | =head2 equals |
249 | |
250 | Determines if this index is the same as another |
251 | |
252 | my $isIdentical = $index1->equals( $index2 ); |
253 | |
254 | =cut |
255 | |
256 | my $self = shift; |
257 | my $other = shift; |
258 | my $case_insensitive = shift; |
d990d84b |
259 | my $ignore_index_names = shift; |
abf315bb |
260 | |
261 | return 0 unless $self->SUPER::equals($other); |
da5a1bae |
262 | |
d990d84b |
263 | unless ($ignore_index_names) { |
da5a1bae |
264 | unless ((!$self->name && ($other->name eq $other->fields->[0])) || |
265 | (!$other->name && ($self->name eq $self->fields->[0]))) { |
d990d84b |
266 | return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name; |
da5a1bae |
267 | } |
d990d84b |
268 | } |
82f6b50e |
269 | #return 0 unless $self->is_valid eq $other->is_valid; |
abf315bb |
270 | return 0 unless $self->type eq $other->type; |
c243ec2b |
271 | |
272 | # Check fields, regardless of order |
273 | my %otherFields = (); # create a hash of the other fields |
274 | foreach my $otherField ($other->fields) { |
275 | $otherField = uc($otherField) if $case_insensitive; |
276 | $otherFields{$otherField} = 1; |
277 | } |
278 | foreach my $selfField ($self->fields) { # check for self fields in hash |
279 | $selfField = uc($selfField) if $case_insensitive; |
280 | return 0 unless $otherFields{$selfField}; |
281 | delete $otherFields{$selfField}; |
282 | } |
283 | # Check all other fields were accounted for |
284 | return 0 unless keys %otherFields == 0; |
285 | |
4598b71c |
286 | return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options); |
287 | return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra); |
abf315bb |
288 | return 1; |
289 | } |
3c5de62a |
290 | |
291 | # ---------------------------------------------------------------------- |
25868dc9 |
292 | sub DESTROY { |
3c5de62a |
293 | my $self = shift; |
25868dc9 |
294 | undef $self->{'table'}; # destroy cyclical reference |
3c5de62a |
295 | } |
296 | |
297 | 1; |
298 | |
299 | # ---------------------------------------------------------------------- |
300 | |
301 | =pod |
302 | |
303 | =head1 AUTHOR |
304 | |
6606c4c6 |
305 | Ken Y. Clark E<lt>kclark@cpan.orgE<gt>. |
3c5de62a |
306 | |
307 | =cut |