blib/lib/DBIO/Generate/Relationships.pm

filter: Statementstmt 87.4% 146 / 167 Branchbran 46.4% 26 / 56 Conditioncond 61.1% 22 / 36 MC/DCmcdc 31.5% 6 / 19 Subroutinesub 85.1% 23 / 27 Podpod 100% 2 / 2 totaltotal 73.2% 225 / 307 CC 48 SCAR 24.3
CC
48
Cov
75%
_apply_rel_name_map
26.1
generate_code
19.4
_generate_m2ms
15.5
CRAP 11.3
« blib/lib/DBIO/Test/Conformance.pm ↑ Summary blib/lib/DBIO/Storage.pm »
1 package DBIO::Generate::Relationships;
2 # ABSTRACT: Relationship inference for DBIO::Generate
3  
4 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 14 our $VERSION = '0.900007';
8  
9 14 ▶use base qw/Class::Accessor::Grouped/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Carp::Clan qw/^DBIO/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use List::Util qw/all any first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use Lingua::EN::Inflect::Phrase ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 ▶use Lingua::EN::Tagger ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶use String::ToIdentifier::EN ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 14 ▶use String::ToIdentifier::EN::Unicode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 14 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 14 __PACKAGE__->mk_group_accessors(simple => qw/
22 inflect_plural
23 inflect_singular
24 rel_name_map
25 rel_collision_map
26 allow_extra_m2m_cols
27 quiet
28 _relnames_seen
29 /);
30  
31  
32 15 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
33 15 my ($class, %args) = @_;
34 15 my $self = bless {}, $class;
35 15 ▶ $self->allow_extra_m2m_cols($args{allow_extra_m2m_cols} // 0);
Condition100%2/2
$args{"allow_extra_m2m_cols"} // 0
l!l
114
MC/DC100%1/1
$args{"allow_extra_m2m_cols"} // 0
$args{"allow_extra_m2m_cols"}
Truth table100%2/2
A
A= $args{"allow_extra_m2m_cols"}
Aresult
11
00
36 15 ▶ $self->quiet($args{quiet} // 0);
Condition100%2/2
$args{"quiet"} // 0
l!l
105
MC/DC100%1/1
$args{"quiet"} // 0
$args{"quiet"}
Truth table100%2/2
A
A= $args{"quiet"}
Aresult
11
00
37 15 $self->rel_name_map($args{rel_name_map});
38 15 ▶ $self->rel_collision_map($args{rel_collision_map} // {});
Condition50%1/2
$args{"rel_collision_map"} // {}
l!l
015
MC/DC0%0/1
$args{"rel_collision_map"} // {}
$args{"rel_collision_map"}
Truth table50%1/2
A
A= $args{"rel_collision_map"}
Aresult
11
00
39 15 $self->inflect_plural($args{inflect_plural});
40 15 $self->inflect_singular($args{inflect_singular});
41 15 return $self;
42 }
43  
44  
45 15 ▶sub generate_code {
Subroutine generate_code: called CC=17 SCAR=29.7
Pod documented
46 15 my ($self, $tables, $class_for, $pk_for, $cols_for) = @_;
47  
48 15 $self->_relnames_seen({});
49  
50 15 my @tables = @$tables;
51 15 my %all_code;
52  
53 15 ▶ while (my ($local_moniker, $rels, $uniqs) = @{ shift @tables || [] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = @{shift @tables or [];}) 25 15 40
Condition100%2/2
shift @tables || []
l!l
2515
MC/DC100%1/1
shift @tables || []
shift @tables
Truth table100%2/2
A
A= shift @tables
Aresult
11
00
54 25 ▶ my $local_class = $class_for->{$local_moniker} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $local_class = $class_for->{$local_moniker} or next 25 0 25
55  
56 25 my %counters;
57 25 for my $rel (@$rels) {
58 18 ▶ next unless $rel->{remote_moniker};
Branch: 1 branch
BranchTrueFalseTotal
unless $rel->{"remote_moniker"} 0 18 18
59 18 $counters{ $rel->{remote_moniker} }++;
60 }
61  
62 25 for my $rel (@$rels) {
63 18 ▶ my $remote_moniker = $rel->{remote_moniker} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $remote_moniker = $rel->{"remote_moniker"} or next 18 0 18
64 18 ▶ my $remote_class = $class_for->{$remote_moniker} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $remote_class = $class_for->{$remote_moniker} or next 18 0 18
65  
66 18 my $remote_cols = $rel->{remote_columns};
67 18 ▶ $remote_cols = $pk_for->{$remote_moniker} // []
Branch: 1 branch
BranchTrueFalseTotal
unless $remote_cols and @$remote_cols 0 18 18
Condition20%1/5
$remote_cols and @$remote_cols
!ll&&!rl&&r
0018
$pk_for->{$remote_moniker} // []
l!l
00
MC/DC0%0/2
$remote_cols and @$remote_cols
$remote_cols @$remote_cols
MC/DC0%0/1
$pk_for->{$remote_moniker} // []
$pk_for->{$remote_moniker}
Truth table33%1/3
A and B
A= $remote_colsB= @$remote_cols
ABresult
0X0
100
111
Truth table0%0/2
A
A= $pk_for->{$remote_moniker}
Aresult
11
00
68 unless $remote_cols && @$remote_cols;
69  
70 18 my $local_cols = $rel->{local_columns};
71  
72 18 ▶ if (@$local_cols != @$remote_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$local_cols != @$remote_cols) 0 18 18
73 0 croak "Column count mismatch: $local_moniker (@$local_cols) "
74 . "$remote_moniker (@$remote_cols)";
75 }
76  
77 18 my %cond;
78 18 @cond{@$remote_cols} = @$local_cols;
79  
80 18 my ($local_relname, $remote_relname, $remote_method) =
81 $self->_relnames_and_method(
82 $local_moniker, $remote_moniker, \%cond, $uniqs, \%counters
83 );
84  
85 18 my $local_method = 'belongs_to';
86  
87 18 ($local_relname) = $self->_apply_rel_name_map($local_relname, $local_method, $local_moniker, $local_cols, $remote_moniker, $remote_cols);
88 18 ($remote_relname) = $self->_apply_rel_name_map($remote_relname, $remote_method, $remote_moniker, $remote_cols, $local_moniker, $local_cols);
89  
90 18 $local_relname = $self->_resolve_collision($local_moniker, $local_relname);
91 18 $remote_relname = $self->_resolve_collision($remote_moniker, $remote_relname);
92  
93 18 ▶ push @{ $all_code{$local_moniker} }, {
Condition50%1/2
$rel->{"attrs"} // {}
l!l
180
MC/DC0%0/1
$rel->{"attrs"} // {}
$rel->{"attrs"}
Truth table50%1/2
A
A= $rel->{"attrs"}
Aresult
11
00
94 method => $local_method,
95 args => [
96 $local_relname,
97 $remote_class,
98 \%cond,
99 $rel->{attrs} // {},
100 18 ],
101 extra => {
102 local_moniker => $local_moniker,
103 remote_moniker => $remote_moniker,
104 },
105 };
106  
107 18 my %rev_cond = reverse %cond;
108 18 for (keys %rev_cond) {
109 18 $rev_cond{"foreign.$_"} = "self." . $rev_cond{$_};
110 18 delete $rev_cond{$_};
111 }
112  
113 18 push @{ $all_code{$remote_moniker} }, {
114 method => $remote_method,
115 args => [
116 $remote_relname,
117 $local_class,
118 \%rev_cond,
119 {},
120 ],
121 extra => {
122 local_moniker => $remote_moniker,
123 remote_moniker => $local_moniker,
124 },
125 };
126 }
127 }
128  
129 15 ▶ $self->_generate_m2ms(\%all_code, $class_for, $pk_for // {}, $cols_for // {});
Condition75%3/4
$pk_for // {}
l!l
150
$cols_for // {}
l!l
132
MC/DC0%0/1
$pk_for // {}
$pk_for
MC/DC100%1/1
$cols_for // {}
$cols_for
Truth table50%1/2
A
A= $pk_for
Aresult
11
00
Truth table100%2/2
A
A= $cols_for
Aresult
11
00
130  
131 15 return \%all_code;
132 }
133  
134 18 ▶sub _relnames_and_method {
Subroutine _relnames_and_method: called CC=3 SCAR=15.3
135 18 my ($self, $local_moniker, $remote_moniker, $cond, $uniqs, $counters) = @_;
136  
137 18 my $local_relname = $self->_local_relname($local_moniker, $cond, $remote_moniker);
138 18 my $remote_relname = $self->_inflect_plural(lc $local_moniker);
139  
140 18 my @local_col_names = values %$cond;
141 0 ▶ my $is_unique = any {
Subroutine __ANON__: not called CC=2 SCAR=17.9
142 0 my ($name, $cols) = @$_;
143 @$cols == @local_col_names
144 0 ▶ && all { my $c = $_; any { $_ eq $c } @local_col_names } @$cols
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@$cols == @local_col_names and &all(sub { my $c = $_; &any(sub { $_ eq $c; } , @local_col_names); } , @$cols) 0 0 0
145 18 } @$uniqs;
146  
147 18 ▶ my $remote_method = $is_unique ? 'might_have' : 'has_many';
Branch: 1 branch
BranchTrueFalseTotal
$is_unique ? : 0 18 18
148  
149 18 ▶ if ($counters->{$remote_moniker} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($counters->{$remote_moniker} > 1) 0 18 18
150 0 my $col_part = lc join '_', @local_col_names;
151 0 $col_part =~ s/_id$//;
152 0 $local_relname = $col_part . '_' . lc $remote_moniker;
153 0 $remote_relname = $self->_inflect_plural(lc $local_moniker) . '_via_' . $col_part;
154 }
155  
156 18 return ($local_relname, $remote_relname, $remote_method);
157 }
158  
159 18 ▶sub _local_relname {
Subroutine _local_relname: called CC=2 SCAR=7.0
160 18 my ($self, $local_moniker, $cond, $remote_moniker) = @_;
161  
162 18 my @local_cols = values %$cond;
163 18 my $col = $local_cols[0];
164 18 (my $stripped = $col) =~ s/_id$//i;
165 18 ▶ return lc($stripped eq lc($local_moniker) ? $remote_moniker : $stripped);
Branch: 1 branch
BranchTrueFalseTotal
$stripped eq lc $local_moniker ? : 0 18 18
166 }
167  
168 # Link-table recognition mirrors DBIx::Class::Schema::Loader::RelBuilder
169 # (_generate_m2ms): exactly two belongs_to, primary key == union of their
170 # local columns, and no further column unless allow_extra_m2m_cols (karr k215).
171 15 ▶sub _generate_m2ms {
Subroutine _generate_m2ms: called CC=15 SCAR=27.4
172 15 my ($self, $all_code, $class_for, $pk_for, $cols_for) = @_;
173  
174 15 for my $link_moniker (sort keys %$all_code) {
175 20 my @bt = grep { $_->{method} eq 'belongs_to' } @{ $all_code->{$link_moniker} };
176 20 ▶ next unless @bt == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @bt == 2 12 8 20
177  
178 8 my ($a, $b) = @bt;
179 8 my $a_moniker = $a->{extra}{remote_moniker};
180 8 my $b_moniker = $b->{extra}{remote_moniker};
181  
182 8 ▶ next unless $class_for->{$a_moniker} && $class_for->{$b_moniker};
Branch: 1 branch
BranchTrueFalseTotal
unless $class_for->{$a_moniker} and $class_for->{$b_moniker} 0 8 8
Condition33%1/3
$class_for->{$a_moniker} and $class_for->{$b_moniker}
!ll&&!rl&&r
008
MC/DC0%0/2
$class_for->{$a_moniker} and $class_for->{$b_moniker}
$class_for->{$a_moniker} $class_for->{$b_moniker}
Truth table33%1/3
A and B
A= $class_for->{$a_moniker}B= $class_for->{$b_moniker}
ABresult
0X0
100
111
183  
184 8 my %fk_cols = map { $_ => 1 } map { values %{ $_->{args}[2] } } $a, $b;
185 8 ▶ my @pk = @{ $pk_for->{$link_moniker} // [] };
Condition50%1/2
$pk_for->{$link_moniker} // []
l!l
80
MC/DC0%0/1
$pk_for->{$link_moniker} // []
$pk_for->{$link_moniker}
Truth table50%1/2
A
A= $pk_for->{$link_moniker}
Aresult
11
00
186 8 ▶ next unless @pk && @pk == keys %fk_cols && all { $fk_cols{$_} } @pk;
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
unless @pk and @pk == keys %fk_cols and &all(sub { $fk_cols{$_}; } , @pk) 4 4 8
Condition66%4/6
@pk and @pk == keys %fk_cols and &all(sub { $fk_cols{$_}; } , @pk)
!ll&&!rl&&r
404
@pk and @pk == keys %fk_cols
!ll&&!rl&&r
044
MC/DC33%1/3
@pk and @pk == keys %fk_cols and &all(sub { $fk_cols{$_}; } , @pk)
@pk @pk == keys %fk_cols &all(sub { $fk_cols{$_}; } , @pk)
Truth table50%2/4
A and B and C
A= @pkB= @pk == keys %fk_colsC= &all(sub { $fk_cols{$_}; } , @pk)
ABCresult
0XX0
10X0
1100
1111
187  
188 4 ▶ unless ($self->allow_extra_m2m_cols) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->allow_extra_m2m_cols) 3 1 4
189 3 ▶ my $cols = $cols_for->{$link_moniker} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $cols = $cols_for->{$link_moniker} or next 2 1 3
190 2 ▶ next unless @$cols == @pk;
Branch: 1 branch
BranchTrueFalseTotal
unless @$cols == @pk 1 1 2
191 }
192  
193 # The bridge is the has_many on the first side that leads into the link
194 # table over the first belongs_to's columns -- a relationship of
195 # $a_moniker, as many_to_many requires.
196 2 my %a_link_cols = map { $_ => 1 } values %{ $a->{args}[2] };
197 2 my $link_class = $class_for->{$link_moniker};
198 my ($bridge) = grep {
199 $_->{method} eq 'has_many'
200 && $_->{args}[1] eq $link_class
201 4 ▶ && all { (my $c = $_) =~ s/^foreign\.//; $a_link_cols{$c} } keys %{ $_->{args}[2] }
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%4/6
$_->{'method'} eq 'has_many' && $_->{'args'}[1] eq $link_class && &all(sub { (my $c = $_) =~ s/^foreign\.//; $a_link_cols{$c}; } , keys %{$$_{'args'}[2];})
!ll&&!rl&&r
202
$_->{"method"} eq "has_many" and $_->{"args"}[1] eq $link_class
!ll&&!rl&&r
022
MC/DC33%1/3
$_->{'method'} eq 'has_many' && $_->{'args'}[1] eq $link_class && &all(sub { (my $c = $_) =~ s/^foreign\.//; $a_link_cols{$c}; } , keys %{$$_{'args'}[2];})
$_->{"method"} eq "has_many" $_->{"args"}[1] eq $link_class &all(sub { (my $c = $_) =~ s/^foreign\.//; $a_link_cols{$c}; } , keys %{$$_{'args'}[2];})
Truth table50%2/4
A and B && C
A= $_->{"method"} eq "has_many"B= $_->{"args"}[1] eq $link_classC= &all(sub { (my $c = $_) =~ s/^foreign\.//; $a_link_cols{$c}; } , keys %{$$_{'args'}[2];})
ABCresult
0XX0
10X0
1100
1111
202 2 } @{ $all_code->{$a_moniker} };
203 2 ▶ next unless $bridge;
Branch: 1 branch
BranchTrueFalseTotal
unless $bridge 0 2 2
204  
205 2 my $m2m_relname = $self->_inflect_plural(lc $b_moniker);
206 2 my $bridge_relname = $bridge->{args}[0];
207  
208 2 push @{ $all_code->{$a_moniker} }, {
209 method => 'many_to_many',
210 args => [
211 $m2m_relname,
212 $bridge_relname,
213 2 $b->{args}[0],
214 ],
215 extra => {
216 local_moniker => $a_moniker,
217 remote_moniker => $b_moniker,
218 },
219 };
220 }
221 }
222  
223 20 ▶sub _inflect_plural {
Subroutine _inflect_plural: called CC=3 SCAR=14.2
224 20 my ($self, $name) = @_;
225 20 ▶ if (my $map = $self->inflect_plural) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $map = $self->inflect_plural) 0 20 20
226 0 ▶ return $map->{$name} if exists $map->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $map->{$name} 0 0 0
227 }
228 20 return Lingua::EN::Inflect::Phrase::to_PL($name);
229 }
230  
231 0 ▶sub _inflect_singular {
Subroutine _inflect_singular: not called CC=3 SCAR=24.8
232 0 my ($self, $name) = @_;
233 0 ▶ if (my $map = $self->inflect_singular) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $map = $self->inflect_singular) 0 0 0
234 0 ▶ return $map->{$name} if exists $map->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $map->{$name} 0 0 0
235 }
236 0 return Lingua::EN::Inflect::Phrase::to_S($name);
237 }
238  
239 36 ▶sub _apply_rel_name_map {
Subroutine _apply_rel_name_map: called CC=6 SCAR=32.6
240 36 my ($self, $relname, $method, $local_moniker, $local_cols, $remote_moniker, $remote_cols) = @_;
241 36 ▶ my $map = $self->rel_name_map or return ($relname);
Branch: 1 branch
BranchTrueFalseTotal
my $map = $self->rel_name_map or return $relname 0 36 36
242 0 ▶ if (ref $map eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $map eq 'HASH') { } 0 0 0
elsif (ref $map eq 'CODE') { } 0 0 0
243 0 ▶ return (exists $map->{$relname} ? $map->{$relname} : $relname);
Branch: 1 branch
BranchTrueFalseTotal
exists $map->{$relname} ? : 0 0 0
244 }
245 elsif (ref $map eq 'CODE') {
246 0 my $mapped = $map->({
247 name => $relname,
248 type => $method,
249 local_moniker => $local_moniker,
250 local_columns => $local_cols,
251 remote_moniker => $remote_moniker,
252 remote_columns => $remote_cols,
253 });
254 0 ▶ return (defined $mapped ? $mapped : $relname);
Branch: 1 branch
BranchTrueFalseTotal
defined $mapped ? : 0 0 0
255 }
256 0 return ($relname);
257 }
258  
259 36 ▶sub _resolve_collision {
Subroutine _resolve_collision: called CC=2 SCAR=7.2
260 36 my ($self, $moniker, $relname) = @_;
261 36 my $seen = $self->_relnames_seen;
262 36 my $key = "$moniker.$relname";
263 36 ▶ if ($seen->{$key}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen->{$key}++) 0 36 36
264 0 return $relname . '_' . $seen->{$key};
265 }
266 36 return $relname;
267 }
268  
269 14 1;
270  
271 __END__
« blib/lib/DBIO/Test/Conformance.pm ↑ Summary blib/lib/DBIO/Storage.pm »