]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Propagate kwargs to all MySQL CAST paths
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 4 Aug 2016 16:34:55 +0000 (12:34 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 4 Aug 2016 16:34:55 +0000 (12:34 -0400)
Change-Id: I23a6abb26bbbe3d118887d043ce761fc4572d8d2
Fixes: #3766
doc/build/changelog/changelog_11.rst
lib/sqlalchemy/dialects/mysql/base.py
test/dialect/mysql/test_compiler.py

index 5693735297621eab03b5cee92799e17de0d160ad..fe6be3b17e7f1886aa7bbcd888bc6c8fd72f4a8f 100644 (file)
 .. 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
 
index 5abb1f3d6f03e0eb80fcd276a5575b117f7a91f9..7ab9fad69fd5bf5593ecb67ae17ce18f9c6d5468 100644 (file)
@@ -811,13 +811,13 @@ class MySQLCompiler(compiler.SQLCompiler):
         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:
@@ -825,9 +825,9 @@ class MySQLCompiler(compiler.SQLCompiler):
                 "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_)
index 8a789344546beb34315c19b8bbfb275abc91ada8..88f9235e11ad9af54eee63c198f5826c97ac1cd3 100644 (file)
@@ -12,6 +12,8 @@ from sqlalchemy import Table, MetaData, Column, select, String, \
 
 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
 
@@ -414,6 +416,39 @@ class SQLTest(fixtures.TestBase, AssertsCompiledSQL):
         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'))