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