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::Schema::Role::Extra
36 SQL::Translator::Schema::Role::Error
37 SQL::Translator::Schema::Role::Compare
40 our ( $TABLE_COUNT, $VIEW_COUNT );
42 our $VERSION = '1.59';
44 my %VALID_INDEX_TYPE = (
47 FULLTEXT => 1, # MySQL only (?)
48 FULL_TEXT => 1, # MySQL only (?)
49 SPATIAL => 1, # MySQL only (?)
56 my $schema = SQL::Translator::Schema::Index->new;
60 Gets and set the fields the index is on. Accepts a string, list or
61 arrayref; returns an array or array reference. Will unique the field
62 names and keep them in order by the first occurrence of a field name.
65 $index->fields('id', 'name');
66 $index->fields( 'id, name' );
67 $index->fields( [ 'id', 'name' ] );
68 $index->fields( qw[ id name ] );
70 my @fields = $index->fields;
76 default => sub { [] },
77 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
80 around fields => sub {
83 my $fields = parse_list_arg( @_ );
84 $self->$orig($fields) if @$fields;
86 return wantarray ? @{ $self->$orig } : $self->$orig;
95 Determine whether the index is valid or not.
97 my $ok = $index->is_valid;
102 my $table = $self->table or return $self->error('No table');
103 my @fields = $self->fields or return $self->error('No fields');
105 for my $field ( @fields ) {
107 "Field '$field' does not exist in table '", $table->name, "'"
108 ) unless $table->get_field( $field );
116 Get or set the index's name.
118 my $name = $index->name('foo');
122 has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } );
126 Get or set the index's options (e.g., "using" or "where" for PG). Returns
127 an array or array reference.
129 my @options = $index->options;
135 default => sub { [] },
136 coerce => \&parse_list_arg,
139 around options => sub {
142 my $options = parse_list_arg( @_ );
144 push @{ $self->$orig }, @$options;
146 return wantarray ? @{ $self->$orig } : $self->$orig;
151 Get or set the index's table object.
153 my $table = $index->table;
157 has table => ( is => 'rw', isa => schema_obj('Table') );
159 around table => \&ex2err;
163 Get or set the index's type.
165 my $type = $index->type('unique');
167 Get or set the index's type.
169 Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
170 and SPATIAL. The latter two might be MySQL-specific. While both lowercase
171 and uppercase types are acceptable input, this method returns the type in
179 my $type = uc $_[0] or return;
180 throw("Invalid index type: $type") unless $VALID_INDEX_TYPE{$type};
182 coerce => sub { uc $_[0] },
183 default => sub { 'NORMAL' },
186 around type => \&ex2err;
190 Determines if this index is the same as another
192 my $isIdentical = $index1->equals( $index2 );
196 around equals => sub {
200 my $case_insensitive = shift;
201 my $ignore_index_names = shift;
203 return 0 unless $self->$orig($other);
205 unless ($ignore_index_names) {
206 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
207 (!$other->name && ($self->name eq $self->fields->[0]))) {
208 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
211 #return 0 unless $self->is_valid eq $other->is_valid;
212 return 0 unless $self->type eq $other->type;
214 # Check fields, regardless of order
215 my %otherFields = (); # create a hash of the other fields
216 foreach my $otherField ($other->fields) {
217 $otherField = uc($otherField) if $case_insensitive;
218 $otherFields{$otherField} = 1;
220 foreach my $selfField ($self->fields) { # check for self fields in hash
221 $selfField = uc($selfField) if $case_insensitive;
222 return 0 unless $otherFields{$selfField};
223 delete $otherFields{$selfField};
225 # Check all other fields were accounted for
226 return 0 unless keys %otherFields == 0;
228 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
229 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
235 undef $self->{'table'}; # destroy cyclical reference
238 # Must come after all 'has' declarations
239 around new => \&ex2err;
247 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.