summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Di Gregorio <fog@initd.org>2005-02-27 15:03:53 +0000
committerFederico Di Gregorio <fog@initd.org>2005-02-27 15:03:53 +0000
commit4b94e544732c18d327e6496422d661885b019cb8 (patch)
treeb94c922152097f07f21175a7a9d13695f57060e8
parentbbb6c8f22c249a35052ad23a623fde3f80fb4499 (diff)
downloadpsycopg2-4b94e544732c18d327e6496422d661885b019cb8.tar.gz
Fixed example/myfirstrecipe.py
-rw-r--r--ChangeLog4
-rw-r--r--examples/myfirstrecipe.py7
-rw-r--r--lib/extras.py42
3 files changed, 50 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index a69958e..e5667c2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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.
diff --git a/examples/myfirstrecipe.py b/examples/myfirstrecipe.py
index 8457c67..0aa1776 100644
--- a/examples/myfirstrecipe.py
+++ b/examples/myfirstrecipe.py
@@ -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:
#
diff --git a/lib/extras.py b/lib/extras.py
index 1e69a4e..5052def 100644
--- a/lib/extras.py
+++ b/lib/extras.py
@@ -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)