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 'parse_list_arg';
31 use List::MoreUtils qw(uniq);
34 SQL::Translator::Schema::Role::Extra
35 SQL::Translator::Schema::Role::Error
36 SQL::Translator::Schema::Role::Compare
39 our ( $TABLE_COUNT, $VIEW_COUNT );
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;
61 $self->$_(scalar $self->$_)
62 foreach qw(fields options);
67 Gets and set the fields the index is on. Accepts a string, list or
68 arrayref; returns an array or array reference. Will unique the field
69 names and keep them in order by the first occurrence of a field name.
72 $index->fields('id', 'name');
73 $index->fields( 'id, name' );
74 $index->fields( [ 'id', 'name' ] );
75 $index->fields( qw[ id name ] );
77 my @fields = $index->fields;
83 default => sub { [] },
84 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
87 around fields => sub {
90 my $fields = parse_list_arg( @_ );
91 $self->$orig($fields) if @$fields;
93 return wantarray ? @{ $self->$orig } : $self->$orig;
102 Determine whether the index is valid or not.
104 my $ok = $index->is_valid;
109 my $table = $self->table or return $self->error('No table');
110 my @fields = $self->fields or return $self->error('No fields');
112 for my $field ( @fields ) {
114 "Field '$field' does not exist in table '", $table->name, "'"
115 ) unless $table->get_field( $field );
123 Get or set the index's name.
125 my $name = $index->name('foo');
129 has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } );
133 Get or set the index's options (e.g., "using" or "where" for PG). Returns
134 an array or array reference.
136 my @options = $index->options;
142 default => sub { [] },
143 coerce => sub { parse_list_arg($_[0]) },
146 around options => sub {
149 my $options = parse_list_arg( @_ );
151 push @{ $self->$orig }, @$options;
153 return wantarray ? @{ $self->$orig } : $self->$orig;
158 Get or set the index's table object.
160 my $table = $index->table;
164 has table => ( is => 'rw' );
166 around table => sub {
169 if ( my $arg = $_[0] ) {
170 return $self->error('Not a table object') unless
171 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
174 return $self->$orig(@_);
179 Get or set the index's type.
181 my $type = $index->type('unique');
183 Get or set the index's type.
185 Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
186 and SPATIAL. The latter two might be MySQL-specific. While both lowercase
187 and uppercase types are acceptable input, this method returns the type in
192 has type => ( is => 'rw', default => sub { 'NORMAL' } );
195 my ( $orig, $self) = (shift, shift);
197 if ( my $type = $_[0] ) {
199 return $self->error("Invalid index type: $type")
200 unless $VALID_INDEX_TYPE{ $type };
203 return $self->$orig(@_);
208 Determines if this index is the same as another
210 my $isIdentical = $index1->equals( $index2 );
214 around equals => sub {
218 my $case_insensitive = shift;
219 my $ignore_index_names = shift;
221 return 0 unless $self->$orig($other);
223 unless ($ignore_index_names) {
224 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
225 (!$other->name && ($self->name eq $self->fields->[0]))) {
226 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
229 #return 0 unless $self->is_valid eq $other->is_valid;
230 return 0 unless $self->type eq $other->type;
232 # Check fields, regardless of order
233 my %otherFields = (); # create a hash of the other fields
234 foreach my $otherField ($other->fields) {
235 $otherField = uc($otherField) if $case_insensitive;
236 $otherFields{$otherField} = 1;
238 foreach my $selfField ($self->fields) { # check for self fields in hash
239 $selfField = uc($selfField) if $case_insensitive;
240 return 0 unless $otherFields{$selfField};
241 delete $otherFields{$selfField};
243 # Check all other fields were accounted for
244 return 0 unless keys %otherFields == 0;
246 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
247 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
253 undef $self->{'table'}; # destroy cyclical reference
262 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.