6d019ad83cc5441811d9d2b888858e81fb5eac84
[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 =head1 DESCRIPTION
21
22 DBIx::Class is faster than older ORMs like Class::DBI but it still isn't 
23 designed primarily for speed. Sometimes you need to quickly retrieve the data
24 from a massive resultset, while skipping the creation of fancy row objects.
25 Specifying this class as a C<result_class> for a resultset will change C<< $rs->next >>
26 to return a plain data hash-ref (or a list of such hash-refs if C<< $rs->all >> is used).
27
28 There are two ways of applying this class to a resultset:
29
30 =over
31
32 =item *
33
34 Specify C<< $rs->result_class >> on a specific resultset to affect only that
35 resultset (and any chained off of it); or
36
37 =item *
38
39 Specify C<< __PACKAGE__->result_class >> on your source object to force all
40 uses of that result source to be inflated to hash-refs - this approach is not
41 recommended.
42
43 =back
44
45 =cut
46
47 ##############
48 # NOTE
49 #
50 # Generally people use this to gain as much speed as possible. If a new &mk_hash is
51 # implemented, it should be benchmarked using the maint/benchmark_hashrefinflator.pl
52 # script (in addition to passing all tests of course :). Additional instructions are
53 # provided in the script itself.
54 #
55
56 # This coderef is a simple recursive function
57 # Arguments: ($me, $prefetch) from inflate_result() below
58 my $mk_hash;
59 $mk_hash = sub {
60     if (ref $_[0] eq 'ARRAY') {     # multi relationship
61         return [ map { $mk_hash->(@$_) || () } (@_) ];
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
70                 { $_ => $mk_hash->( @{$_[1]->{$_}} ) }
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         # an empty arrayref is an empty multi-sub-prefetch - don't consider
77         # those either
78         for (values %$hash) {
79             if (ref $_ eq 'ARRAY') {
80               return $hash if @$_;
81             }
82             elsif (defined $_) {
83               return $hash;
84             }
85         }
86
87         return undef;
88     }
89 };
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 sub inflate_result {
103     return $mk_hash->($_[2], $_[3]);
104 }
105
106
107 =head1 CAVEATS
108
109 =over
110
111 =item *
112
113 This will not work for relationships that have been prefetched. Consider the
114 following:
115
116  my $artist = $artitsts_rs->search({}, {prefetch => 'cds' })->first;
117
118  my $cds = $artist->cds;
119  $cds->result_class('DBIx::Class::ResultClass::HashRefInflator');
120  my $first = $cds->first; 
121
122 C<$first> will B<not> be a hashref, it will be a normal CD row since 
123 HashRefInflator only affects resultsets at inflation time, and prefetch causes
124 relations to be inflated when the master C<$artist> row is inflated.
125
126 =item *
127
128 Column value inflation, e.g., using modules like
129 L<DBIx::Class::InflateColumn::DateTime>, is not performed.
130 The returned hash contains the raw database values.
131
132 =back
133
134 =cut
135
136 1;