]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add support for try_cast function on qlalchemy.dialects.mssql
authorLeonel Atencio <leo.atencio@gmail.com>
Fri, 26 Jul 2019 18:11:34 +0000 (13:11 -0500)
committerLeonel Atencio <leo.atencio@gmail.com>
Fri, 26 Jul 2019 18:11:34 +0000 (13:11 -0500)
lib/sqlalchemy/dialects/mssql/__init__.py
lib/sqlalchemy/dialects/mssql/base.py

index 7cd1d34a558b04a8b90b874f38b3e2778744902a..27edc8fcb04827297c38973a72334e3b51493907 100644 (file)
@@ -42,6 +42,7 @@ from .base import UNIQUEIDENTIFIER
 from .base import VARBINARY
 from .base import VARCHAR
 from .base import XML
+from .base import TRY_CAST
 
 
 base.dialect = dialect = pyodbc.dialect
@@ -79,5 +80,6 @@ __all__ = (
     "UNIQUEIDENTIFIER",
     "SQL_VARIANT",
     "XML",
+    "TRY_CAST",
     "dialect",
 )
index 86116e6a175f5d68e2f89d89b05f93c175bc913a..03027a0c844a6b318f7bad2aca39dcc0eb56e4bf 100644 (file)
@@ -1173,6 +1173,10 @@ class SQL_VARIANT(sqltypes.TypeEngine):
     __visit_name__ = "SQL_VARIANT"
 
 
+class TRY_CAST(elements.Cast):
+    pass
+
+
 # old names.
 MSDateTime = _MSDateTime
 MSDate = _MSDate
@@ -1586,6 +1590,12 @@ class MSSQLCompiler(compiler.SQLCompiler):
         # Limit in mssql is after the select keyword
         return ""
 
+    def _try_cast(self, element, **kw):
+        return "TRY CAST (%s AS %s)" % (
+            compiler.SQLCompiler.process(element.clause, **kw),
+            compiler.SQLCompiler.process(element.typeclause, **kw),
+        )
+
     def visit_select(self, select, **kwargs):
         """Look for ``LIMIT`` and OFFSET in a select statement, and if
         so tries to wrap it in a subquery with ``row_number()`` criterion.