]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Enable native boolean for SQL Server
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 30 Aug 2017 22:06:07 +0000 (18:06 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 30 Aug 2017 22:06:46 +0000 (18:06 -0400)
SQL Server supports what SQLAlchemy calls "native boolean"
with its BIT type, as this type only accepts 0 or 1 and the
DBAPIs return its value as True/False.   So the SQL Server
dialects now enable "native boolean" support, in that a
CHECK constraint is not generated for a :class:`.Boolean`
datatype.  The only difference vs. other native boolean
is that there are no "true" / "false" constants so "1" and
"0" are still rendered here.

Tests are implicit in the existing suites.

Change-Id: I75bbcd549884099fb1a177e68667bf880c40fa7c
Fixes: #4061
doc/build/changelog/unreleased_12/4061.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/mssql/base.py
lib/sqlalchemy/sql/sqltypes.py

diff --git a/doc/build/changelog/unreleased_12/4061.rst b/doc/build/changelog/unreleased_12/4061.rst
new file mode 100644 (file)
index 0000000..63d6b9e
--- /dev/null
@@ -0,0 +1,13 @@
+.. change::
+    :tags: bug, mssql
+    :tickets: 4061
+
+    SQL Server supports what SQLAlchemy calls "native boolean"
+    with its BIT type, as this type only accepts 0 or 1 and the
+    DBAPIs return its value as True/False.   So the SQL Server
+    dialects now enable "native boolean" support, in that a
+    CHECK constraint is not generated for a :class:`.Boolean`
+    datatype.  The only difference vs. other native boolean
+    is that there are no "true" / "false" constants so "1" and
+    "0" are still rendered here.
+
index a4b9fe0ccbdd63ec731300d858d70be847527b45..84be8d0e3c8c39a855bb8f4801e4d33cabe9d2cc 100644 (file)
@@ -1730,7 +1730,7 @@ class MSDialect(default.DefaultDialect):
 
     ischema_names = ischema_names
 
-    supports_native_boolean = False
+    supports_native_boolean = True
     supports_unicode_binds = True
     postfetch_lastrowid = True
 
index fe9a56b6ec3d7644afef8d5341eeccd9bbfd01c6..d0dbc4881609b8f9ac19aefc36dfc344cdbaebe6 100644 (file)
@@ -1603,12 +1603,12 @@ class Boolean(TypeEngine, SchemaType):
         return bool
 
     def literal_processor(self, dialect):
-        if dialect.supports_native_boolean:
-            def process(value):
-                return "true" if value else "false"
-        else:
-            def process(value):
-                return str(1 if value else 0)
+        compiler = dialect.statement_compiler(dialect, None)
+        true = compiler.visit_true(None)
+        false = compiler.visit_false(None)
+
+        def process(value):
+            return true if value else false
         return process
 
     def bind_processor(self, dialect):