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