Moved SQL_IN to extensions and away from extras.

This commit is contained in:
Federico Di Gregorio 2007-01-16 10:58:05 +00:00
parent d342d37e27
commit e59ef4de4b
3 changed files with 29 additions and 24 deletions

View File

@ -1,5 +1,8 @@
2007-01-16 Federico Di Gregorio <fog@initd.org>
* 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.

View File

@ -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('_') ]

View File

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