gh-135795: Support .tables
in the sqlite3 command-line interface
#135796
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.
This adds
.tables
to list table names in the current database connection.The output is made similar to that of SQLite tool to give users familiar experience. Both tables and views, whether temporary or not, are included in the output. Tables with names like
sqlite_%
(which is reserved by SQLite for internal use) are filtered out. Table names from themain
schema are shown as is, while those from other schemas are shown with a schema-name prefix. For example, temporary tabletmp
is shown astemp.tmp
.The SQLite tool allows an optional
TABLE
argument with.tables
to filter table names with LIKE pattern TABLE. This feature is not included because the SQLite standard of argument handling for dot commands is too complex, I am worried about overcomplicating the code in this single PR.A connection has three kinds of databases: the main database; the temporary database used to store temporary tables/views etc; attached databases added to current connection using the
ATTACH
command. Schema-names of all these three can be obtained byPRAGMA database_list
. For historical compatibility,{schema-name}.sqlite_master
instead of{schema-name}.sqlite_schema
is used to get table names. The CPythonsqlite3
module accepts SQLite>=3.15.2, butsqlite_schema
was not added in SQLite until 3.33.0.In the
select_clauses
generator, the inconsistent usage of double- and single-quotes is intentional. The SQL standard requires double-quotes around identifiers and single-quotes around string literals. The double quotes in"{schema}"
are to ensure thatschema
is recognized as an identifier even if user attaches a database with a numeric schema name, e.g.,ATTACH '/path/to/db' AS 123;
. This corner case is included in thetest_interact_tables()
.