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='table' AND
pti.name LIKE REPLACE(:text, '_', '^_') || '%' ESCAPE '^'"""
for schema in schemata
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_sqlite3/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,30 @@ def test_color(self):
'\x1b[35mnear "sel": syntax error\x1b[0m', err)


class Completer(unittest.TestCase):

@lkk7 lkk7 Jul 15, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new test class because the existing class below is a bit too "end-to-end", while those tests just test the _complete function.


def test_schema_name_with_double_quote(self):
from sqlite3._completer import _complete

con = sqlite3.connect(":memory:")
self.addCleanup(con.close)
con.execute("ATTACH ? AS ?", (":memory:", 'double"quote'))
con.execute('CREATE TABLE [double"quote].table_match(value)')
self.assertEqual(_complete(con, "table_", 0), "table_match ")

@unittest.skipIf(sqlite3.sqlite_version_info < (3, 16, 0),
"PRAGMA table-valued function is not available until "
"SQLite 3.16.0")
def test_schema_name_with_single_quote(self):
from sqlite3._completer import _complete

con = sqlite3.connect(":memory:")
self.addCleanup(con.close)
con.execute("ATTACH ? AS ?", (":memory:", "single'quote"))
con.execute("CREATE TABLE [single'quote].other(column_match)")
self.assertEqual(_complete(con, "column_", 0), "column_match ")


@requires_subprocess()
@force_not_colorized_test_class
class Completion(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix sqlite3 CLI completion for attached database names containing quotes.
Loading