Skip to content

Commit c743049

Browse files
committed
Add .tables for sqlite3 command-line interface
1 parent b14986c commit c743049

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lib/sqlite3/__main__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,28 @@ def runsource(self, source, filename="<input>", symbol="single"):
6262
return False
6363
if source[0] == ".":
6464
match source[1:].strip():
65+
case "tables":
66+
schema_names = tuple(row[1]
67+
for row in self._cur.execute("PRAGMA database_list"))
68+
select_clauses = (f"""SELECT
69+
CASE '{schema}'
70+
WHEN 'main' THEN name
71+
ELSE CONCAT('{schema}.', name)
72+
END
73+
FROM "{schema}".sqlite_master
74+
WHERE type IN ('table', 'view')
75+
AND name NOT LIKE 'sqlite_%'"""
76+
for schema in schema_names
77+
)
78+
command = " UNION ALL ".join(select_clauses) + " ORDER BY 1"
79+
for row in self._cur.execute(command):
80+
print(row[0])
6581
case "version":
6682
print(sqlite3.sqlite_version)
6783
case "help":
6884
t = theme.syntax
6985
print(f"Enter SQL code or one of the below commands, and press enter.\n\n"
86+
f"{t.builtin}.tables{t.reset} List names of tables\n"
7087
f"{t.builtin}.version{t.reset} Print underlying SQLite library version\n"
7188
f"{t.builtin}.help{t.reset} Print this help message\n"
7289
f"{t.builtin}.quit{t.reset} Exit the CLI, equivalent to CTRL-D\n")

Lib/test/test_sqlite3/test_cli.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ def test_interact_version(self):
125125
self.assertEqual(out.count(self.PS2), 0)
126126
self.assertIn(sqlite3.sqlite_version, out)
127127

128+
def test_interact_tables(self):
129+
out, err = self.run_cli(commands=(
130+
"CREATE TABLE table_ (id INTEGER);",
131+
"CREATE TEMP TABLE temp_table (id INTEGER);",
132+
"CREATE VIEW view_ AS SELECT * FROM table_;",
133+
"CREATE TEMP VIEW temp_view As SELECT * FROM table_;",
134+
"ATTACH ':memory:' AS attach_;",
135+
"CREATE TABLE attach_.table_ (id INTEGER);",
136+
"CREATE VIEW attach_.view_ AS SELECT * FROM table_;",
137+
"ATTACH ':memory:' AS 123;",
138+
"CREATE TABLE \"123\".table_ (id INTEGER);",
139+
"CREATE VIEW \"123\".view_ AS SELECT * FROM table_;",
140+
".tables",
141+
))
142+
self.assertIn(self.MEMORY_DB_MSG, err)
143+
self.assertEndsWith(out, self.PS1)
144+
self.assertEqual(out.count(self.PS1), 12)
145+
self.assertEqual(out.count(self.PS2), 0)
146+
self.assertIn("123.table_\n123.view_\nattach_.table_\nattach_.view_\n"
147+
"table_\ntemp.temp_table\ntemp.temp_view\nview_\n", out)
148+
128149
def test_interact_empty_source(self):
129150
out, err = self.run_cli(commands=("", " "))
130151
self.assertIn(self.MEMORY_DB_MSG, err)

0 commit comments

Comments
 (0)