Fixed example/myfirstrecipe.py

This commit is contained in:
Federico Di Gregorio 2005-02-27 15:03:53 +00:00
parent bbb6c8f22c
commit 4b94e54473
3 changed files with 50 additions and 3 deletions

View File

@ -1,3 +1,7 @@
2005-2-27 Federico Di Gregorio <fog@initd.org>
* examples/myfirstrecipe.py: fixed adapter registration.
2005-2-7 Federico Di Gregorio <fog@initd.org>
* setup.py: added patch by Valentino Volonghi to build on MacOS X.

View File

@ -69,6 +69,7 @@ and on the psycopg 2 wiki:
import psycopg
import psycopg.extensions
from psycopg.extensions import adapt as psycoadapt
from psycopg.extensions import register_adapter
class AsIs(object):
"""An adapter that just return the object 'as is'.
@ -99,9 +100,9 @@ class SQL_IN(object):
__str__ = getquoted
# add our new adapter class to psycopg list of adapters
psycopg.extensions.adapters[tuple] = SQL_IN
psycopg.extensions.adapters[float] = AsIs
psycopg.extensions.adapters[int] = AsIs
register_adapter(tuple, SQL_IN)
register_adapter(float, AsIs)
register_adapter(int, AsIs)
# usually we would call:
#

View File

@ -18,6 +18,9 @@ and classes untill a better place in the distribution is found.
# for more details.
from psycopg.extensions import cursor as _cursor
from psycopg.extensions import register_adapter as _RA
from psycopg.extensions import adapt as _A
class DictCursor(_cursor):
"""A cursor that keeps a list of column name -> index mappings."""
@ -65,3 +68,42 @@ class DictRow(list):
if type(x) != int:
x = self._cursor.index[x]
return list.__getitem__(self, x)
class AsIs(object):
"""An adapter that just return the object 'as is'.
psycopg 1.99.9 has some optimizations that make impossible to call adapt()
without adding some basic adapters externally. This limitation will be
lifted in a future release.In the meantime you can use the AsIs adapter.
"""
def __init__(self, obj):
self.__obj = obj
def getquoted(self):
return self.__obj
def prepare(self, conn):
pass
class SQL_IN(object):
"""Adapt any iterable to an SQL quotable object."""
def __init__(self, seq):
self._seq = seq
def prepare(self, conn):
pass
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
qobjs = [str(_A(o).getquoted()) for o in self._seq]
return '(' + ', '.join(qobjs) + ')'
__str__ = getquoted
_RA(tuple, SQL_IN)
_RA(int, AsIs)
_RA(float, AsIs)