Mooify SQLT::Schema::Index
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Index.pm
1 package SQL::Translator::Schema::Index;
2
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
22 Primary and unique keys are table constraints, not indices.
23
24 =head1 METHODS
25
26 =cut
27
28 use Moo;
29 use SQL::Translator::Schema::Constants;
30 use SQL::Translator::Utils 'parse_list_arg';
31 use List::MoreUtils qw(uniq);
32
33 with qw(
34   SQL::Translator::Schema::Role::Extra
35   SQL::Translator::Schema::Role::Error
36   SQL::Translator::Schema::Role::Compare
37 );
38
39 our ( $TABLE_COUNT, $VIEW_COUNT );
40
41 our $VERSION = '1.59';
42
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 (?)
49 );
50
51 =head2 new
52
53 Object constructor.
54
55   my $schema = SQL::Translator::Schema::Index->new;
56
57 =cut
58
59 sub BUILD {
60     my ($self) = @_;
61     $self->$_(scalar $self->$_)
62         foreach qw(fields options);
63 }
64
65 =head2 fields
66
67 Gets and set the fields the index is on.  Accepts a string, list or
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.
70
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 ] );
76
77   my @fields = $index->fields;
78
79 =cut
80
81 has fields => (
82     is => 'rw',
83     default => sub { [] },
84     coerce => sub { [uniq @{parse_list_arg($_[0])}] },
85 );
86
87 around fields => sub {
88     my $orig   = shift;
89     my $self   = shift;
90     my $fields = parse_list_arg( @_ );
91     $self->$orig($fields) if @$fields;
92
93     return wantarray ? @{ $self->$orig } : $self->$orig;
94 };
95
96 sub is_valid {
97
98 =pod
99
100 =head2 is_valid
101
102 Determine whether the index is valid or not.
103
104   my $ok = $index->is_valid;
105
106 =cut
107
108     my $self   = shift;
109     my $table  = $self->table  or return $self->error('No table');
110     my @fields = $self->fields or return $self->error('No fields');
111
112     for my $field ( @fields ) {
113         return $self->error(
114             "Field '$field' does not exist in table '", $table->name, "'"
115         ) unless $table->get_field( $field );
116     }
117
118     return 1;
119 }
120
121 =head2 name
122
123 Get or set the index's name.
124
125   my $name = $index->name('foo');
126
127 =cut
128
129 has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } );
130
131 =head2 options
132
133 Get or set the index's options (e.g., "using" or "where" for PG).  Returns
134 an array or array reference.
135
136   my @options = $index->options;
137
138 =cut
139
140 has options => (
141     is => 'rw',
142     default => sub { [] },
143     coerce => sub { parse_list_arg($_[0]) },
144 );
145
146 around options => sub {
147     my $orig    = shift;
148     my $self    = shift;
149     my $options = parse_list_arg( @_ );
150
151     push @{ $self->$orig }, @$options;
152
153     return wantarray ? @{ $self->$orig } : $self->$orig;
154 };
155
156 =head2 table
157
158 Get or set the index's table object.
159
160   my $table = $index->table;
161
162 =cut
163
164 has table => ( is => 'rw' );
165
166 around table => sub {
167     my $orig = shift;
168     my $self = shift;
169     if ( my $arg = $_[0] ) {
170         return $self->error('Not a table object') unless
171             UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
172     }
173
174     return $self->$orig(@_);
175 };
176
177 =head2 type
178
179 Get or set the index's type.
180
181   my $type = $index->type('unique');
182
183 Get or set the index's type.
184
185 Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
186 and SPATIAL. The latter two might be MySQL-specific. While both lowercase
187 and uppercase types are acceptable input, this method returns the type in
188 uppercase.
189
190 =cut
191
192 has type => ( is => 'rw', default => sub { 'NORMAL' } );
193
194 around type => sub {
195     my ( $orig, $self) = (shift, shift);
196
197     if ( my $type = $_[0] ) {
198         my $type = uc $type;
199         return $self->error("Invalid index type: $type")
200             unless $VALID_INDEX_TYPE{ $type };
201     }
202
203     return $self->$orig(@_);
204 };
205
206 =head2 equals
207
208 Determines if this index is the same as another
209
210   my $isIdentical = $index1->equals( $index2 );
211
212 =cut
213
214 around equals => sub {
215     my $orig = shift;
216     my $self = shift;
217     my $other = shift;
218     my $case_insensitive = shift;
219     my $ignore_index_names = shift;
220
221     return 0 unless $self->$orig($other);
222
223     unless ($ignore_index_names) {
224       unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
225         (!$other->name && ($self->name eq $self->fields->[0]))) {
226         return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
227       }
228     }
229     #return 0 unless $self->is_valid eq $other->is_valid;
230     return 0 unless $self->type eq $other->type;
231
232     # Check fields, regardless of order
233     my %otherFields = ();  # create a hash of the other fields
234     foreach my $otherField ($other->fields) {
235       $otherField = uc($otherField) if $case_insensitive;
236       $otherFields{$otherField} = 1;
237     }
238     foreach my $selfField ($self->fields) { # check for self fields in hash
239       $selfField = uc($selfField) if $case_insensitive;
240       return 0 unless $otherFields{$selfField};
241       delete $otherFields{$selfField};
242     }
243     # Check all other fields were accounted for
244     return 0 unless keys %otherFields == 0;
245
246     return 0 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
247     return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
248     return 1;
249 };
250
251 sub DESTROY {
252     my $self = shift;
253     undef $self->{'table'}; # destroy cyclical reference
254 }
255
256 1;
257
258 =pod
259
260 =head1 AUTHOR
261
262 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
263
264 =cut