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