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

Kevin Jacobs jacobs at penguin.theopalgroup.com
Thu Jun 5 07:46:46 EDT 2003

On Thu, 5 Jun 2003, Gerhard Häring wrote:
> 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.

Again, I think this is a terrible idea.  First off, __xxx__ names are
reserved in Python.  Secondly, there is never a guarantee that there is a
single canonical quoted representation for any datum.  Different backends,
bound backend schema types, encoding parameters, application contexts can
all alter these representations.  I see __quote__ as a terrible violation of

> 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.

And pyPgSQL's _quote is designed only to be used on simple data values in
the context of an INSERT or an UPDATE.  Used out of context, it is
incomplete and in some cases wrong.  For one, with ambiguously typed
datetime literals).  e.g.:

SELECT 'foo' = '2001-01-01';      -> FALSE
SELECT 'foo' = DATE '2001-01-01'; -> ERROR:  Bad date external
                                     representation 'foo'

PySQLite has a chance of getting _quote right since (last I checked) it only
stores untyped string values.


Kevin Jacobs
The OPAL Group - Enterprise Systems Architect
Voice: (216) 986-0710 x 19         E-mail: jacobs at theopalgroup.com
Fax:   (216) 986-0714              WWW:    http://www.theopalgroup.com

More information about the DB-SIG mailing list