Skip to content

Commit bf944d8

Browse files
committed
address PR feedback: add comment explaining limitation and test for LIKE + parameters
1 parent ab5eb59 commit bf944d8

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

tests/test_cursor.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,13 @@ def test_plain_query_without_parameters(self):
8585
sql = "SELECT * FROM table"
8686
cursor.execute(sql)
8787
assert captured["sql"] == sql
88+
89+
def test_like_with_parameters(self):
90+
"""A LIKE expression combined with named parameters should work.
91+
Literal percent signs must be escaped as %% when parameters are used."""
92+
cursor, captured = _make_cursor()
93+
sql = "SELECT * FROM table WHERE name LIKE '%%good%%' AND id = %(id)s"
94+
cursor.execute(sql, parameters={"id": 42})
95+
assert captured["sql"] == (
96+
"SELECT * FROM table WHERE name LIKE '%good%' AND id = 42"
97+
)

wherobots/db/cursor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ def execute(
9898
self.__rowcount = -1
9999
self.__description = None
100100

101+
# Only apply %-formatting when parameters are provided; skipping avoids
102+
# misinterpreting literal % in SQL (e.g. LIKE '%good') as format specifiers.
103+
# Note: queries that combine literal % with named parameters must escape
104+
# the literal percent signs as %% per Python's %-formatting rules.
101105
sql = operation % parameters if parameters else operation
102106
self.__current_execution_id = self.__exec_fn(
103107
sql, self.__on_execution_result, store

0 commit comments

Comments
 (0)