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 | |
2cb3ea55 |
28 | use Moo; |
25868dc9 |
29 | use SQL::Translator::Schema::Constants; |
45287c81 |
30 | use SQL::Translator::Utils qw(parse_list_arg ex2err throw); |
31 | use SQL::Translator::Types qw(schema_obj); |
2cb3ea55 |
32 | use List::MoreUtils qw(uniq); |
3c5de62a |
33 | |
2cb3ea55 |
34 | with qw( |
46ad748f |
35 | SQL::Translator::Schema::Role::BuildArgs |
2cb3ea55 |
36 | SQL::Translator::Schema::Role::Extra |
37 | SQL::Translator::Schema::Role::Error |
38 | SQL::Translator::Schema::Role::Compare |
39 | ); |
b6a880d1 |
40 | |
0c04c5a2 |
41 | our $VERSION = '1.59'; |
3c5de62a |
42 | |
11ad2df9 |
43 | my %VALID_INDEX_TYPE = ( |
44 | UNIQUE => 1, |
45 | NORMAL => 1, |
46 | FULLTEXT => 1, # MySQL only (?) |
47 | FULL_TEXT => 1, # MySQL only (?) |
48 | SPATIAL => 1, # MySQL only (?) |
c8efc003 |
49 | ); |
3c5de62a |
50 | |
3c5de62a |
51 | =head2 new |
52 | |
53 | Object constructor. |
54 | |
55 | my $schema = SQL::Translator::Schema::Index->new; |
56 | |
3c5de62a |
57 | =head2 fields |
58 | |
b54199ae |
59 | Gets and set the fields the index is on. Accepts a string, list or |
25868dc9 |
60 | arrayref; returns an array or array reference. Will unique the field |
61 | names and keep them in order by the first occurrence of a field name. |
3c5de62a |
62 | |
b54199ae |
63 | $index->fields('id'); |
64 | $index->fields('id', 'name'); |
65 | $index->fields( 'id, name' ); |
66 | $index->fields( [ 'id', 'name' ] ); |
67 | $index->fields( qw[ id name ] ); |
25868dc9 |
68 | |
b54199ae |
69 | my @fields = $index->fields; |
3c5de62a |
70 | |
71 | =cut |
72 | |
2cb3ea55 |
73 | has fields => ( |
74 | is => 'rw', |
75 | default => sub { [] }, |
76 | coerce => sub { [uniq @{parse_list_arg($_[0])}] }, |
77 | ); |
78 | |
79 | around fields => sub { |
80 | my $orig = shift; |
3c5de62a |
81 | my $self = shift; |
25868dc9 |
82 | my $fields = parse_list_arg( @_ ); |
2cb3ea55 |
83 | $self->$orig($fields) if @$fields; |
3c5de62a |
84 | |
2cb3ea55 |
85 | return wantarray ? @{ $self->$orig } : $self->$orig; |
86 | }; |
3c5de62a |
87 | |
b54199ae |
88 | sub is_valid { |
89 | |
90 | =pod |
91 | |
92 | =head2 is_valid |
93 | |
94 | Determine whether the index is valid or not. |
95 | |
96 | my $ok = $index->is_valid; |
97 | |
98 | =cut |
99 | |
100 | my $self = shift; |
101 | my $table = $self->table or return $self->error('No table'); |
102 | my @fields = $self->fields or return $self->error('No fields'); |
103 | |
104 | for my $field ( @fields ) { |
105 | return $self->error( |
106 | "Field '$field' does not exist in table '", $table->name, "'" |
107 | ) unless $table->get_field( $field ); |
108 | } |
109 | |
110 | return 1; |
111 | } |
112 | |
3c5de62a |
113 | =head2 name |
114 | |
115 | Get or set the index's name. |
116 | |
117 | my $name = $index->name('foo'); |
118 | |
119 | =cut |
120 | |
2cb3ea55 |
121 | has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } ); |
25868dc9 |
122 | |
123 | =head2 options |
124 | |
125 | Get or set the index's options (e.g., "using" or "where" for PG). Returns |
126 | an array or array reference. |
127 | |
128 | my @options = $index->options; |
129 | |
130 | =cut |
131 | |
2cb3ea55 |
132 | has options => ( |
133 | is => 'rw', |
134 | default => sub { [] }, |
45287c81 |
135 | coerce => \&parse_list_arg, |
2cb3ea55 |
136 | ); |
137 | |
138 | around options => sub { |
139 | my $orig = shift; |
25868dc9 |
140 | my $self = shift; |
141 | my $options = parse_list_arg( @_ ); |
142 | |
2cb3ea55 |
143 | push @{ $self->$orig }, @$options; |
43b9dc7a |
144 | |
2cb3ea55 |
145 | return wantarray ? @{ $self->$orig } : $self->$orig; |
146 | }; |
43b9dc7a |
147 | |
148 | =head2 table |
149 | |
150 | Get or set the index's table object. |
151 | |
152 | my $table = $index->table; |
153 | |
154 | =cut |
155 | |
45287c81 |
156 | has table => ( is => 'rw', isa => schema_obj('Table') ); |
43b9dc7a |
157 | |
45287c81 |
158 | around table => \&ex2err; |
3c5de62a |
159 | |
160 | =head2 type |
161 | |
162 | Get or set the index's type. |
163 | |
164 | my $type = $index->type('unique'); |
165 | |
d06db857 |
166 | Get or set the index's type. |
19ad0cee |
167 | |
168 | Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT, |
169 | and SPATIAL. The latter two might be MySQL-specific. While both lowercase |
170 | and uppercase types are acceptable input, this method returns the type in |
171 | uppercase. |
172 | |
3c5de62a |
173 | =cut |
174 | |
45287c81 |
175 | has type => ( |
176 | is => 'rw', |
177 | isa => sub { |
178 | my $type = uc $_[0] or return; |
179 | throw("Invalid index type: $type") unless $VALID_INDEX_TYPE{$type}; |
180 | }, |
181 | coerce => sub { uc $_[0] }, |
182 | default => sub { 'NORMAL' }, |
183 | ); |
3c5de62a |
184 | |
45287c81 |
185 | around type => \&ex2err; |
abf315bb |
186 | |
187 | =head2 equals |
188 | |
189 | Determines if this index is the same as another |
190 | |
191 | my $isIdentical = $index1->equals( $index2 ); |
192 | |
193 | =cut |
194 | |
2cb3ea55 |
195 | around equals => sub { |
196 | my $orig = shift; |
abf315bb |
197 | my $self = shift; |
198 | my $other = shift; |
199 | my $case_insensitive = shift; |
d990d84b |
200 | my $ignore_index_names = shift; |
ea93df61 |
201 | |
2cb3ea55 |
202 | return 0 unless $self->$orig($other); |
da5a1bae |
203 | |
d990d84b |
204 | unless ($ignore_index_names) { |
da5a1bae |
205 | unless ((!$self->name && ($other->name eq $other->fields->[0])) || |
206 | (!$other->name && ($self->name eq $self->fields->[0]))) { |
d990d84b |
207 | return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name; |
da5a1bae |
208 | } |
d990d84b |
209 | } |
82f6b50e |
210 | #return 0 unless $self->is_valid eq $other->is_valid; |
abf315bb |
211 | return 0 unless $self->type eq $other->type; |
ea93df61 |
212 | |
c243ec2b |
213 | # Check fields, regardless of order |
ea93df61 |
214 | my %otherFields = (); # create a hash of the other fields |
c243ec2b |
215 | foreach my $otherField ($other->fields) { |
ea93df61 |
216 | $otherField = uc($otherField) if $case_insensitive; |
217 | $otherFields{$otherField} = 1; |
c243ec2b |
218 | } |
219 | foreach my $selfField ($self->fields) { # check for self fields in hash |
ea93df61 |
220 | $selfField = uc($selfField) if $case_insensitive; |
221 | return 0 unless $otherFields{$selfField}; |
222 | delete $otherFields{$selfField}; |
c243ec2b |
223 | } |
224 | # Check all other fields were accounted for |
225 | return 0 unless keys %otherFields == 0; |
226 | |
4598b71c |
227 | return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options); |
228 | return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra); |
abf315bb |
229 | return 1; |
2cb3ea55 |
230 | }; |
3c5de62a |
231 | |
45287c81 |
232 | # Must come after all 'has' declarations |
233 | around new => \&ex2err; |
234 | |
3c5de62a |
235 | 1; |
236 | |
3c5de62a |
237 | =pod |
238 | |
239 | =head1 AUTHOR |
240 | |
c3b0b535 |
241 | Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>. |
3c5de62a |
242 | |
243 | =cut |