From 05e17afab179f3708325d9d85198f309642c2f00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Sabati=C3=A9?= Date: Fri, 20 Mar 2015 18:15:25 +0100 Subject: [PATCH] psqlcmd.pm : added fetchall_hashref function --- database/postgres/psqlcmd.pm | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/database/postgres/psqlcmd.pm b/database/postgres/psqlcmd.pm index 178310995..9fd26eb1c 100644 --- a/database/postgres/psqlcmd.pm +++ b/database/postgres/psqlcmd.pm @@ -259,6 +259,27 @@ sub fetchall_arrayref { return $array_ref; } +sub fetchall_hashref { + my ($self, %options) = @_; + my $array_ref = []; + my $array_result = undef; + + if (!defined($self->{columns})) { + $self->{stdout} =~ s/^(.*?)\Q$self->{record_separator}\E//ms; + @{$self->{columns}} = split(/\Q$self->{field_separator}\E/, $1); + } + foreach (split /\Q$self->{record_separator}\E/, $self->{stdout}) { + $array_result = {}; + my @values = split(/\Q$self->{field_separator}\E/, $_); + for (my $i = 0; $i < scalar(@values); $i++) { + my $value = $values[$i]; + $array_result->{$self->{columns}[$i]} = $value; + } + push @$array_ref, $array_result; + } + return $array_ref; +} + sub fetchrow_array { my ($self, %options) = @_; my @array_result = ();