diff --git a/src/crate/client/cursor.py b/src/crate/client/cursor.py index b77ee4a3..e0c4bf85 100644 --- a/src/crate/client/cursor.py +++ b/src/crate/client/cursor.py @@ -49,7 +49,13 @@ 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) @@ -57,11 +63,11 @@ def _replace(match: "re.Match[str]") -> str: 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: diff --git a/tests/client/test_cursor.py b/tests/client/test_cursor.py index 798ca63a..3888e475 100644 --- a/tests/client/test_cursor.py +++ b/tests/client/test_cursor.py @@ -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 ) @@ -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 )