summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects/sqlite/pysqlite.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2021-12-08 08:57:44 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2021-12-26 19:32:53 -0500
commit6d589ffbb5fe04a4ee606819e948974045f62b80 (patch)
tree95fc3ac54ae23945e3bf810f85294193f4fbbd82 /lib/sqlalchemy/dialects/sqlite/pysqlite.py
parent2bb6cfc7c9b8f09eaa4efeffc337a1162993979c (diff)
downloadsqlalchemy-6d589ffbb5fe04a4ee606819e948974045f62b80.tar.gz
consider truediv as truediv; support floordiv operator
Implemented full support for "truediv" and "floordiv" using the "/" and "//" operators. A "truediv" operation between two expressions using :class:`_types.Integer` now considers the result to be :class:`_types.Numeric`, and the dialect-level compilation will cast the right operand to a numeric type on a dialect-specific basis to ensure truediv is achieved. For floordiv, conversion is also added for those databases that don't already do floordiv by default (MySQL, Oracle) and the ``FLOOR()`` function is rendered in this case, as well as for cases where the right operand is not an integer (needed for PostgreSQL, others). The change resolves issues both with inconsistent behavior of the division operator on different backends and also fixes an issue where integer division on Oracle would fail to be able to fetch a result due to inappropriate outputtypehandlers. Fixes: #4926 Change-Id: Id54cc018c1fb7a49dd3ce1216d68d40f43fe2659
Diffstat (limited to 'lib/sqlalchemy/dialects/sqlite/pysqlite.py')
-rw-r--r--lib/sqlalchemy/dialects/sqlite/pysqlite.py18
1 files changed, 14 insertions, 4 deletions
diff --git a/lib/sqlalchemy/dialects/sqlite/pysqlite.py b/lib/sqlalchemy/dialects/sqlite/pysqlite.py
index 944d714a3..77c9ebce7 100644
--- a/lib/sqlalchemy/dialects/sqlite/pysqlite.py
+++ b/lib/sqlalchemy/dialects/sqlite/pysqlite.py
@@ -402,6 +402,7 @@ by adding the desired locking mode to our ``"BEGIN"``::
""" # noqa
+import math
import os
import re
@@ -505,14 +506,23 @@ class SQLiteDialect_pysqlite(SQLiteDialect):
return None
return re.search(a, b) is not None
+ create_func_kw = {"deterministic": True} if util.py38 else {}
+
def set_regexp(dbapi_connection):
dbapi_connection.create_function(
- "regexp",
- 2,
- regexp,
+ "regexp", 2, regexp, **create_func_kw
+ )
+
+ def floor_func(dbapi_connection):
+ # NOTE: floor is optionally present in sqlite 3.35+ , however
+ # as it is normally non-present we deliver floor() unconditionally
+ # for now.
+ # https://www.sqlite.org/lang_mathfunc.html
+ dbapi_connection.create_function(
+ "floor", 1, math.floor, **create_func_kw
)
- fns = [set_regexp]
+ fns = [set_regexp, floor_func]
def connect(conn):
for fn in fns: