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 | |
8273e845 |
31 | DBIx::Class is faster than older ORMs like Class::DBI but it still isn't |
a5b29361 |
32 | designed primarily for speed. Sometimes you need to quickly retrieve the data |
fb13a49f |
33 | from a massive resultset, while skipping the creation of fancy result objects. |
a5b29361 |
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 |
9839286b |
61 | # script (in addition to passing all tests of course :) |
2328814a |
62 | |
a5b29361 |
63 | # This coderef is a simple recursive function |
fd4b0742 |
64 | # Arguments: ($me, $prefetch, $is_root) from inflate_result() below |
a5b29361 |
65 | my $mk_hash; |
66 | $mk_hash = sub { |
7f7f54cd |
67 | |
68 | my $hash = { |
52864fbd |
69 | |
7f7f54cd |
70 | # the main hash could be an undef if we are processing a skipped-over join |
71 | $_[0] ? %{$_[0]} : (), |
72 | |
73 | # the second arg is a hash of arrays for each prefetched relation |
52864fbd |
74 | map { $_ => ( |
75 | |
76 | # null-branch or not |
77 | ref $_[1]->{$_} eq $DBIx::Class::ResultSource::RowParser::Util::null_branch_class |
78 | |
79 | ? ref $_[1]->{$_}[0] eq 'ARRAY' ? [] : undef |
80 | |
81 | : ref $_[1]->{$_}[0] eq 'ARRAY' |
82 | ? [ map { $mk_hash->( @$_ ) || () } @{$_[1]->{$_}} ] |
83 | : $mk_hash->( @{$_[1]->{$_}} ) |
84 | |
85 | ) } ($_[1] ? keys %{$_[1]} : ()) |
7f7f54cd |
86 | }; |
87 | |
52864fbd |
88 | ($_[2] || keys %$hash) ? $hash : undef; |
a5b29361 |
89 | }; |
90 | |
a5b29361 |
91 | =head1 METHODS |
92 | |
a5b29361 |
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 | |
e1540ee0 |
99 | ################################################################################## |
100 | # inflate_result is invoked as: |
101 | # HRI->inflate_result ($resultsource_instance, $main_data_hashref, $prefetch_data_hashref) |
a5b29361 |
102 | sub inflate_result { |
7f7f54cd |
103 | return $mk_hash->($_[2], $_[3], 'is_root'); |
2328814a |
104 | } |
105 | |
a2bd3796 |
106 | 1; |
107 | |
108 | __END__ |
a5b29361 |
109 | |
110 | =head1 CAVEATS |
111 | |
112 | =over |
113 | |
114 | =item * |
419ff184 |
115 | |
116 | This will not work for relationships that have been prefetched. Consider the |
117 | following: |
118 | |
119 | my $artist = $artitsts_rs->search({}, {prefetch => 'cds' })->first; |
120 | |
121 | my $cds = $artist->cds; |
122 | $cds->result_class('DBIx::Class::ResultClass::HashRefInflator'); |
8273e845 |
123 | my $first = $cds->first; |
419ff184 |
124 | |
8273e845 |
125 | C<$first> will B<not> be a hashref, it will be a normal CD row since |
419ff184 |
126 | HashRefInflator only affects resultsets at inflation time, and prefetch causes |
127 | relations to be inflated when the master C<$artist> row is inflated. |
128 | |
088476d4 |
129 | =item * |
130 | |
131 | Column value inflation, e.g., using modules like |
132 | L<DBIx::Class::InflateColumn::DateTime>, is not performed. |
133 | The returned hash contains the raw database values. |
134 | |
a5b29361 |
135 | =back |
136 | |
a2bd3796 |
137 | =head1 FURTHER QUESTIONS? |
419ff184 |
138 | |
a2bd3796 |
139 | Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>. |
140 | |
141 | =head1 COPYRIGHT AND LICENSE |
142 | |
143 | This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE> |
144 | by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can |
145 | redistribute it and/or modify it under the same terms as the |
146 | L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>. |