public inbox for [email protected]  
help / color / mirror / Atom feed
From: Daniele Varrazzo <[email protected]>
To: Vladimir Ryabtsev <[email protected]>
Cc: Adrian Klaver <[email protected]>
Cc: [email protected]
Subject: Re: psycopg3 and adaptation choices
Date: Mon, 9 Nov 2020 11:32:12 +0000
Message-ID: <CA+mi_8Z3+tYZMGkV2tE7dk34EuQcryL=UYk8JuW+ka7pOrnZ8g@mail.gmail.com> (raw)
In-Reply-To: <CAMqTPqmNC8EWQ0T1qtF6HyxkXehb09QuGBNwB0bDFR-QDsqpew@mail.gmail.com>
References: <CA+mi_8Zdk_beGcbOjkpdo+fchTR5ot1XM2zZc=ujSGqP+WVh-g@mail.gmail.com>
	<[email protected]>
	<CA+mi_8ZfV+rEDKaqM-pcSf43FuN4FUJGoGTxvEEXap-x_GWP0Q@mail.gmail.com>
	<[email protected]>
	<CA+mi_8YUEPfTPqD-1Jxm6NUwgJpWxPgejRZ+2-mPQr=0DQWiHA@mail.gmail.com>
	<CAMqTPqmVdEK+BDPeq=Lv4=tcpSPUPUyyfVCjps_QbjFwwaUB1A@mail.gmail.com>
	<CAMqTPqmNC8EWQ0T1qtF6HyxkXehb09QuGBNwB0bDFR-QDsqpew@mail.gmail.com>

On Mon, 9 Nov 2020 at 02:49, Vladimir Ryabtsev <[email protected]> wrote:
>
> A wild idea: support both client-side (like in psycopg2) and server-side binding. Keep old '%s' syntax and provide a separate method for client-side binding (not in 'cur.execute()'). This could alleviate cases like parameters roundtripping and other cases of safe query composition. At the same time use '$N' for true server-side binding.
> Is it an overcomplication or there are valid use-cases of that?

Such a thing exists already: there is a client-side binding library,
which is pretty much a direct porting of the psycopg2.sql module
(https://www.psycopg.org/docs/sql.html). Using that facility, you can
compose argument client-side with `query = sql.SQL("select {},
%s").format("hello")` and pass the above to `cursor.execute(query,
[42]). It uses `{}` and `{name}`-style parameters to bind client-side
so it leaves %s placeholders untouched for execute().

In psycopg3 I've made it slightly easier to use by letting
`SQL.format()` to accept any Python object, and automatically wrapping
it into a `Literal()` object, whereas in psycopg2 `format()` only
accepted "`Composible`" objects, and you should have written the above
as `sql.SQL("select {}, %s").format(Literal("hello"))`.

-- Daniele





view thread (23+ messages)  latest in thread

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: psycopg3 and adaptation choices
  In-Reply-To: <CA+mi_8Z3+tYZMGkV2tE7dk34EuQcryL=UYk8JuW+ka7pOrnZ8g@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