From c86b95038d84b40617fccd485e4596da4b139f5a Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 8 Aug 2017 12:56:43 -0400 Subject: 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 --- lib/sqlalchemy/dialects/postgresql/pg8000.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'lib/sqlalchemy/dialects/postgresql/pg8000.py') diff --git a/lib/sqlalchemy/dialects/postgresql/pg8000.py b/lib/sqlalchemy/dialects/postgresql/pg8000.py index 5f6cd0a3f..3ec7798c3 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 } ) -- cgit v1.2.1