]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Enable uuid for pg8000
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 8 Aug 2017 16:56:43 +0000 (12:56 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 8 Aug 2017 22:00:18 +0000 (18:00 -0400)
Enabled UUID support for the pg8000 driver, which supports native Python
uuid round trips for this datatype.  Arrays of UUID are still not supported,
however.

Change-Id: I44ca323c5d9f2cd87327210233bc36a3556eb050
Fixes: #4016
doc/build/changelog/unreleased_12/4016.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/pg8000.py
test/dialect/postgresql/test_types.py

diff --git a/doc/build/changelog/unreleased_12/4016.rst b/doc/build/changelog/unreleased_12/4016.rst
new file mode 100644 (file)
index 0000000..bc91614
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+       :tags: bug, postgresql
+       :tickets: 4016
+
+       Enabled UUID support for the pg8000 driver, which supports native Python
+       uuid round trips for this datatype.  Arrays of UUID are still not supported,
+       however.
\ No newline at end of file
index 5f6cd0a3f648a0329eb87c2e0b041e1d2a28e6b1..3ec7798c3681feda134b3e9ab9545820df75683d 100644 (file)
@@ -70,11 +70,16 @@ from ... import processors
 from ... import types as sqltypes
 from .base import (
     PGDialect, PGCompiler, PGIdentifierPreparer, PGExecutionContext,
-    _DECIMAL_TYPES, _FLOAT_TYPES, _INT_TYPES)
+    _DECIMAL_TYPES, _FLOAT_TYPES, _INT_TYPES, UUID)
 import re
 from sqlalchemy.dialects.postgresql.json import JSON
 from ...sql.elements import quoted_name
 
+try:
+    from uuid import UUID as _python_UUID
+except ImportError:
+    _python_UUID = None
+
 
 class _PGNumeric(sqltypes.Numeric):
     def result_processor(self, dialect, coltype):
@@ -113,6 +118,24 @@ class _PGJSON(JSON):
             return super(_PGJSON, self).result_processor(dialect, coltype)
 
 
+class _PGUUID(UUID):
+    def bind_processor(self, dialect):
+        if not self.as_uuid:
+            def process(value):
+                if value is not None:
+                    value = _python_UUID(value)
+                return value
+            return process
+
+    def result_processor(self, dialect, coltype):
+        if not self.as_uuid:
+            def process(value):
+                if value is not None:
+                    value = str(value)
+                return value
+            return process
+
+
 class PGExecutionContext_pg8000(PGExecutionContext):
     pass
 
@@ -156,7 +179,8 @@ class PGDialect_pg8000(PGDialect):
             sqltypes.Numeric: _PGNumericNoBind,
             sqltypes.Float: _PGNumeric,
             JSON: _PGJSON,
-            sqltypes.JSON: _PGJSON
+            sqltypes.JSON: _PGJSON,
+            UUID: _PGUUID
         }
     )
 
index b157070c5f8f6d027b6a4255633ca176cbc9530d..efcf1f0e52c74f98c8d99ffaa052a763a791bcd4 100644 (file)
@@ -1701,7 +1701,6 @@ class UUIDTest(fixtures.TestBase):
         'postgresql+zxjdbc',
         'column "data" is of type uuid but expression '
         'is of type character varying')
-    @testing.fails_on('postgresql+pg8000', 'No support for UUID type')
     def test_uuid_string(self):
         import uuid
         self._test_round_trip(
@@ -1716,7 +1715,6 @@ class UUIDTest(fixtures.TestBase):
         'postgresql+zxjdbc',
         'column "data" is of type uuid but expression is '
         'of type character varying')
-    @testing.fails_on('postgresql+pg8000', 'No support for UUID type')
     def test_uuid_uuid(self):
         import uuid
         self._test_round_trip(
@@ -1730,7 +1728,7 @@ class UUIDTest(fixtures.TestBase):
     @testing.fails_on('postgresql+zxjdbc',
                       'column "data" is of type uuid[] but '
                       'expression is of type character varying')
-    @testing.fails_on('postgresql+pg8000', 'No support for UUID type')
+    @testing.fails_on('postgresql+pg8000', 'No support for UUID with ARRAY')
     def test_uuid_array(self):
         import uuid
         self._test_round_trip(
@@ -1745,7 +1743,7 @@ class UUIDTest(fixtures.TestBase):
     @testing.fails_on('postgresql+zxjdbc',
                       'column "data" is of type uuid[] but '
                       'expression is of type character varying')
-    @testing.fails_on('postgresql+pg8000', 'No support for UUID type')
+    @testing.fails_on('postgresql+pg8000', 'No support for UUID with ARRAY')
     def test_uuid_string_array(self):
         import uuid
         self._test_round_trip(