[pypy-svn] pypy default: sqlite: Implement the adapter microprotocol

amauryfa commits-noreply at bitbucket.org
Mon Mar 7 13:47:26 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r42457:675d9bb684ca
Date: 2011-03-07 11:09 +0100
http://bitbucket.org/pypy/pypy/changeset/675d9bb684ca/

Log:	sqlite: Implement the adapter microprotocol

diff --git a/lib_pypy/_sqlite3.py b/lib_pypy/_sqlite3.py
--- a/lib_pypy/_sqlite3.py
+++ b/lib_pypy/_sqlite3.py
@@ -890,9 +890,7 @@
         if cvt is not None:
             cvt = param = cvt(param)
 
-        adapter = adapters.get((type(param), PrepareProtocol), None)
-        if adapter is not None:
-            param = adapter(param)
+        param = adapt(param)
 
         if param is None:
             sqlite.sqlite3_bind_null(self.statement, idx)
@@ -1101,12 +1099,6 @@
                 return 1
     return 0
 
-def register_adapter(typ, callable):
-    adapters[typ, PrepareProtocol] = callable
-
-def register_converter(name, callable):
-    converters[name.upper()] = callable
-
 def _convert_params(con, nargs, params):
     _params  = []
     for i in range(nargs):
@@ -1187,6 +1179,12 @@
 class PrepareProtocol(object):
     pass
 
+def register_adapter(typ, callable):
+    adapters[typ, PrepareProtocol] = callable
+
+def register_converter(name, callable):
+    converters[name.upper()] = callable
+
 def register_adapters_and_converters():
     def adapt_date(val):
         return val.isoformat()
@@ -1216,11 +1214,39 @@
     register_converter("date", convert_date)
     register_converter("timestamp", convert_timestamp)
 
+def adapt(val, proto=PrepareProtocol):
+    # look for an adapter in the registry
+    adapter = adapters.get((type(val), proto), None)
+    if adapter is not None:
+        return adapter(val)
+
+    # try to have the protocol adapt this object
+    if hasattr(proto, '__adapt__'):
+        try:
+            adapted = proto.__adapt__(val)
+        except TypeError:
+            pass
+        else:
+            if adapted is not None:
+                return adapted
+
+    # and finally try to have the object adapt itself
+    if hasattr(val, '__conform__'):
+        try:
+            adapted = val.__conform__(proto)
+        except TypeError:
+            pass
+        else:
+            if adapted is not None:
+                return adapted
+
+    return val
+
+register_adapters_and_converters()
+
 def OptimizedUnicode(s):
     try:
         val = unicode(s, "ascii").encode("ascii")
     except UnicodeDecodeError:
         val = unicode(s, "utf-8")
     return val
-
-register_adapters_and_converters()


More information about the Pypy-commit mailing list