Remove pointless DESTROY methods
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema / Index.pm
CommitLineData
3c5de62a 1package SQL::Translator::Schema::Index;
2
3c5de62a 3=pod
4
5=head1 NAME
6
7SQL::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
20C<SQL::Translator::Schema::Index> is the index object.
21
b54199ae 22Primary and unique keys are table constraints, not indices.
3c5de62a 23
24=head1 METHODS
25
26=cut
27
2cb3ea55 28use Moo;
25868dc9 29use SQL::Translator::Schema::Constants;
45287c81 30use SQL::Translator::Utils qw(parse_list_arg ex2err throw);
31use SQL::Translator::Types qw(schema_obj);
2cb3ea55 32use List::MoreUtils qw(uniq);
3c5de62a 33
2cb3ea55 34with 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 41our $VERSION = '1.59';
3c5de62a 42
11ad2df9 43my %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
53Object constructor.
54
55 my $schema = SQL::Translator::Schema::Index->new;
56
3c5de62a 57=head2 fields
58
b54199ae 59Gets and set the fields the index is on. Accepts a string, list or
25868dc9 60arrayref; returns an array or array reference. Will unique the field
61names 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 73has fields => (
74 is => 'rw',
75 default => sub { [] },
76 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
77);
78
79around 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 88sub is_valid {
89
90=pod
91
92=head2 is_valid
93
94Determine 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
115Get or set the index's name.
116
117 my $name = $index->name('foo');
118
119=cut
120
2cb3ea55 121has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } );
25868dc9 122
123=head2 options
124
125Get or set the index's options (e.g., "using" or "where" for PG). Returns
126an array or array reference.
127
128 my @options = $index->options;
129
130=cut
131
2cb3ea55 132has options => (
133 is => 'rw',
134 default => sub { [] },
45287c81 135 coerce => \&parse_list_arg,
2cb3ea55 136);
137
138around 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
150Get or set the index's table object.
151
152 my $table = $index->table;
153
154=cut
155
45287c81 156has table => ( is => 'rw', isa => schema_obj('Table') );
43b9dc7a 157
45287c81 158around table => \&ex2err;
3c5de62a 159
160=head2 type
161
162Get or set the index's type.
163
164 my $type = $index->type('unique');
165
d06db857 166Get or set the index's type.
19ad0cee 167
168Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
169and SPATIAL. The latter two might be MySQL-specific. While both lowercase
170and uppercase types are acceptable input, this method returns the type in
171uppercase.
172
3c5de62a 173=cut
174
45287c81 175has 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 185around type => \&ex2err;
abf315bb 186
187=head2 equals
188
189Determines if this index is the same as another
190
191 my $isIdentical = $index1->equals( $index2 );
192
193=cut
194
2cb3ea55 195around 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
233around new => \&ex2err;
234
3c5de62a 2351;
236
3c5de62a 237=pod
238
239=head1 AUTHOR
240
c3b0b535 241Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
3c5de62a 242
243=cut