POD fix
[dbsrgits/DBIx-Class-ResultSet-HashRef.git] / lib / DBIx / Class / ResultSet / HashRef.pm
CommitLineData
8d028daa 1package DBIx::Class::ResultSet::HashRef;
2
3use warnings;
4use strict;
698b98d0 5use Carp;
8d028daa 6use base qw( DBIx::Class::ResultSet );
7use DBIx::Class::ResultClass::HashRefInflator;
8
c4d22621 9our $VERSION = '1.002';
8d028daa 10
11=head1 NAME
12
200650cd 13DBIx::Class::ResultSet::HashRef - Adds syntactic sugar to skip the fancy objects
8d028daa 14
15=head1 SYNOPSIS
16
17 # in your resultsource class
18 __PACKAGE__->resultset_class( 'DBIx::Class::ResultSet::HashRef' );
19
20 # in your calling code
21 my $rs = $schema->resultset('User')->search( { } )->hashref_rs;
22 while (my $row = $rs->next) {
23 print Dumper $row;
24 }
25
26 You can chain up every L<DBIx::Class::ResultSet> method to ->hashref_rs:
27
28 * ->hashref_rs->all (same as ->hashref_array)
29
30 * ->hashref_rs->first (same as ->hashref_first)
31
32=head1 DESCRIPTION
33
34This is a simple way to allow you to set result_class to L<DBIx::Class::ResultClass::HashRefInflator> to
35skip the fancy objects.
36
37=head1 INSTALLATION
38
39 perl Makefile.PL
40 make
41 make test
42 make install
43
44=head1 METHODS
45
46=head2 hashref_rs( )
47
48Sets result_class to L<DBIx::Class::ResultClass::HashRefInflator> and returns the resultset.
49
50=cut
51
52sub hashref_rs {
53 my ($self) = @_;
54 $self->result_class('DBIx::Class::ResultClass::HashRefInflator');
55 return $self;
56}
57
58=head2 hashref_array( )
59
60Calls ->hashref_rs->all and returns depending on the calling context an array or an reference to an array.
61
62 my $rs = $schema->resultset('User')->search( { } )->hashref_array;
63 print Dumper $rs;
64
65 my @rs = $schema->resultset('User')->search( { } )->hashref_array;
66 print Dumper @rs;
67
68=cut
69
70sub hashref_array {
71 return wantarray ? shift->hashref_rs->all : [ shift->hashref_rs->all ];
72}
73
74=head2 hashref_first( )
75
76Returns the first row of the resultset inflated by L<DBIx::Class::ResultClass::HashRefInflator>.
77
78 my $first_row = $schema->resultset('User')->search( { } )->hashref_first;
79 print Dumper $first_row
80
81=cut
82
83sub hashref_first {
84 return shift->hashref_rs->first;
85}
86
698b98d0 87=head2 hashref_pk( )
88
fe3df202 89Returns a hash or reference to hash, depending on the calling context. The keys of the hash are
90the primary keys of each row return by L</hashref_array( )>:
91
92 {
93 1 => {
94 'id' => '1',
95 'login' => 'root'
96 },
97 2 => {
98 'id' => '2',
99 'login' => 'toor'
100 },
101 }
102
103Example usage:
698b98d0 104
105 my $hashref_pk = $schema->resultset('User')->search( { } )->hashref_pk;
106 print Dumper $hashref_pk
107
108=cut
109
110sub hashref_pk{
111 my $self = shift;
112 my @primary_columns = $self->result_source->primary_columns;
113 croak "Multi-column primary keys are not supported." if (scalar @primary_columns > 1 );
114 croak "No primary key found." if (scalar @primary_columns == 0 );
115 my $primary_key = shift @primary_columns;
116 my %hash_pk = ();
117 %hash_pk = map { $_->{$primary_key} => $_ } $self->hashref_array;
118 return wantarray ? %hash_pk : \%hash_pk ;
119}
120
8d028daa 121=head1 AUTHOR
122
123Johannes Plunien E<lt>plu@cpan.orgE<gt>
124
c4d22621 125=head1 CONTRIBUTORS
126
127Robert Bohne E<lt>rbo@cpan.orgE<gt>
128
8d028daa 129=head1 COPYRIGHT AND LICENSE
130
131Copyright 2008 by Johannes Plunien
132
133This library is free software; you can redistribute it and/or modify
134it under the same terms as Perl itself.
135
136Thanks to mst for his patience.
137
138=head1 SEE ALSO
139
140=over 4
141
142=item * L<DBIx::Class>
143
144=item * L<DBIx::Class::ResultClass::HashRefInflator>
145
146=back
147
148=cut
149
1501;