Remove unused variables and module import
[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 qw(parse_list_arg ex2err throw);
31 use SQL::Translator::Types qw(schema_obj);
32 use List::MoreUtils qw(uniq);
33
34 with qw(
35   SQL::Translator::Schema::Role::BuildArgs
36   SQL::Translator::Schema::Role::Extra
37   SQL::Translator::Schema::Role::Error
38   SQL::Translator::Schema::Role::Compare
39 );
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 =head2 fields
58
59 Gets and set the fields the index is on.  Accepts a string, list or
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.
62
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 ] );
68
69   my @fields = $index->fields;
70
71 =cut
72
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;
81     my $self   = shift;
82     my $fields = parse_list_arg( @_ );
83     $self->$orig($fields) if @$fields;
84
85     return wantarray ? @{ $self->$orig } : $self->$orig;
86 };
87
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
113 =head2 name
114
115 Get or set the index's name.
116
117   my $name = $index->name('foo');
118
119 =cut
120
121 has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } );
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
132 has options => (
133     is => 'rw',
134     default => sub { [] },
135     coerce => \&parse_list_arg,
136 );
137
138 around options => sub {
139     my $orig    = shift;
140     my $self    = shift;
141     my $options = parse_list_arg( @_ );
142
143     push @{ $self->$orig }, @$options;
144
145     return wantarray ? @{ $self->$orig } : $self->$orig;
146 };
147
148 =head2 table
149
150 Get or set the index's table object.
151
152   my $table = $index->table;
153
154 =cut
155
156 has table => ( is => 'rw', isa => schema_obj('Table') );
157
158 around table => \&ex2err;
159
160 =head2 type
161
162 Get or set the index's type.
163
164   my $type = $index->type('unique');
165
166 Get or set the index's type.
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
173 =cut
174
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 );
184
185 around type => \&ex2err;
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
195 around equals => sub {
196     my $orig = shift;
197     my $self = shift;
198     my $other = shift;
199     my $case_insensitive = shift;
200     my $ignore_index_names = shift;
201
202     return 0 unless $self->$orig($other);
203
204     unless ($ignore_index_names) {
205       unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
206         (!$other->name && ($self->name eq $self->fields->[0]))) {
207         return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
208       }
209     }
210     #return 0 unless $self->is_valid eq $other->is_valid;
211     return 0 unless $self->type eq $other->type;
212
213     # Check fields, regardless of order
214     my %otherFields = ();  # create a hash of the other fields
215     foreach my $otherField ($other->fields) {
216       $otherField = uc($otherField) if $case_insensitive;
217       $otherFields{$otherField} = 1;
218     }
219     foreach my $selfField ($self->fields) { # check for self fields in hash
220       $selfField = uc($selfField) if $case_insensitive;
221       return 0 unless $otherFields{$selfField};
222       delete $otherFields{$selfField};
223     }
224     # Check all other fields were accounted for
225     return 0 unless keys %otherFields == 0;
226
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);
229     return 1;
230 };
231
232 sub DESTROY {
233     my $self = shift;
234     undef $self->{'table'}; # destroy cyclical reference
235 }
236
237 # Must come after all 'has' declarations
238 around new => \&ex2err;
239
240 1;
241
242 =pod
243
244 =head1 AUTHOR
245
246 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
247
248 =cut