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 qw(parse_list_arg ex2err throw);
31 use SQL::Translator::Types qw(schema_obj);
32 use List::MoreUtils qw(uniq);
34 extends 'SQL::Translator::Schema::Object';
36 our $VERSION = '1.59';
38 my %VALID_INDEX_TYPE = (
41 FULLTEXT => 1, # MySQL only (?)
42 FULL_TEXT => 1, # MySQL only (?)
43 SPATIAL => 1, # MySQL only (?)
50 my $schema = SQL::Translator::Schema::Index->new;
54 Gets and set the fields the index is on. Accepts a string, list or
55 arrayref; returns an array or array reference. Will unique the field
56 names and keep them in order by the first occurrence of a field name.
59 $index->fields('id', 'name');
60 $index->fields( 'id, name' );
61 $index->fields( [ 'id', 'name' ] );
62 $index->fields( qw[ id name ] );
64 my @fields = $index->fields;
70 default => sub { [] },
71 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
74 around fields => sub {
77 my $fields = parse_list_arg( @_ );
78 $self->$orig($fields) if @$fields;
80 return wantarray ? @{ $self->$orig } : $self->$orig;
89 Determine whether the index is valid or not.
91 my $ok = $index->is_valid;
96 my $table = $self->table or return $self->error('No table');
97 my @fields = $self->fields or return $self->error('No fields');
99 for my $field ( @fields ) {
101 "Field '$field' does not exist in table '", $table->name, "'"
102 ) unless $table->get_field( $field );
110 Get or set the index's name.
112 my $name = $index->name('foo');
116 has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } );
120 Get or set the index's options (e.g., "using" or "where" for PG). Returns
121 an array or array reference.
123 my @options = $index->options;
129 default => sub { [] },
130 coerce => \&parse_list_arg,
133 around options => sub {
136 my $options = parse_list_arg( @_ );
138 push @{ $self->$orig }, @$options;
140 return wantarray ? @{ $self->$orig } : $self->$orig;
145 Get or set the index's table object.
147 my $table = $index->table;
151 has table => ( is => 'rw', isa => schema_obj('Table'), weak_ref => 1 );
153 around table => \&ex2err;
157 Get or set the index's type.
159 my $type = $index->type('unique');
161 Get or set the index's type.
163 Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
164 and SPATIAL. The latter two might be MySQL-specific. While both lowercase
165 and uppercase types are acceptable input, this method returns the type in
173 my $type = uc $_[0] or return;
174 throw("Invalid index type: $type") unless $VALID_INDEX_TYPE{$type};
176 coerce => sub { uc $_[0] },
177 default => sub { 'NORMAL' },
180 around type => \&ex2err;
184 Determines if this index is the same as another
186 my $isIdentical = $index1->equals( $index2 );
190 around equals => sub {
194 my $case_insensitive = shift;
195 my $ignore_index_names = shift;
197 return 0 unless $self->$orig($other);
199 unless ($ignore_index_names) {
200 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
201 (!$other->name && ($self->name eq $self->fields->[0]))) {
202 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
205 #return 0 unless $self->is_valid eq $other->is_valid;
206 return 0 unless $self->type eq $other->type;
208 # Check fields, regardless of order
209 my %otherFields = (); # create a hash of the other fields
210 foreach my $otherField ($other->fields) {
211 $otherField = uc($otherField) if $case_insensitive;
212 $otherFields{$otherField} = 1;
214 foreach my $selfField ($self->fields) { # check for self fields in hash
215 $selfField = uc($selfField) if $case_insensitive;
216 return 0 unless $otherFields{$selfField};
217 delete $otherFields{$selfField};
219 # Check all other fields were accounted for
220 return 0 unless keys %otherFields == 0;
222 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
223 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
227 # Must come after all 'has' declarations
228 around new => \&ex2err;
236 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.