[Bast-commits] r6674 - in DBIx-Class-ResultSet-HashRef/1.000/trunk: . lib/DBIx/Class/ResultSet t

plu at dev.catalyst.perl.org plu at dev.catalyst.perl.org
Sun Jun 14 12:47:38 GMT 2009


Author: plu
Date: 2009-06-14 12:47:38 +0000 (Sun, 14 Jun 2009)
New Revision: 6674

Modified:
   DBIx-Class-ResultSet-HashRef/1.000/trunk/README
   DBIx-Class-ResultSet-HashRef/1.000/trunk/lib/DBIx/Class/ResultSet/HashRef.pm
   DBIx-Class-ResultSet-HashRef/1.000/trunk/t/10-hashref.t
Log:
Added patch by rbo: New method hashref_pk: Calls hashref_array and returns a reference to a hash containing the primary key

Modified: DBIx-Class-ResultSet-HashRef/1.000/trunk/README
===================================================================
--- DBIx-Class-ResultSet-HashRef/1.000/trunk/README	2009-06-14 12:47:31 UTC (rev 6673)
+++ DBIx-Class-ResultSet-HashRef/1.000/trunk/README	2009-06-14 12:47:38 UTC (rev 6674)
@@ -50,6 +50,15 @@
         my $first_row = $schema->resultset('User')->search( { } )->hashref_first;
         print Dumper $first_row
 
+  hashref_pk( )
+    Calls hashref_array and returns a reference to a hash containing the
+    primary key. For each key the corresponding value is a reference to a
+    hash of the resultset inflated by
+    DBIx::Class::ResultClass::HashRefInflator.
+
+        my $hashref_pk = $schema->resultset('User')->search( { } )->hashref_pk;
+        print Dumper $hashref_pk
+
 AUTHOR
     Johannes Plunien <plu at cpan.org>
 

Modified: DBIx-Class-ResultSet-HashRef/1.000/trunk/lib/DBIx/Class/ResultSet/HashRef.pm
===================================================================
--- DBIx-Class-ResultSet-HashRef/1.000/trunk/lib/DBIx/Class/ResultSet/HashRef.pm	2009-06-14 12:47:31 UTC (rev 6673)
+++ DBIx-Class-ResultSet-HashRef/1.000/trunk/lib/DBIx/Class/ResultSet/HashRef.pm	2009-06-14 12:47:38 UTC (rev 6674)
@@ -2,6 +2,7 @@
 
 use warnings;
 use strict;
+use Carp;
 use base qw( DBIx::Class::ResultSet );
 use DBIx::Class::ResultClass::HashRefInflator;
 
@@ -83,6 +84,26 @@
     return shift->hashref_rs->first;
 }
 
+=head2 hashref_pk( )
+
+Calls hashref_array and returns a reference to a hash containing the primary key. For each key the corresponding value is a reference to a hash of the resultset inflated by L<DBIx::Class::ResultClass::HashRefInflator>.
+
+    my $hashref_pk = $schema->resultset('User')->search( { } )->hashref_pk;
+    print Dumper $hashref_pk
+
+=cut
+
+sub hashref_pk{
+    my $self = shift;
+    my @primary_columns = $self->result_source->primary_columns;
+    croak "Multi-column primary keys are not supported." if (scalar @primary_columns > 1 );
+    croak "No primary key found." if (scalar @primary_columns == 0 );
+    my $primary_key = shift @primary_columns;
+    my %hash_pk = ();
+    %hash_pk = map { $_->{$primary_key} => $_ } $self->hashref_array;
+    return wantarray ? %hash_pk : \%hash_pk ;
+}
+
 =head1 AUTHOR
 
 Johannes Plunien E<lt>plu at cpan.orgE<gt>

Modified: DBIx-Class-ResultSet-HashRef/1.000/trunk/t/10-hashref.t
===================================================================
--- DBIx-Class-ResultSet-HashRef/1.000/trunk/t/10-hashref.t	2009-06-14 12:47:31 UTC (rev 6673)
+++ DBIx-Class-ResultSet-HashRef/1.000/trunk/t/10-hashref.t	2009-06-14 12:47:38 UTC (rev 6674)
@@ -10,7 +10,7 @@
     eval "use SQL::Translator ();";
     plan skip_all => 'SQL::Translator required to run this test' if $@;
 
-    plan( tests => 10 );
+    plan( tests => 11 );
 }
 
 use lib 't/lib';
@@ -312,3 +312,37 @@
         "hashref_first"
     );
 }
+
+{
+    my $hashref = $schema->resultset('User')->search( {}, { order_by => 'me.id ASC' } )->hashref_pk;
+    is_deeply(
+        $hashref,
+        {
+            1 => {
+                'id'    => '1',
+                'login' => 'root'
+            },
+            2 => {
+                'id'    => '2',
+                'login' => 'toor'
+            },
+            3 => {
+                'id'    => '3',
+                'login' => 'daemon'
+            },
+            4 => {
+                'id'    => '4',
+                'login' => 'operator'
+            },
+            5 => {
+                'id'    => '5',
+                'login' => 'bin'
+            },
+            6 => {
+                'id'    => '6',
+                'login' => 'tty'
+            }
+        },
+        'hashref_pk'
+    );
+}




More information about the Bast-commits mailing list