| 1 | package DBIx::Class::Relationship::ManyToMany::Async; | ||||||||||||
| 2 | |||||||||||||
| 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 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 2 | ▶ | use Future; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use Exporter 'import'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 2 | our $VERSION = '0.01'; | |||||||||||
| 13 | 2 | our @EXPORT = qw(many_to_many_async); | |||||||||||
| 14 | |||||||||||||
| 15 | |||||||||||||
| 16 | sub many_to_many_async { | ||||||||||||
| 17 | 4 | ▶ | my ($class, $meth, $rel, $f_rel) = @_; | ||||||||||
|
Subroutine
many_to_many_async: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 18 | |||||||||||||
| 19 | 4 | ▶ | $class->throw_exception("missing relation in many-to-many") unless $rel; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | 4 | ▶ | $class->throw_exception("missing foreign relation") unless $f_rel; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 21 | |||||||||||||
| 22 | { | ||||||||||||
| 23 | 2 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | 2 | ▶ | no warnings 'redefine'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 25 | |||||||||||||
| 26 | 4 | my $add_meth = "add_to_${meth}"; | |||||||||||
| 27 | 4 | my $remove_meth = "remove_from_${meth}"; | |||||||||||
| 28 | 4 | my $set_meth = "set_${meth}"; | |||||||||||
| 29 | |||||||||||||
| 30 | # ── $meth — list accessor (returns Future → arrayref of targets) ── | ||||||||||||
| 31 | # Single DB query: pivot rows with target prefetched via JOIN. | ||||||||||||
| 32 | # Each $_->$f_rel returns a Future resolved from memory (no extra query). | ||||||||||||
| 33 | { | ||||||||||||
| 34 | 4 | my $meth_name = join '::', $class, $meth; | |||||||||||
| 35 | *$meth_name = sub { | ||||||||||||
| 36 | 7 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 37 | return $self->search_related($rel, {}, { prefetch => $f_rel })->all | ||||||||||||
| 38 | ->then(sub { | ||||||||||||
| 39 | 7 | ▶ | my $pivot_rows = shift; | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 40 | 7 | my @futures = map { $_->$f_rel } @$pivot_rows; | |||||||||||
| 41 | return Future->needs_all(@futures) | ||||||||||||
| 42 | 6 | ▶ | ->then(sub { [@_] }) | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 43 | 7 | ▶ | if @futures; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 44 | 1 | return Future->done([]); | |||||||||||
| 45 | 7 | }); | |||||||||||
| 46 | 4 | }; | |||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | # ── add_to_${meth} — link a target object (returns a Future) ── | ||||||||||||
| 50 | { | ||||||||||||
| 51 | 4 | my $add_meth_name = join '::', $class, $add_meth; | |||||||||||
| 52 | *$add_meth_name = sub { | ||||||||||||
| 53 | 7 | ▶ | my ($self, $obj, $link_vals) = @_; | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||
| 54 | 7 | ▶ | $obj | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 55 | or $self->throw_exception("${add_meth} needs an object"); | ||||||||||||
| 56 | |||||||||||||
| 57 | 7 | my $rel_info = $self->result_source->relationship_info($rel); | |||||||||||
| 58 | 7 | my %cond = %{ $rel_info->{cond} }; | |||||||||||
| 59 | 7 | my ($fk_col, $self_col) = %cond; | |||||||||||
| 60 | 7 | $fk_col =~ s/^foreign\.//; | |||||||||||
| 61 | 7 | $self_col =~ s/^self\.//; | |||||||||||
| 62 | |||||||||||||
| 63 | my $link = $self->search_related($rel)->new_result({ | ||||||||||||
| 64 | $fk_col => $self->get_column($self_col), | ||||||||||||
| 65 | 7 | ▶ | %{ $link_vals // {} }, | ||||||||||
|
Condition50%1/2
$link_vals // {}
MC/DC0%0/1
$link_vals // {}
$link_vals
Truth table50%1/2
A
A= $link_vals
| |||||||||||||
| 66 | }); | ||||||||||||
| 67 | 7 | $link->set_from_related($f_rel, $obj); | |||||||||||
| 68 | |||||||||||||
| 69 | 7 | ▶ | return $link->insert->then(sub { return $obj }); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 70 | 4 | }; | |||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | # ── remove_from_${meth} — unlink a target object (returns a Future) ── | ||||||||||||
| 74 | { | ||||||||||||
| 75 | 4 | my $remove_meth_name = join '::', $class, $remove_meth; | |||||||||||
| 76 | *$remove_meth_name = sub { | ||||||||||||
| 77 | 1 | ▶ | my ($self, $obj) = @_; | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||
| 78 | 1 | ▶ | $self->throw_exception("${remove_meth} needs an object") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | unless ref $obj; | ||||||||||||
| 80 | |||||||||||||
| 81 | 1 | my $rel_source = $self->search_related($rel)->result_source; | |||||||||||
| 82 | 1 | my %cond = %{ $rel_source->relationship_info($f_rel)->{cond} }; | |||||||||||
| 83 | 1 | my ($fk_col) = values %cond; | |||||||||||
| 84 | 1 | $fk_col =~ s/^self\.//; | |||||||||||
| 85 | |||||||||||||
| 86 | 1 | return $self->search_related($rel, { | |||||||||||
| 87 | $fk_col => $obj->get_column('id') | ||||||||||||
| 88 | })->delete; | ||||||||||||
| 89 | 4 | }; | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | # ── set_${meth} — replace all links (returns a Future) ── | ||||||||||||
| 93 | { | ||||||||||||
| 94 | 4 | my $set_meth_name = join '::', $class, $set_meth; | |||||||||||
| 95 | *$set_meth_name = sub { | ||||||||||||
| 96 | 3 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 97 | 3 | ▶ | my @to_set = (ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : @_); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | |||||||||||||
| 99 | return $self->search_related($rel, {})->delete->then(sub { | ||||||||||||
| 100 | 3 | ▶ | my @futures = map { $self->$add_meth($_) } @to_set; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 101 | 3 | return Future->needs_all(@futures); | |||||||||||
| 102 | 3 | }); | |||||||||||
| 103 | 4 | }; | |||||||||||
| 104 | } | ||||||||||||
| 105 | } | ||||||||||||
| 106 | } | ||||||||||||
| 107 | |||||||||||||
| 108 | 2 | 1; | |||||||||||
| 109 | |||||||||||||
| 110 | __END__ | ||||||||||||