1 package SQL::Translator::Schema::Index;
3 # ----------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; version 2.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 # -------------------------------------------------------------------
25 SQL::Translator::Schema::Index - SQL::Translator index object
29 use SQL::Translator::Schema::Index;
30 my $index = SQL::Translator::Schema::Index->new(
38 C<SQL::Translator::Schema::Index> is the index object.
40 Primary and unique keys are table constraints, not indices.
47 use SQL::Translator::Schema::Constants;
48 use SQL::Translator::Utils 'parse_list_arg';
50 use base 'SQL::Translator::Schema::Object';
52 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
56 my %VALID_INDEX_TYPE = (
59 FULLTEXT => 1, # MySQL only (?)
60 FULL_TEXT => 1, # MySQL only (?)
61 SPATIAL => 1, # MySQL only (?)
64 __PACKAGE__->_attributes( qw/
65 name type fields table options
74 my $schema = SQL::Translator::Schema::Index->new;
84 Gets and set the fields the index is on. Accepts a string, list or
85 arrayref; returns an array or array reference. Will unique the field
86 names and keep them in order by the first occurrence of a field name.
89 $index->fields('id', 'name');
90 $index->fields( 'id, name' );
91 $index->fields( [ 'id', 'name' ] );
92 $index->fields( qw[ id name ] );
94 my @fields = $index->fields;
99 my $fields = parse_list_arg( @_ );
102 my ( %unique, @unique );
103 for my $f ( @$fields ) {
104 next if $unique{ $f };
109 $self->{'fields'} = \@unique;
112 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
121 Determine whether the index is valid or not.
123 my $ok = $index->is_valid;
128 my $table = $self->table or return $self->error('No table');
129 my @fields = $self->fields or return $self->error('No fields');
131 for my $field ( @fields ) {
133 "Field '$field' does not exist in table '", $table->name, "'"
134 ) unless $table->get_field( $field );
146 Get or set the index's name.
148 my $name = $index->name('foo');
153 $self->{'name'} = shift if @_;
154 return $self->{'name'} || '';
163 Get or set the index's options (e.g., "using" or "where" for PG). Returns
164 an array or array reference.
166 my @options = $index->options;
171 my $options = parse_list_arg( @_ );
173 push @{ $self->{'options'} }, @$options;
175 if ( ref $self->{'options'} ) {
176 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
179 return wantarray ? () : [];
189 Get or set the index's table object.
191 my $table = $index->table;
196 if ( my $arg = shift ) {
197 return $self->error('Not a table object') unless
198 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
199 $self->{'table'} = $arg;
202 return $self->{'table'};
211 Get or set the index's type.
213 my $type = $index->type('unique');
215 Get or set the index's options (e.g., "using" or "where" for PG). Returns
217 Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
218 and SPATIAL. The latter two might be MySQL-specific. While both lowercase
219 and uppercase types are acceptable input, this method returns the type in
224 my ( $self, $type ) = @_;
228 return $self->error("Invalid index type: $type")
229 unless $VALID_INDEX_TYPE{ $type };
230 $self->{'type'} = $type;
233 return $self->{'type'} || 'NORMAL';
242 Determines if this index is the same as another
244 my $isIdentical = $index1->equals( $index2 );
250 my $case_insensitive = shift;
251 my $ignore_index_names = shift;
253 return 0 unless $self->SUPER::equals($other);
255 unless ($ignore_index_names) {
256 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
257 (!$other->name && ($self->name eq $self->fields->[0]))) {
258 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
261 #return 0 unless $self->is_valid eq $other->is_valid;
262 return 0 unless $self->type eq $other->type;
264 # Check fields, regardless of order
265 my %otherFields = (); # create a hash of the other fields
266 foreach my $otherField ($other->fields) {
267 $otherField = uc($otherField) if $case_insensitive;
268 $otherFields{$otherField} = 1;
270 foreach my $selfField ($self->fields) { # check for self fields in hash
271 $selfField = uc($selfField) if $case_insensitive;
272 return 0 unless $otherFields{$selfField};
273 delete $otherFields{$selfField};
275 # Check all other fields were accounted for
276 return 0 unless keys %otherFields == 0;
278 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
279 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
285 undef $self->{'table'}; # destroy cyclical reference
294 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.