1 package SQL::Translator::Schema::Index;
7 SQL::Translator::Schema::Index - SQL::Translator index object
11 use SQL::Translator::Schema::Index;
12 my $index = SQL::Translator::Schema::Index->new(
20 C<SQL::Translator::Schema::Index> is the index object.
22 Primary and unique keys are table constraints, not indices.
29 use SQL::Translator::Schema::Constants;
30 use SQL::Translator::Utils 'parse_list_arg';
32 use base 'SQL::Translator::Schema::Object';
34 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
38 my %VALID_INDEX_TYPE = (
41 FULLTEXT => 1, # MySQL only (?)
42 FULL_TEXT => 1, # MySQL only (?)
43 SPATIAL => 1, # MySQL only (?)
46 # ----------------------------------------------------------------------
48 __PACKAGE__->_attributes( qw/
49 name type fields table options
58 my $schema = SQL::Translator::Schema::Index->new;
62 # ----------------------------------------------------------------------
69 Gets and set the fields the index is on. Accepts a string, list or
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.
74 $index->fields('id', 'name');
75 $index->fields( 'id, name' );
76 $index->fields( [ 'id', 'name' ] );
77 $index->fields( qw[ id name ] );
79 my @fields = $index->fields;
84 my $fields = parse_list_arg( @_ );
87 my ( %unique, @unique );
88 for my $f ( @$fields ) {
89 next if $unique{ $f };
94 $self->{'fields'} = \@unique;
97 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
100 # ----------------------------------------------------------------------
107 Determine whether the index is valid or not.
109 my $ok = $index->is_valid;
114 my $table = $self->table or return $self->error('No table');
115 my @fields = $self->fields or return $self->error('No fields');
117 for my $field ( @fields ) {
119 "Field '$field' does not exist in table '", $table->name, "'"
120 ) unless $table->get_field( $field );
126 # ----------------------------------------------------------------------
133 Get or set the index's name.
135 my $name = $index->name('foo');
140 $self->{'name'} = shift if @_;
141 return $self->{'name'} || '';
144 # ----------------------------------------------------------------------
151 Get or set the index's options (e.g., "using" or "where" for PG). Returns
152 an array or array reference.
154 my @options = $index->options;
159 my $options = parse_list_arg( @_ );
161 push @{ $self->{'options'} }, @$options;
163 if ( ref $self->{'options'} ) {
164 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
167 return wantarray ? () : [];
171 # ----------------------------------------------------------------------
178 Get or set the index's table object.
180 my $table = $index->table;
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;
191 return $self->{'table'};
194 # ----------------------------------------------------------------------
201 Get or set the index's type.
203 my $type = $index->type('unique');
205 Get or set the index's options (e.g., "using" or "where" for PG). Returns
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
214 my ( $self, $type ) = @_;
218 return $self->error("Invalid index type: $type")
219 unless $VALID_INDEX_TYPE{ $type };
220 $self->{'type'} = $type;
223 return $self->{'type'} || 'NORMAL';
226 # ----------------------------------------------------------------------
233 Determines if this index is the same as another
235 my $isIdentical = $index1->equals( $index2 );
241 my $case_insensitive = shift;
242 my $ignore_index_names = shift;
244 return 0 unless $self->SUPER::equals($other);
246 unless ($ignore_index_names) {
247 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
248 (!$other->name && ($self->name eq $self->fields->[0]))) {
249 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
252 #return 0 unless $self->is_valid eq $other->is_valid;
253 return 0 unless $self->type eq $other->type;
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;
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};
266 # Check all other fields were accounted for
267 return 0 unless keys %otherFields == 0;
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);
274 # ----------------------------------------------------------------------
277 undef $self->{'table'}; # destroy cyclical reference
282 # ----------------------------------------------------------------------
288 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.