| 1 | package DBIx::Class::Relationship::ManyToMany::Async; | ||||||||||||||||||||||||||||
| 2 | 3 | $DBIx::Class::Relationship::ManyToMany::Async::VERSION = '0.02'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: many_to_many for DBIx::Class::Async — generates Future-returning | ||||||||||||||||||||||||||||
| 4 | # accessor methods (groups, add_to_groups, remove_from_groups, set_groups). | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | our @EXPORT = qw(many_to_many_async); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 6 | ▶ | sub many_to_many_async { | ||||||||||||||||||||||||||
|
Subroutine
many_to_many_async: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 6 | my ($class, $meth, $rel, $f_rel) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 6 | ▶ | $class->throw_exception("missing relation in many-to-many") unless $rel; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 6 | ▶ | $class->throw_exception("missing foreign relation") unless $f_rel; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | { | ||||||||||||||||||||||||||||
| 22 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 6 | my $add_meth = "add_to_${meth}"; | |||||||||||||||||||||||||||
| 26 | 6 | my $remove_meth = "remove_from_${meth}"; | |||||||||||||||||||||||||||
| 27 | 6 | my $set_meth = "set_${meth}"; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # ── $meth — list accessor (returns Future → arrayref of targets) ── | ||||||||||||||||||||||||||||
| 30 | # When prefetched data is available (via search_with_prefetch), | ||||||||||||||||||||||||||||
| 31 | # uses the cached ResultSet directly — no extra DB queries. | ||||||||||||||||||||||||||||
| 32 | # Otherwise falls back to the standard async chain. | ||||||||||||||||||||||||||||
| 33 | { | ||||||||||||||||||||||||||||
| 34 | 6 | my $meth_name = join '::', $class, $meth; | |||||||||||||||||||||||||||
| 35 | 9 | ▶ | *$meth_name = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.9
| |||||||||||||||||||||||||||||
| 36 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # Prefetched path: data already loaded via search_with_prefetch. | ||||||||||||||||||||||||||||
| 39 | # Prefetched path: collapse => 1 stores nested data in | ||||||||||||||||||||||||||||
| 40 | # _relationship_data (arrayref of raw hashrefs) or | ||||||||||||||||||||||||||||
| 41 | # _prefetched (ResultSet with _entries). | ||||||||||||||||||||||||||||
| 42 | my $rs = $self->{_relationship_data}{$rel} | ||||||||||||||||||||||||||||
| 43 | 9 | ▶ | || $self->{_prefetched}{$rel}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{'_relationship_data'}{$rel} || $self->{'_prefetched'}{$rel}
MC/DC50%1/2
$self->{'_relationship_data'}{$rel} || $self->{'_prefetched'}{$rel}
$self->{'_relationship_data'}{$rel}
$self->{'_prefetched'}{$rel}
Truth table66%2/3
A || B
A= $self->{'_relationship_data'}{$rel}B= $self->{'_prefetched'}{$rel}
| |||||||||||||||||||||||||||||
| 44 | 9 | ▶ | if ($rs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | my $entries = ref $rs eq 'ARRAY' ? $rs : $rs->{_entries}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 1 | my @targets; | |||||||||||||||||||||||||||
| 47 | 1 | ▶ | if ($entries && @$entries) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$entries and @$entries
MC/DC0%0/2
$entries and @$entries
$entries
@$entries
Truth table33%1/3
A and B
A= $entriesB= @$entries
| |||||||||||||||||||||||||||||
| 48 | 1 | for my $entry (@$entries) { | |||||||||||||||||||||||||||
| 49 | my $target = ref $entry eq 'HASH' | ||||||||||||||||||||||||||||
| 50 | ? $entry->{$f_rel} | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | : eval { $entry->$f_rel }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 2 | ▶ | push @targets, $target if $target; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 1 | return Future->done(\@targets); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # Standard async path | ||||||||||||||||||||||||||||
| 59 | return $self->search_related($rel, {}, { prefetch => $f_rel })->all | ||||||||||||||||||||||||||||
| 60 | 8 | ▶ | ->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 61 | 8 | my $pivot_rows = shift; | |||||||||||||||||||||||||||
| 62 | 8 | my @futures = map { $_->$f_rel } @$pivot_rows; | |||||||||||||||||||||||||||
| 63 | return Future->needs_all(@futures) | ||||||||||||||||||||||||||||
| 64 | 7 | ▶ | ->then(sub { [@_] }) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 8 | ▶ | if @futures; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 1 | return Future->done([]); | |||||||||||||||||||||||||||
| 67 | 8 | }); | |||||||||||||||||||||||||||
| 68 | 6 | }; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # ── add_to_${meth} — link a target object (returns a Future) ── | ||||||||||||||||||||||||||||
| 72 | { | ||||||||||||||||||||||||||||
| 73 | 6 | my $add_meth_name = join '::', $class, $add_meth; | |||||||||||||||||||||||||||
| 74 | 9 | ▶ | *$add_meth_name = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 75 | 9 | my ($self, $obj, $link_vals) = @_; | |||||||||||||||||||||||||||
| 76 | 9 | ▶ | $obj | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | or $self->throw_exception("${add_meth} needs an object"); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 9 | my $rel_info = $self->result_source->relationship_info($rel); | |||||||||||||||||||||||||||
| 80 | 9 | my %cond = %{ $rel_info->{cond} }; | |||||||||||||||||||||||||||
| 81 | 9 | my ($fk_col, $self_col) = %cond; | |||||||||||||||||||||||||||
| 82 | 9 | $fk_col =~ s/^foreign\.//; | |||||||||||||||||||||||||||
| 83 | 9 | $self_col =~ s/^self\.//; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | my $link = $self->search_related($rel)->new_result({ | ||||||||||||||||||||||||||||
| 86 | $fk_col => $self->get_column($self_col), | ||||||||||||||||||||||||||||
| 87 | 9 | ▶ | %{ $link_vals // {} }, | ||||||||||||||||||||||||||
|
Condition50%1/2
$link_vals // {}
MC/DC0%0/1
$link_vals // {}
$link_vals
Truth table50%1/2
A
A= $link_vals
| |||||||||||||||||||||||||||||
| 88 | }); | ||||||||||||||||||||||||||||
| 89 | 9 | $link->set_from_related($f_rel, $obj); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 9 | ▶ | return $link->insert->then(sub { return $obj }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 6 | }; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # ── remove_from_${meth} — unlink a target object (returns a Future) ── | ||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | 6 | my $remove_meth_name = join '::', $class, $remove_meth; | |||||||||||||||||||||||||||
| 98 | 1 | ▶ | *$remove_meth_name = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 99 | 1 | my ($self, $obj) = @_; | |||||||||||||||||||||||||||
| 100 | 1 | ▶ | $self->throw_exception("${remove_meth} needs an object") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | unless ref $obj; | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | my $rel_source = $self->search_related($rel)->result_source; | |||||||||||||||||||||||||||
| 104 | 1 | my %cond = %{ $rel_source->relationship_info($f_rel)->{cond} }; | |||||||||||||||||||||||||||
| 105 | 1 | my ($fk_col) = values %cond; | |||||||||||||||||||||||||||
| 106 | 1 | $fk_col =~ s/^self\.//; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | return $self->search_related($rel, { | |||||||||||||||||||||||||||
| 109 | $fk_col => $obj->get_column('id') | ||||||||||||||||||||||||||||
| 110 | })->delete; | ||||||||||||||||||||||||||||
| 111 | 6 | }; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # ── set_${meth} — replace all links (returns a Future) ── | ||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||
| 116 | 6 | my $set_meth_name = join '::', $class, $set_meth; | |||||||||||||||||||||||||||
| 117 | 3 | ▶ | *$set_meth_name = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 118 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 119 | 3 | ▶ | my @to_set = (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 3 | ▶ | return $self->search_related($rel, {})->delete->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | 3 | my @futures = map { $self->$add_meth($_) } @to_set; | |||||||||||||||||||||||||||
| 123 | 3 | return Future->needs_all(@futures); | |||||||||||||||||||||||||||
| 124 | 3 | }); | |||||||||||||||||||||||||||
| 125 | 6 | }; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | { | ||||||||||||||||||||||||||||
| 129 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | # Store in a package variable so workers (forked processes) | ||||||||||||||||||||||||||||
| 131 | # inherit the metadata correctly without triggering reload issues. | ||||||||||||||||||||||||||||
| 132 | 6 | ▶ | ${ $class . '::_many_to_many' } //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
${$class . '::_many_to_many';} //= {}
MC/DC0%0/1
${$class . '::_many_to_many';} //= {}
${$class . '::_many_to_many';}
Truth table50%1/2
A
A= ${$class . '::_many_to_many';}
| |||||||||||||||||||||||||||||
| 133 | 6 | ${ $class . '::_many_to_many' }->{$meth} | |||||||||||||||||||||||||||
| 134 | = { rel => $rel, f_rel => $f_rel }; | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 3 | 1; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||||||||||||