Skip to content

Commit 2b64759

Browse files
committed
gh-153768: Fix sqlite3 CLI completion for database names with quotes
1 parent 87411d0 commit 2b64759

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

Lib/sqlite3/_completer.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
_completion_matches = []
1212

1313

14+
def _quote_identifier(value):
15+
return '"' + value.replace('"', '""') + '"'
16+
17+
18+
def _quote_string_literal(value):
19+
return "'" + value.replace("'", "''") + "'"
20+
21+
1422
def _complete(con, text, state):
1523
global _completion_matches
1624

@@ -32,7 +40,7 @@ def _complete(con, text, state):
3240
# escape '_' which can appear in attached database names
3341
select_clauses = (
3442
f"""\
35-
SELECT name || ' ' FROM \"{schema}\".sqlite_master
43+
SELECT name || ' ' FROM {_quote_identifier(schema)}.sqlite_master
3644
WHERE name LIKE REPLACE(:text, '_', '^_') || '%' ESCAPE '^'"""
3745
for schema in schemata
3846
)
@@ -46,8 +54,8 @@ def _complete(con, text, state):
4654
try:
4755
select_clauses = (
4856
f"""\
49-
SELECT pti.name || ' ' FROM "{schema}".sqlite_master AS sm
50-
JOIN pragma_table_xinfo(sm.name,'{schema}') AS pti
57+
SELECT pti.name || ' ' FROM {_quote_identifier(schema)}.sqlite_master AS sm
58+
JOIN pragma_table_xinfo(sm.name,{_quote_string_literal(schema)}) AS pti
5159
WHERE sm.type='table' AND
5260
pti.name LIKE REPLACE(:text, '_', '^_') || '%' ESCAPE '^'"""
5361
for schema in schemata

Lib/test/test_sqlite3/test_cli.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,30 @@ def test_color(self):
209209
'\x1b[35mnear "sel": syntax error\x1b[0m', err)
210210

211211

212+
class Completer(unittest.TestCase):
213+
214+
def test_schema_name_with_double_quote(self):
215+
from sqlite3._completer import _complete
216+
217+
con = sqlite3.connect(":memory:")
218+
self.addCleanup(con.close)
219+
con.execute("ATTACH ? AS ?", (":memory:", 'double"quote'))
220+
con.execute('CREATE TABLE [double"quote].table_match(value)')
221+
self.assertEqual(_complete(con, "table_", 0), "table_match ")
222+
223+
@unittest.skipIf(sqlite3.sqlite_version_info < (3, 16, 0),
224+
"PRAGMA table-valued function is not available until "
225+
"SQLite 3.16.0")
226+
def test_schema_name_with_single_quote(self):
227+
from sqlite3._completer import _complete
228+
229+
con = sqlite3.connect(":memory:")
230+
self.addCleanup(con.close)
231+
con.execute("ATTACH ? AS ?", (":memory:", "single'quote"))
232+
con.execute("CREATE TABLE [single'quote].other(column_match)")
233+
self.assertEqual(_complete(con, "column_", 0), "column_match ")
234+
235+
212236
@requires_subprocess()
213237
@force_not_colorized_test_class
214238
class Completion(unittest.TestCase):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix sqlite3 CLI completion for attached database names containing quotes.

0 commit comments

Comments
 (0)