- Updated GraphViz producer module per the modifications discussed on the sqlfairy...
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Index.pm
CommitLineData
3c5de62a 1package SQL::Translator::Schema::Index;
2
3# ----------------------------------------------------------------------
da5a1bae 4# $Id: Index.pm,v 1.18 2007-10-24 10:55:44 schiffbruechige Exp $
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
da5a1bae 56$VERSION = sprintf "%d.%02d", q$Revision: 1.18 $ =~ /(\d+)\.(\d+)/;
3c5de62a 57
c8efc003 58my %VALID_INDEX_TYPE = (
25868dc9 59 UNIQUE, 1,
60 NORMAL, 1,
61 FULL_TEXT, 1, # MySQL only (?)
531652d6 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
224=cut
225
226 my $self = shift;
227
228 if ( my $type = shift ) {
229 return $self->error("Invalid index type: $type")
c8efc003 230 unless $VALID_INDEX_TYPE{ $type };
3c5de62a 231 $self->{'type'} = $type;
232 }
233
25868dc9 234 return $self->{'type'} || NORMAL;
3c5de62a 235}
236
abf315bb 237# ----------------------------------------------------------------------
238sub equals {
239
240=pod
241
242=head2 equals
243
244Determines if this index is the same as another
245
246 my $isIdentical = $index1->equals( $index2 );
247
248=cut
249
250 my $self = shift;
251 my $other = shift;
252 my $case_insensitive = shift;
d990d84b 253 my $ignore_index_names = shift;
abf315bb 254
255 return 0 unless $self->SUPER::equals($other);
da5a1bae 256
d990d84b 257 unless ($ignore_index_names) {
da5a1bae 258 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
259 (!$other->name && ($self->name eq $self->fields->[0]))) {
d990d84b 260 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
da5a1bae 261 }
d990d84b 262 }
82f6b50e 263 #return 0 unless $self->is_valid eq $other->is_valid;
abf315bb 264 return 0 unless $self->type eq $other->type;
c243ec2b 265
266 # Check fields, regardless of order
267 my %otherFields = (); # create a hash of the other fields
268 foreach my $otherField ($other->fields) {
269 $otherField = uc($otherField) if $case_insensitive;
270 $otherFields{$otherField} = 1;
271 }
272 foreach my $selfField ($self->fields) { # check for self fields in hash
273 $selfField = uc($selfField) if $case_insensitive;
274 return 0 unless $otherFields{$selfField};
275 delete $otherFields{$selfField};
276 }
277 # Check all other fields were accounted for
278 return 0 unless keys %otherFields == 0;
279
4598b71c 280 return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
281 return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
abf315bb 282 return 1;
283}
3c5de62a 284
285# ----------------------------------------------------------------------
25868dc9 286sub DESTROY {
3c5de62a 287 my $self = shift;
25868dc9 288 undef $self->{'table'}; # destroy cyclical reference
3c5de62a 289}
290
2911;
292
293# ----------------------------------------------------------------------
294
295=pod
296
297=head1 AUTHOR
298
6606c4c6 299Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
3c5de62a 300
301=cut