1. Michael Bayer
  2. sqlalchemy
  3. Issues

Issues

Issue #2596 resolved

type level events for bind/result processors

Michael Bayer
repo owner created an issue

this would allow easy addition of bind and result handlers across existing types and their subclasses without the need for using custom types.

@event.listens_for(String, "bind_processor", retval=True)
def _str_bind_processor(type, dialect):
   if dialect.name == 'oracle':
        def process(value):
            if value is not None:
                value = value.encode(dialect.encoding)
            return value
        return process
   else:
        # type will return default bind processor
        return None



#!diff
diff -r 7c6bde1c15c97cae34ef9449aa595168910fe87d lib/sqlalchemy/types.py
--- a/lib/sqlalchemy/types.py   Thu Oct 25 12:07:38 2012 -0400
+++ b/lib/sqlalchemy/types.py   Thu Oct 25 12:14:41 2012 -0400
@@ -273,7 +273,14 @@
             return dialect._type_memos[self](self)['bind']('bind')
         except KeyError:
             d = self._dialect_info(dialect)
-            d['bind']('bind') = bp = d['impl']('impl').bind_processor(dialect)
+            if self.dispatch.bind_processor:
+                bp = self.dispatch.bind_processor(self, dialect)
+                if bp is None:
+                    bp = d['impl']('impl').bind_processor(dialect)
+            else:
+                bp = d['impl']('impl').bind_processor(dialect)
+
+            d['bind']('bind') = bp
             return bp

     def _cached_result_processor(self, dialect, coltype):
@@ -283,10 +290,18 @@
             return dialect._type_memos[self](self)[coltype](coltype)
         except KeyError:
             d = self._dialect_info(dialect)
+
+            if self.dispatch.result_processor:
+                rp = self.dispatch.result_processor(self, dialect, coltype)
+                if rp is None:
+                    rp = d['impl']('impl').result_processor(dialect, coltype)
+            else:
+                rp = d['impl']('impl').result_processor(dialect, coltype)
+
             # key assumption: DBAPI type codes are
             # constants.  Else this dictionary would
             # grow unbounded.
-            d[coltype](coltype) = rp = d['impl']('impl').result_processor(dialect, coltype)
+            d[coltype](coltype) = rp
             return rp

     def _dialect_info(self, dialect):

Comments (4)

  1. Log in to comment