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(ex2err throw);
31 use SQL::Translator::Role::ListAttr;
32 use SQL::Translator::Types qw(schema_obj);
33 use Sub::Quote qw(quote_sub);
35 extends 'SQL::Translator::Schema::Object';
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 (?)
51 my $schema = SQL::Translator::Schema::Index->new;
55 Gets and set the fields the index is on. Accepts a string, list or
56 arrayref; returns an array or array reference. Will unique the field
57 names and keep them in order by the first occurrence of a field name.
60 $index->fields('id', 'name');
61 $index->fields( 'id, name' );
62 $index->fields( [ 'id', 'name' ] );
63 $index->fields( qw[ id name ] );
65 my @fields = $index->fields;
69 with ListAttr fields => ( uniq => 1 );
77 Determine whether the index is valid or not.
79 my $ok = $index->is_valid;
84 my $table = $self->table or return $self->error('No table');
85 my @fields = $self->fields or return $self->error('No fields');
87 for my $field ( @fields ) {
89 "Field '$field' does not exist in table '", $table->name, "'"
90 ) unless $table->get_field( $field );
98 Get or set the index's name.
100 my $name = $index->name('foo');
104 has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => quote_sub(q{ '' }) );
108 Get or set the index's options (e.g., "using" or "where" for PG). Returns
109 an array or array reference.
111 my @options = $index->options;
115 with ListAttr options => ();
119 Get or set the index's table object.
121 my $table = $index->table;
125 has table => ( is => 'rw', isa => schema_obj('Table'), weak_ref => 1 );
127 around table => \&ex2err;
131 Get or set the index's type.
133 my $type = $index->type('unique');
135 Get or set the index's type.
137 Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
138 and SPATIAL. The latter two might be MySQL-specific. While both lowercase
139 and uppercase types are acceptable input, this method returns the type in
147 my $type = uc $_[0] or return;
148 throw("Invalid index type: $type") unless $VALID_INDEX_TYPE{$type};
150 coerce => sub { uc $_[0] },
151 default => quote_sub(q{ 'NORMAL' }),
154 around type => \&ex2err;
158 Determines if this index is the same as another
160 my $isIdentical = $index1->equals( $index2 );
164 around equals => sub {
168 my $case_insensitive = shift;
169 my $ignore_index_names = shift;
171 return 0 unless $self->$orig($other);
173 unless ($ignore_index_names) {
174 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
175 (!$other->name && ($self->name eq $self->fields->[0]))) {
176 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
179 #return 0 unless $self->is_valid eq $other->is_valid;
180 return 0 unless $self->type eq $other->type;
182 # Check fields, regardless of order
183 my %otherFields = (); # create a hash of the other fields
184 foreach my $otherField ($other->fields) {
185 $otherField = uc($otherField) if $case_insensitive;
186 $otherFields{$otherField} = 1;
188 foreach my $selfField ($self->fields) { # check for self fields in hash
189 $selfField = uc($selfField) if $case_insensitive;
190 return 0 unless $otherFields{$selfField};
191 delete $otherFields{$selfField};
193 # Check all other fields were accounted for
194 return 0 unless keys %otherFields == 0;
196 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
197 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
201 # Must come after all 'has' declarations
202 around new => \&ex2err;
210 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.