| 1 | package Mojolicious::Plugin::Fondation::Model::DBIx::Async::ResultSet; | ||||||||||||||||||||||||||||||||
| 2 | 7 | $Mojolicious::Plugin::Fondation::Model::DBIx::Async::ResultSet::VERSION = '0.06'; | |||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Fondation ResultSet — with() for fluent prefetch (many_to_many + has_many) | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use base 'DBIx::Class::Async::ResultSet'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | # ─── with() — declare relationships to prefetch ─────────────────────────── | ||||||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||||||
| 11 | # $rs->with('groups') # many_to_many | ||||||||||||||||||||||||||||||||
| 12 | # $rs->with('orders') # has_many | ||||||||||||||||||||||||||||||||
| 13 | # $rs->with('groups', 'orders') # both at once | ||||||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||||||
| 15 | # Many_to_many relationships are discovered via the _many_to_many | ||||||||||||||||||||||||||||||||
| 16 | # metadata hash (populated by many_to_many_async). Has_many relationships | ||||||||||||||||||||||||||||||||
| 17 | # are discovered via DBIx::Class's has_relationship(). | ||||||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||||||
| 19 | # All metadata is stored on the ResultSet and consumed by all(), find(), | ||||||||||||||||||||||||||||||||
| 20 | # and (indirectly) search(). | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 8 | ▶ | sub with { | ||||||||||||||||||||||||||||||
|
Subroutine
with: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 23 | 8 | my ($self, @names) = @_; | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 8 | my $source = $self->result_source; | |||||||||||||||||||||||||||||||
| 26 | 8 | ▶ | my $class = $source->result_class | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 27 | or die "Cannot resolve result_class for source"; | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | 7 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 30 | 8 | my $mtm_meta = ${ $class . '::_many_to_many' }; | |||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 8 | for my $name (@names) { | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | # 1. Check many_to_many registry | ||||||||||||||||||||||||||||||||
| 35 | 9 | ▶ | if ($mtm_meta && (my $meta = $mtm_meta->{$name})) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$mtm_meta and my $meta = $mtm_meta->{$name}
MC/DC100%2/2
$mtm_meta and my $meta = $mtm_meta->{$name}
$mtm_meta
my $meta = $mtm_meta->{$name}
Truth table100%3/3
A and B
A= $mtm_metaB= my $meta = $mtm_meta->{$name}
| |||||||||||||||||||||||||||||||||
| 36 | # many_to_many → { pivot_rel => target_rel } | ||||||||||||||||||||||||||||||||
| 37 | 4 | $self->{_fondation_with}{ $meta->{rel} } = $meta->{f_rel}; | |||||||||||||||||||||||||||||||
| 38 | 4 | next; | |||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | # 2. Check DBIx::Class relationship | ||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | if ($source->has_relationship($name)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 4 | my $rel_info = $source->relationship_info($name); | |||||||||||||||||||||||||||||||
| 44 | 4 | ▶ | if (($rel_info->{attrs}{accessor} // '') eq 'multi') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$rel_info->{"attrs"}{"accessor"} // ""
MC/DC0%0/1
$rel_info->{"attrs"}{"accessor"} // ""
$rel_info->{"attrs"}{"accessor"}
Truth table50%1/2
A
A= $rel_info->{"attrs"}{"accessor"}
| |||||||||||||||||||||||||||||||||
| 45 | # has_many → direct prefetch, no pivot | ||||||||||||||||||||||||||||||||
| 46 | 3 | push @{ $self->{_fondation_with_has_many} }, $name; | |||||||||||||||||||||||||||||||
| 47 | 3 | next; | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | 1 | die "Relationship '$name' on $class is 'single' (belongs_to/might_have), " | |||||||||||||||||||||||||||||||
| 50 | . "not a has_many or many_to_many"; | ||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 1 | die "No many_to_many or has_many relationship '$name' on $class"; | |||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | 6 | return $self; | |||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | # ─── all() — execute query with prefetch if with() was called ───────────── | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 12 | ▶ | sub all { | ||||||||||||||||||||||||||||||
|
Subroutine
all: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 62 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 63 | return $self->SUPER::all | ||||||||||||||||||||||||||||||||
| 64 | 12 | ▶ | unless $self->{_fondation_with} || $self->{_fondation_with_has_many}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"_fondation_with"} or $self->{"_fondation_with_has_many"}
MC/DC100%2/2
$self->{"_fondation_with"} or $self->{"_fondation_with_has_many"}
$self->{"_fondation_with"}
$self->{"_fondation_with_has_many"}
Truth table100%3/3
A or B
A= $self->{"_fondation_with"}B= $self->{"_fondation_with_has_many"}
| |||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 4 | my $schema = $self->{_schema_instance}; | |||||||||||||||||||||||||||||||
| 67 | 4 | my $source = $self->result_source->source_name; | |||||||||||||||||||||||||||||||
| 68 | 4 | ▶ | my $cond = $self->{_attrs}{where} // {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"_attrs"}{"where"} // {}
MC/DC0%0/1
$self->{"_attrs"}{"where"} // {}
$self->{"_attrs"}{"where"}
Truth table50%1/2
A
A= $self->{"_attrs"}{"where"}
| |||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 4 | return $schema->search_with_prefetch($source, $cond, $self->_build_prefetch); | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | # ─── find() — single-row lookup with prefetch ───────────────────────────── | ||||||||||||||||||||||||||||||||
| 74 | # | ||||||||||||||||||||||||||||||||
| 75 | # DBIx::Class::Async::ResultSet::find() is a Future → single row. | ||||||||||||||||||||||||||||||||
| 76 | # When prefetch is active, we use search_with_prefetch + collapse => 1 | ||||||||||||||||||||||||||||||||
| 77 | # to get the row with nested data in a single query, then return the | ||||||||||||||||||||||||||||||||
| 78 | # first result (Future→row, matching the original find() contract). | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 8 | ▶ | sub find { | ||||||||||||||||||||||||||||||
|
Subroutine
find: called CC=7 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 81 | 8 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||
| 82 | return $self->SUPER::find(@args) | ||||||||||||||||||||||||||||||||
| 83 | 8 | ▶ | unless $self->{_fondation_with} || $self->{_fondation_with_has_many}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"_fondation_with"} or $self->{"_fondation_with_has_many"}
MC/DC100%2/2
$self->{"_fondation_with"} or $self->{"_fondation_with_has_many"}
$self->{"_fondation_with"}
$self->{"_fondation_with_has_many"}
Truth table100%3/3
A or B
A= $self->{"_fondation_with"}B= $self->{"_fondation_with_has_many"}
| |||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 2 | my $schema = $self->{_schema_instance}; | |||||||||||||||||||||||||||||||
| 86 | 2 | my $source = $self->result_source->source_name; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | # Build the condition from find() arguments (same dispatch as DBIC) | ||||||||||||||||||||||||||||||||
| 89 | 2 | my $cond; | |||||||||||||||||||||||||||||||
| 90 | 2 | ▶ | if (@args == 1 && !ref $args[0]) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
@args == 1 and not ref $args[0]
MC/DC0%0/2
@args == 1 and not ref $args[0]
@args == 1
not ref $args[0]
Truth table33%1/3
A and not(B)
A= @args == 1B= not ref $args[0]
| |||||||||||||||||||||||||||||||||
| 91 | # Single scalar → primary key lookup (qualify column for JOINs) | ||||||||||||||||||||||||||||||||
| 92 | 2 | my @pks = $self->result_source->primary_columns; | |||||||||||||||||||||||||||||||
| 93 | 2 | $cond = { 'me.' . $pks[0] => $args[0] }; | |||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||
| 95 | ▶ | elsif (@args == 1 && ref $args[0] eq 'HASH') { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
@args == 1 and ref $args[0] eq "HASH"
MC/DC0%0/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1
ref $args[0] eq "HASH"
Truth table0%0/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
| |||||||||||||||||||||||||||||||||
| 96 | 0 | $cond = $args[0]; | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||||||||||||||
| 99 | # Multi-column PK or unknown → fall back to SUPER | ||||||||||||||||||||||||||||||||
| 100 | 0 | return $self->SUPER::find(@args); | |||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | return $schema->search_with_prefetch($source, $cond, $self->_build_prefetch) | ||||||||||||||||||||||||||||||||
| 104 | 2 | ▶ | ->then(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||||||
| 105 | 2 | my ($rows) = @_; | |||||||||||||||||||||||||||||||
| 106 | 2 | ▶ | return $rows && @$rows ? $rows->[0] : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$rows and @$rows
MC/DC0%0/2
$rows and @$rows
$rows
@$rows
Truth table33%1/3
A and B
A= $rowsB= @$rows
| |||||||||||||||||||||||||||||||||
| 107 | 2 | }); | |||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | # ─── search() — propagate with() metadata to the new ResultSet ──────────── | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 14 | ▶ | sub search { | ||||||||||||||||||||||||||||||
|
Subroutine
search: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 113 | 14 | my ($self, $cond, $attrs) = @_; | |||||||||||||||||||||||||||||||
| 114 | 14 | my $rs = $self->SUPER::search($cond, $attrs); | |||||||||||||||||||||||||||||||
| 115 | $rs->{_fondation_with} = $self->{_fondation_with} | ||||||||||||||||||||||||||||||||
| 116 | 14 | ▶ | if $self->{_fondation_with}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | $rs->{_fondation_with_has_many} = $self->{_fondation_with_has_many} | ||||||||||||||||||||||||||||||||
| 118 | 14 | ▶ | if $self->{_fondation_with_has_many}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | 14 | return $rs; | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | # ─── _build_prefetch — merge many_to_many and has_many into one hashref ──── | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | # ─── TO_JSON — async serialization via all() ────────────────────────────── | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub TO_JSON { | ||||||||||||||||||||||||||||||
|
Subroutine
TO_JSON: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 127 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | return $self->all->then(sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 129 | 0 | return [ map { $_->TO_JSON } @{shift()} ]; | |||||||||||||||||||||||||||||||
| 130 | 0 | }); | |||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||
| 133 | 6 | ▶ | sub _build_prefetch { | ||||||||||||||||||||||||||||||
|
Subroutine
_build_prefetch: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 134 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 135 | 6 | ▶ | my %prefetch = %{ $self->{_fondation_with} // {} }; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"_fondation_with"} // {}
MC/DC100%1/1
$self->{"_fondation_with"} // {}
$self->{"_fondation_with"}
Truth table100%2/2
A
A= $self->{"_fondation_with"}
| |||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||
| 137 | 6 | ▶ | for my $rel (@{ $self->{_fondation_with_has_many} // [] }) { | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"_fondation_with_has_many"} // []
MC/DC100%1/1
$self->{"_fondation_with_has_many"} // []
$self->{"_fondation_with_has_many"}
Truth table100%2/2
A
A= $self->{"_fondation_with_has_many"}
| |||||||||||||||||||||||||||||||||
| 138 | # has_many: direct prefetch, no nested relationship. | ||||||||||||||||||||||||||||||||
| 139 | # Don't overwrite if a many_to_many already set a nested | ||||||||||||||||||||||||||||||||
| 140 | # value for the same key (e.g. user_group => 'group'). | ||||||||||||||||||||||||||||||||
| 141 | 3 | ▶ | $prefetch{$rel} //= undef; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$prefetch{$rel} //= undef
MC/DC100%1/1
$prefetch{$rel} //= undef
$prefetch{$rel}
Truth table100%2/2
A
A= $prefetch{$rel}
| |||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||
| 144 | 6 | return \%prefetch; | |||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 7 | 1; | |||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||||||