Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/crate/client/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,25 @@ def _convert_named_to_positional(
params = {"a": 1, "b": 2}
# returns: ("SELECT * FROM t WHERE a = ? AND b = ?", [1, 2])
"""
positional: t.List[t.Any] = []
positions = {}
idx = 1
new_params = []
for k, v in params.items():
positions[k] = idx
new_params.append(v)
idx += 1

def _replace(match: "re.Match[str]") -> str:
name = match.group(1)
if name not in params:
raise ProgrammingError(
f"Named parameter '{name}' not found in the parameters dict"
)
positional.append(params[name])
return "?"
position = positions[name]
return f"${position}"

converted_sql = _NAMED_PARAM_RE.sub(_replace, sql)
return converted_sql, positional
return converted_sql, new_params


class Cursor:
Expand Down
4 changes: 2 additions & 2 deletions tests/client/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ def test_execute_with_named_params(mocked_connection):
{"a": 1, "b": 2},
)
mocked_connection.client.sql.assert_called_once_with(
"SELECT * FROM t WHERE a = ? AND b = ?", [1, 2], None
"SELECT * FROM t WHERE a = $1 AND b = $2", [1, 2], None
)


Expand All @@ -515,7 +515,7 @@ def test_execute_with_named_params_repeated(mocked_connection):
cursor = mocked_connection.cursor()
cursor.execute("SELECT %(x)s, %(x)s", {"x": 42})
mocked_connection.client.sql.assert_called_once_with(
"SELECT ?, ?", [42, 42], None
"SELECT $1, $1", [42], None
)


Expand Down
Loading