Added note that column inflation is not performed
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultClass / HashRefInflator.pm
CommitLineData
b0930c1e 1package DBIx::Class::ResultClass::HashRefInflator;
2
83304545 3use strict;
4use warnings;
5
137c657c 6=head1 NAME
7
b24d86a1 8DBIx::Class::ResultClass::HashRefInflator - Get raw hashrefs from a resultset
137c657c 9
10=head1 SYNOPSIS
11
a5b29361 12 use DBIx::Class::ResultClass::HashRefInflator;
13
137c657c 14 my $rs = $schema->resultset('CD');
137c657c 15 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
a5b29361 16 while (my $hashref = $rs->next) {
17 ...
18 }
137c657c 19
20=head1 DESCRIPTION
21
a5b29361 22DBIx::Class is faster than older ORMs like Class::DBI but it still isn't
23designed primarily for speed. Sometimes you need to quickly retrieve the data
24from a massive resultset, while skipping the creation of fancy row objects.
25Specifying this class as a C<result_class> for a resultset will change C<< $rs->next >>
26to return a plain data hash-ref (or a list of such hash-refs if C<< $rs->all >> is used).
137c657c 27
a5b29361 28There are two ways of applying this class to a resultset:
2328814a 29
a5b29361 30=over
137c657c 31
a5b29361 32=item *
137c657c 33
a5b29361 34Specify C<< $rs->result_class >> on a specific resultset to affect only that
35resultset (and any chained off of it); or
137c657c 36
a5b29361 37=item *
137c657c 38
a5b29361 39Specify C<< __PACKAGE__->result_class >> on your source object to force all
40uses of that result source to be inflated to hash-refs - this approach is not
41recommended.
137c657c 42
a5b29361 43=back
137c657c 44
45=cut
b0930c1e 46
2328814a 47##############
48# NOTE
49#
a5b29361 50# Generally people use this to gain as much speed as possible. If a new &mk_hash is
2328814a 51# implemented, it should be benchmarked using the maint/benchmark_hashrefinflator.pl
a5b29361 52# script (in addition to passing all tests of course :). Additional instructions are
2328814a 53# provided in the script itself.
54#
55
a5b29361 56# This coderef is a simple recursive function
57# Arguments: ($me, $prefetch) from inflate_result() below
58my $mk_hash;
59$mk_hash = sub {
2328814a 60 if (ref $_[0] eq 'ARRAY') { # multi relationship
a5b29361 61 return [ map { $mk_hash->(@$_) || () } (@_) ];
2328814a 62 }
63 else {
64 my $hash = {
65 # the main hash could be an undef if we are processing a skipped-over join
66 $_[0] ? %{$_[0]} : (),
67
68 # the second arg is a hash of arrays for each prefetched relation
69 map
a5b29361 70 { $_ => $mk_hash->( @{$_[1]->{$_}} ) }
2328814a 71 ( $_[1] ? (keys %{$_[1]}) : () )
72 };
73
74 # if there is at least one defined column consider the resultset real
75 # (and not an emtpy has_many rel containing one empty hashref)
76 for (values %$hash) {
77 return $hash if defined $_;
78 }
b0930c1e 79
2328814a 80 return undef;
81 }
a5b29361 82};
83
a5b29361 84=head1 METHODS
85
a5b29361 86=head2 inflate_result
87
88Inflates the result and prefetched data into a hash-ref (invoked by L<DBIx::Class::ResultSet>)
89
90=cut
91
e1540ee0 92##################################################################################
93# inflate_result is invoked as:
94# HRI->inflate_result ($resultsource_instance, $main_data_hashref, $prefetch_data_hashref)
a5b29361 95sub inflate_result {
e1540ee0 96 return $mk_hash->($_[2], $_[3]);
2328814a 97}
98
a5b29361 99
100=head1 CAVEATS
101
102=over
103
104=item *
419ff184 105
106This will not work for relationships that have been prefetched. Consider the
107following:
108
109 my $artist = $artitsts_rs->search({}, {prefetch => 'cds' })->first;
110
111 my $cds = $artist->cds;
112 $cds->result_class('DBIx::Class::ResultClass::HashRefInflator');
113 my $first = $cds->first;
114
115C<$first> will B<not> be a hashref, it will be a normal CD row since
116HashRefInflator only affects resultsets at inflation time, and prefetch causes
117relations to be inflated when the master C<$artist> row is inflated.
118
088476d4 119=item *
120
121Column value inflation, e.g., using modules like
122L<DBIx::Class::InflateColumn::DateTime>, is not performed.
123The returned hash contains the raw database values.
124
a5b29361 125=back
126
419ff184 127=cut
128
b0930c1e 1291;