Skip to content

Commit 8020e18

Browse files
Substitute favorite query args $10 and above (#2449)
prepare_favorite_query_args() replaced "$1" with plain str.replace(), which also matched the start of "$10", "$11" and so on. With ten or more arguments the query was rejected because "$10" was no longer present, and with a single argument "select $1, $10" silently became "select x, x0" instead of reporting the missing "$10". Only match a parameter when it is not followed by another digit. Co-authored-by: Roland Walker <walker@pobox.com>
1 parent 357fed4 commit 8020e18

4 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Upcoming (TBD)
33

44
Bug Fixes
55
--------
6+
* Substitute favorite query parameters `$10` and above instead of replacing them as `$1` followed by a digit.
67
* Write the mycli log file as UTF-8 instead of the locale encoding, so non-ASCII queries and data no longer raise `UnicodeEncodeError`.
78

89

‎mycli/AUTHORS.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Contributors:
128128
* Jay Hemnani
129129
* Alpenj
130130
* goranai
131+
* zzoraemon043
131132

132133

133134
Created by:

‎mycli/packages/special_commands/io_commands.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,13 +624,15 @@ def prepare_favorite_query_args(query: str, args: list[str]) -> tuple[str | None
624624
positional_values: dict[str, str] = {}
625625
for idx, val in enumerate(args):
626626
subst_var = "$" + str(idx + 1)
627-
if subst_var not in query:
627+
# Don't match the start of a longer parameter, such as $1 in $10.
628+
subst_pattern = re.compile(re.escape(subst_var) + r'(?!\d)')
629+
if not subst_pattern.search(query):
628630
display_query = restore_favorite_query_args(query, positional_values)
629631
error = "query does not have substitution parameter " + subst_var + ":\n " + display_query
630632
return (None, {}, error)
631633

632634
marker = f'{marker_prefix}{idx + 1}__'
633-
query = query.replace(subst_var, marker)
635+
query = subst_pattern.sub(marker, query)
634636
positional_values[marker] = val
635637

636638
match = re.search(r"\$\d+", query)

‎mycli_test/pytests/test_special_io_commands.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,6 +1557,23 @@ def test_favorite_eval_errors_match_execution(monkeypatch, query: str | None, ar
15571557
assert eval_result.command is None
15581558

15591559

1560+
def test_subst_favorite_query_args_with_two_digit_parameters() -> None:
1561+
query = 'select ' + ', '.join(f'${i}' for i in range(1, 12))
1562+
args = [f'v{i}' for i in range(1, 12)]
1563+
assert io_commands.subst_favorite_query_args(query, args) == [
1564+
'select ' + ', '.join(args),
1565+
None,
1566+
]
1567+
assert io_commands.subst_favorite_query_args('select $1, $10', ['x']) == [
1568+
None,
1569+
'missing substitution for $10 in query:\n select x, $10',
1570+
]
1571+
assert io_commands.subst_favorite_query_args('select $10', ['x']) == [
1572+
None,
1573+
'query does not have substitution parameter $1:\n select $10',
1574+
]
1575+
1576+
15601577
def test_list_substitute_save_delete_and_redirect_state(tmp_path: Path, monkeypatch) -> None:
15611578
empty_favorites = FakeFavoriteQueries()
15621579
monkeypatch.setattr(io_commands.FavoriteQueries, 'instance', empty_favorites, raising=False)

0 commit comments

Comments
 (0)