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