]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Update bindparam cache key
authorFederico Caselli <cfederico87@gmail.com>
Wed, 30 Mar 2022 20:50:18 +0000 (22:50 +0200)
committerFederico Caselli <cfederico87@gmail.com>
Wed, 30 Mar 2022 21:36:18 +0000 (23:36 +0200)
The ``literal_execute`` parameter now takes part of the cache
generation of a bindparam, since it changes the sql string generated
by the compiler.
Previously the correct bind values were used, but the ``literal_execute``
would be ignored on subsequent executions of the same query.

Fixes: #7876
Change-Id: I6bf887f1a2fe31f9d0ab68f5b4ff315004d006b2

doc/build/changelog/unreleased_14/7876.rst [new file with mode: 0644]
lib/sqlalchemy/sql/elements.py
test/sql/test_compare.py

diff --git a/doc/build/changelog/unreleased_14/7876.rst b/doc/build/changelog/unreleased_14/7876.rst
new file mode 100644 (file)
index 0000000..c3b1c77
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, sql
+    :tickets: 7876
+
+    The :paramref:`.bindparam.literal_execute` parameter now takes part
+    of the cache generation of a :func:`.bindparam`, since it changes
+    the sql string generated by the compiler.
+    Previously the correct bind values were used, but the ``literal_execute``
+    would be ignored on subsequent executions of the same query.
index da1d50a53588cbfea2dd2f1a9e6b1339f1671450..c735085f83669a12e46592c6c34ab426c052a26b 100644 (file)
@@ -1759,6 +1759,7 @@ class BindParameter(roles.InElementRole, ColumnElement[_T]):
         ("type", InternalTraversal.dp_type),
         ("callable", InternalTraversal.dp_plain_dict),
         ("value", InternalTraversal.dp_plain_obj),
+        ("literal_execute", InternalTraversal.dp_boolean),
     ]
 
     key: str
@@ -1967,6 +1968,7 @@ class BindParameter(roles.InElementRole, ColumnElement[_T]):
             self.__class__,
             self.type._static_cache_key,
             self.key % anon_map if self._key_is_anon else self.key,
+            self.literal_execute,
         )
 
     def _convert_to_unique(self):
index 06ebb0122722257b99dd097e401b818703bdba57..dd073d2a59c62bbb752399c809a138e3fa37eda4 100644 (file)
@@ -285,6 +285,7 @@ class CoreFixtures:
         ),
         lambda: (
             bindparam("x"),
+            bindparam("x", literal_execute=True),
             bindparam("y"),
             bindparam("x", type_=Integer),
             bindparam("x", type_=String),
@@ -1647,6 +1648,7 @@ class CompareClausesTest(fixtures.TestBase):
 
     def test_compare_binds(self):
         b1 = bindparam("foo", type_=Integer())
+        b1l = bindparam("foo", type_=Integer(), literal_execute=True)
         b2 = bindparam("foo", type_=Integer())
         b3 = bindparam("foo", type_=String())
 
@@ -1657,6 +1659,9 @@ class CompareClausesTest(fixtures.TestBase):
             return 6
 
         b4 = bindparam("foo", type_=Integer(), callable_=c1)
+        b4l = bindparam(
+            "foo", type_=Integer(), callable_=c1, literal_execute=True
+        )
         b5 = bindparam("foo", type_=Integer(), callable_=c2)
         b6 = bindparam("foo", type_=Integer(), callable_=c1)
 
@@ -1677,6 +1682,22 @@ class CompareClausesTest(fixtures.TestBase):
         is_false(b7.compare(b8))
         is_true(b7.compare(b7))
 
+        # cache key
+        def compare_key(left, right, expected):
+            lk = left._generate_cache_key().key
+            rk = right._generate_cache_key().key
+            is_(lk == rk, expected)
+
+        compare_key(b1, b4, True)
+        compare_key(b1, b5, True)
+        compare_key(b8, b5, True)
+        compare_key(b8, b7, True)
+        compare_key(b8, b3, False)
+        compare_key(b1, b1l, False)
+        compare_key(b1, b4l, False)
+        compare_key(b4, b4l, False)
+        compare_key(b7, b4l, False)
+
     def test_compare_tables(self):
         is_true(table_a.compare(table_a_2))