[DB-SIG] pyformat Parameter Style
Chris Cogdon
chris at cogdon.org
Tue May 13 09:27:48 EDT 2003
On Tuesday, May 13, 2003, at 02:31 US/Pacific, M.-A. Lemburg wrote:
> However, I don't understand why the current implementations
> don't use Python's wealth of format characters (and it's formatting
> implementation).
*I* understand why :)
When one is passing data to the SQL backend, it should be up to the
BACKEND to determine how it is passed, and by that, I mean the python
interface to the backend. Take this example:
Say I want to store a float into a row. The current way, which works,
would be to do this:
cursor.execute ( "insert into data ( %s )", my_float )
The interface sees the floating point value, turns that into the
'appropriate' representation that the SQL syntax accepts, and then
passes it to the DBMS. Now, it is 'expected' that the interface knows
what the 'appropriate' format is, and does what's Right. Not only
should the application writer not know, and not be forced to worry
about it, but he shouldn't be ALLOWED to worry about it. For example:
cursor.execute ( "insert into data ( %2.2f )", my_float )
Now, why in the world would you ever WANT to strip off the precision
from the value? It just doesn't make any sense.
I think here's the reasoning for the mistake:
THe '%' operator in python is a DISPLAY formatting operator. Ie, grab a
value, turn it into a string appropriate for the particular
application's DISPLAY purposes. However, in the 'pass values to the SQL
backend' application, we're not displaying anything, but instead
passing data to the sql backend.
The '%s' and '%(keyname)s' syntax was used because it was convenient,
did the job, required VERY little coding and didn't interfere with
anything else. This does not mean that "Oh, since the python % operator
is used in the interface, that means we can use all the OTHER python
formatting codes, too"
I agree that the DB-API spec as written implies that the other codes
can be used too, but I do not think this was the intention and, I would
argue, given the above reasons, that we do NOT code for this
circumstance. Using all the python formatting operators does not make
sense in this circumstance because it should NOT be up to the
application writer to determine how the data is being passed to the
backend. It should be up to the interface, which knows a lot more about
the backend's requirements than the application writer.
--
("`-/")_.-'"``-._ Chris Cogdon <chris at cogdon.org>
. . `; -._ )-;-,_`)
(v_,)' _ )`-.\ ``-'
_.- _..-_/ / ((.'
((,.-' ((,/ fL
More information about the DB-SIG
mailing list