From: José Duarte Date: Wed, 12 Jan 2022 22:00:00 +0000 (+0000) Subject: Fixes(#7561) Add support for postgres.UUID literal_binds compilation X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=590ef4199f4c7b581c47dfff538c4bc5756af43a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fixes(#7561) Add support for postgres.UUID literal_binds compilation --- diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index bf8812ae54..3a055429ed 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1410,7 +1410,6 @@ from ...types import SMALLINT from ...types import TEXT from ...types import VARCHAR - IDX_USING = re.compile(r"^(?:btree|hash|gist|gin|[\w_]+)$", re.I) RESERVED_WORDS = set( @@ -1751,6 +1750,14 @@ class UUID(sqltypes.TypeEngine): else: return None + def literal_processor(self, dialect): + bp = self.bind_processor(dialect) + + def process(value): + return "'%s'" % bp(value) + + return process + PGUuid = UUID diff --git a/test/dialect/postgresql/test_types.py b/test/dialect/postgresql/test_types.py index e8c18acb04..e3dd4ae5c8 100644 --- a/test/dialect/postgresql/test_types.py +++ b/test/dialect/postgresql/test_types.py @@ -48,6 +48,7 @@ from sqlalchemy.dialects.postgresql import TSTZRANGE from sqlalchemy.exc import CompileError from sqlalchemy.orm import declarative_base from sqlalchemy.orm import Session +from sqlalchemy.sql import bindparam from sqlalchemy.sql import operators from sqlalchemy.sql import sqltypes from sqlalchemy.testing import fixtures @@ -929,10 +930,10 @@ class NumericInterpretationTest(fixtures.TestBase): def test_numeric_codes(self): from sqlalchemy.dialects.postgresql import ( + base, pg8000, psycopg2, psycopg2cffi, - base, ) dialects = ( @@ -1420,9 +1421,11 @@ class ArrayTest(AssertsCompiledSQL, fixtures.TestBase): argnames="with_enum, using_aggregate_order_by", ) def test_array_agg_specific(self, with_enum, using_aggregate_order_by): - from sqlalchemy.dialects.postgresql import aggregate_order_by - from sqlalchemy.dialects.postgresql import array_agg - from sqlalchemy.dialects.postgresql import ENUM + from sqlalchemy.dialects.postgresql import ( + ENUM, + aggregate_order_by, + array_agg, + ) element_type = ENUM if with_enum else Integer expr = ( @@ -2785,6 +2788,46 @@ class UUIDTest(fixtures.TestBase): def test_uuid_array(self, datatype, value1, value2, connection): self.test_round_trip(datatype, value1, value2, connection) + @testing.combinations( + ( + "not_as_uuid", + postgresql.UUID(as_uuid=False), + str(uuid.uuid4()), + str(uuid.uuid4()), + ), + ( + "as_uuid", + postgresql.UUID(as_uuid=True), + uuid.uuid4(), + uuid.uuid4(), + ), + id_="iaaa", + argnames="datatype, value1, value2", + ) + def test_uuid_literal(self, datatype, value1, value2, connection): + v1 = connection.execute( + select( + bindparam( + "uuid_literal", + literal_execute=True, + type_=datatype, + ) + ), + uuid_literal=value1, + ).first() + v2 = connection.execute( + select( + bindparam( + "uuid_literal", + literal_execute=True, + type_=datatype, + ) + ), + uuid_literal=value2, + ).first() + eq_(v1, value1) + eq_(v2, value2) + class HStoreTest(AssertsCompiledSQL, fixtures.TestBase): __dialect__ = "postgresql"