blib/lib/SQL/Translator/Schema/Index.pm

filter: Statementstmt 100% 66 / 66 Branchbran 83.3% 25 / 30 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 92.5% 112 / 121 CC 21 SCAR 22.6
CC
21
Cov
93%
__ANON__
16.3
is_valid
5.0
__ANON__
2.1
CRAP 9.6
« blib/lib/SQL/Translator/Producer/SQLite.pm ↑ Summary blib/lib/SQL/Translator/Producer/TTSchema.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 76 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 76 ▶use SQL::Translator::Schema::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 76 ▶use SQL::Translator::Schema::IndexField;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 76 ▶use SQL::Translator::Utils qw(ex2err throw parse_list_arg);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 76 ▶use SQL::Translator::Role::ListAttr;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 76 ▶use SQL::Translator::Types qw(schema_obj enum);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 76 ▶use Sub::Quote qw(quote_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 76 extends 'SQL::Translator::Schema::Object';
37  
38 76 our $VERSION = '1.66';
39  
40 76 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 162 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 162 my %seen;
74 return [
75 grep !$seen{ $_->name }++,
76 map SQL::Translator::Schema::IndexField->new($_),
77 162 @{ parse_list_arg($_[0]) }
78 ];
79 }
80 76 );
81  
82 20 ▶sub is_valid {
Subroutine is_valid: called CC=5 SCAR=16.1
Pod documented
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 my $self = shift;
95 20 ▶ my $table = $self->table or return $self->error('No table');
Branch: 1 branch
BranchTrueFalseTotal
my $table = $self->table or return $self->error("No table") 20 0 20
96 20 ▶ my @fields = $self->fields or return $self->error('No fields');
Branch: 1 branch
BranchTrueFalseTotal
my(@fields) = $self->fields or return $self->error("No fields") 19 1 20
97  
98 19 for my $field (@fields) {
99 23 ▶ return $self->error("Field '$field' does not exist in table '", $table->name, "'")
Branch: 1 branch
BranchTrueFalseTotal
unless $table->get_field($field) 2 21 23
100 unless $table->get_field($field);
101 }
102  
103 17 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 76 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 76 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 76 has table => (is => 'rw', isa => schema_obj('Table'), weak_ref => 1);
140  
141 76 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 76 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 76 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 137 ▶around equals => sub {
Subroutine __ANON__: called CC=16 SCAR=27.9
182 137 my $orig = shift;
183 137 my $self = shift;
184 137 my $other = shift;
185 137 my $case_insensitive = shift;
186 137 my $ignore_index_names = shift;
187  
188 137 ▶ return 0 unless $self->$orig($other);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$orig($other) 0 137 137
189  
190 137 ▶ unless ($ignore_index_names) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ignore_index_names) 129 8 137
191 129 ▶ unless ((!$self->name && ($other->name eq $other->fields->[0]->name))
Branch: 1 branch
BranchTrueFalseTotal
unless (not $self->name and $other->name eq $other->fields->[0]->name or not $other->name and $self->name eq $self->fields->[0]->name) 128 1 129
Condition77%7/9
not $self->name and $other->name eq $other->fields->[0]->name or not $other->name and $self->name eq $self->fields->[0]->name
l!l&&r!l&&!r
10128
not $self->name and $other->name eq $other->fields->[0]->name
!ll&&!rl&&r
115131
not $other->name and $self->name eq $self->fields->[0]->name
!ll&&!rl&&r
113150
MC/DC50%2/4
not $self->name and $other->name eq $other->fields->[0]->name or not $other->name and $self->name eq $self->fields->[0]->name
not $self->name $other->name eq $other->fields->[0]->name not $other->name $self->name eq $self->fields->[0]->name
Truth table71%5/7
not(A) and B or not(C) and D
A= not $self->nameB= $other->name eq $other->fields->[0]->nameC= not $other->nameD= $self->name eq $self->fields->[0]->name
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
192 || (!$other->name && ($self->name eq $self->fields->[0]->name))) {
193 128 ▶ return 0
Branch: 2 branches
BranchTrueFalseTotal
unless $case_insensitive ? uc $self->name eq uc $other->name : $self->name eq $other->name 92 36 128
$case_insensitive ? : 0 128 128
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 45 ▶ return 0 unless $self->type eq $other->type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type eq $other->type 1 44 45
202  
203 # Check fields, regardless of order
204 44 ▶ my $get_name = sub { return $case_insensitive ? uc(shift->name) : shift->name; };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$case_insensitive ? : 0 101 101
205 44 my @otherFields = sort { $a->{key} cmp $b->{key} }
206 map +{ item => $_, key => $get_name->($_) }, $other->fields;
207 44 my @selfFields = sort { $a->{key} cmp $b->{key} }
208 map +{ item => $_, key => $get_name->($_) }, $self->fields;
209 44 ▶ return 0 unless @otherFields == @selfFields;
Branch: 1 branch
BranchTrueFalseTotal
unless @otherFields == @selfFields 9 35 44
210 35 for my $idx (0 .. $#selfFields) {
211 37 ▶ return 0 unless $selfFields[$idx]{key} eq $otherFields[$idx]{key};
Branch: 1 branch
BranchTrueFalseTotal
unless $selfFields[$idx]{"key"} eq $otherFields[$idx]{"key"} 6 31 37
212 return 0
213 31 ▶ unless $self->_compare_objects(scalar $selfFields[$idx]{item}->extra, scalar $otherFields[$idx]{item}->extra);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_compare_objects(scalar $selfFields[$idx]{"item"}->extra, scalar $otherFields[$idx]{"item"}->extra) 3 28 31
214 }
215  
216 26 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_compare_objects(scalar $self->options, scalar $other->options) 1 25 26
217 unless $self->_compare_objects(scalar $self->options, scalar $other->options);
218 25 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_compare_objects(scalar $self->extra, scalar $other->extra) 0 25 25
219 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra);
220 25 return 1;
221 76 };
222  
223 # Must come after all 'has' declarations
224 76 around new => \&ex2err;
225  
226 76 1;
227  
228 =pod
229  
230 =head1 AUTHOR
231  
232 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
233  
234 =cut
« blib/lib/SQL/Translator/Producer/SQLite.pm ↑ Summary blib/lib/SQL/Translator/Producer/TTSchema.pm »