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