From: Mike Bayer Date: Thu, 12 Dec 2013 01:00:39 +0000 (-0500) Subject: - implement "literal binds" for the text() clause, [ticket:2882] X-Git-Tag: rel_0_9_0~38^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d5384d601107b76bca1634c55fb72372b2d68d42;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - implement "literal binds" for the text() clause, [ticket:2882] --- diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst index b1da813cb5..c40c4c6c8d 100644 --- a/doc/build/changelog/changelog_09.rst +++ b/doc/build/changelog/changelog_09.rst @@ -128,13 +128,15 @@ .. change:: :tags: feature, sql - :tickets: 2877 + :tickets: 2877, 2882 New improvements to the :func:`.text` construct, including more flexible ways to set up bound parameters and return types; in particular, a :func:`.text` can now be turned into a full FROM-object, embeddable in other statements as an alias or CTE - using the new method :meth:`.TextClause.columns`. + using the new method :meth:`.TextClause.columns`. The :func:`.text` + construct can also render "inline" bound parameters when the construct + is compiled in a "literal bound" context. .. seealso:: diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 0c252089c2..3c8d71331d 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -585,13 +585,13 @@ class SQLCompiler(Compiled): def post_process_text(self, text): return text - def visit_textclause(self, textclause, **kwargs): + def visit_textclause(self, textclause, **kw): def do_bindparam(m): name = m.group(1) if name in textclause._bindparams: - return self.process(textclause._bindparams[name]) + return self.process(textclause._bindparams[name], **kw) else: - return self.bindparam_string(name, **kwargs) + return self.bindparam_string(name, **kw) # un-escape any \:params return BIND_PARAMS_ESC.sub(lambda m: m.group(1), diff --git a/test/sql/test_text.py b/test/sql/test_text.py index 827f4dfb98..37346437ec 100644 --- a/test/sql/test_text.py +++ b/test/sql/test_text.py @@ -165,6 +165,17 @@ class BindParamTest(fixtures.TestBase, AssertsCompiledSQL): checkparams={'bar': 4, 'whee': 7}, ) + def test_literal_binds(self): + t = text("select * from foo where lala=:bar and hoho=:whee") + t = t.bindparams(bindparam('bar', 4), whee='whee') + + self.assert_compile( + t, + "select * from foo where lala=4 and hoho='whee'", + checkparams={}, + literal_binds=True + ) + def _assert_type_map(self, t, compare): map_ = dict( (b.key, b.type) for b in t._bindparams.values()