Re: Doing a \set through perl DBI ?

Поиск
Список
Период
Сортировка
От Francisco Olarte
Тема Re: Doing a \set through perl DBI ?
Дата
Msg-id CA+bJJby8tOo+gGu1iW7zEwtfmC_v5LeDQLTTfQDScKfyEvNyjw@mail.gmail.com
обсуждение исходный текст
Ответ на Doing a \set through perl DBI ?  (David Gauthier <davegauthierpg@gmail.com>)
Список pgsql-general
David:

On Wed, Jun 6, 2018 at 5:36 PM, David Gauthier <davegauthierpg@gmail.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


В списке pgsql-general по дате отправления:

Предыдущее
От: Pablo Hendrickx
Дата:
Сообщение: Re: Code of Conduct plan
Следующее
От: Adarsh Sharma
Дата:
Сообщение: iso-8859-1 database folders in base directory