decode hstore column back into HASH
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 9 Jul 2011 20:59:38 +0000 (22:59 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 9 Jul 2011 20:59:38 +0000 (22:59 +0200)
lib/APKPM/Store.pm
t/Store.t

index eafd73d..fed5741 100644 (file)
@@ -66,9 +66,24 @@ sub sql : Job : Encode(e_json) : MinProcesses(0) {
 
        warn "# $rows rows get_username_table $workload\n";
 
+       $rows = $sth->fetchall_arrayref;
+       my @columns = @{ $sth->{NAME} };
+
+       # decode hash column
+       my $hash_col;
+       foreach ( 0 .. $#columns ) {
+               $hash_col = $_ if $columns[$_] eq 'h';
+       }
+       if ( defined $hash_col ) {
+               map {
+                       $_->[$hash_col] = eval '{ ' . $_->[$hash_col] . ' }';
+               } @$rows
+       }
+
        return {
-               columns => $sth->{NAME},
-               rows => $sth->fetchall_arrayref,
+               columns => \@columns,
+               rows => $rows,
+               hash_col => $hash_col,
        };
 }
 
index dfcc5a6..0f8fa87 100755 (executable)
--- a/t/Store.t
+++ b/t/Store.t
@@ -2,7 +2,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 7;
+use Test::More tests => 9;
 use Data::Dump qw(dump);
 
 use lib 'lib';
@@ -11,23 +11,6 @@ use_ok 'APKPM::Store';
 
 ok my $o = APKPM::Store->new, 'new';
 
-ok my $r = $o->insert('job',{
-_table => 'adsl',
-ip => '127.0.0.1',
-username => 'nobody',
-ATTNRX => "36.5",
-ATTNTX => "17.8",
-MAXRX  => 13500,
-MAXTX  => 880,
-PWRRX  => "0.0",
-PWRTX  => "12.6",
-RX     => 8500,
-SNRRX  => "11.4",
-SNRTX  => "16.0",
-TX     => 798,
-}), 'ADSL';
-diag dump($r);
-
 ok my $r = $o->insert('job',{
 _table => 'ping',
 ip => '127.0.0.1',
@@ -42,3 +25,9 @@ diag dump($r);
 ok my $r = $o->sql('job', "select xx from fake"), 'sql with error';
 ok exists $r->{error}, 'error';
 diag dump($r);
+
+ok my $r = $o->sql('job', "select * from cpe_Davolink limit 1"), 'cpe_Davolink';
+ok( $r->{hash_col}, 'hash_col' );
+isa_ok $r->{rows}->[0]->[ $r->{hash_col} ], 'HASH', 'hstore column';
+diag dump $r;
+