Commit | Line | Data |
3c5de62a |
1 | package SQL::Translator::Schema::Index; |
2 | |
44659089 |
3 | # ---------------------------------------------------------------------- |
4 | # Copyright (C) 2002-2009 SQLFairy Authors |
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 | |
3c5de62a |
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 | |
11ad2df9 |
54 | $VERSION = '1.59'; |
3c5de62a |
55 | |
11ad2df9 |
56 | my %VALID_INDEX_TYPE = ( |
57 | UNIQUE => 1, |
58 | NORMAL => 1, |
59 | FULLTEXT => 1, # MySQL only (?) |
60 | FULL_TEXT => 1, # MySQL only (?) |
61 | SPATIAL => 1, # MySQL only (?) |
c8efc003 |
62 | ); |
3c5de62a |
63 | |
9371be50 |
64 | __PACKAGE__->_attributes( qw/ |
bdd8e79a |
65 | name type fields table options |
9371be50 |
66 | /); |
3c5de62a |
67 | |
68 | =pod |
69 | |
70 | =head2 new |
71 | |
72 | Object constructor. |
73 | |
74 | my $schema = SQL::Translator::Schema::Index->new; |
75 | |
76 | =cut |
77 | |
3c5de62a |
78 | sub fields { |
79 | |
80 | =pod |
81 | |
82 | =head2 fields |
83 | |
b54199ae |
84 | Gets and set the fields the index is on. Accepts a string, list or |
25868dc9 |
85 | arrayref; returns an array or array reference. Will unique the field |
86 | names and keep them in order by the first occurrence of a field name. |
3c5de62a |
87 | |
b54199ae |
88 | $index->fields('id'); |
89 | $index->fields('id', 'name'); |
90 | $index->fields( 'id, name' ); |
91 | $index->fields( [ 'id', 'name' ] ); |
92 | $index->fields( qw[ id name ] ); |
25868dc9 |
93 | |
b54199ae |
94 | my @fields = $index->fields; |
3c5de62a |
95 | |
96 | =cut |
97 | |
98 | my $self = shift; |
25868dc9 |
99 | my $fields = parse_list_arg( @_ ); |
3c5de62a |
100 | |
101 | if ( @$fields ) { |
25868dc9 |
102 | my ( %unique, @unique ); |
103 | for my $f ( @$fields ) { |
104 | next if $unique{ $f }; |
105 | $unique{ $f } = 1; |
106 | push @unique, $f; |
107 | } |
108 | |
109 | $self->{'fields'} = \@unique; |
3c5de62a |
110 | } |
111 | |
112 | return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'}; |
113 | } |
114 | |
b54199ae |
115 | sub is_valid { |
116 | |
117 | =pod |
118 | |
119 | =head2 is_valid |
120 | |
121 | Determine whether the index is valid or not. |
122 | |
123 | my $ok = $index->is_valid; |
124 | |
125 | =cut |
126 | |
127 | my $self = shift; |
128 | my $table = $self->table or return $self->error('No table'); |
129 | my @fields = $self->fields or return $self->error('No fields'); |
130 | |
131 | for my $field ( @fields ) { |
132 | return $self->error( |
133 | "Field '$field' does not exist in table '", $table->name, "'" |
134 | ) unless $table->get_field( $field ); |
135 | } |
136 | |
137 | return 1; |
138 | } |
139 | |
3c5de62a |
140 | sub name { |
141 | |
142 | =pod |
143 | |
144 | =head2 name |
145 | |
146 | Get or set the index's name. |
147 | |
148 | my $name = $index->name('foo'); |
149 | |
150 | =cut |
151 | |
152 | my $self = shift; |
153 | $self->{'name'} = shift if @_; |
154 | return $self->{'name'} || ''; |
155 | } |
156 | |
25868dc9 |
157 | sub options { |
158 | |
159 | =pod |
160 | |
161 | =head2 options |
162 | |
163 | Get or set the index's options (e.g., "using" or "where" for PG). Returns |
164 | an array or array reference. |
165 | |
166 | my @options = $index->options; |
167 | |
168 | =cut |
169 | |
170 | my $self = shift; |
171 | my $options = parse_list_arg( @_ ); |
172 | |
173 | push @{ $self->{'options'} }, @$options; |
174 | |
175 | if ( ref $self->{'options'} ) { |
176 | return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'}; |
177 | } |
178 | else { |
179 | return wantarray ? () : []; |
180 | } |
181 | } |
182 | |
43b9dc7a |
183 | sub table { |
184 | |
185 | =pod |
186 | |
187 | =head2 table |
188 | |
189 | Get or set the index's table object. |
190 | |
191 | my $table = $index->table; |
192 | |
193 | =cut |
194 | |
195 | my $self = shift; |
196 | if ( my $arg = shift ) { |
197 | return $self->error('Not a table object') unless |
198 | UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' ); |
199 | $self->{'table'} = $arg; |
200 | } |
201 | |
202 | return $self->{'table'}; |
203 | } |
204 | |
3c5de62a |
205 | sub type { |
206 | |
207 | =pod |
208 | |
209 | =head2 type |
210 | |
211 | Get or set the index's type. |
212 | |
213 | my $type = $index->type('unique'); |
214 | |
19ad0cee |
215 | Get or set the index's options (e.g., "using" or "where" for PG). Returns |
216 | |
217 | Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT, |
218 | and SPATIAL. The latter two might be MySQL-specific. While both lowercase |
219 | and uppercase types are acceptable input, this method returns the type in |
220 | uppercase. |
221 | |
3c5de62a |
222 | =cut |
223 | |
c3b0b535 |
224 | my ( $self, $type ) = @_; |
3c5de62a |
225 | |
c3b0b535 |
226 | if ( $type ) { |
227 | $type = uc $type; |
ea93df61 |
228 | return $self->error("Invalid index type: $type") |
c8efc003 |
229 | unless $VALID_INDEX_TYPE{ $type }; |
3c5de62a |
230 | $self->{'type'} = $type; |
231 | } |
232 | |
19ad0cee |
233 | return $self->{'type'} || 'NORMAL'; |
3c5de62a |
234 | } |
235 | |
abf315bb |
236 | sub equals { |
237 | |
238 | =pod |
239 | |
240 | =head2 equals |
241 | |
242 | Determines if this index is the same as another |
243 | |
244 | my $isIdentical = $index1->equals( $index2 ); |
245 | |
246 | =cut |
247 | |
248 | my $self = shift; |
249 | my $other = shift; |
250 | my $case_insensitive = shift; |
d990d84b |
251 | my $ignore_index_names = shift; |
ea93df61 |
252 | |
abf315bb |
253 | return 0 unless $self->SUPER::equals($other); |
da5a1bae |
254 | |
d990d84b |
255 | unless ($ignore_index_names) { |
da5a1bae |
256 | unless ((!$self->name && ($other->name eq $other->fields->[0])) || |
257 | (!$other->name && ($self->name eq $self->fields->[0]))) { |
d990d84b |
258 | return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name; |
da5a1bae |
259 | } |
d990d84b |
260 | } |
82f6b50e |
261 | #return 0 unless $self->is_valid eq $other->is_valid; |
abf315bb |
262 | return 0 unless $self->type eq $other->type; |
ea93df61 |
263 | |
c243ec2b |
264 | # Check fields, regardless of order |
ea93df61 |
265 | my %otherFields = (); # create a hash of the other fields |
c243ec2b |
266 | foreach my $otherField ($other->fields) { |
ea93df61 |
267 | $otherField = uc($otherField) if $case_insensitive; |
268 | $otherFields{$otherField} = 1; |
c243ec2b |
269 | } |
270 | foreach my $selfField ($self->fields) { # check for self fields in hash |
ea93df61 |
271 | $selfField = uc($selfField) if $case_insensitive; |
272 | return 0 unless $otherFields{$selfField}; |
273 | delete $otherFields{$selfField}; |
c243ec2b |
274 | } |
275 | # Check all other fields were accounted for |
276 | return 0 unless keys %otherFields == 0; |
277 | |
4598b71c |
278 | return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options); |
279 | return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra); |
abf315bb |
280 | return 1; |
281 | } |
3c5de62a |
282 | |
25868dc9 |
283 | sub DESTROY { |
3c5de62a |
284 | my $self = shift; |
25868dc9 |
285 | undef $self->{'table'}; # destroy cyclical reference |
3c5de62a |
286 | } |
287 | |
288 | 1; |
289 | |
3c5de62a |
290 | =pod |
291 | |
292 | =head1 AUTHOR |
293 | |
c3b0b535 |
294 | Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>. |
3c5de62a |
295 | |
296 | =cut |