]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Changed pg8000 dialect to cope with native JSON
authorTony Locke <tlocke@tlocke.org.uk>
Sat, 3 Jan 2015 16:59:17 +0000 (16:59 +0000)
committerTony Locke <tlocke@tlocke.org.uk>
Sat, 3 Jan 2015 16:59:17 +0000 (16:59 +0000)
For versions > 1.10.1 pg8000 returns de-serialized JSON objects rather
than a string. SQL parameters are still strings though.

lib/sqlalchemy/dialects/postgresql/pg8000.py

index 17d83fa6166c7f19ca6f268ceb1ed30f0e2017c9..4bb376a963cd2b3947382b772ffa2270397b940e 100644 (file)
@@ -72,6 +72,7 @@ from .base import (
     PGDialect, PGCompiler, PGIdentifierPreparer, PGExecutionContext,
     _DECIMAL_TYPES, _FLOAT_TYPES, _INT_TYPES)
 import re
+from sqlalchemy.dialects.postgresql.json import JSON
 
 
 class _PGNumeric(sqltypes.Numeric):
@@ -102,6 +103,15 @@ class _PGNumericNoBind(_PGNumeric):
         return None
 
 
+class _PGJSON(JSON):
+
+    def result_processor(self, dialect, coltype):
+        if dialect._dbapi_version > (1, 10, 1):
+            return None  # Has native JSON
+        else:
+            return super(_PGJSON, self).result_processor(dialect, coltype)
+
+
 class PGExecutionContext_pg8000(PGExecutionContext):
     pass
 
@@ -143,7 +153,8 @@ class PGDialect_pg8000(PGDialect):
         PGDialect.colspecs,
         {
             sqltypes.Numeric: _PGNumericNoBind,
-            sqltypes.Float: _PGNumeric
+            sqltypes.Float: _PGNumeric,
+            JSON: _PGJSON,
         }
     )