From: Mike Bayer Date: Tue, 8 Aug 2017 16:56:43 +0000 (-0400) Subject: Enable uuid for pg8000 X-Git-Tag: origin~71^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c86b95038d84b40617fccd485e4596da4b139f5a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Enable uuid for pg8000 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 --- diff --git a/doc/build/changelog/unreleased_12/4016.rst b/doc/build/changelog/unreleased_12/4016.rst new file mode 100644 index 0000000000..bc91614b05 --- /dev/null +++ b/doc/build/changelog/unreleased_12/4016.rst @@ -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 diff --git a/lib/sqlalchemy/dialects/postgresql/pg8000.py b/lib/sqlalchemy/dialects/postgresql/pg8000.py index 5f6cd0a3f6..3ec7798c36 100644 --- a/lib/sqlalchemy/dialects/postgresql/pg8000.py +++ b/lib/sqlalchemy/dialects/postgresql/pg8000.py @@ -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 } ) diff --git a/test/dialect/postgresql/test_types.py b/test/dialect/postgresql/test_types.py index b157070c5f..efcf1f0e52 100644 --- a/test/dialect/postgresql/test_types.py +++ b/test/dialect/postgresql/test_types.py @@ -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(