Commit | Line | Data |
b0930c1e |
1 | package DBIx::Class::ResultClass::HashRefInflator; |
2 | |
83304545 |
3 | use strict; |
4 | use warnings; |
5 | |
137c657c |
6 | =head1 NAME |
7 | |
b24d86a1 |
8 | DBIx::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) { |
630ba6e5 |
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 | ... |
a5b29361 |
27 | } |
137c657c |
28 | |
29 | =head1 DESCRIPTION |
30 | |
a5b29361 |
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). |
137c657c |
36 | |
a5b29361 |
37 | There are two ways of applying this class to a resultset: |
2328814a |
38 | |
a5b29361 |
39 | =over |
137c657c |
40 | |
a5b29361 |
41 | =item * |
137c657c |
42 | |
a5b29361 |
43 | Specify C<< $rs->result_class >> on a specific resultset to affect only that |
44 | resultset (and any chained off of it); or |
137c657c |
45 | |
a5b29361 |
46 | =item * |
137c657c |
47 | |
a5b29361 |
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. |
137c657c |
51 | |
a5b29361 |
52 | =back |
137c657c |
53 | |
54 | =cut |
b0930c1e |
55 | |
2328814a |
56 | ############## |
57 | # NOTE |
58 | # |
a5b29361 |
59 | # Generally people use this to gain as much speed as possible. If a new &mk_hash is |
2328814a |
60 | # implemented, it should be benchmarked using the maint/benchmark_hashrefinflator.pl |
a5b29361 |
61 | # script (in addition to passing all tests of course :). Additional instructions are |
2328814a |
62 | # provided in the script itself. |
63 | # |
64 | |
a5b29361 |
65 | # This coderef is a simple recursive function |
fd4b0742 |
66 | # Arguments: ($me, $prefetch, $is_root) from inflate_result() below |
a5b29361 |
67 | my $mk_hash; |
68 | $mk_hash = sub { |
2328814a |
69 | if (ref $_[0] eq 'ARRAY') { # multi relationship |
a5b29361 |
70 | return [ map { $mk_hash->(@$_) || () } (@_) ]; |
2328814a |
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 |
a5b29361 |
79 | { $_ => $mk_hash->( @{$_[1]->{$_}} ) } |
2328814a |
80 | ( $_[1] ? (keys %{$_[1]}) : () ) |
81 | }; |
82 | |
fd4b0742 |
83 | # if there is at least one defined column *OR* we are at the root of |
84 | # the resultset - consider the result real (and not an emtpy has_many |
85 | # rel containing one empty hashref) |
e8b32a6d |
86 | # an empty arrayref is an empty multi-sub-prefetch - don't consider |
87 | # those either |
fd4b0742 |
88 | return $hash if $_[2]; |
89 | |
2328814a |
90 | for (values %$hash) { |
e8b32a6d |
91 | if (ref $_ eq 'ARRAY') { |
92 | return $hash if @$_; |
93 | } |
94 | elsif (defined $_) { |
95 | return $hash; |
96 | } |
2328814a |
97 | } |
b0930c1e |
98 | |
2328814a |
99 | return undef; |
100 | } |
a5b29361 |
101 | }; |
102 | |
a5b29361 |
103 | =head1 METHODS |
104 | |
a5b29361 |
105 | =head2 inflate_result |
106 | |
107 | Inflates the result and prefetched data into a hash-ref (invoked by L<DBIx::Class::ResultSet>) |
108 | |
109 | =cut |
110 | |
e1540ee0 |
111 | ################################################################################## |
112 | # inflate_result is invoked as: |
113 | # HRI->inflate_result ($resultsource_instance, $main_data_hashref, $prefetch_data_hashref) |
a5b29361 |
114 | sub inflate_result { |
fd4b0742 |
115 | return $mk_hash->($_[2], $_[3], 'is_root'); |
116 | |
2328814a |
117 | } |
118 | |
a5b29361 |
119 | |
120 | =head1 CAVEATS |
121 | |
122 | =over |
123 | |
124 | =item * |
419ff184 |
125 | |
126 | This will not work for relationships that have been prefetched. Consider the |
127 | following: |
128 | |
129 | my $artist = $artitsts_rs->search({}, {prefetch => 'cds' })->first; |
130 | |
131 | my $cds = $artist->cds; |
132 | $cds->result_class('DBIx::Class::ResultClass::HashRefInflator'); |
133 | my $first = $cds->first; |
134 | |
135 | C<$first> will B<not> be a hashref, it will be a normal CD row since |
136 | HashRefInflator only affects resultsets at inflation time, and prefetch causes |
137 | relations to be inflated when the master C<$artist> row is inflated. |
138 | |
088476d4 |
139 | =item * |
140 | |
141 | Column value inflation, e.g., using modules like |
142 | L<DBIx::Class::InflateColumn::DateTime>, is not performed. |
143 | The returned hash contains the raw database values. |
144 | |
a5b29361 |
145 | =back |
146 | |
419ff184 |
147 | =cut |
148 | |
b0930c1e |
149 | 1; |