1 package SQL::Translator::Schema::Index;
3 # ----------------------------------------------------------------------
4 # $Id: Index.pm,v 1.2 2003-05-05 04:32:39 kycl4rk Exp $
5 # ----------------------------------------------------------------------
6 # Copyright (C) 2003 Ken Y. Clark <kclark@cpan.org>
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 keys will be considered table constraints, not indices.
51 use base 'Class::Base';
52 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
56 use constant VALID_TYPE => {
59 full_text => 1, # MySQL only (?)
62 # ----------------------------------------------------------------------
71 my $schema = SQL::Translator::Schema::Index->new;
75 my ( $self, $config ) = @_;
76 for my $arg ( qw[ name type fields ] ) {
77 next unless $config->{ $arg };
78 $self->$arg( $config->{ $arg } ) or return;
83 # ----------------------------------------------------------------------
90 Gets and set the fields the index is on. Accepts a list or arrayref,
93 my @fields = $index->fields( 'id' );
98 my $fields = ref $_[0] eq 'ARRAY' ? shift : [ @_ ];
101 $self->{'fields'} = $fields;
104 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
107 # ----------------------------------------------------------------------
114 Get or set the index's name.
116 my $name = $index->name('foo');
121 $self->{'name'} = shift if @_;
122 return $self->{'name'} || '';
125 # ----------------------------------------------------------------------
132 Get or set the index's table object.
134 my $table = $index->table;
139 if ( my $arg = shift ) {
140 return $self->error('Not a table object') unless
141 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
142 $self->{'table'} = $arg;
145 return $self->{'table'};
148 # ----------------------------------------------------------------------
155 Get or set the index's type.
157 my $type = $index->type('unique');
163 if ( my $type = shift ) {
164 return $self->error("Invalid index type: $type")
165 unless VALID_TYPE->{ $type };
166 $self->{'type'} = $type;
169 return $self->{'type'} || '';
173 # ----------------------------------------------------------------------
180 Determine whether the index is valid or not.
182 my $ok = $index->is_valid;
187 return ( $self->name && $self->{'type'} && @{ $self->fields } ) ? 1 : 0;
192 # ----------------------------------------------------------------------
198 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>