Commit | Line | Data |
3c5de62a |
1 | package SQL::Translator::Schema::Index; |
2 | |
3c5de62a |
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 | |
b54199ae |
22 | Primary and unique keys are table constraints, not indices. |
3c5de62a |
23 | |
24 | =head1 METHODS |
25 | |
26 | =cut |
27 | |
28 | use strict; |
f27f9229 |
29 | use warnings; |
25868dc9 |
30 | use SQL::Translator::Schema::Constants; |
31 | use SQL::Translator::Utils 'parse_list_arg'; |
3c5de62a |
32 | |
b6a880d1 |
33 | use base 'SQL::Translator::Schema::Object'; |
34 | |
0c04c5a2 |
35 | our ( $TABLE_COUNT, $VIEW_COUNT ); |
da06ac74 |
36 | |
0c04c5a2 |
37 | our $VERSION = '1.59'; |
3c5de62a |
38 | |
11ad2df9 |
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 (?) |
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 | |
55 | Object constructor. |
56 | |
57 | my $schema = SQL::Translator::Schema::Index->new; |
58 | |
59 | =cut |
60 | |
3c5de62a |
61 | sub fields { |
62 | |
63 | =pod |
64 | |
65 | =head2 fields |
66 | |
b54199ae |
67 | Gets and set the fields the index is on. Accepts a string, list or |
25868dc9 |
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. |
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 |
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 | |
3c5de62a |
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 | |
25868dc9 |
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 | |
43b9dc7a |
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 | |
3c5de62a |
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 | |
d06db857 |
198 | Get or set the index's type. |
19ad0cee |
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 | |
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 |
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; |
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 |
266 | sub DESTROY { |
3c5de62a |
267 | my $self = shift; |
25868dc9 |
268 | undef $self->{'table'}; # destroy cyclical reference |
3c5de62a |
269 | } |
270 | |
271 | 1; |
272 | |
3c5de62a |
273 | =pod |
274 | |
275 | =head1 AUTHOR |
276 | |
c3b0b535 |
277 | Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>. |
3c5de62a |
278 | |
279 | =cut |