]> 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)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 1 Feb 2015 00:05:55 +0000 (19:05 -0500)
(cherry picked from commit e7d61d594b8a89535696436a667a526bd2999fb2)

lib/sqlalchemy/dialects/postgresql/psycopg2.py

index 35c779a1bd92adc626778831ad12747173fc9e8d..034e02251d1a75339e3389407db80ccd94c0da6b 100644 (file)
@@ -298,7 +298,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')
@@ -363,6 +363,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(
@@ -453,6 +462,7 @@ class PGDialect_psycopg2(PGDialect):
 
     _has_native_hstore = False
     _has_native_json = False
+    _has_native_jsonb = False
 
     colspecs = util.update_copy(
         PGDialect.colspecs,
@@ -461,7 +471,8 @@ class PGDialect_psycopg2(PGDialect):
             ENUM: _PGEnum,  # needs force_unicode
             sqltypes.Enum: _PGEnum,  # needs force_unicode
             HSTORE: _PGHStore,
-            JSON: _PGJSON
+            JSON: _PGJSON,
+            JSONB: _PGJSONB
         }
     )
 
@@ -490,6 +501,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)