Mooify SQLT::Schema::Index
[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;
30use SQL::Translator::Utils 'parse_list_arg';
2cb3ea55 31use List::MoreUtils qw(uniq);
3c5de62a 32
2cb3ea55 33with qw(
34 SQL::Translator::Schema::Role::Extra
35 SQL::Translator::Schema::Role::Error
36 SQL::Translator::Schema::Role::Compare
37);
b6a880d1 38
0c04c5a2 39our ( $TABLE_COUNT, $VIEW_COUNT );
da06ac74 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
57=cut
58
2cb3ea55 59sub BUILD {
60 my ($self) = @_;
61 $self->$_(scalar $self->$_)
62 foreach qw(fields options);
63}
3c5de62a 64
65=head2 fields
66
b54199ae 67Gets and set the fields the index is on. Accepts a string, list or
25868dc9 68arrayref; returns an array or array reference. Will unique the field
69names and keep them in order by the first occurrence of a field name.
3c5de62a 70
b54199ae 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 ] );
25868dc9 76
b54199ae 77 my @fields = $index->fields;
3c5de62a 78
79=cut
80
2cb3ea55 81has fields => (
82 is => 'rw',
83 default => sub { [] },
84 coerce => sub { [uniq @{parse_list_arg($_[0])}] },
85);
86
87around fields => sub {
88 my $orig = shift;
3c5de62a 89 my $self = shift;
25868dc9 90 my $fields = parse_list_arg( @_ );
2cb3ea55 91 $self->$orig($fields) if @$fields;
3c5de62a 92
2cb3ea55 93 return wantarray ? @{ $self->$orig } : $self->$orig;
94};
3c5de62a 95
b54199ae 96sub is_valid {
97
98=pod
99
100=head2 is_valid
101
102Determine 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
3c5de62a 121=head2 name
122
123Get or set the index's name.
124
125 my $name = $index->name('foo');
126
127=cut
128
2cb3ea55 129has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } );
25868dc9 130
131=head2 options
132
133Get or set the index's options (e.g., "using" or "where" for PG). Returns
134an array or array reference.
135
136 my @options = $index->options;
137
138=cut
139
2cb3ea55 140has options => (
141 is => 'rw',
142 default => sub { [] },
143 coerce => sub { parse_list_arg($_[0]) },
144);
145
146around options => sub {
147 my $orig = shift;
25868dc9 148 my $self = shift;
149 my $options = parse_list_arg( @_ );
150
2cb3ea55 151 push @{ $self->$orig }, @$options;
43b9dc7a 152
2cb3ea55 153 return wantarray ? @{ $self->$orig } : $self->$orig;
154};
43b9dc7a 155
156=head2 table
157
158Get or set the index's table object.
159
160 my $table = $index->table;
161
162=cut
163
2cb3ea55 164has table => ( is => 'rw' );
165
166around table => sub {
167 my $orig = shift;
43b9dc7a 168 my $self = shift;
2cb3ea55 169 if ( my $arg = $_[0] ) {
43b9dc7a 170 return $self->error('Not a table object') unless
171 UNIVERSAL::isa( $arg, 'SQL::Translator::Schema::Table' );
43b9dc7a 172 }
173
2cb3ea55 174 return $self->$orig(@_);
175};
3c5de62a 176
177=head2 type
178
179Get or set the index's type.
180
181 my $type = $index->type('unique');
182
d06db857 183Get or set the index's type.
19ad0cee 184
185Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
186and SPATIAL. The latter two might be MySQL-specific. While both lowercase
187and uppercase types are acceptable input, this method returns the type in
188uppercase.
189
3c5de62a 190=cut
191
2cb3ea55 192has type => ( is => 'rw', default => sub { 'NORMAL' } );
193
194around type => sub {
195 my ( $orig, $self) = (shift, shift);
3c5de62a 196
2cb3ea55 197 if ( my $type = $_[0] ) {
198 my $type = uc $type;
ea93df61 199 return $self->error("Invalid index type: $type")
c8efc003 200 unless $VALID_INDEX_TYPE{ $type };
3c5de62a 201 }
202
2cb3ea55 203 return $self->$orig(@_);
204};
abf315bb 205
206=head2 equals
207
208Determines if this index is the same as another
209
210 my $isIdentical = $index1->equals( $index2 );
211
212=cut
213
2cb3ea55 214around equals => sub {
215 my $orig = shift;
abf315bb 216 my $self = shift;
217 my $other = shift;
218 my $case_insensitive = shift;
d990d84b 219 my $ignore_index_names = shift;
ea93df61 220
2cb3ea55 221 return 0 unless $self->$orig($other);
da5a1bae 222
d990d84b 223 unless ($ignore_index_names) {
da5a1bae 224 unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
225 (!$other->name && ($self->name eq $self->fields->[0]))) {
d990d84b 226 return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name;
da5a1bae 227 }
d990d84b 228 }
82f6b50e 229 #return 0 unless $self->is_valid eq $other->is_valid;
abf315bb 230 return 0 unless $self->type eq $other->type;
ea93df61 231
c243ec2b 232 # Check fields, regardless of order
ea93df61 233 my %otherFields = (); # create a hash of the other fields
c243ec2b 234 foreach my $otherField ($other->fields) {
ea93df61 235 $otherField = uc($otherField) if $case_insensitive;
236 $otherFields{$otherField} = 1;
c243ec2b 237 }
238 foreach my $selfField ($self->fields) { # check for self fields in hash
ea93df61 239 $selfField = uc($selfField) if $case_insensitive;
240 return 0 unless $otherFields{$selfField};
241 delete $otherFields{$selfField};
c243ec2b 242 }
243 # Check all other fields were accounted for
244 return 0 unless keys %otherFields == 0;
245
4598b71c 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);
abf315bb 248 return 1;
2cb3ea55 249};
3c5de62a 250
25868dc9 251sub DESTROY {
3c5de62a 252 my $self = shift;
25868dc9 253 undef $self->{'table'}; # destroy cyclical reference
3c5de62a 254}
255
2561;
257
3c5de62a 258=pod
259
260=head1 AUTHOR
261
c3b0b535 262Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
3c5de62a 263
264=cut