[DB-SIG] API suggestion: expose 'quote' method

M.-A. Lemburg mal at lemburg.com
Thu Jun 5 13:26:12 EDT 2003


Gerhard Häring wrote:
> M.-A. Lemburg wrote:
>>> Certainly. Many drivers implement things beyond the mandatory
>>> standard, and if it's possible to get the extras compatible
>>> across drivers, that's a big bonus in my opinion.
>>
>> No objection to adding a note to the spec about this. I still
>> think that you have to flesh out a reasonable API for this,
>> though, e.g. quoting should implemented on a per data type
>> basis rather than on a per SQL statement basis.
> 
> The pyPgSQL, PySQLite and psycopg developers already agreed on an API 
> for the three projects (the newest versions implement this API):
> 
> If you want to add support for a new datatype for the DB-API module, 
> this data type should implement a __quote__ method that returns the 
> appropriate string.

Ouch. __xxx__ names are reserved for interpreter internal use.
You shouldn't use those in your applications, since it is not
clear what will happen to those names in the future, e.g. they
could undergo some serious optimizations which could break your
code.

Other than that, I don't see why a data type should know about
10+ different database backends. The quoting mechanism for
a particular database should know about the quoting, not the
various data types.

> PySQLite and pyPgSQL already have a _quote method like the module-level 
> quote method Chris was arguing for. But it's currently only an 
> implementation detail, not part of the public APIs.

Before going too much into details, we should vote for the kind
of quoting mechanism you would like to have in the spec:

1. quote(sql, parameters) -> quoted SQL statement with embedded SQL
                              literals

2. quote(dataobject, datatype) -> quoted SQL literal

Option 2 can be implemented outside the DB API level. Option 1
is possible, but it's not likely to be easily implementable
unless the database API already provides a suitable function
for it (parsing SQL is hard).

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Software directly from the Source  (#1, Jun 05 2003)
 >>> Python/Zope Products & Consulting ...         http://www.egenix.com/
 >>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
EuroPython 2003, Charleroi, Belgium:                        19 days left




More information about the DB-SIG mailing list