.. changelog::
:version: 1.1.0
+ .. change::
+ :tags: bug, mysql
+ :tickets: 3766
+
+ Fixed bug where the "literal_binds" flag would not be propagated
+ to a CAST expression under MySQL.
+
.. change::
:tags: change, orm
else:
return None
- def visit_cast(self, cast, **kwargs):
+ def visit_cast(self, cast, **kw):
# No cast until 4, no decimals until 5.
if not self.dialect._supports_cast:
util.warn(
"Current MySQL version does not support "
"CAST; the CAST will be skipped.")
- return self.process(cast.clause.self_group())
+ return self.process(cast.clause.self_group(), **kw)
type_ = self.process(cast.typeclause)
if type_ is None:
"Datatype %s does not support CAST on MySQL; "
"the CAST will be skipped." %
self.dialect.type_compiler.process(cast.typeclause.type))
- return self.process(cast.clause.self_group())
+ return self.process(cast.clause.self_group(), **kw)
- return 'CAST(%s AS %s)' % (self.process(cast.clause), type_)
+ return 'CAST(%s AS %s)' % (self.process(cast.clause, **kw), type_)
def render_literal_value(self, value, type_):
value = super(MySQLCompiler, self).render_literal_value(value, type_)
from sqlalchemy.dialects.mysql import base as mysql
from sqlalchemy.testing import fixtures, AssertsCompiledSQL
+from sqlalchemy.testing import mock
+from sqlalchemy import testing
from sqlalchemy.sql import table, column
import re
self.assert_compile(
cast(t.c.col, type_), "CAST(t.col AS SIGNED INTEGER)")
+ def test_cast_literal_bind(self):
+ expr = cast(column('foo', Integer) + 5, Integer())
+
+ self.assert_compile(
+ expr,
+ "CAST(foo + 5 AS SIGNED INTEGER)",
+ literal_binds=True
+ )
+
+ def test_unsupported_cast_literal_bind(self):
+ expr = cast(column('foo', Integer) + 5, Float)
+
+ with expect_warnings(
+ "Datatype FLOAT does not support CAST on MySQL;"
+ ):
+ self.assert_compile(
+ expr,
+ "(foo + 5)",
+ literal_binds=True
+ )
+
+ dialect = mysql.MySQLDialect()
+ dialect.server_version_info = (3, 9, 8)
+ with expect_warnings(
+ "Current MySQL version does not support CAST"
+ ):
+ eq_(
+ str(expr.compile(
+ dialect=dialect,
+ compile_kwargs={"literal_binds": True})),
+ "(foo + 5)"
+ )
+
def test_unsupported_casts(self):
t = sql.table('t', sql.column('col'))