From ab8b4f25eebfbd2ef5819ff98dd372d5392c4b6b Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 8 Jun 2022 21:35:02 -0400 Subject: restore parameter escaping for public methods Adjusted the fix made for :ticket:`8056` which adjusted the escaping of bound parameter names with special characters such that the escaped names were translated after the SQL compilation step, which broke a published recipe on the FAQ illustrating how to merge parameter names into the string output of a compiled SQL string. The change restores the escaped names that come from ``compiled.params`` and adds a conditional parameter to :meth:`.SQLCompiler.construct_params` named ``escape_names`` that defaults to ``True``, restoring the old behavior by default. Fixes: #8113 Change-Id: I9cbedb1080bc06d51f287fd2cbf26aaab1c74653 --- test/sql/test_compiler.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'test/sql') diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index 94c38548f..930f32b7b 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -3752,10 +3752,14 @@ class BindParameterTest(AssertsCompiledSQL, fixtures.TestBase): """general bind param escape unit tests added as a result of #8053. - However, note that the final application of an escaped param name + The final application of an escaped param name was moved out of compiler and into DefaultExecutionContext in related issue #8056. + However in #8113 we made this conditional to suit usage recipes + posted in the FAQ. + + """ SomeEnum = pep435_enum("SomeEnum") @@ -3788,14 +3792,33 @@ class BindParameterTest(AssertsCompiledSQL, fixtures.TestBase): compiled = t.insert().compile( dialect=dialect, compile_kwargs=dict(compile_keys=("_id", "_data")) ) - params = compiled.construct_params({"_id": 1, "_data": one}) + # not escaped + params = compiled.construct_params( + {"_id": 1, "_data": one}, escape_names=False + ) eq_(params, {"_id": 1, "_data": one}) + + # escaped by default + params = compiled.construct_params({"_id": 1, "_data": one}) + eq_(params, {'"_id"': 1, '"_data"': one}) + + # escaped here as well + eq_(compiled.params, {'"_data"': None, '"_id"': None}) + + # bind processors aren't part of this eq_(compiled._bind_processors, {"_data": mock.ANY}) - # previously, this was: - # eq_(params, {'"_id"': 1, '"_data"': one}) - # eq_(compiled._bind_processors, {'"_data"': mock.ANY}) + dialect.paramstyle = "pyformat" + compiled = t.insert().compile( + dialect=dialect, compile_kwargs=dict(compile_keys=("_id", "_data")) + ) + + # FAQ recipe works + eq_( + compiled.string % compiled.params, + "INSERT INTO t (_id, _data) VALUES (None, None)", + ) def test_expanding_non_expanding_conflict(self): """test #8018""" -- cgit v1.2.1