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