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);
35 SQL::Translator::Role::Error
36 SQL::Translator::Role::BuildArgs
37 SQL::Translator::Schema::Role::Extra
38 SQL::Translator::Schema::Role::Compare
41 our $VERSION = '1.59';
43 my %VALID_INDEX_TYPE = (
46 FULLTEXT => 1, # MySQL only (?)
47 FULL_TEXT => 1, # MySQL only (?)
48 SPATIAL => 1, # MySQL only (?)
55 my $schema = SQL::Translator::Schema::Index->new;
59 Gets and set the fields the index is on. Accepts a string, list or
60 arrayref; returns an array or array reference. Will unique the field
61 names and keep them in order by the first occurrence of a field name.
64 $index->fields('id', 'name');
65 $index->fields( 'id, name' );
66 $index->fields( [ 'id', 'name' ] );
67 $index->fields( qw[ id name ] );
69 my @fields = $index->fields;
75 default => sub { [] },
76 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
79 around fields => sub {
82 my $fields = parse_list_arg( @_ );
83 $self->$orig($fields) if @$fields;
85 return wantarray ? @{ $self->$orig } : $self->$orig;
94 Determine whether the index is valid or not.
96 my $ok = $index->is_valid;
101 my $table = $self->table or return $self->error('No table');
102 my @fields = $self->fields or return $self->error('No fields');
104 for my $field ( @fields ) {
106 "Field '$field' does not exist in table '", $table->name, "'"
107 ) unless $table->get_field( $field );
115 Get or set the index's name.
117 my $name = $index->name('foo');
121 has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } );
125 Get or set the index's options (e.g., "using" or "where" for PG). Returns
126 an array or array reference.
128 my @options = $index->options;
134 default => sub { [] },
135 coerce => \&parse_list_arg,
138 around options => sub {
141 my $options = parse_list_arg( @_ );
143 push @{ $self->$orig }, @$options;
145 return wantarray ? @{ $self->$orig } : $self->$orig;
150 Get or set the index's table object.
152 my $table = $index->table;
156 has table => ( is => 'rw', isa => schema_obj('Table'), weak_ref => 1 );
158 around table => \&ex2err;
162 Get or set the index's type.
164 my $type = $index->type('unique');
166 Get or set the index's type.
168 Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
169 and SPATIAL. The latter two might be MySQL-specific. While both lowercase
170 and uppercase types are acceptable input, this method returns the type in
178 my $type = uc $_[0] or return;
179 throw("Invalid index type: $type") unless $VALID_INDEX_TYPE{$type};
181 coerce => sub { uc $_[0] },
182 default => sub { 'NORMAL' },
185 around type => \&ex2err;
189 Determines if this index is the same as another
191 my $isIdentical = $index1->equals( $index2 );
195 around equals => sub {
199 my $case_insensitive = shift;
200 my $ignore_index_names = shift;
202 return 0 unless $self->$orig($other);
204 unless ($ignore_index_names) {
205 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
206 (!$other->name && ($self->name eq $self->fields->[0]))) {
207 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
210 #return 0 unless $self->is_valid eq $other->is_valid;
211 return 0 unless $self->type eq $other->type;
213 # Check fields, regardless of order
214 my %otherFields = (); # create a hash of the other fields
215 foreach my $otherField ($other->fields) {
216 $otherField = uc($otherField) if $case_insensitive;
217 $otherFields{$otherField} = 1;
219 foreach my $selfField ($self->fields) { # check for self fields in hash
220 $selfField = uc($selfField) if $case_insensitive;
221 return 0 unless $otherFields{$selfField};
222 delete $otherFields{$selfField};
224 # Check all other fields were accounted for
225 return 0 unless keys %otherFields == 0;
227 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
228 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
232 # Must come after all 'has' declarations
233 around new => \&ex2err;
241 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.