Skip to content
Open
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: 11 additions & 3 deletions Lib/sqlite3/_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
)
Expand All @@ -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
Expand Down
21 changes: 20 additions & 1 deletion Lib/test/test_sqlite3/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
Expand All @@ -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",
Expand All @@ -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_)
Expand All @@ -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 "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :mod:`sqlite3` CLI completion for attached database names containing quotes.
Loading