[DB-SIG] DBAPI 3.0: Unified parameter style

Tony Locke tlocke at tlocke.org.uk
Sat Sep 21 15:55:07 CEST 2013


Just for fun I've put up a version of pg8000 that works without a
paramstyle parameter:

https://github.com/tlocke/pg8000/tree/three

In this version, you can do:

execute("select * from emp where name = :1", ['horatio'])

or:

execute("select * from emp where name = :name", {'name': 'horatio'})

The way it works is that the execute() signature remains as:

def execute(query, args):

and then 'query' is parsed for labels, eg '1' or 'name'. The labels
are converted into ints if they can be. Then it evaluates:

args[label]

To return the correct value. So if 'label' is an int and 'args' is a
sequence, the value at that index is returned, and if 'label' is a
string and 'args' is a mapping, then the value for that key is
returned.

The reason I like the :label notation is that it's more convenient for
users because it's easy to remember. Having both ? and :key is
inconsistent (to my tastes). It'll also lead to inconsistencies in
parsing and escaping behaviour.

Some people have raised objections to detecting mapping or sequence
types of the args. The above approach uses duck typing where we just
do args[label] and use what comes back, not caring what args actually
is. I'd argue that this is a perfectly acceptable, pythonic way of
going about things.

Btw, I'm enjoying the list, it's a good group!

Cheers,

Tony.



On 17 September 2013 13:41, Vernon D. Cole <vernondcole at gmail.com> wrote:
> Tony:
>   I should also mention that the reason that "qmark" ("?") delimiters were
> selected as the choice for specified-by-order parameter markers, rather than
> "numeric" (":n") in simply popularity.  Postgres is pretty much alone in
> using numeric, where several other popular databases all use qmark.
>   The most important reason for leaving the .paramstyle attribute in place
> is so that Postgres applications need not be re-written -- we assume that
> anyone who writes a Postgres adapter will allow "numeric" parameters as an
> extension to the standard.
>
>
>>
>> On Fri, Sep 13, 2013 at 6:54 PM, Tony Locke <tlocke at tlocke.org.uk> wrote:
>>>
>>> Hi, I'm a contributor to the PG8000 Postgres driver:
>>>
>>> https://github.com/mfenniak/pg8000/tree/py3
>>>
>>> Having read the interesting DBAPI 3.0 wiki page:
>>>
>>> https://wiki.python.org/moin/DbApi3
>>>
>>> I thought I'd give a suggestion for unified parameters. Apologies if
>>> this has been suggested before, I did have a look at the archives :-)
>>>
>>> In DBAPI 2.0, the parameters to execute() can be a sequence or a
>>> mapping. In DBAPI 3.0, the paramstyle attribute should be removed, and
>>> the style determined by whether the parameters are a sequence or
>>> mapping. For a sequence the 'numeric' style is expected, and for a
>>> mapping the 'named' style. For example:
>>>
>>> execute("select * from emp where name = :1", ['horatio'])
>>>
>>> execute("select * from emp where name = :name", {'name': 'horatio'})
>>>
>>> The reason I like this solution is that if someone asked me how
>>> parameters work in DBAPI, this one would be the easiest to explain!
>>>
>>> Any thoughts?
>>>
>>> Cheers,
>>>
>>> Tony.
>>> _______________________________________________
>>> DB-SIG maillist  -  DB-SIG at python.org
>>> https://mail.python.org/mailman/listinfo/db-sig
>>
>>
>


More information about the DB-SIG mailing list