1 package SQL::Translator::Schema::Index;
3 # ----------------------------------------------------------------------
4 # $Id: Index.pm,v 1.14 2005-08-10 16:46:55 duality72 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.14 $ =~ /(\d+)\.(\d+)/;
58 my %VALID_INDEX_TYPE = (
61 FULL_TEXT, 1, # MySQL only (?)
64 # ----------------------------------------------------------------------
66 __PACKAGE__->_attributes( qw/
67 name type fields table
76 my $schema = SQL::Translator::Schema::Index->new;
80 # ----------------------------------------------------------------------
87 Gets and set the fields the index is on. Accepts a string, list or
88 arrayref; returns an array or array reference. Will unique the field
89 names and keep them in order by the first occurrence of a field name.
92 $index->fields('id', 'name');
93 $index->fields( 'id, name' );
94 $index->fields( [ 'id', 'name' ] );
95 $index->fields( qw[ id name ] );
97 my @fields = $index->fields;
102 my $fields = parse_list_arg( @_ );
105 my ( %unique, @unique );
106 for my $f ( @$fields ) {
107 next if $unique{ $f };
112 $self->{'fields'} = \@unique;
115 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
118 # ----------------------------------------------------------------------
125 Determine whether the index is valid or not.
127 my $ok = $index->is_valid;
132 my $table = $self->table or return $self->error('No table');
133 my @fields = $self->fields or return $self->error('No fields');
135 for my $field ( @fields ) {
137 "Field '$field' does not exist in table '", $table->name, "'"
138 ) unless $table->get_field( $field );
144 # ----------------------------------------------------------------------
151 Get or set the index's name.
153 my $name = $index->name('foo');
158 $self->{'name'} = shift if @_;
159 return $self->{'name'} || '';
162 # ----------------------------------------------------------------------
169 Get or set the index's options (e.g., "using" or "where" for PG). Returns
170 an array or array reference.
172 my @options = $index->options;
177 my $options = parse_list_arg( @_ );
179 push @{ $self->{'options'} }, @$options;
181 if ( ref $self->{'options'} ) {
182 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
185 return wantarray ? () : [];
189 # ----------------------------------------------------------------------
196 Get or set the index's table object.
198 my $table = $index->table;
203 if ( my $arg = shift ) {
204 return $self->error('Not a table object') unless
205 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
206 $self->{'table'} = $arg;
209 return $self->{'table'};
212 # ----------------------------------------------------------------------
219 Get or set the index's type.
221 my $type = $index->type('unique');
227 if ( my $type = shift ) {
228 return $self->error("Invalid index type: $type")
229 unless $VALID_INDEX_TYPE{ $type };
230 $self->{'type'} = $type;
233 return $self->{'type'} || NORMAL;
236 # ----------------------------------------------------------------------
243 Determines if this index is the same as another
245 my $isIdentical = $index1->equals( $index2 );
251 my $case_insensitive = shift;
253 return 0 unless $self->SUPER::equals($other);
254 # return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
255 return 0 unless $self->is_valid eq $other->is_valid;
256 return 0 unless $self->type eq $other->type;
257 my $selfFields = join(":", $self->fields);
258 my $otherFields = join(":", $other->fields);
259 return 0 unless $case_insensitive ? uc($selfFields) eq uc($otherFields) : $selfFields eq $otherFields;
260 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
261 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
265 # ----------------------------------------------------------------------
268 undef $self->{'table'}; # destroy cyclical reference
273 # ----------------------------------------------------------------------
279 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.