From e59ef4de4b58a16ba907b6af8073e867e56e14cd Mon Sep 17 00:00:00 2001 From: Federico Di Gregorio Date: Tue, 16 Jan 2007 10:58:05 +0000 Subject: [PATCH] Moved SQL_IN to extensions and away from extras. --- ChangeLog | 3 +++ lib/extensions.py | 26 ++++++++++++++++++++++++++ lib/extras.py | 24 ------------------------ 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2ad85cec..823f911d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2007-01-16 Federico Di Gregorio + * lib/extras.py: moved SQL_IN to extensions.py; we're now officially + adapting tuples. + * psycopg/adapt_mxdatetime.c: fixed #137 by accessing the 'date' and 'time' attributes of the mx.DateTime instance: they are always in ISO format. diff --git a/lib/extensions.py b/lib/extensions.py index 9233d1d7..c17878dc 100644 --- a/lib/extensions.py +++ b/lib/extensions.py @@ -66,4 +66,30 @@ def register_adapter(typ, callable): """Register 'callable' as an ISQLQuote adapter for type 'typ'.""" adapters[(typ, ISQLQuote)] = callable + +# The SQL_IN class is the official adapter for tuples starting from 2.0.6. +class SQL_IN(object): + """Adapt any iterable to an SQL quotable object.""" + + def __init__(self, seq): + self._seq = seq + + def prepare(self, conn): + self._conn = conn + + def getquoted(self): + # this is the important line: note how every object in the + # list is adapted and then how getquoted() is called on it + pobjs = [adapt(o) for o in self._seq] + for obj in pobjs: + if hasattr(obj, 'prepare'): + obj.prepare(self._conn) + qobjs = [str(o.getquoted()) for o in pobjs] + return '(' + ', '.join(qobjs) + ')' + + __str__ = getquoted + +register_adapter(tuple, SQL_IN) + + __all__ = [ k for k in locals().keys() if not k.startswith('_') ] diff --git a/lib/extras.py b/lib/extras.py index dd136191..906c6f42 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -27,7 +27,6 @@ except: from psycopg2.extensions import cursor as _cursor from psycopg2.extensions import connection as _connection -from psycopg2.extensions import register_adapter as _RA from psycopg2.extensions import adapt as _A @@ -122,29 +121,6 @@ class DictRow(list): for n, v in self._index.items(): yield n, list.__getitem__(self, v) -class SQL_IN(object): - """Adapt any iterable to an SQL quotable object.""" - - def __init__(self, seq): - self._seq = seq - - def prepare(self, conn): - self._conn = conn - - def getquoted(self): - # this is the important line: note how every object in the - # list is adapted and then how getquoted() is called on it - pobjs = [_A(o) for o in self._seq] - for obj in pobjs: - if hasattr(obj, 'prepare'): - obj.prepare(self._conn) - qobjs = [str(o.getquoted()) for o in pobjs] - return '(' + ', '.join(qobjs) + ')' - - __str__ = getquoted - -_RA(tuple, SQL_IN) - class LoggingConnection(_connection): """A connection that logs all queries to a file or logger object."""