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.
30 use SQL::Translator::Schema::Constants;
31 use SQL::Translator::Utils 'parse_list_arg';
33 use base 'SQL::Translator::Schema::Object';
35 our ( $TABLE_COUNT, $VIEW_COUNT );
37 our $VERSION = '1.59';
39 my %VALID_INDEX_TYPE = (
42 FULLTEXT => 1, # MySQL only (?)
43 FULL_TEXT => 1, # MySQL only (?)
44 SPATIAL => 1, # MySQL only (?)
47 __PACKAGE__->_attributes( qw/
48 name type fields table options
57 my $schema = SQL::Translator::Schema::Index->new;
67 Gets and set the fields the index is on. Accepts a string, list or
68 arrayref; returns an array or array reference. Will unique the field
69 names and keep them in order by the first occurrence of a field name.
72 $index->fields('id', 'name');
73 $index->fields( 'id, name' );
74 $index->fields( [ 'id', 'name' ] );
75 $index->fields( qw[ id name ] );
77 my @fields = $index->fields;
82 my $fields = parse_list_arg( @_ );
85 my ( %unique, @unique );
86 for my $f ( @$fields ) {
87 next if $unique{ $f };
92 $self->{'fields'} = \@unique;
95 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
104 Determine whether the index is valid or not.
106 my $ok = $index->is_valid;
111 my $table = $self->table or return $self->error('No table');
112 my @fields = $self->fields or return $self->error('No fields');
114 for my $field ( @fields ) {
116 "Field '$field' does not exist in table '", $table->name, "'"
117 ) unless $table->get_field( $field );
129 Get or set the index's name.
131 my $name = $index->name('foo');
136 $self->{'name'} = shift if @_;
137 return $self->{'name'} || '';
146 Get or set the index's options (e.g., "using" or "where" for PG). Returns
147 an array or array reference.
149 my @options = $index->options;
154 my $options = parse_list_arg( @_ );
156 push @{ $self->{'options'} }, @$options;
158 if ( ref $self->{'options'} ) {
159 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
162 return wantarray ? () : [];
172 Get or set the index's table object.
174 my $table = $index->table;
179 if ( my $arg = shift ) {
180 return $self->error('Not a table object') unless
181 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
182 $self->{'table'} = $arg;
185 return $self->{'table'};
194 Get or set the index's type.
196 my $type = $index->type('unique');
198 Get or set the index's type.
200 Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
201 and SPATIAL. The latter two might be MySQL-specific. While both lowercase
202 and uppercase types are acceptable input, this method returns the type in
207 my ( $self, $type ) = @_;
211 return $self->error("Invalid index type: $type")
212 unless $VALID_INDEX_TYPE{ $type };
213 $self->{'type'} = $type;
216 return $self->{'type'} || 'NORMAL';
225 Determines if this index is the same as another
227 my $isIdentical = $index1->equals( $index2 );
233 my $case_insensitive = shift;
234 my $ignore_index_names = shift;
236 return 0 unless $self->SUPER::equals($other);
238 unless ($ignore_index_names) {
239 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
240 (!$other->name && ($self->name eq $self->fields->[0]))) {
241 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
244 #return 0 unless $self->is_valid eq $other->is_valid;
245 return 0 unless $self->type eq $other->type;
247 # Check fields, regardless of order
248 my %otherFields = (); # create a hash of the other fields
249 foreach my $otherField ($other->fields) {
250 $otherField = uc($otherField) if $case_insensitive;
251 $otherFields{$otherField} = 1;
253 foreach my $selfField ($self->fields) { # check for self fields in hash
254 $selfField = uc($selfField) if $case_insensitive;
255 return 0 unless $otherFields{$selfField};
256 delete $otherFields{$selfField};
258 # Check all other fields were accounted for
259 return 0 unless keys %otherFields == 0;
261 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
262 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
268 undef $self->{'table'}; # destroy cyclical reference
277 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.