explain how to use HashRefInflator with search
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultClass / HashRefInflator.pm
1 package DBIx::Class::ResultClass::HashRefInflator;
2
3 use strict;
4 use warnings;
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 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).
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 :). Additional instructions are
62 # provided in the script itself.
63 #
64
65 # This coderef is a simple recursive function
66 # Arguments: ($me, $prefetch) from inflate_result() below
67 my $mk_hash;
68 $mk_hash = sub {
69     if (ref $_[0] eq 'ARRAY') {     # multi relationship
70         return [ map { $mk_hash->(@$_) || () } (@_) ];
71     }
72     else {
73         my $hash = {
74             # the main hash could be an undef if we are processing a skipped-over join
75             $_[0] ? %{$_[0]} : (),
76
77             # the second arg is a hash of arrays for each prefetched relation
78             map
79                 { $_ => $mk_hash->( @{$_[1]->{$_}} ) }
80                 ( $_[1] ? (keys %{$_[1]}) : () )
81         };
82
83         # if there is at least one defined column consider the resultset real
84         # (and not an emtpy has_many rel containing one empty hashref)
85         # an empty arrayref is an empty multi-sub-prefetch - don't consider
86         # those either
87         for (values %$hash) {
88             if (ref $_ eq 'ARRAY') {
89               return $hash if @$_;
90             }
91             elsif (defined $_) {
92               return $hash;
93             }
94         }
95
96         return undef;
97     }
98 };
99
100 =head1 METHODS
101
102 =head2 inflate_result
103
104 Inflates the result and prefetched data into a hash-ref (invoked by L<DBIx::Class::ResultSet>)
105
106 =cut
107
108 ##################################################################################
109 # inflate_result is invoked as:
110 # HRI->inflate_result ($resultsource_instance, $main_data_hashref, $prefetch_data_hashref)
111 sub inflate_result {
112     return $mk_hash->($_[2], $_[3]);
113 }
114
115
116 =head1 CAVEATS
117
118 =over
119
120 =item *
121
122 This will not work for relationships that have been prefetched. Consider the
123 following:
124
125  my $artist = $artitsts_rs->search({}, {prefetch => 'cds' })->first;
126
127  my $cds = $artist->cds;
128  $cds->result_class('DBIx::Class::ResultClass::HashRefInflator');
129  my $first = $cds->first; 
130
131 C<$first> will B<not> be a hashref, it will be a normal CD row since 
132 HashRefInflator only affects resultsets at inflation time, and prefetch causes
133 relations to be inflated when the master C<$artist> row is inflated.
134
135 =item *
136
137 Column value inflation, e.g., using modules like
138 L<DBIx::Class::InflateColumn::DateTime>, is not performed.
139 The returned hash contains the raw database values.
140
141 =back
142
143 =cut
144
145 1;