public inbox for [email protected]  
help / color / mirror / Atom feed
From: Daniele Varrazzo <[email protected]>
To: Lembark, Steven <[email protected]>
Cc: [email protected] <[email protected]>
Cc: Loga, Dharani <[email protected]>
Subject: Re: Using standard SQL placeholders in PG
Date: Wed, 13 Oct 2021 15:56:14 +0200
Message-ID: <CA+mi_8Zicw5XEoX-6ovn7++tYcPaUGibXf=PE-RPG3vtv=USsg@mail.gmail.com> (raw)
In-Reply-To: <BN0PR20MB391249E4640F43A8A14B84A4FDB79@BN0PR20MB3912.namprd20.prod.outlook.com>
References: <BN0PR20MB391249E4640F43A8A14B84A4FDB79@BN0PR20MB3912.namprd20.prod.outlook.com>

On Wed, 13 Oct 2021 at 15:44, Lembark, Steven
<[email protected]> wrote:

>... The doller-notation allows re-use of positional parameters and avoids SQL injection entirely by placing the input is appropriately-typed buffers w/in the SQL statement handler. It also avoids issues with double-interpolating quotes as the placeholders are not literals and do not require SQL-quoting to be interpolated properly.

You still require to convert items to string in the right format
understood by Postgres, which might be different from the Python
string form.

> Q: Is there any way to prepare and execute standard PG SQL statements with dollar placeholders in PsychoPG2?

No. psycopg2 is entirely built around "convert to postgres format and
add quotes". We have just released Psycopg 3 exactly to solve this
kind of problem. It also has no direct access to PQexecParams, but it
only uses PQexec so there's no easy way to plug in the $1 params.

Note that still Psycopg 3 doesn't allow you to use $1 placeholders,
but they are the ones it uses internally. There is an internal object,
PostgresQuery, that takes a %s-style query and, among other things,
converts it in $n format. It wouldn't be a difficult thing to write a
cursor to do less work and just convert the arguments.

In Psycopg 3 you can also go a level lower and call directly
PQexecParams passing the query and arguments you want, in postgres
format, but then you have to do the conversion from Python objects to
Postgres format yourself. I would rather put together a cursor to do
so, as above.

So, things can be done, but in Psycopg 3.

-- Daniele





view thread (2+ messages)

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: Using standard SQL placeholders in PG
  In-Reply-To: <CA+mi_8Zicw5XEoX-6ovn7++tYcPaUGibXf=PE-RPG3vtv=USsg@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox