]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Psycopg2 JSONB support.
authorMateusz Susik <mateusz.susik@cern.ch>
Fri, 24 Oct 2014 11:27:29 +0000 (13:27 +0200)
committerMateusz Susik <mateusz.susik@cern.ch>
Fri, 24 Oct 2014 11:27:29 +0000 (13:27 +0200)
lib/sqlalchemy/dialects/postgresql/psycopg2.py

index 1a2a1ffe477c868d518a9af8dbe744671cea4929..f90d3054a04583a968058bec7daa50d4fb0874e6 100644 (file)
@@ -261,7 +261,7 @@ from .base import PGDialect, PGCompiler, \
     ENUM, ARRAY, _DECIMAL_TYPES, _FLOAT_TYPES,\
     _INT_TYPES
 from .hstore import HSTORE
-from .json import JSON
+from .json import JSON, JSONB
 
 
 logger = logging.getLogger('sqlalchemy.dialects.postgresql')
@@ -326,6 +326,15 @@ class _PGJSON(JSON):
         else:
             return super(_PGJSON, self).result_processor(dialect, coltype)
 
+
+class _PGJSONB(JSONB):
+
+    def result_processor(self, dialect, coltype):
+        if dialect._has_native_jsonb:
+            return None
+        else:
+            return super(_PGJSONB, self).result_processor(dialect, coltype)
+
 # 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(
@@ -416,6 +425,7 @@ class PGDialect_psycopg2(PGDialect):
 
     _has_native_hstore = False
     _has_native_json = False
+    _has_native_jsonb = False
 
     colspecs = util.update_copy(
         PGDialect.colspecs,
@@ -424,7 +434,8 @@ class PGDialect_psycopg2(PGDialect):
             ENUM: _PGEnum,  # needs force_unicode
             sqltypes.Enum: _PGEnum,  # needs force_unicode
             HSTORE: _PGHStore,
-            JSON: _PGJSON
+            JSON: _PGJSON,
+            JSONB: _PGJSONB
         }
     )
 
@@ -453,6 +464,7 @@ class PGDialect_psycopg2(PGDialect):
             self._hstore_oids(connection.connection) \
             is not None
         self._has_native_json = self.psycopg2_version >= (2, 5)
+        self._has_native_jsonb = self.psycopg2_version >= (2, 5, 4)
 
         # http://initd.org/psycopg/docs/news.html#what-s-new-in-psycopg-2-0-9
         self.supports_sane_multi_rowcount = self.psycopg2_version >= (2, 0, 9)