| 1 | package DBIx::Class::ResultSet::CustomPrefetch; | ||||||||||||
| 2 | |||||||||||||
| 3 | 3 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 3 | ▶ | use MRO::Compat; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 3 | ▶ | use mro 'c3'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 3 | ▶ | use parent 'DBIx::Class::ResultSet'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 3 | ▶ | use constant DEBUG => 0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | =head1 NAME | ||||||||||||
| 12 | |||||||||||||
| 13 | DBIx::Class::ResultSet::CustomPrefetch | ||||||||||||
| 14 | |||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||
| 16 | |||||||||||||
| 17 | DBIx::Class allows prefetches only using joins. But sometimes you can't use JOIN for prefetch. E.g. for prefetching | ||||||||||||
| 18 | many related objects to resultset with paging. | ||||||||||||
| 19 | |||||||||||||
| 20 | This module provides other logic for prefetching data to resultsets. | ||||||||||||
| 21 | |||||||||||||
| 22 | =head1 METHODS | ||||||||||||
| 23 | |||||||||||||
| 24 | =cut | ||||||||||||
| 25 | |||||||||||||
| 26 | 4 | ▶ | sub _prefetch_relation { | ||||||||||
|
Subroutine
_prefetch_relation: called CC=9 SCAR=22.1
| |||||||||||||
| 27 | 4 | my ( $self, $accessor_name, $rs_callback, $condition ) = @_; | |||||||||||
| 28 | my $resultset = | ||||||||||||
| 29 | ref $rs_callback | ||||||||||||
| 30 | ? $rs_callback->( $self->result_source->schema, $self->{attrs} ) | ||||||||||||
| 31 | 4 | ▶ | : $self->result_source->schema->resultset($rs_callback); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | 4 | ▶ | return unless $resultset; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | 3 | my $objects = $self->get_cache; | |||||||||||
| 34 | 3 | my %ids = (); | |||||||||||
| 35 | 3 | my %relations = (); | |||||||||||
| 36 | 3 | my ( $foreign_accessor, $source_accessor ) = %$condition; | |||||||||||
| 37 | 3 | $foreign_accessor =~ s/^foreign\.//; | |||||||||||
| 38 | 3 | $source_accessor =~ s/^self\.//; | |||||||||||
| 39 | |||||||||||||
| 40 | 3 | foreach (@$objects) { | |||||||||||
| 41 | 30 | ▶ | next unless defined $_->$source_accessor; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | 30 | $ids{ $_->$source_accessor } = 1; | |||||||||||
| 43 | } | ||||||||||||
| 44 | 3 | my $related_source_alias = $resultset->current_source_alias; | |||||||||||
| 45 | 3 | my @related_objects; | |||||||||||
| 46 | 3 | ▶ | @related_objects = $resultset->search( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | { | ||||||||||||
| 48 | "$related_source_alias.$foreign_accessor" => | ||||||||||||
| 49 | { -in => [ keys %ids ] } | ||||||||||||
| 50 | }, | ||||||||||||
| 51 | )->all if %ids; | ||||||||||||
| 52 | 3 | push @{ $relations{ $_->$foreign_accessor } }, $_ foreach @related_objects; | |||||||||||
| 53 | 3 | warn "Setting accessors:\n" if DEBUG; | |||||||||||
| 54 | 3 | foreach (@$objects) { | |||||||||||
| 55 | 30 | warn "$_ $accessor_name => $source_accessor\n" if DEBUG; | |||||||||||
| 56 | 30 | ▶ | $_->$accessor_name( $relations{ $_->$source_accessor || '' }[0] ); | ||||||||||
|
Condition50%1/2
$_->$source_accessor || ''
MC/DC0%0/1
$_->$source_accessor || ''
$_->$source_accessor
Truth table50%1/2
A
A= $_->$source_accessor
| |||||||||||||
| 57 | } | ||||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | =head2 all | ||||||||||||
| 61 | |||||||||||||
| 62 | Prefetches predefined relations | ||||||||||||
| 63 | |||||||||||||
| 64 | =cut | ||||||||||||
| 65 | |||||||||||||
| 66 | 2 | ▶ | sub all { | ||||||||||
|
Subroutine
all: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 67 | 2 | my ( $self, @args ) = @_; | |||||||||||
| 68 | 2 | warn "Enter SUB: " . (ref $self) . "\n" if DEBUG; | |||||||||||
| 69 | 2 | my @objects = $self->next::method(@args); | |||||||||||
| 70 | 2 | $self->set_cache( \@objects ); | |||||||||||
| 71 | 2 | foreach ( values %{ $self->result_source->{_custom_relations} } ) { | |||||||||||
| 72 | 4 | warn "Prefetching $_->[0]\n" if DEBUG; | |||||||||||
| 73 | 4 | $self->_prefetch_relation(@$_); | |||||||||||
| 74 | } | ||||||||||||
| 75 | 2 | return @objects; | |||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | =head2 next | ||||||||||||
| 79 | |||||||||||||
| 80 | Retrieves ALL relations and does next::method | ||||||||||||
| 81 | |||||||||||||
| 82 | =cut | ||||||||||||
| 83 | |||||||||||||
| 84 | 0 | ▶ | sub next { | ||||||||||
|
Subroutine
next: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 85 | 0 | my $self = shift; | |||||||||||
| 86 | 0 | $self->all; | |||||||||||
| 87 | 0 | $self->next::method(@_); | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | =head1 BUGS | ||||||||||||
| 91 | |||||||||||||
| 92 | =head1 NOTES | ||||||||||||
| 93 | |||||||||||||
| 94 | =head1 AUTHOR | ||||||||||||
| 95 | |||||||||||||
| 96 | Andrey Kostenko (), <andrey@kostenko.name> | ||||||||||||
| 97 | |||||||||||||
| 98 | =head1 COMPANY | ||||||||||||
| 99 | |||||||||||||
| 100 | Rambler Internet Holding | ||||||||||||
| 101 | |||||||||||||
| 102 | =head1 CREATED | ||||||||||||
| 103 | |||||||||||||
| 104 | 17.06.2009 18:12:46 MSD | ||||||||||||
| 105 | |||||||||||||
| 106 | =cut | ||||||||||||
| 107 | |||||||||||||
| 108 | 3 | 1; | |||||||||||
| 109 | |||||||||||||