-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-133390: Support SQL keyword completion for sqlite3 CLI #133393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 28 commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
1b96be3
Support basic completion for sqlite3 command-line interface
tanloong 5e50871
Add news entry
tanloong c1941cb
Move completion code to separate module
tanloong 47daca5
Update Lib/sqlite3/_completer.py
tanloong c54c2f6
Update Doc/whatsnew/3.14.rst
tanloong 8fff491
Add test
tanloong a766805
Move keyword list to module level
tanloong da55014
Remove whatsnew entry from 3.14
tanloong ca587e0
Avoid regeneration of candidates. Store them when state is 0 and returns
tanloong 311b4f3
Add whatsnew entry to 3.15
tanloong 70f46e9
Address Bénédikt's review
tanloong 9d03730
Remove color handling of output; If CI fails might need to add back
tanloong bfcff38
Fix `run_pty()` doesn't return and test hangs
tanloong 805d997
Revert "Remove color handling of output; If CI fails might need to ad…
tanloong 276b4a7
Turn off colored-completion-prefix for readline
tanloong 09eeac8
No need to pass "NO_COLOR" to `run_pty()`
tanloong fc57d71
Flip name
tanloong c508069
Triggering completion on Ubuntu requires 2 tabs
tanloong 231b9e7
Move KEYWORDS to C
tanloong 121b069
Improve style of C code
tanloong 90a86cf
Improve tests
tanloong 5170733
Address Bénédikt's review
tanloong b40982a
Revert "Improve style of C code"
tanloong 226ea9f
Revert "Move KEYWORDS to C"
tanloong 4eebbd9
Read keyword names dynamically
encukou 3f9b2c1
Check candidates against KEYWORDS
tanloong 0410fa2
Use slice to get candidates
tanloong bd0b9ce
Address Bénédikt's review
tanloong 35a17e7
Make candidates tuple
tanloong 3dd16b3
Revert "Revert "Move KEYWORDS to C""
tanloong f3ea951
Revert "Revert "Improve style of C code""
tanloong a493ad3
Merge pull request #2 from encukou/sqlite3-cli-completion
tanloong 34cfc78
Fix 'KEYWORDS' not found
tanloong 477b48b
Sort keywords before checking the equality
tanloong 68bb4f3
Fix comparing between tuple and list
tanloong 4c3b122
Fix comparing between tuple and list
tanloong 4f1221e
Rename 'test_completion_order' to 'test_completion_for_nothing'
tanloong 3865131
Don't decrease reference for `PyModule_Add()` and `PyTuple_SetItem()`
tanloong 8d4f659
Merge branch 'main' into sqlite3-cli-completion
encukou ccd98a5
Add @force_not_colorized_test_class
tanloong d681425
Merge branch 'main' into sqlite3-cli-completion
encukou ffd0f02
Add two '\b\b'; Skip tests on FreeBSD
tanloong 6188a6d
Amend skipping reason
tanloong 370dd8b
Remove comment 'set the keyword tuple'
tanloong 16b1674
Disable keyword completion for SQLite<3.24.0
tanloong ea108ba
Don't disable the whole completion in case there will be more completion
tanloong 13b527e
Use compile-time check
tanloong fafd1bb
Correct #if usage
tanloong 140818c
Wrap add_keyword_tuple() definition and its call in #if/#endif
tanloong fd6c89e
Suggestions to python/cpython#133393
erlend-aasland 588fb6a
Merge pull request #3 from erlend-aasland/suggestion
tanloong 5623f16
Merge branch 'main' into sqlite3-cli-completion
erlend-aasland 88c8d59
Update Doc/whatsnew/3.15.rst
erlend-aasland b3a2b88
Merge branch 'main' into sqlite3-cli-completion
erlend-aasland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from contextlib import contextmanager | ||
|
||
|
||
KEYWORDS = ("ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ALWAYS", | ||
"ANALYZE", "AND", "AS", "ASC", "ATTACH", "AUTOINCREMENT", | ||
"BEFORE", "BEGIN", "BETWEEN", "BY", "CASCADE", "CASE", "CAST", | ||
"CHECK", "COLLATE", "COLUMN", "COMMIT", "CONFLICT", | ||
"CONSTRAINT", "CREATE", "CROSS", "CURRENT", "CURRENT_DATE", | ||
"CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", | ||
"DEFERRABLE", "DEFERRED", "DELETE", "DESC", "DETACH", | ||
"DISTINCT", "DO", "DROP", "EACH", "ELSE", "END", "ESCAPE", | ||
"EXCEPT", "EXCLUDE", "EXCLUSIVE", "EXISTS", "EXPLAIN", "FAIL", | ||
"FILTER", "FIRST", "FOLLOWING", "FOR", "FOREIGN", "FROM", | ||
"FULL", "GENERATED", "GLOB", "GROUP", "GROUPS", "HAVING", "IF", | ||
"IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", | ||
"INNER", "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", | ||
"ISNULL", "JOIN", "KEY", "LAST", "LEFT", "LIKE", "LIMIT", | ||
"MATCH", "MATERIALIZED", "NATURAL", "NO", "NOT", "NOTHING", | ||
"NOTNULL", "NULL", "NULLS", "OF", "OFFSET", "ON", "OR", | ||
"ORDER", "OTHERS", "OUTER", "OVER", "PARTITION", "PLAN", | ||
"PRAGMA", "PRECEDING", "PRIMARY", "QUERY", "RAISE", "RANGE", | ||
"RECURSIVE", "REFERENCES", "REGEXP", "REINDEX", "RELEASE", | ||
"RENAME", "REPLACE", "RESTRICT", "RETURNING", "RIGHT", | ||
"ROLLBACK", "ROW", "ROWS", "SAVEPOINT", "SELECT", "SET", | ||
"TABLE", "TEMP", "TEMPORARY", "THEN", "TIES", "TO", | ||
"TRANSACTION", "TRIGGER", "UNBOUNDED", "UNION", "UNIQUE", | ||
"UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"WHEN", "WHERE", "WINDOW", "WITH", "WITHOUT") | ||
|
||
_completion_matches = [] | ||
|
||
def _complete(text, state): | ||
global _completion_matches | ||
if state == 0: | ||
text_upper = text.upper() | ||
_completion_matches = [c for c in KEYWORDS if c.startswith(text_upper)] | ||
try: | ||
return _completion_matches[state] + " " | ||
except IndexError: | ||
return None | ||
|
||
|
||
@contextmanager | ||
def enable_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" | ||
tanloong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
command_string = "bind ^I rl_complete" | ||
else: | ||
command_string = "tab: complete" | ||
erlend-aasland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
readline.parse_and_bind(command_string) | ||
yield | ||
finally: | ||
readline.set_completer(old_completer) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
"""sqlite3 CLI tests.""" | ||
import sqlite3 | ||
import textwrap | ||
import unittest | ||
|
||
from sqlite3.__main__ import main as cli | ||
from sqlite3._completer import KEYWORDS | ||
from test.support.import_helper import import_module | ||
from test.support.os_helper import TESTFN, unlink | ||
from test.support.pty_helper import run_pty | ||
from test.support import ( | ||
captured_stdout, | ||
captured_stderr, | ||
captured_stdin, | ||
force_not_colorized_test_class, | ||
requires_subprocess, | ||
) | ||
|
||
|
||
|
@@ -200,5 +205,76 @@ def test_color(self): | |
self.assertIn('\x1b[1;35mOperationalError (SQLITE_ERROR)\x1b[0m: ' | ||
'\x1b[35mnear "sel": syntax error\x1b[0m', err) | ||
|
||
|
||
@requires_subprocess() | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class CompletionTest(unittest.TestCase): | ||
tanloong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PS1 = "sqlite> " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is no longer freely customizable by users via sys.ps1 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain your remark. Do you want changes? |
||
|
||
@classmethod | ||
def setUpClass(cls): | ||
readline = import_module("readline") | ||
if readline.backend == "editline": | ||
raise unittest.SkipTest("libedit readline is not supported") | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def write_input(self, input, env=None): | ||
script = textwrap.dedent(""" | ||
import readline | ||
readline.parse_and_bind("set colored-completion-prefix off") | ||
from sqlite3.__main__ import main; main() | ||
""") | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return run_pty(script, input, env) | ||
|
||
def test_keyword_completion(self): | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# List candidates starting with 'S', there should be multiple matches. | ||
input = b"S\t\tEL\t 1;\n.quit\n" | ||
output = self.write_input(input) | ||
self.assertIn(b"SELECT", output) | ||
self.assertIn(b"SET", output) | ||
self.assertIn(b"SAVEPOINT", output) | ||
self.assertIn(b"(1,)", output) | ||
|
||
# Keywords are completed in upper case for even lower case user input | ||
input = b"sel\t\t 1;\n.quit\n" | ||
output = self.write_input(input) | ||
self.assertIn(b"SELECT", output) | ||
self.assertIn(b"(1,)", output) | ||
|
||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_nothing_to_complete(self): | ||
input = b"xyzzy\t\t\b\b\b\b\b.quit\n" | ||
# set NO_COLOR to disable coloring for self.PS1 | ||
output = self.write_input(input, env={"NO_COLOR": "1"}) | ||
output_lines = output.decode().splitlines() | ||
line_num = next((i for i, line in enumerate(output_lines, 1) | ||
if line.startswith(f"{self.PS1}xyzzy")), -1) | ||
self.assertNotEqual(line_num, -1) | ||
# completions occupy lines, assert no extra lines when there is nothing | ||
# to complete | ||
self.assertEqual(line_num, len(output_lines)) | ||
|
||
def test_completion_order(self): | ||
script = textwrap.dedent(""" | ||
import readline | ||
readline.parse_and_bind("set colored-completion-prefix off") | ||
# hide control sequences surrounding each candidate | ||
readline.parse_and_bind("set colored-stats off") | ||
# hide "Display all xxx possibilities? (y or n)" | ||
readline.parse_and_bind("set completion-query-items 0") | ||
# hide "--More--" | ||
readline.parse_and_bind("set page-completions off") | ||
# show candidates one per line | ||
readline.parse_and_bind("set completion-display-width 0") | ||
from sqlite3.__main__ import main; main() | ||
""") | ||
input = b"\t\t.quit\n" | ||
output = run_pty(script, input, env={"NO_COLOR": "1"}) | ||
output_lines = output.decode().splitlines() | ||
indices = [i for i, line in enumerate(output_lines) | ||
if line.startswith(self.PS1)] | ||
self.assertEqual(len(indices), 2) | ||
start, end = indices[0] + 1, indices[1] | ||
candidates = tuple(map(str.strip, output_lines[start:end])) | ||
self.assertEqual(candidates, KEYWORDS) | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
if __name__ == "__main__": | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2025-05-05-03-14-08.gh-issue-133390.AuTggn.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support keyword completion in the :mod:`sqlite3` command-line interface. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.