1 package SQL::Translator::Schema::Index;
3 # ----------------------------------------------------------------------
4 # $Id: Index.pm,v 1.8 2004-02-09 22:15:15 kycl4rk 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.
50 use SQL::Translator::Schema::Constants;
51 use SQL::Translator::Utils 'parse_list_arg';
53 use base 'Class::Base';
54 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
56 $VERSION = sprintf "%d.%02d", q$Revision: 1.8 $ =~ /(\d+)\.(\d+)/;
58 my %VALID_INDEX_TYPE = (
61 FULL_TEXT, 1, # MySQL only (?)
64 # ----------------------------------------------------------------------
73 my $schema = SQL::Translator::Schema::Index->new;
77 my ( $self, $config ) = @_;
79 for my $arg ( qw[ name type fields table ] ) {
80 next unless $config->{ $arg };
81 defined $self->$arg( $config->{ $arg } ) or return;
87 # ----------------------------------------------------------------------
94 Gets and set the fields the index is on. Accepts a string, list or
95 arrayref; returns an array or array reference. Will unique the field
96 names and keep them in order by the first occurrence of a field name.
99 $index->fields('id', 'name');
100 $index->fields( 'id, name' );
101 $index->fields( [ 'id', 'name' ] );
102 $index->fields( qw[ id name ] );
104 my @fields = $index->fields;
109 my $fields = parse_list_arg( @_ );
112 my ( %unique, @unique );
113 for my $f ( @$fields ) {
114 next if $unique{ $f };
119 $self->{'fields'} = \@unique;
122 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
125 # ----------------------------------------------------------------------
132 Determine whether the index is valid or not.
134 my $ok = $index->is_valid;
139 my $table = $self->table or return $self->error('No table');
140 my @fields = $self->fields or return $self->error('No fields');
142 for my $field ( @fields ) {
144 "Field '$field' does not exist in table '", $table->name, "'"
145 ) unless $table->get_field( $field );
151 # ----------------------------------------------------------------------
158 Get or set the index's name.
160 my $name = $index->name('foo');
165 $self->{'name'} = shift if @_;
166 return $self->{'name'} || '';
169 # ----------------------------------------------------------------------
176 Get or set the index's options (e.g., "using" or "where" for PG). Returns
177 an array or array reference.
179 my @options = $index->options;
184 my $options = parse_list_arg( @_ );
186 push @{ $self->{'options'} }, @$options;
188 if ( ref $self->{'options'} ) {
189 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
192 return wantarray ? () : [];
196 # ----------------------------------------------------------------------
203 Get or set the index's table object.
205 my $table = $index->table;
210 if ( my $arg = shift ) {
211 return $self->error('Not a table object') unless
212 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
213 $self->{'table'} = $arg;
216 return $self->{'table'};
219 # ----------------------------------------------------------------------
226 Get or set the index's type.
228 my $type = $index->type('unique');
234 if ( my $type = shift ) {
235 return $self->error("Invalid index type: $type")
236 unless $VALID_INDEX_TYPE{ $type };
237 $self->{'type'} = $type;
240 return $self->{'type'} || NORMAL;
244 # ----------------------------------------------------------------------
247 undef $self->{'table'}; # destroy cyclical reference
252 # ----------------------------------------------------------------------
258 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.