Re: Doing a \set through perl DBI ?

From: Francisco Olarte <folarte(at)peoplecall(dot)com>
To: David Gauthier <davegauthierpg(at)gmail(dot)com>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Doing a \set through perl DBI ?
Date: 2018-06-07 08:24:45
Message-ID: CA+bJJby8tOo+gGu1iW7zEwtfmC_v5LeDQLTTfQDScKfyEvNyjw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

David:

On Wed, Jun 6, 2018 at 5:36 PM, David Gauthier <davegauthierpg(at)gmail(dot)com> wrote:
> Hi:
>
> Is there a way to do the equivalent of a "\set foo 1" through perl dbi ?
> I tried...
> $dbh->do("\\set foo 1");
> and got a syntax error
>
> Of course, I'd also have to be able to access the value of foo once its set.
> I'm guessing the usual way ??? (select :foo)

You are looking at it wrong. Psql vars are similar to perl vars, and
it is psql ( the program in your machine ) who interpolates them
before sending the query to the server.

The equivalent perl code to

\set foo 1
select :foo;

Would be

my $foo = '1';
$dbh->do("select $foo")

Which is not terribly useful, in perl.

To use the current ( client ) user in a query ( like your next message
states ), you just need

$dbh->do("select * from some_table where user_id_column=$<")

( Of course, you could "use English;" to get $UID, or use placeholders
to avoid injections, that's just an initial pointer )
( or "perldoc perlvar" if you do not know what $< is, and that is
enough perl stuff for a postres list )

Francisco Olarte

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adarsh Sharma 2018-06-07 08:52:02 Unknown database folders in base directory
Previous Message Pablo Hendrickx 2018-06-07 08:04:41 Re: Code of Conduct plan