File Coverage

blib/lib/SQL/Translator/Schema/Index.pm
Criterion Covered Total %
statement 27 27 100.0
branch 5 6 83.3
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 41 42 97.6


line stmt bran cond sub pod time code
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 is the index object.
21              
22             Primary and unique keys are table constraints, not indices.
23              
24             =head1 METHODS
25              
26             =cut
27              
28 76     76   665 use Moo;
  76         190  
  76         639  
29 76     76   39086 use SQL::Translator::Schema::Constants;
  76         251  
  76         7906  
30 76     76   41792 use SQL::Translator::Schema::IndexField;
  76         314  
  76         3832  
31 76     76   580 use SQL::Translator::Utils qw(ex2err throw parse_list_arg);
  76         156  
  76         7838  
32 76     76   571 use SQL::Translator::Role::ListAttr;
  76         184  
  76         970  
33 76     76   5736 use SQL::Translator::Types qw(schema_obj enum);
  76         161  
  76         6678  
34 76     76   541 use Sub::Quote qw(quote_sub);
  76         152  
  76         78336  
35              
36             extends 'SQL::Translator::Schema::Object';
37              
38             our $VERSION = '1.66';
39              
40             my %VALID_INDEX_TYPE = (
41             UNIQUE => 1,
42             NORMAL => 1,
43             FULLTEXT => 1, # MySQL only (?)
44             FULL_TEXT => 1, # MySQL only (?)
45             SPATIAL => 1, # MySQL only (?)
46             );
47              
48             =head2 new
49              
50             Object constructor.
51              
52             my $schema = SQL::Translator::Schema::Index->new;
53              
54             =head2 fields
55              
56             Gets and set the fields the index is on. Accepts a string, list or
57             arrayref; returns an array or array reference. Will unique the field
58             names and keep them in order by the first occurrence of a field name.
59              
60             $index->fields('id');
61             $index->fields('id', 'name');
62             $index->fields( 'id, name' );
63             $index->fields( [ 'id', 'name' ] );
64             $index->fields( qw[ id name ] );
65             $index->fields(id => { name => 'name', order_by => 'ASC NULLS LAST' });
66              
67             my @fields = $index->fields;
68              
69             =cut
70              
71             with ListAttr fields => (
72             coerce => sub {
73             my %seen;
74             return [
75             grep !$seen{ $_->name }++,
76             map SQL::Translator::Schema::IndexField->new($_),
77             @{ parse_list_arg($_[0]) }
78             ];
79             }
80             );
81              
82             sub is_valid {
83              
84             =pod
85              
86             =head2 is_valid
87              
88             Determine whether the index is valid or not.
89              
90             my $ok = $index->is_valid;
91              
92             =cut
93              
94 20     20 1 986 my $self = shift;
95 20 50       549 my $table = $self->table or return $self->error('No table');
96 20 100       761 my @fields = $self->fields or return $self->error('No fields');
97              
98 19         54 for my $field (@fields) {
99 23 100       228 return $self->error("Field '$field' does not exist in table '", $table->name, "'")
100             unless $table->get_field($field);
101             }
102              
103 17         371 return 1;
104             }
105              
106             =head2 name
107              
108             Get or set the index's name.
109              
110             my $name = $index->name('foo');
111              
112             =cut
113              
114             has name => (
115             is => 'rw',
116             coerce => quote_sub(q{ defined $_[0] ? $_[0] : '' }),
117             default => quote_sub(q{ '' }),
118             );
119              
120             =head2 options
121              
122             Get or set the index's options (e.g., "using" or "where" for PG). Returns
123             an array or array reference.
124              
125             my @options = $index->options;
126              
127             =cut
128              
129             with ListAttr options => ();
130              
131             =head2 table
132              
133             Get or set the index's table object.
134              
135             my $table = $index->table;
136              
137             =cut
138              
139             has table => (is => 'rw', isa => schema_obj('Table'), weak_ref => 1);
140              
141             around table => \&ex2err;
142              
143             =head2 type
144              
145             Get or set the index's type.
146              
147             my $type = $index->type('unique');
148              
149             Get or set the index's type.
150              
151             Currently there are only four acceptable types: UNIQUE, NORMAL, FULL_TEXT,
152             and SPATIAL. The latter two might be MySQL-specific. While both lowercase
153             and uppercase types are acceptable input, this method returns the type in
154             uppercase.
155              
156             =cut
157              
158             has type => (
159             is => 'rw',
160             coerce => quote_sub(q{ uc $_[0] }),
161             default => quote_sub(q{ 'NORMAL' }),
162             isa => enum(
163             [ keys %VALID_INDEX_TYPE ],
164             {
165             msg => "Invalid index type: %s",
166             allow_false => 1,
167             }
168             ),
169             );
170              
171             around type => \&ex2err;
172              
173             =head2 equals
174              
175             Determines if this index is the same as another
176              
177             my $isIdentical = $index1->equals( $index2 );
178              
179             =cut
180              
181             around equals => sub {
182             my $orig = shift;
183             my $self = shift;
184             my $other = shift;
185             my $case_insensitive = shift;
186             my $ignore_index_names = shift;
187              
188             return 0 unless $self->$orig($other);
189              
190             unless ($ignore_index_names) {
191             unless ((!$self->name && ($other->name eq $other->fields->[0]->name))
192             || (!$other->name && ($self->name eq $self->fields->[0]->name))) {
193             return 0
194             unless $case_insensitive
195             ? uc($self->name) eq uc($other->name)
196             : $self->name eq $other->name;
197             }
198             }
199              
200             #return 0 unless $self->is_valid eq $other->is_valid;
201             return 0 unless $self->type eq $other->type;
202              
203             # Check fields, regardless of order
204             my $get_name = sub { return $case_insensitive ? uc(shift->name) : shift->name; };
205             my @otherFields = sort { $a->{key} cmp $b->{key} }
206             map +{ item => $_, key => $get_name->($_) }, $other->fields;
207             my @selfFields = sort { $a->{key} cmp $b->{key} }
208             map +{ item => $_, key => $get_name->($_) }, $self->fields;
209             return 0 unless @otherFields == @selfFields;
210             for my $idx (0 .. $#selfFields) {
211             return 0 unless $selfFields[$idx]{key} eq $otherFields[$idx]{key};
212             return 0
213             unless $self->_compare_objects(scalar $selfFields[$idx]{item}->extra, scalar $otherFields[$idx]{item}->extra);
214             }
215              
216             return 0
217             unless $self->_compare_objects(scalar $self->options, scalar $other->options);
218             return 0
219             unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
220             return 1;
221             };
222              
223             # Must come after all 'has' declarations
224             around new => \&ex2err;
225              
226             1;
227              
228             =pod
229              
230             =head1 AUTHOR
231              
232             Ken Youens-Clark Ekclark@cpan.orgE.
233              
234             =cut