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
1 package SQL::Translator::Schema::Index;
2
3 # ----------------------------------------------------------------------
4 # Copyright (C) 2002-2009 SQLFairy Authors
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
25 SQL::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
38 C<SQL::Translator::Schema::Index> is the index object.
39
40 Primary and unique keys are table constraints, not indices.
41
42 =head1 METHODS
43
44 =cut
45
46 use strict;
47 use SQL::Translator::Schema::Constants;
48 use SQL::Translator::Utils 'parse_list_arg';
49
50 use base 'SQL::Translator::Schema::Object';
51
52 use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT);
53
54 $VERSION = '1.59';
55
56 my %VALID_INDEX_TYPE = (
57   UNIQUE         => 1,
58   NORMAL         => 1,
59   FULL_TEXT      => 1, # MySQL only (?)
60   SPATIAL        => 1, # MySQL only (?)
61 );
62
63 # ----------------------------------------------------------------------
64
65 __PACKAGE__->_attributes( qw/
66     name type fields table options
67 /);
68
69 =pod
70
71 =head2 new
72
73 Object constructor.
74
75   my $schema = SQL::Translator::Schema::Index->new;
76
77 =cut
78
79 # ----------------------------------------------------------------------
80 sub fields {
81
82 =pod
83
84 =head2 fields
85
86 Gets and set the fields the index is on.  Accepts a string, list or
87 arrayref; returns an array or array reference.  Will unique the field
88 names and keep them in order by the first occurrence of a field name.
89
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 ] );
95
96   my @fields = $index->fields;
97
98 =cut
99
100     my $self   = shift;
101     my $fields = parse_list_arg( @_ );
102
103     if ( @$fields ) {
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;
112     }
113
114     return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
115 }
116
117 # ----------------------------------------------------------------------
118 sub is_valid {
119
120 =pod
121
122 =head2 is_valid
123
124 Determine 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 # ----------------------------------------------------------------------
144 sub name {
145
146 =pod
147
148 =head2 name
149
150 Get 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 # ----------------------------------------------------------------------
162 sub options {
163
164 =pod
165
166 =head2 options
167
168 Get or set the index's options (e.g., "using" or "where" for PG).  Returns
169 an 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 # ----------------------------------------------------------------------
189 sub table {
190
191 =pod
192
193 =head2 table
194
195 Get 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 # ----------------------------------------------------------------------
212 sub type {
213
214 =pod
215
216 =head2 type
217
218 Get or set the index's type.
219
220   my $type = $index->type('unique');
221
222 Get or set the index's options (e.g., "using" or "where" for PG).  Returns
223
224 Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
225 and SPATIAL. The latter two might be MySQL-specific. While both lowercase
226 and uppercase types are acceptable input, this method returns the type in
227 uppercase.
228
229 =cut
230
231     my $self = shift;
232
233     if ( my $type = uc shift ) {
234         return $self->error("Invalid index type: $type") 
235             unless $VALID_INDEX_TYPE{ $type };
236         $self->{'type'} = $type;
237     }
238
239     return $self->{'type'} || 'NORMAL';
240 }
241
242 # ----------------------------------------------------------------------
243 sub equals {
244
245 =pod
246
247 =head2 equals
248
249 Determines 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;
258     my $ignore_index_names = shift;
259     
260     return 0 unless $self->SUPER::equals($other);
261
262     unless ($ignore_index_names) {
263       unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
264         (!$other->name && ($self->name eq $self->fields->[0]))) {
265         return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
266       }
267     }
268     #return 0 unless $self->is_valid eq $other->is_valid;
269     return 0 unless $self->type eq $other->type;
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
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);
287     return 1;
288 }
289
290 # ----------------------------------------------------------------------
291 sub DESTROY {
292     my $self = shift;
293     undef $self->{'table'}; # destroy cyclical reference
294 }
295
296 1;
297
298 # ----------------------------------------------------------------------
299
300 =pod
301
302 =head1 AUTHOR
303
304 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
305
306 =cut