mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
gh-133390: Support SQL keyword completion for sqlite3 CLI (#133393)
This commit is contained in:
parent
b22b964a5c
commit
62b3d2d443
7 changed files with 193 additions and 6 deletions
|
@ -12,6 +12,8 @@ from code import InteractiveConsole
|
|||
from textwrap import dedent
|
||||
from _colorize import get_theme, theme_no_color
|
||||
|
||||
from ._completer import completer
|
||||
|
||||
|
||||
def execute(c, sql, suppress_errors=True, theme=theme_no_color):
|
||||
"""Helper that wraps execution of SQL code.
|
||||
|
@ -136,12 +138,9 @@ def main(*args):
|
|||
execute(con, args.sql, suppress_errors=False, theme=theme)
|
||||
else:
|
||||
# No SQL provided; start the REPL.
|
||||
console = SqliteInteractiveConsole(con, use_color=True)
|
||||
try:
|
||||
import readline # noqa: F401
|
||||
except ImportError:
|
||||
pass
|
||||
console.interact(banner, exitmsg="")
|
||||
with completer():
|
||||
console = SqliteInteractiveConsole(con, use_color=True)
|
||||
console.interact(banner, exitmsg="")
|
||||
finally:
|
||||
con.close()
|
||||
|
||||
|
|
42
Lib/sqlite3/_completer.py
Normal file
42
Lib/sqlite3/_completer.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
from contextlib import contextmanager
|
||||
|
||||
try:
|
||||
from _sqlite3 import SQLITE_KEYWORDS
|
||||
except ImportError:
|
||||
SQLITE_KEYWORDS = ()
|
||||
|
||||
_completion_matches = []
|
||||
|
||||
|
||||
def _complete(text, state):
|
||||
global _completion_matches
|
||||
|
||||
if state == 0:
|
||||
text_upper = text.upper()
|
||||
_completion_matches = [c for c in SQLITE_KEYWORDS if c.startswith(text_upper)]
|
||||
try:
|
||||
return _completion_matches[state] + " "
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
|
||||
@contextmanager
|
||||
def completer():
|
||||
try:
|
||||
import readline
|
||||
except ImportError:
|
||||
yield
|
||||
return
|
||||
|
||||
old_completer = readline.get_completer()
|
||||
try:
|
||||
readline.set_completer(_complete)
|
||||
if readline.backend == "editline":
|
||||
# libedit uses "^I" instead of "tab"
|
||||
command_string = "bind ^I rl_complete"
|
||||
else:
|
||||
command_string = "tab: complete"
|
||||
readline.parse_and_bind(command_string)
|
||||
yield
|
||||
finally:
|
||||
readline.set_completer(old_completer)
|
Loading…
Add table
Add a link
Reference in a new issue