Обсуждение: geting results of query in plperl

Поиск
Список
Период
Сортировка

geting results of query in plperl

От
stan
Дата:
I have looked at:

https://www.postgresql.org/docs/8.4/plperl-database.html

I am also comfortable querying data from tables in perl.  But I do not
quite see how to get the results of a query in plperl.  Here is what I
tried, and it is not working:

my $rv2 = spi_exec_query('SELECT current_user');
my $user = $rv2->{rows}[1]->{my_column};


I have used this query in SQL functions, so I know it works. I also ran it
in plsql.

What do I have wrong here?

-- 
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
                        -- Benjamin Franklin



Re: geting results of query in plperl

От
Andy Colson
Дата:
On 3/6/20 6:57 AM, stan wrote:
> I have looked at:
> 
> https://www.postgresql.org/docs/8.4/plperl-database.html
> 
> I am also comfortable querying data from tables in perl.  But I do not
> quite see how to get the results of a query in plperl.  Here is what I
> tried, and it is not working:
> 
> my $rv2 = spi_exec_query('SELECT current_user');
> my $user = $rv2->{rows}[1]->{my_column};
> 
> 
> I have used this query in SQL functions, so I know it works. I also ran it
> in plsql.
> 
> What do I have wrong here?
> 

It starts at zero:
> my $user = $rv2->{rows}[0]->{my_column};

Here is some live code:

    my ($q, $i, $row, %map);
    $q = spi_exec_query('select lower(username) as username, id from employee');
    foreach $i (0.. $q->{processed} - 1)
    {
        $row = $q->{rows}[$i];
        $map{ $row->{username} } = $row->{id};
    }
    $q = undef;