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