| 1 | package DBIx::Class::ResultClass::HashRefInflator; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 27 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 27 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | DBIx::Class::ResultClass::HashRefInflator - Get raw hashrefs from a resultset | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | use DBIx::Class::ResultClass::HashRefInflator; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | my $rs = $schema->resultset('CD'); | ||||||||||||||||||||||||||||
| 15 | $rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); | ||||||||||||||||||||||||||||
| 16 | while (my $hashref = $rs->next) { | ||||||||||||||||||||||||||||
| 17 | ... | ||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | OR as an attribute: | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | my $rs = $schema->resultset('CD')->search({}, { | ||||||||||||||||||||||||||||
| 23 | result_class => 'DBIx::Class::ResultClass::HashRefInflator', | ||||||||||||||||||||||||||||
| 24 | }); | ||||||||||||||||||||||||||||
| 25 | while (my $hashref = $rs->next) { | ||||||||||||||||||||||||||||
| 26 | ... | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | DBIx::Class is faster than older ORMs like Class::DBI but it still isn't | ||||||||||||||||||||||||||||
| 32 | designed primarily for speed. Sometimes you need to quickly retrieve the data | ||||||||||||||||||||||||||||
| 33 | from a massive resultset, while skipping the creation of fancy result objects. | ||||||||||||||||||||||||||||
| 34 | Specifying this class as a C<result_class> for a resultset will change C<< $rs->next >> | ||||||||||||||||||||||||||||
| 35 | to return a plain data hash-ref (or a list of such hash-refs if C<< $rs->all >> is used). | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | There are two ways of applying this class to a resultset: | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =over | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =item * | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | Specify C<< $rs->result_class >> on a specific resultset to affect only that | ||||||||||||||||||||||||||||
| 44 | resultset (and any chained off of it); or | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =item * | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Specify C<< __PACKAGE__->result_class >> on your source object to force all | ||||||||||||||||||||||||||||
| 49 | uses of that result source to be inflated to hash-refs - this approach is not | ||||||||||||||||||||||||||||
| 50 | recommended. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =back | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =cut | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | ############## | ||||||||||||||||||||||||||||
| 57 | # NOTE | ||||||||||||||||||||||||||||
| 58 | # | ||||||||||||||||||||||||||||
| 59 | # Generally people use this to gain as much speed as possible. If a new &mk_hash is | ||||||||||||||||||||||||||||
| 60 | # implemented, it should be benchmarked using the maint/benchmark_hashrefinflator.pl | ||||||||||||||||||||||||||||
| 61 | # script (in addition to passing all tests of course :) | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # This coderef is a simple recursive function | ||||||||||||||||||||||||||||
| 64 | # Arguments: ($me, $prefetch, $is_root) from inflate_result() below | ||||||||||||||||||||||||||||
| 65 | 27 | my $mk_hash; | |||||||||||||||||||||||||||
| 66 | 108 | ▶ | $mk_hash = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.3
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | my $hash = { | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # the main hash could be an undef if we are processing a skipped-over join | ||||||||||||||||||||||||||||
| 71 | 82 | $_[0] ? %{$_[0]} : (), | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # the second arg is a hash of arrays for each prefetched relation | ||||||||||||||||||||||||||||
| 74 | map { $_ => ( | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # null-branch or not | ||||||||||||||||||||||||||||
| 77 | ref $_[1]->{$_} eq $DBIx::Class::ResultSource::RowParser::Util::null_branch_class | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | ? ref $_[1]->{$_}[0] eq 'ARRAY' ? [] : undef | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | : ref $_[1]->{$_}[0] eq 'ARRAY' | ||||||||||||||||||||||||||||
| 82 | 18 | ▶ | ? [ map { $mk_hash->( @$_ ) || () } @{$_[1]->{$_}} ] | ||||||||||||||||||||||||||
|
Condition33%1/3
&$mk_hash(@$_) || ()
MC/DC0%0/2
&$mk_hash(@$_) || ()
&$mk_hash(@$_)
()
Truth table33%1/3
A || B
A= &$mk_hash(@$_)B= ()
| |||||||||||||||||||||||||||||
| 83 | 83 | ▶ | : $mk_hash->( @{$_[1]->{$_}} ) | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 108 | ▶ | ) } ($_[1] ? keys %{$_[1]} : ()) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | }; | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 108 | ▶ | ($_[2] || keys %$hash) ? $hash : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[2] or keys %$hash
MC/DC0%0/2
$_[2] or keys %$hash
$_[2]
keys %$hash
Truth table66%2/3
A or B
A= $_[2]B= keys %$hash
| |||||||||||||||||||||||||||||
| 89 | 27 | }; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 METHODS | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head2 inflate_result | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Inflates the result and prefetched data into a hash-ref (invoked by L<DBIx::Class::ResultSet>) | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | ################################################################################## | ||||||||||||||||||||||||||||
| 100 | # inflate_result is invoked as: | ||||||||||||||||||||||||||||
| 101 | # HRI->inflate_result ($resultsource_instance, $main_data_hashref, $prefetch_data_hashref) | ||||||||||||||||||||||||||||
| 102 | 29 | ▶ | sub inflate_result { | ||||||||||||||||||||||||||
|
Subroutine
inflate_result: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 29 | return $mk_hash->($_[2], $_[3], 'is_root'); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 27 | 1; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||||||||||