Re: [psycopg] Solving the SQL composition problem - Mailing list psycopg

From Daniele Varrazzo
Subject Re: [psycopg] Solving the SQL composition problem
Date
Msg-id CA+mi_8ZCuTkb9OJc5NRqTBv4R0-ukP3qx1bCGJ6eSS7_3zoRSg@mail.gmail.com
Whole thread Raw
In response to Re: [psycopg] Solving the SQL composition problem  (Jim Nasby <Jim.Nasby@BlueTreble.com>)
Responses Re: [psycopg] Solving the SQL composition problem  (Adrian Klaver <adrian.klaver@aklaver.com>)
Re: [psycopg] Solving the SQL composition problem  (Jim Nasby <Jim.Nasby@BlueTreble.com>)
Re: [psycopg] Solving the SQL composition problem  (Daniele Varrazzo <daniele.varrazzo@gmail.com>)
List psycopg
On Mon, Jan 2, 2017 at 4:05 PM, Jim Nasby <Jim.Nasby@bluetreble.com> wrote:
> On 1/1/17 2:11 AM, Daniele Varrazzo wrote:
>>
>>         sql.SQL("insert into %s values (%%s)") %
>> [sql.Identifier('mytable')],
>
>
> Since %s isn't standard parameter replacement anyway, I'm wondering if both
> considerations could just be handled by execute(), by using different
> replacement syntax. IE:
>
> execute('insert into %s values ($1)', [42], ['my table'])
>
> Obviously this would be backwards incompatible, but I think that's
> manageable.

mmm... what I think is that if these objects' replacement rules were
different one could leave the %s and %(name)s placeholder untouched
for the query params.

A natural choice could be to use the str.format syntax for the query
composition, or a subset of it. Hence my example could be:

cur.execute(
    sql.SQL("insert into {} values (%s,
%s)").format(sql.Identifier('my_table')),
    [10, 20])

This would largely remove the need for double escaping.

-- Daniele


psycopg by date:

Previous
From: Karsten Hilbert
Date:
Subject: Re: [psycopg] speed concerns with executemany()
Next
From: Daniele Varrazzo
Date:
Subject: Re: [psycopg] speed concerns with executemany()