diff --git a/Lib/sqlite3/_completer.py b/Lib/sqlite3/_completer.py index 12cafc26602f8bb..d4811f54316bd3c 100644 --- a/Lib/sqlite3/_completer.py +++ b/Lib/sqlite3/_completer.py @@ -11,6 +11,14 @@ _completion_matches = [] +def _quote_identifier(value): + return '"' + value.replace('"', '""') + '"' + + +def _quote_string_literal(value): + return "'" + value.replace("'", "''") + "'" + + def _complete(con, text, state): global _completion_matches @@ -32,7 +40,7 @@ def _complete(con, text, state): # escape '_' which can appear in attached database names select_clauses = ( f"""\ - SELECT name || ' ' FROM \"{schema}\".sqlite_master + SELECT name || ' ' FROM {_quote_identifier(schema)}.sqlite_master WHERE name LIKE REPLACE(:text, '_', '^_') || '%' ESCAPE '^'""" for schema in schemata ) @@ -46,8 +54,8 @@ def _complete(con, text, state): try: select_clauses = ( f"""\ - SELECT pti.name || ' ' FROM "{schema}".sqlite_master AS sm - JOIN pragma_table_xinfo(sm.name,'{schema}') AS pti + SELECT pti.name || ' ' FROM {_quote_identifier(schema)}.sqlite_master AS sm + JOIN pragma_table_xinfo(sm.name,{_quote_string_literal(schema)}) AS pti WHERE sm.type IN ('table', 'view') AND pti.name LIKE REPLACE(:text, '_', '^_') || '%' ESCAPE '^'""" for schema in schemata diff --git a/Lib/test/test_sqlite3/test_cli.py b/Lib/test/test_sqlite3/test_cli.py index 2dced0ee1f52753..fa89a17aa01d80b 100644 --- a/Lib/test/test_sqlite3/test_cli.py +++ b/Lib/test/test_sqlite3/test_cli.py @@ -299,6 +299,11 @@ def test_complete_table_indexes_triggers_views(self): ON _Attached_table BEGIN SELECT 1; END; CREATE VIEW attached._Attached_view AS SELECT 1; + ATTACH ':memory:' AS "double""quote"; + CREATE TABLE "double""quote"._Double_quote (id); + ATTACH ':memory:' AS "single'quote"; + CREATE TABLE "single'quote"._Single_quote (id); + SELECT id FROM _\t\tta\t; .quit\n""").encode() output = self.write_input(input_) @@ -313,7 +318,9 @@ def test_complete_table_indexes_triggers_views(self): "_Attached_table", "_Attached_trigger", "_Attached_view", + "_Double_quote", "_Index", + "_Single_quote", "_Table", "_Temp_index", "_Temp_table", @@ -340,6 +347,11 @@ def test_complete_columns(self): ATTACH ':memory:' AS attached; CREATE TABLE attached._attached_table (_col_attached); + ATTACH ':memory:' AS "double""quote"; + CREATE TABLE "double""quote"._double_quote (_col_double_quote); + ATTACH ':memory:' AS "single'quote"; + CREATE TABLE "single'quote"._single_quote (_col_single_quote); + SELECT _col_\t\tta\tFROM _table; .quit\n""").encode() output = self.write_input(input_) @@ -352,7 +364,14 @@ def test_complete_columns(self): self.assertEqual( candidates, - ["_col_attached", "_col_table", "_col_table_alias", "_col_temp"], + [ + "_col_attached", + "_col_double_quote", + "_col_single_quote", + "_col_table", + "_col_table_alias", + "_col_temp", + ], ) @unittest.skipIf(sqlite3.sqlite_version_info < (3, 30, 0), "PRAGMA function_list is not available until " diff --git a/Misc/NEWS.d/next/Library/2026-07-15-16-29-05.gh-issue-153768.Q-M37F.rst b/Misc/NEWS.d/next/Library/2026-07-15-16-29-05.gh-issue-153768.Q-M37F.rst new file mode 100644 index 000000000000000..6ba7948bb15c9da --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-15-16-29-05.gh-issue-153768.Q-M37F.rst @@ -0,0 +1 @@ +Fix :mod:`sqlite3` CLI completion for attached database names containing quotes.