- Added some stuff to MANIFEST.SKIP
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Index.pm
CommitLineData
3c5de62a 1package SQL::Translator::Schema::Index;
2
3# ----------------------------------------------------------------------
821a0fde 4# $Id$
3c5de62a 5# ----------------------------------------------------------------------
6606c4c6 6# Copyright (C) 2002-4 SQLFairy Authors
3c5de62a 7#
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.
11#
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.
16#
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
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
23=pod
24
25=head1 NAME
26
27SQL::Translator::Schema::Index - SQL::Translator index object
28
29=head1 SYNOPSIS
30
31 use SQL::Translator::Schema::Index;
32 my $index = SQL::Translator::Schema::Index->new(
33 name => 'foo',
34 fields => [ id ],
35 type => 'unique',
36 );
37
38=head1 DESCRIPTION
39
40C<SQL::Translator::Schema::Index> is the index object.
41
b54199ae 42Primary and unique keys are table constraints, not indices.
3c5de62a 43
44=head1 METHODS
45
46=cut
47
48use strict;
25868dc9 49use SQL::Translator::Schema::Constants;
50use SQL::Translator::Utils 'parse_list_arg';
3c5de62a 51
b6a880d1 52use base 'SQL::Translator::Schema::Object';
53
3c5de62a 54use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
55
821a0fde 56$VERSION = sprintf "%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/;
3c5de62a 57
c8efc003 58my %VALID_INDEX_TYPE = (
19ad0cee 59 UNIQUE => 1,
60 NORMAL => 1,
61 FULL_TEXT => 1, # MySQL only (?)
62 SPATIAL => 1, # MySQL only (?)
c8efc003 63);
3c5de62a 64
65# ----------------------------------------------------------------------
9371be50 66
67__PACKAGE__->_attributes( qw/
bdd8e79a 68 name type fields table options
9371be50 69/);
3c5de62a 70
71=pod
72
73=head2 new
74
75Object constructor.
76
77 my $schema = SQL::Translator::Schema::Index->new;
78
79=cut
80
3c5de62a 81# ----------------------------------------------------------------------
82sub fields {
83
84=pod
85
86=head2 fields
87
b54199ae 88Gets and set the fields the index is on. Accepts a string, list or
25868dc9 89arrayref; returns an array or array reference. Will unique the field
90names and keep them in order by the first occurrence of a field name.
3c5de62a 91
b54199ae 92 $index->fields('id');
93 $index->fields('id', 'name');
94 $index->fields( 'id, name' );
95 $index->fields( [ 'id', 'name' ] );
96 $index->fields( qw[ id name ] );
25868dc9 97
b54199ae 98 my @fields = $index->fields;
3c5de62a 99
100=cut
101
102 my $self = shift;
25868dc9 103 my $fields = parse_list_arg( @_ );
3c5de62a 104
105 if ( @$fields ) {
25868dc9 106 my ( %unique, @unique );
107 for my $f ( @$fields ) {
108 next if $unique{ $f };
109 $unique{ $f } = 1;
110 push @unique, $f;
111 }
112
113 $self->{'fields'} = \@unique;
3c5de62a 114 }
115
116 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
117}
118
119# ----------------------------------------------------------------------
b54199ae 120sub is_valid {
121
122=pod
123
124=head2 is_valid
125
126Determine whether the index is valid or not.
127
128 my $ok = $index->is_valid;
129
130=cut
131
132 my $self = shift;
133 my $table = $self->table or return $self->error('No table');
134 my @fields = $self->fields or return $self->error('No fields');
135
136 for my $field ( @fields ) {
137 return $self->error(
138 "Field '$field' does not exist in table '", $table->name, "'"
139 ) unless $table->get_field( $field );
140 }
141
142 return 1;
143}
144
145# ----------------------------------------------------------------------
3c5de62a 146sub name {
147
148=pod
149
150=head2 name
151
152Get or set the index's name.
153
154 my $name = $index->name('foo');
155
156=cut
157
158 my $self = shift;
159 $self->{'name'} = shift if @_;
160 return $self->{'name'} || '';
161}
162
163# ----------------------------------------------------------------------
25868dc9 164sub options {
165
166=pod
167
168=head2 options
169
170Get or set the index's options (e.g., "using" or "where" for PG). Returns
171an array or array reference.
172
173 my @options = $index->options;
174
175=cut
176
177 my $self = shift;
178 my $options = parse_list_arg( @_ );
179
180 push @{ $self->{'options'} }, @$options;
181
182 if ( ref $self->{'options'} ) {
183 return wantarray ? @{ $self->{'options'} || [] } : $self->{'options'};
184 }
185 else {
186 return wantarray ? () : [];
187 }
188}
189
190# ----------------------------------------------------------------------
43b9dc7a 191sub table {
192
193=pod
194
195=head2 table
196
197Get or set the index's table object.
198
199 my $table = $index->table;
200
201=cut
202
203 my $self = shift;
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;
208 }
209
210 return $self->{'table'};
211}
212
213# ----------------------------------------------------------------------
3c5de62a 214sub type {
215
216=pod
217
218=head2 type
219
220Get or set the index's type.
221
222 my $type = $index->type('unique');
223
19ad0cee 224Get or set the index's options (e.g., "using" or "where" for PG). Returns
225
226Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
227and SPATIAL. The latter two might be MySQL-specific. While both lowercase
228and uppercase types are acceptable input, this method returns the type in
229uppercase.
230
3c5de62a 231=cut
232
233 my $self = shift;
234
19ad0cee 235 if ( my $type = uc shift ) {
3c5de62a 236 return $self->error("Invalid index type: $type")
c8efc003 237 unless $VALID_INDEX_TYPE{ $type };
3c5de62a 238 $self->{'type'} = $type;
239 }
240
19ad0cee 241 return $self->{'type'} || 'NORMAL';
3c5de62a 242}
243
abf315bb 244# ----------------------------------------------------------------------
245sub equals {
246
247=pod
248
249=head2 equals
250
251Determines if this index is the same as another
252
253 my $isIdentical = $index1->equals( $index2 );
254
255=cut
256
257 my $self = shift;
258 my $other = shift;
259 my $case_insensitive = shift;
d990d84b 260 my $ignore_index_names = shift;
abf315bb 261
262 return 0 unless $self->SUPER::equals($other);
da5a1bae 263
d990d84b 264 unless ($ignore_index_names) {
da5a1bae 265 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
266 (!$other->name && ($self->name eq $self->fields->[0]))) {
d990d84b 267 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
da5a1bae 268 }
d990d84b 269 }
82f6b50e 270 #return 0 unless $self->is_valid eq $other->is_valid;
abf315bb 271 return 0 unless $self->type eq $other->type;
c243ec2b 272
273 # Check fields, regardless of order
274 my %otherFields = (); # create a hash of the other fields
275 foreach my $otherField ($other->fields) {
276 $otherField = uc($otherField) if $case_insensitive;
277 $otherFields{$otherField} = 1;
278 }
279 foreach my $selfField ($self->fields) { # check for self fields in hash
280 $selfField = uc($selfField) if $case_insensitive;
281 return 0 unless $otherFields{$selfField};
282 delete $otherFields{$selfField};
283 }
284 # Check all other fields were accounted for
285 return 0 unless keys %otherFields == 0;
286
4598b71c 287 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
288 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
abf315bb 289 return 1;
290}
3c5de62a 291
292# ----------------------------------------------------------------------
25868dc9 293sub DESTROY {
3c5de62a 294 my $self = shift;
25868dc9 295 undef $self->{'table'}; # destroy cyclical reference
3c5de62a 296}
297
2981;
299
300# ----------------------------------------------------------------------
301
302=pod
303
304=head1 AUTHOR
305
6606c4c6 306Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
3c5de62a 307
308=cut