--- /dev/null
+.. change::
+ :tags: bug, sql
+ :tickets: 6858
+
+ Fixed an issue in the ``CacheKey.to_offline_string()`` method used by the
+ dogpile.caching example where attempting to create a proper cache key from
+ the special "lambda" query generated by the lazy loader would fail to
+ include the parameter values, leading to an incorrect cache key.
+
else:
sql_str = statement_cache[self.key]
- return repr(
- (
- sql_str,
- tuple(
- parameters.get(bindparam.key, bindparam.value)
- for bindparam in self.bindparams
- ),
+ if not self.bindparams:
+ param_tuple = tuple(parameters[key] for key in sorted(parameters))
+ else:
+ param_tuple = tuple(
+ parameters.get(bindparam.key, bindparam.value)
+ for bindparam in self.bindparams
)
- )
+
+ return repr((sql_str, param_tuple))
def __eq__(self, other):
return self.key == other.key
checkparams={"q_1": 1},
)
+ def test_offline_cache_key_no_paramtrack(self):
+ def go():
+ stmt = lambdas.lambda_stmt(
+ lambda: select(column("x")).where(
+ column("y") == bindparam("q")
+ ),
+ global_track_bound_values=False,
+ )
+
+ return stmt
+
+ s1 = go()
+
+ eq_(
+ s1._generate_cache_key().to_offline_string({}, s1, {"q": 5}),
+ "('SELECT x \\nWHERE y = :q', (5,))",
+ )
+
+ def test_offline_cache_key_paramtrack(self):
+ def go(param):
+ stmt = lambdas.lambda_stmt(
+ lambda: select(column("x")).where(column("y") == param),
+ )
+
+ return stmt
+
+ s1 = go(5)
+
+ param_key = s1._resolved._where_criteria[0].right.key
+ eq_(
+ s1._generate_cache_key().to_offline_string(
+ {}, s1, {param_key: 10}
+ ),
+ "('SELECT x \\nWHERE y = :param_1', (10,))",
+ )
+
def test_stmt_lambda_w_list_of_opts(self):
def go(opts):
stmt = lambdas.lambda_stmt(lambda: select(column("x")))