1 package SQL::Translator::Schema::Index;
3 # ----------------------------------------------------------------------
4 # $Id: Index.pm,v 1.18 2007-10-24 10:55:44 schiffbruechige Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2002-4 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($VERSION $TABLE_COUNT $VIEW_COUNT);
56 $VERSION = sprintf "%d.%02d", q$Revision: 1.18 $ =~ /(\d+)\.(\d+)/;
58 my %VALID_INDEX_TYPE = (
61 FULL_TEXT, 1, # MySQL only (?)
62 SPATIAL, 1, # MySQL only (?)
65 # ----------------------------------------------------------------------
67 __PACKAGE__->_attributes( qw/
68 name type fields table options
77 my $schema = SQL::Translator::Schema::Index->new;
81 # ----------------------------------------------------------------------
88 Gets and set the fields the index is on. Accepts a string, list or
89 arrayref; returns an array or array reference. Will unique the field
90 names and keep them in order by the first occurrence of a field name.
93 $index->fields('id', 'name');
94 $index->fields( 'id, name' );
95 $index->fields( [ 'id', 'name' ] );
96 $index->fields( qw[ id name ] );
98 my @fields = $index->fields;
103 my $fields = parse_list_arg( @_ );
106 my ( %unique, @unique );
107 for my $f ( @$fields ) {
108 next if $unique{ $f };
113 $self->{'fields'} = \@unique;
116 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
119 # ----------------------------------------------------------------------
126 Determine whether the index is valid or not.
128 my $ok = $index->is_valid;
133 my $table = $self->table or return $self->error('No table');
134 my @fields = $self->fields or return $self->error('No fields');
136 for my $field ( @fields ) {
138 "Field '$field' does not exist in table '", $table->name, "'"
139 ) unless $table->get_field( $field );
145 # ----------------------------------------------------------------------
152 Get or set the index's name.
154 my $name = $index->name('foo');
159 $self->{'name'} = shift if @_;
160 return $self->{'name'} || '';
163 # ----------------------------------------------------------------------
170 Get or set the index's options (e.g., "using" or "where" for PG). Returns
171 an array or array reference.
173 my @options = $index->options;
178 my $options = parse_list_arg( @_ );
180 push @{ $self->{'options'} }, @$options;
182 if ( ref $self->{'options'} ) {
183 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
186 return wantarray ? () : [];
190 # ----------------------------------------------------------------------
197 Get or set the index's table object.
199 my $table = $index->table;
204 if ( my $arg = shift ) {
205 return $self->error('Not a table object') unless
206 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
207 $self->{'table'} = $arg;
210 return $self->{'table'};
213 # ----------------------------------------------------------------------
220 Get or set the index's type.
222 my $type = $index->type('unique');
228 if ( my $type = shift ) {
229 return $self->error("Invalid index type: $type")
230 unless $VALID_INDEX_TYPE{ $type };
231 $self->{'type'} = $type;
234 return $self->{'type'} || NORMAL;
237 # ----------------------------------------------------------------------
244 Determines if this index is the same as another
246 my $isIdentical = $index1->equals( $index2 );
252 my $case_insensitive = shift;
253 my $ignore_index_names = shift;
255 return 0 unless $self->SUPER::equals($other);
257 unless ($ignore_index_names) {
258 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
259 (!$other->name && ($self->name eq $self->fields->[0]))) {
260 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
263 #return 0 unless $self->is_valid eq $other->is_valid;
264 return 0 unless $self->type eq $other->type;
266 # Check fields, regardless of order
267 my %otherFields = (); # create a hash of the other fields
268 foreach my $otherField ($other->fields) {
269 $otherField = uc($otherField) if $case_insensitive;
270 $otherFields{$otherField} = 1;
272 foreach my $selfField ($self->fields) { # check for self fields in hash
273 $selfField = uc($selfField) if $case_insensitive;
274 return 0 unless $otherFields{$selfField};
275 delete $otherFields{$selfField};
277 # Check all other fields were accounted for
278 return 0 unless keys %otherFields == 0;
280 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
281 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
285 # ----------------------------------------------------------------------
288 undef $self->{'table'}; # destroy cyclical reference
293 # ----------------------------------------------------------------------
299 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.