1 package DBIx::Class::ResultClass::HashRefInflator;
8 DBIx::Class::ResultClass::HashRefInflator
12 my $rs = $schema->resultset('CD');
14 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
18 DBIx::Class is not built for speed: it's built for convenience and
19 ease of use. But sometimes you just need to get the data, and skip the
20 fancy objects. That is what this class provides.
22 There are two ways of using this class.
28 Specify C<< $rs->result_class >> on a specific resultset to affect only that
29 resultset (and any chained off of it); or
33 Specify C<< __PACKAGE__->result_class >> on your source object to force all
34 uses of that result source to be inflated to hash-refs - this approach is not
39 =head1 AUTOMATICALLY INFLATING COLUMN VALUES
41 So you want to skip the DBIx::Class object creation part, but you still want
42 all your data to be inflated according to the rules you defined in your table
43 classes. Setting the global variable
44 C<$DBIx::Class::ResultClass::HashRefInflator::inflate_data> to a true value
45 will instruct L<mk_hash> to interrogate the processed columns and apply any
46 inflation methods declared via L<DBIx::Class::InflateColumn/inflate_column>.
48 For increased speed the inflation method lookups are cached in
49 C<%DBIx::Class::ResultClass::HashRefInflator::inflator_cache>. Make sure to
50 reset this hash if you modify column inflators at run time.
56 Inflates the result and prefetched data into a hash-ref using L<mk_hash>.
61 my ($self, $source, $me, $prefetch) = @_;
63 my $hashref = mk_hash($me, $prefetch);
69 This does all the work of inflating the (pre)fetched data.
76 # Generally people use this to gain as much speed as possible. If a new mk_hash is
77 # implemented, it should be benchmarked using the maint/benchmark_hashrefinflator.pl
78 # script (in addition to passing all tests of course :). Additional instructions are
79 # provided in the script itself.
83 if (ref $_[0] eq 'ARRAY') { # multi relationship
84 return [ map { mk_hash (@$_) || () } (@_) ];
88 # the main hash could be an undef if we are processing a skipped-over join
89 $_[0] ? %{$_[0]} : (),
91 # the second arg is a hash of arrays for each prefetched relation
93 { $_ => mk_hash( @{$_[1]->{$_}} ) }
94 ( $_[1] ? (keys %{$_[1]}) : () )
97 # if there is at least one defined column consider the resultset real
98 # (and not an emtpy has_many rel containing one empty hashref)
100 return $hash if defined $_;
109 This will not work for relationships that have been prefetched. Consider the
112 my $artist = $artitsts_rs->search({}, {prefetch => 'cds' })->first;
114 my $cds = $artist->cds;
115 $cds->result_class('DBIx::Class::ResultClass::HashRefInflator');
116 my $first = $cds->first;
118 C<$first> will B<not> be a hashref, it will be a normal CD row since
119 HashRefInflator only affects resultsets at inflation time, and prefetch causes
120 relations to be inflated when the master C<$artist> row is inflated.