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