Support for numeric paramstyle

35 views
Skip to first unread message

Cédric Krier

unread,
Jun 1, 2015, 7:20:02 AM6/1/15
to python-sql
Hi,

I need to have support for numeric paramstyle [1].
It is one of the three format that should be prefered as said the
PEP0249. The two others are named and pyformat but they are really not
logical for a library like python-sql as we will have to guess the name.

But to implement numeric, it is quite tricky because we don't know when
to start counting the parameters because queries can be inside other
queries. So the logical counting should be done outside the string
generation. I see two options:

* Having an explicit counter to reset explicitly before stringify
the main query.
This will look like this:

Flavor.set(Flavor(paramstyle='numeric'))
query = table.select(...)
Flavor.get().reset() # reset the param counter
cursor.execute(*query)

Pros: explicit, doesn't add new loop
Cons: new API, new method call, wierd error if reset is forgetten
(but could be converted as context manager)

* Or using the 'format' style and having a method that takes the
query tuple and convert the '%s' into ':i'.
This will look like this:

query = table.select(...)
cursor.execute(Flavor.numberify(*query))

and numberify will do something like:

def numberify(query, params):
return query % [':%s' % i for i, _ in enumerate(params)]

Pros: not intrusif
Cons: not explicit, could lead to formatting error

What do you think? Have you any other ideas?

[1] https://www.python.org/dev/peps/pep-0249/#paramstyle
--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Cédric Krier

unread,
Jun 2, 2015, 4:25:03 AM6/2/15
to python-sql
Here is the implementation of the converter:
https://python-sql.tryton.org/bug39

It is not yet set if this will be the choosen solution but at least we
have an implementation example.
Reply all
Reply to author
Forward
0 new messages