Skip to content

gh-133390: Support basic completion for sqlite3 command-line interface #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

Open
wants to merge 37 commits into
base: main
Choose a base branch
from

Conversation

tanloong
Copy link
Contributor

@tanloong tanloong commented May 4, 2025

This adds tab-completion for the 147 SQLite Keywords. A whitespace is appended to completion candidates for users' convenience and to mimic the behavior of sqlite3 tool.

@python-cla-bot
Copy link

python-cla-bot bot commented May 4, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented May 4, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Contributor

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

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

This needs a whatsnew entry, (maybe a note in the docs?), a NEWS entry and tests.

This is quite limited completion so I guess it could work.

@picnixz
Copy link
Member

picnixz commented May 4, 2025

I think we need a separate nodule for this because if we want to have a smarter completion later the code is better to be isolated, so something like sqlite3._completer.

If possible, can we have an autogenerated list of keywords? and those keywords could also be stored in a (private) module-level list.

@picnixz
Copy link
Member

picnixz commented May 4, 2025

Please add tests

@tanloong
Copy link
Contributor Author

tanloong commented May 4, 2025

Yes, tests is coming. I will try to find a way to autogenerate list of keywords.

@StanFromIreland
Copy link
Contributor

You can mark the pr as a draft.

@tanloong tanloong marked this pull request as draft May 4, 2025 21:23
@tanloong tanloong force-pushed the sqlite3-cli-completion branch 8 times, most recently from 782a599 to 5005d85 Compare May 5, 2025 04:47
@tanloong
Copy link
Contributor Author

tanloong commented May 5, 2025

Added tests and moved keyword list to module level.

Keywords seems not easy to auto-generate, the SQLite document says they can be accessed by sqlite3_keyword_count() and sqlite3_keyword_name() using C, but I am bad at C and didn't figure out how to call these two at Python level. I think these two functions are not exposed to Python?

@tanloong tanloong marked this pull request as ready for review May 5, 2025 05:08
Copy link
Contributor

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

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

With #133447 in mind the keywords list would be quite handy (I think hardcoding is fine, it doesn't change often anyway). Maybe it could be moved elsewhere, what do you think Benedikt?

@tanloong tanloong force-pushed the sqlite3-cli-completion branch from 15019ea to 22652ae Compare May 5, 2025 18:20
@tanloong
Copy link
Contributor Author

tanloong commented May 5, 2025

Hi @picnixz, as Stan mentioned, beta freeze is around the corner, but I really hope this can be shipped in 3.14, do you think there is a chance to merge it before beta freeze?

@bedevere-app
Copy link

bedevere-app bot commented May 16, 2025

Thanks for making the requested changes!

@picnixz: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested a review from picnixz May 16, 2025 08:10
@picnixz
Copy link
Member

picnixz commented May 16, 2025

The SQLITE_KEYWORDS is moved from C back to Python.

If you want it in C, you need to combine int sqlite3_keyword_count(void) and int sqlite3_keyword_name(int,const char**,int*);. Stated otherwise, you need to first count the number of keywords, then loop over this number and get the n-th keyword using sqlite3_keyword_name. But for now, let's simply keep them in Python. We'll do it in a follow-up PR (or I'll do it as the task is a bit harder)

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

You can yourself to ACKS if it's not already done.

@picnixz picnixz dismissed their stale review May 16, 2025 08:19

Changes were made.

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

On my side it looks great but I'll also want @erlend-aasland to have a look as he's the main maintainer. Maybe he'l ask for some stylistic changes (in which case I'm sorry if I suggested something wrong).


@requires_subprocess()
class CompletionTest(unittest.TestCase):
PS1 = "sqlite> "
Copy link
Contributor

Choose a reason for hiding this comment

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

This is no longer freely customizable by users via sys.ps1 ?

@tanloong
Copy link
Contributor Author

On my side it looks great but I'll also want @erlend-aasland to have a look as he's the main maintainer. Maybe he'l ask for some stylistic changes (in which case I'm sorry if I suggested something wrong).

Thank you so much for your review. Really appreciate your guidance!

@tanloong
Copy link
Contributor Author

tanloong commented May 16, 2025

The test_completion_order() fails. The SQLITE_KEYWORDS given by sqlite3_keyword_name() and sqlite3_keyword_count() is not in alphabetical order, but GNU Readline always sorts completions before displaying them.

Unfortunately I didn't find a disable-completion-sorting-like option in GNU Readline's document. There is an rl_sort_completion_matches in Readline's source code but it's not exposed as a config flag.

Can we remove this test_completion_order(), as the order of displayed candidates is controlled by GNU Readline.

@picnixz
Copy link
Member

picnixz commented May 16, 2025

Let's just check that the completion of nothing returns all keywords. You can sort the SQLITE_KEYWORDS list before checking for the equality (I think it's better not to sort it at the C level as it could be helpful in the future for someone interacting with python and C)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants