Mooify SQLT::Schema::Constraint
[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(
35 SQL::Translator::Schema::Role::Extra
36 SQL::Translator::Schema::Role::Error
37 SQL::Translator::Schema::Role::Compare
38);
b6a880d1 39
0c04c5a2 40our ( $TABLE_COUNT, $VIEW_COUNT );
da06ac74 41
0c04c5a2 42our $VERSION = '1.59';
3c5de62a 43
11ad2df9 44my %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 (?)
c8efc003 50);
3c5de62a 51
3c5de62a 52=head2 new
53
54Object constructor.
55
56 my $schema = SQL::Translator::Schema::Index->new;
57
3c5de62a 58=head2 fields
59
b54199ae 60Gets and set the fields the index is on. Accepts a string, list or
25868dc9 61arrayref; returns an array or array reference. Will unique the field
62names and keep them in order by the first occurrence of a field name.
3c5de62a 63
b54199ae 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 ] );
25868dc9 69
b54199ae 70 my @fields = $index->fields;
3c5de62a 71
72=cut
73
2cb3ea55 74has fields => (
75 is => 'rw',
76 default => sub { [] },
77 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
78);
79
80around fields => sub {
81 my $orig = shift;
3c5de62a 82 my $self = shift;
25868dc9 83 my $fields = parse_list_arg( @_ );
2cb3ea55 84 $self->$orig($fields) if @$fields;
3c5de62a 85
2cb3ea55 86 return wantarray ? @{ $self->$orig } : $self->$orig;
87};
3c5de62a 88
b54199ae 89sub is_valid {
90
91=pod
92
93=head2 is_valid
94
95Determine 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
3c5de62a 114=head2 name
115
116Get or set the index's name.
117
118 my $name = $index->name('foo');
119
120=cut
121
2cb3ea55 122has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } );
25868dc9 123
124=head2 options
125
126Get or set the index's options (e.g., "using" or "where" for PG). Returns
127an array or array reference.
128
129 my @options = $index->options;
130
131=cut
132
2cb3ea55 133has options => (
134 is => 'rw',
135 default => sub { [] },
45287c81 136 coerce => \&parse_list_arg,
2cb3ea55 137);
138
139around options => sub {
140 my $orig = shift;
25868dc9 141 my $self = shift;
142 my $options = parse_list_arg( @_ );
143
2cb3ea55 144 push @{ $self->$orig }, @$options;
43b9dc7a 145
2cb3ea55 146 return wantarray ? @{ $self->$orig } : $self->$orig;
147};
43b9dc7a 148
149=head2 table
150
151Get or set the index's table object.
152
153 my $table = $index->table;
154
155=cut
156
45287c81 157has table => ( is => 'rw', isa => schema_obj('Table') );
43b9dc7a 158
45287c81 159around table => \&ex2err;
3c5de62a 160
161=head2 type
162
163Get or set the index's type.
164
165 my $type = $index->type('unique');
166
d06db857 167Get or set the index's type.
19ad0cee 168
169Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
170and SPATIAL. The latter two might be MySQL-specific. While both lowercase
171and uppercase types are acceptable input, this method returns the type in
172uppercase.
173
3c5de62a 174=cut
175
45287c81 176has 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);
3c5de62a 185
45287c81 186around type => \&ex2err;
abf315bb 187
188=head2 equals
189
190Determines if this index is the same as another
191
192 my $isIdentical = $index1->equals( $index2 );
193
194=cut
195
2cb3ea55 196around equals => sub {
197 my $orig = shift;
abf315bb 198 my $self = shift;
199 my $other = shift;
200 my $case_insensitive = shift;
d990d84b 201 my $ignore_index_names = shift;
ea93df61 202
2cb3ea55 203 return 0 unless $self->$orig($other);
da5a1bae 204
d990d84b 205 unless ($ignore_index_names) {
da5a1bae 206 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
207 (!$other->name && ($self->name eq $self->fields->[0]))) {
d990d84b 208 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
da5a1bae 209 }
d990d84b 210 }
82f6b50e 211 #return 0 unless $self->is_valid eq $other->is_valid;
abf315bb 212 return 0 unless $self->type eq $other->type;
ea93df61 213
c243ec2b 214 # Check fields, regardless of order
ea93df61 215 my %otherFields = (); # create a hash of the other fields
c243ec2b 216 foreach my $otherField ($other->fields) {
ea93df61 217 $otherField = uc($otherField) if $case_insensitive;
218 $otherFields{$otherField} = 1;
c243ec2b 219 }
220 foreach my $selfField ($self->fields) { # check for self fields in hash
ea93df61 221 $selfField = uc($selfField) if $case_insensitive;
222 return 0 unless $otherFields{$selfField};
223 delete $otherFields{$selfField};
c243ec2b 224 }
225 # Check all other fields were accounted for
226 return 0 unless keys %otherFields == 0;
227
4598b71c 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);
abf315bb 230 return 1;
2cb3ea55 231};
3c5de62a 232
25868dc9 233sub DESTROY {
3c5de62a 234 my $self = shift;
25868dc9 235 undef $self->{'table'}; # destroy cyclical reference
3c5de62a 236}
237
45287c81 238# Must come after all 'has' declarations
239around new => \&ex2err;
240
3c5de62a 2411;
242
3c5de62a 243=pod
244
245=head1 AUTHOR
246
c3b0b535 247Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
3c5de62a 248
249=cut