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::BuildArgs
36 SQL::Translator::Schema::Role::Extra
37 SQL::Translator::Schema::Role::Error
38 SQL::Translator::Schema::Role::Compare
41 our ( $TABLE_COUNT, $VIEW_COUNT );
43 our $VERSION = '1.59';
45 my %VALID_INDEX_TYPE = (
48 FULLTEXT => 1, # MySQL only (?)
49 FULL_TEXT => 1, # MySQL only (?)
50 SPATIAL => 1, # MySQL only (?)
57 my $schema = SQL::Translator::Schema::Index->new;
61 Gets and set the fields the index is on. Accepts a string, list or
62 arrayref; returns an array or array reference. Will unique the field
63 names and keep them in order by the first occurrence of a field name.
66 $index->fields('id', 'name');
67 $index->fields( 'id, name' );
68 $index->fields( [ 'id', 'name' ] );
69 $index->fields( qw[ id name ] );
71 my @fields = $index->fields;
77 default => sub { [] },
78 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
81 around fields => sub {
84 my $fields = parse_list_arg( @_ );
85 $self->$orig($fields) if @$fields;
87 return wantarray ? @{ $self->$orig } : $self->$orig;
96 Determine whether the index is valid or not.
98 my $ok = $index->is_valid;
103 my $table = $self->table or return $self->error('No table');
104 my @fields = $self->fields or return $self->error('No fields');
106 for my $field ( @fields ) {
108 "Field '$field' does not exist in table '", $table->name, "'"
109 ) unless $table->get_field( $field );
117 Get or set the index's name.
119 my $name = $index->name('foo');
123 has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } );
127 Get or set the index's options (e.g., "using" or "where" for PG). Returns
128 an array or array reference.
130 my @options = $index->options;
136 default => sub { [] },
137 coerce => \&parse_list_arg,
140 around options => sub {
143 my $options = parse_list_arg( @_ );
145 push @{ $self->$orig }, @$options;
147 return wantarray ? @{ $self->$orig } : $self->$orig;
152 Get or set the index's table object.
154 my $table = $index->table;
158 has table => ( is => 'rw', isa => schema_obj('Table') );
160 around table => \&ex2err;
164 Get or set the index's type.
166 my $type = $index->type('unique');
168 Get or set the index's type.
170 Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
171 and SPATIAL. The latter two might be MySQL-specific. While both lowercase
172 and uppercase types are acceptable input, this method returns the type in
180 my $type = uc $_[0] or return;
181 throw("Invalid index type: $type") unless $VALID_INDEX_TYPE{$type};
183 coerce => sub { uc $_[0] },
184 default => sub { 'NORMAL' },
187 around type => \&ex2err;
191 Determines if this index is the same as another
193 my $isIdentical = $index1->equals( $index2 );
197 around equals => sub {
201 my $case_insensitive = shift;
202 my $ignore_index_names = shift;
204 return 0 unless $self->$orig($other);
206 unless ($ignore_index_names) {
207 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
208 (!$other->name && ($self->name eq $self->fields->[0]))) {
209 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
212 #return 0 unless $self->is_valid eq $other->is_valid;
213 return 0 unless $self->type eq $other->type;
215 # Check fields, regardless of order
216 my %otherFields = (); # create a hash of the other fields
217 foreach my $otherField ($other->fields) {
218 $otherField = uc($otherField) if $case_insensitive;
219 $otherFields{$otherField} = 1;
221 foreach my $selfField ($self->fields) { # check for self fields in hash
222 $selfField = uc($selfField) if $case_insensitive;
223 return 0 unless $otherFields{$selfField};
224 delete $otherFields{$selfField};
226 # Check all other fields were accounted for
227 return 0 unless keys %otherFields == 0;
229 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
230 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
236 undef $self->{'table'}; # destroy cyclical reference
239 # Must come after all 'has' declarations
240 around new => \&ex2err;
248 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.