1 package SQL::Translator::Schema::Index;
3 # ----------------------------------------------------------------------
4 # $Id: Index.pm 1440 2009-01-17 16:31:57Z jawnsy $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2002-2009 SQLFairy Authors
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
27 SQL::Translator::Schema::Index - SQL::Translator index object
31 use SQL::Translator::Schema::Index;
32 my $index = SQL::Translator::Schema::Index->new(
40 C<SQL::Translator::Schema::Index> is the index object.
42 Primary and unique keys are table constraints, not indices.
49 use SQL::Translator::Schema::Constants;
50 use SQL::Translator::Utils 'parse_list_arg';
52 use base 'SQL::Translator::Schema::Object';
54 use vars qw($TABLE_COUNT $VIEW_COUNT);
56 my %VALID_INDEX_TYPE = (
59 FULL_TEXT => 1, # MySQL only (?)
60 SPATIAL => 1, # MySQL only (?)
63 # ----------------------------------------------------------------------
65 __PACKAGE__->_attributes( qw/
66 name type fields table options
75 my $schema = SQL::Translator::Schema::Index->new;
79 # ----------------------------------------------------------------------
86 Gets and set the fields the index is on. Accepts a string, list or
87 arrayref; returns an array or array reference. Will unique the field
88 names and keep them in order by the first occurrence of a field name.
91 $index->fields('id', 'name');
92 $index->fields( 'id, name' );
93 $index->fields( [ 'id', 'name' ] );
94 $index->fields( qw[ id name ] );
96 my @fields = $index->fields;
101 my $fields = parse_list_arg( @_ );
104 my ( %unique, @unique );
105 for my $f ( @$fields ) {
106 next if $unique{ $f };
111 $self->{'fields'} = \@unique;
114 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
117 # ----------------------------------------------------------------------
124 Determine whether the index is valid or not.
126 my $ok = $index->is_valid;
131 my $table = $self->table or return $self->error('No table');
132 my @fields = $self->fields or return $self->error('No fields');
134 for my $field ( @fields ) {
136 "Field '$field' does not exist in table '", $table->name, "'"
137 ) unless $table->get_field( $field );
143 # ----------------------------------------------------------------------
150 Get or set the index's name.
152 my $name = $index->name('foo');
157 $self->{'name'} = shift if @_;
158 return $self->{'name'} || '';
161 # ----------------------------------------------------------------------
168 Get or set the index's options (e.g., "using" or "where" for PG). Returns
169 an array or array reference.
171 my @options = $index->options;
176 my $options = parse_list_arg( @_ );
178 push @{ $self->{'options'} }, @$options;
180 if ( ref $self->{'options'} ) {
181 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
184 return wantarray ? () : [];
188 # ----------------------------------------------------------------------
195 Get or set the index's table object.
197 my $table = $index->table;
202 if ( my $arg = shift ) {
203 return $self->error('Not a table object') unless
204 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
205 $self->{'table'} = $arg;
208 return $self->{'table'};
211 # ----------------------------------------------------------------------
218 Get or set the index's type.
220 my $type = $index->type('unique');
222 Get or set the index's options (e.g., "using" or "where" for PG). Returns
224 Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
225 and SPATIAL. The latter two might be MySQL-specific. While both lowercase
226 and uppercase types are acceptable input, this method returns the type in
233 if ( my $type = uc shift ) {
234 return $self->error("Invalid index type: $type")
235 unless $VALID_INDEX_TYPE{ $type };
236 $self->{'type'} = $type;
239 return $self->{'type'} || 'NORMAL';
242 # ----------------------------------------------------------------------
249 Determines if this index is the same as another
251 my $isIdentical = $index1->equals( $index2 );
257 my $case_insensitive = shift;
258 my $ignore_index_names = shift;
260 return 0 unless $self->SUPER::equals($other);
262 unless ($ignore_index_names) {
263 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
264 (!$other->name && ($self->name eq $self->fields->[0]))) {
265 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
268 #return 0 unless $self->is_valid eq $other->is_valid;
269 return 0 unless $self->type eq $other->type;
271 # Check fields, regardless of order
272 my %otherFields = (); # create a hash of the other fields
273 foreach my $otherField ($other->fields) {
274 $otherField = uc($otherField) if $case_insensitive;
275 $otherFields{$otherField} = 1;
277 foreach my $selfField ($self->fields) { # check for self fields in hash
278 $selfField = uc($selfField) if $case_insensitive;
279 return 0 unless $otherFields{$selfField};
280 delete $otherFields{$selfField};
282 # Check all other fields were accounted for
283 return 0 unless keys %otherFields == 0;
285 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
286 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
290 # ----------------------------------------------------------------------
293 undef $self->{'table'}; # destroy cyclical reference
298 # ----------------------------------------------------------------------
304 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.