1 package DBIx::Class::ResultClass::HashRefInflator;
8 DBIx::Class::ResultClass::HashRefInflator - Get raw hashrefs from a resultset
12 use DBIx::Class::ResultClass::HashRefInflator;
14 my $rs = $schema->resultset('CD');
15 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
16 while (my $hashref = $rs->next) {
22 my $rs = $schema->resultset('CD')->search({}, {
23 result_class => 'DBIx::Class::ResultClass::HashRefInflator',
25 while (my $hashref = $rs->next) {
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 row 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).
37 There are two ways of applying this class to a resultset:
43 Specify C<< $rs->result_class >> on a specific resultset to affect only that
44 resultset (and any chained off of it); or
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
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 :)
63 # This coderef is a simple recursive function
64 # Arguments: ($me, $prefetch, $is_root) from inflate_result() below
69 # the main hash could be an undef if we are processing a skipped-over join
70 $_[0] ? %{$_[0]} : (),
72 # the second arg is a hash of arrays for each prefetched relation
74 ref $_[1]->{$_}[0] eq 'ARRAY' # multi rel or not?
76 { $mk_hash->(@$_) || () }
79 : ( $_ => $mk_hash->( @{$_[1]->{$_}} ) )
81 } ( $_[1] ? ( keys %{$_[1]} ) : () )
84 # if there is at least one defined column *OR* we are at the root of
85 # the resultset - consider the result real (and not an emtpy has_many
86 # rel containing one empty hashref)
87 # an empty arrayref is an empty multi-sub-prefetch - don't consider
89 return $hash if $_[2];
95 (ref $_ ne 'ARRAY' or scalar @$_)
104 =head2 inflate_result
106 Inflates the result and prefetched data into a hash-ref (invoked by L<DBIx::Class::ResultSet>)
110 ##################################################################################
111 # inflate_result is invoked as:
112 # HRI->inflate_result ($resultsource_instance, $main_data_hashref, $prefetch_data_hashref)
114 return $mk_hash->($_[2], $_[3], 'is_root');
124 This will not work for relationships that have been prefetched. Consider the
127 my $artist = $artitsts_rs->search({}, {prefetch => 'cds' })->first;
129 my $cds = $artist->cds;
130 $cds->result_class('DBIx::Class::ResultClass::HashRefInflator');
131 my $first = $cds->first;
133 C<$first> will B<not> be a hashref, it will be a normal CD row since
134 HashRefInflator only affects resultsets at inflation time, and prefetch causes
135 relations to be inflated when the master C<$artist> row is inflated.
139 Column value inflation, e.g., using modules like
140 L<DBIx::Class::InflateColumn::DateTime>, is not performed.
141 The returned hash contains the raw database values.