| 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
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 15 | ▶ | $self->quiet($args{quiet} // 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{"quiet"} // 0
MC/DC100%1/1
$args{"quiet"} // 0
$args{"quiet"}
Truth table100%2/2
A
A= $args{"quiet"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // {}
MC/DC0%0/1
$args{"rel_collision_map"} // {}
$args{"rel_collision_map"}
Truth table50%1/2
A
A= $args{"rel_collision_map"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
shift @tables || []
MC/DC100%1/1
shift @tables || []
shift @tables
Truth table100%2/2
A
A= shift @tables
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 25 | ▶ | my $local_class = $class_for->{$local_moniker} or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 25 | my %counters; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 25 | for my $rel (@$rels) { | |||||||||||||||||||||||||||||||||||||||||
| 58 | 18 | ▶ | next unless $rel->{remote_moniker}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 18 | ▶ | my $remote_class = $class_for->{$remote_moniker} or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 18 | my $remote_cols = $rel->{remote_columns}; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 18 | ▶ | $remote_cols = $pk_for->{$remote_moniker} // [] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
$remote_cols and @$remote_cols
$pk_for->{$remote_moniker} // []
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
Truth table0%0/2
A
A= $pk_for->{$remote_moniker}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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"} // {}
MC/DC0%0/1
$rel->{"attrs"} // {}
$rel->{"attrs"}
Truth table50%1/2
A
A= $rel->{"attrs"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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 // {}
$cols_for // {}
MC/DC0%0/1
$pk_for // {}
$pk_for
MC/DC100%1/1
$cols_for // {}
$cols_for
Truth table50%1/2
A
A= $pk_for
Truth table100%2/2
A
A= $cols_for
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 18 | } @$uniqs; | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 18 | ▶ | my $remote_method = $is_unique ? 'might_have' : 'has_many'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 18 | ▶ | if ($counters->{$remote_moniker} > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$class_for->{$a_moniker} and $class_for->{$b_moniker}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 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} // []
MC/DC0%0/1
$pk_for->{$link_moniker} // []
$pk_for->{$link_moniker}
Truth table50%1/2
A
A= $pk_for->{$link_moniker}
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 8 | ▶ | next unless @pk && @pk == keys %fk_cols && all { $fk_cols{$_} } @pk; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
Condition66%4/6
@pk and @pk == keys %fk_cols and &all(sub {
$fk_cols{$_};
}
, @pk)
@pk and @pk == keys %fk_cols
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 4 | ▶ | unless ($self->allow_extra_m2m_cols) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 3 | ▶ | my $cols = $cols_for->{$link_moniker} or next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 190 | 2 | ▶ | next unless @$cols == @pk; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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];})
$_->{"method"} eq "has_many" and $_->{"args"}[1] eq $link_class
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];})
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 2 | } @{ $all_code->{$a_moniker} }; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 2 | ▶ | next unless $bridge; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | return $map->{$name} if exists $map->{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | return $map->{$name} if exists $map->{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | if (ref $map eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | return (exists $map->{$relname} ? $map->{$relname} : $relname); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | return $relname . '_' . $seen->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 36 | return $relname; | |||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | 14 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | __END__ | ||||||||||||||||||||||||||||||||||||||||||