Skip to content
Closed
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
11 changes: 8 additions & 3 deletions Lib/sqlite3/_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
_completion_matches = []


def _quote_schema(schema):
# Quote an SQLite identifier, doubling embedded double quotes.
return '"' + schema.replace('"', '""') + '"'


def _complete(con, text, state):
global _completion_matches

Expand All @@ -32,7 +37,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_schema(schema)}.sqlite_master
WHERE name LIKE REPLACE(:text, '_', '^_') || '%' ESCAPE '^'"""
for schema in schemata
)
Expand All @@ -46,8 +51,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_schema(schema)}.sqlite_master AS sm
JOIN pragma_table_xinfo(sm.name,{_quote_schema(schema)}) AS pti
WHERE sm.type='table' AND
pti.name LIKE REPLACE(:text, '_', '^_') || '%' ESCAPE '^'"""
for schema in schemata
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_sqlite3/test_completer.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please just add it to existing tests.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import sqlite3
import unittest
from sqlite3 import _completer


class CompleterTests(unittest.TestCase):
def test_schema_with_double_quote(self):
con = sqlite3.connect(':memory:')
self.addCleanup(con.close)
con.execute('ATTACH DATABASE \':memory:\' AS \'weird"name\'')
matches = []
state = 0
while True:
match = _completer._complete(con, 'a', state)
if match is None:
break
matches.append(match)
state += 1
self.assertTrue(matches)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :mod:`sqlite3` completer to handle attached database names
containing a double quote. Patch by Tony Leung.
Loading