our > use vars
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Index.pm
CommitLineData
3c5de62a 1package SQL::Translator::Schema::Index;
2
3c5de62a 3=pod
4
5=head1 NAME
6
7SQL::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
20C<SQL::Translator::Schema::Index> is the index object.
21
b54199ae 22Primary and unique keys are table constraints, not indices.
3c5de62a 23
24=head1 METHODS
25
26=cut
27
28use strict;
f27f9229 29use warnings;
25868dc9 30use SQL::Translator::Schema::Constants;
31use SQL::Translator::Utils 'parse_list_arg';
3c5de62a 32
b6a880d1 33use base 'SQL::Translator::Schema::Object';
34
0c04c5a2 35our ( $TABLE_COUNT, $VIEW_COUNT );
da06ac74 36
0c04c5a2 37our $VERSION = '1.59';
3c5de62a 38
11ad2df9 39my %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 (?)
c8efc003 45);
3c5de62a 46
9371be50 47__PACKAGE__->_attributes( qw/
bdd8e79a 48 name type fields table options
9371be50 49/);
3c5de62a 50
51=pod
52
53=head2 new
54
55Object constructor.
56
57 my $schema = SQL::Translator::Schema::Index->new;
58
59=cut
60
3c5de62a 61sub fields {
62
63=pod
64
65=head2 fields
66
b54199ae 67Gets and set the fields the index is on. Accepts a string, list or
25868dc9 68arrayref; returns an array or array reference. Will unique the field
69names and keep them in order by the first occurrence of a field name.
3c5de62a 70
b54199ae 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 ] );
25868dc9 76
b54199ae 77 my @fields = $index->fields;
3c5de62a 78
79=cut
80
81 my $self = shift;
25868dc9 82 my $fields = parse_list_arg( @_ );
3c5de62a 83
84 if ( @$fields ) {
25868dc9 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;
3c5de62a 93 }
94
95 return wantarray ? @{ $self->{'fields'} || [] } : $self->{'fields'};
96}
97
b54199ae 98sub is_valid {
99
100=pod
101
102=head2 is_valid
103
104Determine 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
3c5de62a 123sub name {
124
125=pod
126
127=head2 name
128
129Get 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
25868dc9 140sub options {
141
142=pod
143
144=head2 options
145
146Get or set the index's options (e.g., "using" or "where" for PG). Returns
147an 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
43b9dc7a 166sub table {
167
168=pod
169
170=head2 table
171
172Get 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
3c5de62a 188sub type {
189
190=pod
191
192=head2 type
193
194Get or set the index's type.
195
196 my $type = $index->type('unique');
197
19ad0cee 198Get or set the index's options (e.g., "using" or "where" for PG). Returns
199
200Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
201and SPATIAL. The latter two might be MySQL-specific. While both lowercase
202and uppercase types are acceptable input, this method returns the type in
203uppercase.
204
3c5de62a 205=cut
206
c3b0b535 207 my ( $self, $type ) = @_;
3c5de62a 208
c3b0b535 209 if ( $type ) {
210 $type = uc $type;
ea93df61 211 return $self->error("Invalid index type: $type")
c8efc003 212 unless $VALID_INDEX_TYPE{ $type };
3c5de62a 213 $self->{'type'} = $type;
214 }
215
19ad0cee 216 return $self->{'type'} || 'NORMAL';
3c5de62a 217}
218
abf315bb 219sub equals {
220
221=pod
222
223=head2 equals
224
225Determines 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;
d990d84b 234 my $ignore_index_names = shift;
ea93df61 235
abf315bb 236 return 0 unless $self->SUPER::equals($other);
da5a1bae 237
d990d84b 238 unless ($ignore_index_names) {
da5a1bae 239 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
240 (!$other->name && ($self->name eq $self->fields->[0]))) {
d990d84b 241 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
da5a1bae 242 }
d990d84b 243 }
82f6b50e 244 #return 0 unless $self->is_valid eq $other->is_valid;
abf315bb 245 return 0 unless $self->type eq $other->type;
ea93df61 246
c243ec2b 247 # Check fields, regardless of order
ea93df61 248 my %otherFields = (); # create a hash of the other fields
c243ec2b 249 foreach my $otherField ($other->fields) {
ea93df61 250 $otherField = uc($otherField) if $case_insensitive;
251 $otherFields{$otherField} = 1;
c243ec2b 252 }
253 foreach my $selfField ($self->fields) { # check for self fields in hash
ea93df61 254 $selfField = uc($selfField) if $case_insensitive;
255 return 0 unless $otherFields{$selfField};
256 delete $otherFields{$selfField};
c243ec2b 257 }
258 # Check all other fields were accounted for
259 return 0 unless keys %otherFields == 0;
260
4598b71c 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);
abf315bb 263 return 1;
264}
3c5de62a 265
25868dc9 266sub DESTROY {
3c5de62a 267 my $self = shift;
25868dc9 268 undef $self->{'table'}; # destroy cyclical reference
3c5de62a 269}
270
2711;
272
3c5de62a 273=pod
274
275=head1 AUTHOR
276
c3b0b535 277Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
3c5de62a 278
279=cut