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 __PACKAGE__->_attributes( qw/
47 name type fields table options
56 my $schema = SQL::Translator::Schema::Index->new;
66 Gets and set the fields the index is on. Accepts a string, list or
67 arrayref; returns an array or array reference. Will unique the field
68 names and keep them in order by the first occurrence of a field name.
71 $index->fields('id', 'name');
72 $index->fields( 'id, name' );
73 $index->fields( [ 'id', 'name' ] );
74 $index->fields( qw[ id name ] );
76 my @fields = $index->fields;
81 my $fields = parse_list_arg( @_ );
84 my ( %unique, @unique );
85 for my $f ( @$fields ) {
86 next if $unique{ $f };
91 $self->{'fields'} = \@unique;
94 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
103 Determine whether the index is valid or not.
105 my $ok = $index->is_valid;
110 my $table = $self->table or return $self->error('No table');
111 my @fields = $self->fields or return $self->error('No fields');
113 for my $field ( @fields ) {
115 "Field '$field' does not exist in table '", $table->name, "'"
116 ) unless $table->get_field( $field );
128 Get or set the index's name.
130 my $name = $index->name('foo');
135 $self->{'name'} = shift if @_;
136 return $self->{'name'} || '';
145 Get or set the index's options (e.g., "using" or "where" for PG). Returns
146 an array or array reference.
148 my @options = $index->options;
153 my $options = parse_list_arg( @_ );
155 push @{ $self->{'options'} }, @$options;
157 if ( ref $self->{'options'} ) {
158 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
161 return wantarray ? () : [];
171 Get or set the index's table object.
173 my $table = $index->table;
178 if ( my $arg = shift ) {
179 return $self->error('Not a table object') unless
180 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
181 $self->{'table'} = $arg;
184 return $self->{'table'};
193 Get or set the index's type.
195 my $type = $index->type('unique');
197 Get or set the index's options (e.g., "using" or "where" for PG). Returns
199 Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
200 and SPATIAL. The latter two might be MySQL-specific. While both lowercase
201 and uppercase types are acceptable input, this method returns the type in
206 my ( $self, $type ) = @_;
210 return $self->error("Invalid index type: $type")
211 unless $VALID_INDEX_TYPE{ $type };
212 $self->{'type'} = $type;
215 return $self->{'type'} || 'NORMAL';
224 Determines if this index is the same as another
226 my $isIdentical = $index1->equals( $index2 );
232 my $case_insensitive = shift;
233 my $ignore_index_names = shift;
235 return 0 unless $self->SUPER::equals($other);
237 unless ($ignore_index_names) {
238 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
239 (!$other->name && ($self->name eq $self->fields->[0]))) {
240 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
243 #return 0 unless $self->is_valid eq $other->is_valid;
244 return 0 unless $self->type eq $other->type;
246 # Check fields, regardless of order
247 my %otherFields = (); # create a hash of the other fields
248 foreach my $otherField ($other->fields) {
249 $otherField = uc($otherField) if $case_insensitive;
250 $otherFields{$otherField} = 1;
252 foreach my $selfField ($self->fields) { # check for self fields in hash
253 $selfField = uc($selfField) if $case_insensitive;
254 return 0 unless $otherFields{$selfField};
255 delete $otherFields{$selfField};
257 # Check all other fields were accounted for
258 return 0 unless keys %otherFields == 0;
260 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
261 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
267 undef $self->{'table'}; # destroy cyclical reference
276 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.