]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Register HStore adapter and typecaster in psycopg2 dialect
authorAudrius Kažukauskas <audrius@neutrino.lt>
Thu, 15 Nov 2012 14:03:45 +0000 (16:03 +0200)
committerAudrius Kažukauskas <audrius@neutrino.lt>
Thu, 15 Nov 2012 14:03:45 +0000 (16:03 +0200)
lib/sqlalchemy/dialects/postgresql/psycopg2.py

index 700f76793afc0c4f524e48befaf6697ed3c1fdcc..05286ce203a5fcdc58041c66bd157b35f50e1f24 100644 (file)
@@ -147,6 +147,7 @@ from .base import PGDialect, PGCompiler, \
                                 PGIdentifierPreparer, PGExecutionContext, \
                                 ENUM, ARRAY, _DECIMAL_TYPES, _FLOAT_TYPES,\
                                 _INT_TYPES
+from .hstore import HSTORE
 
 
 logger = logging.getLogger('sqlalchemy.dialects.postgresql')
@@ -195,6 +196,13 @@ class _PGArray(ARRAY):
             self.item_type.convert_unicode = "force"
         # end Py2K
 
+class _PGHStore(HSTORE):
+    def bind_processor(self, dialect):
+        return None
+
+    def result_processor(self, dialect, coltype):
+        return None
+
 # When we're handed literal SQL, ensure it's a SELECT-query. Since
 # 8.3, combining cursors and "FOR UPDATE" has been fine.
 SERVER_SIDE_CURSOR_RE = re.compile(
@@ -282,6 +290,7 @@ class PGDialect_psycopg2(PGDialect):
             ENUM : _PGEnum, # needs force_unicode
             sqltypes.Enum : _PGEnum, # needs force_unicode
             ARRAY : _PGArray, # needs force_unicode
+            HSTORE : _PGHStore,
         }
     )
 
@@ -300,6 +309,16 @@ class PGDialect_psycopg2(PGDialect):
                                             int(x)
                                             for x in m.group(1, 2, 3)
                                             if x is not None)
+        self._hstore_oids = None
+
+    def initialize(self, connection):
+        super(PGDialect_psycopg2, self).initialize(connection)
+
+        if self.psycopg2_version >= (2, 4):
+            extras = __import__('psycopg2.extras').extras
+            oids = extras.HstoreAdapter.get_oids(connection.connection)
+            if oids is not None and oids[0]:
+                self._hstore_oids = oids[0], oids[1]
 
     @classmethod
     def dbapi(cls):
@@ -346,6 +365,13 @@ class PGDialect_psycopg2(PGDialect):
                 extensions.register_type(extensions.UNICODE, conn)
             fns.append(on_connect)
 
+        extras = __import__('psycopg2.extras').extras
+        def on_connect(conn):
+            if self._hstore_oids is not None:
+                oid, array_oid = self._hstore_oids
+                extras.register_hstore(conn, oid=oid, array_oid=array_oid)
+        fns.append(on_connect)
+
         if fns:
             def on_connect(conn):
                 for fn in fns: