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