Subject: | Add the support for hstore type introduced in new postgres |
This is my temporary solution to reliably read/write hstore data type.
At least i don't have problems with utf8 :)
Thank you very much!
Subject: | hstore.pl |
use Test::More;
sub pack_hstore {
my $h = shift;
my $r = '';
while (my ($k,$v) = each %$h) {
$h->{$k} = '' if(!defined $v); # for test...
$k = uri_escape_utf8($k);
$v = uri_escape_utf8($v);
$r.="\"$k\" => \"$v\",";
}
if(!eq_hash(unpack_hstore($r), $h)){
use Test::Deep;
die cmp_deeply(unpack_hstore($r), $h,"packed hstore does not match unpacked");
};
return $r;
}
sub unpack_hstore {
my $ho = shift;
$ho = '' if !defined $ho;
my $h = $ho;
my $r = eval("{$h}");
if($@) {
die $@;
}
my %res;
while (my ($k,$v) = each %$r) {
$k = uri_unescape($k);
$v = uri_unescape($v);
utf8::decode($k);
utf8::decode($v);
$res{$k} = $v;
}
return \%res || {};
}