Skip to content

Commit bac35fa

Browse files
committed
Add .indexes
1 parent 9924bc5 commit bac35fa

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

Lib/sqlite3/__main__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,21 @@ def runsource(self, source, filename="<input>", symbol="single"):
6262
return False
6363
# Remember to update CLI_COMMANDS in _completer.py
6464
if source[0] == ".":
65-
match source[1:].strip():
66-
case "tables":
65+
cmd_name = source[1:].strip()
66+
match cmd_name:
67+
case "tables" | "indices" | "indexes":
6768
schema_names = (row[1]
6869
for row in self._cur.execute("PRAGMA database_list"))
70+
where_clause = ("""WHERE type IN ('table', 'view')
71+
AND name NOT LIKE 'sqlite^_%' ESCAPE '^'"""
72+
if cmd_name == "tables" else "WHERE type='index'"
73+
)
6974
select_clauses = (f"""SELECT
7075
CASE '{schema}'
7176
WHEN 'main' THEN name
7277
ELSE '{schema}.' || name
7378
END
74-
FROM "{schema}".sqlite_master
75-
WHERE type IN ('table', 'view')
76-
AND name NOT LIKE 'sqlite^_%' ESCAPE '^'"""
79+
FROM "{schema}".sqlite_master {where_clause}"""
7780
for schema in schema_names
7881
)
7982
command = " UNION ALL ".join(select_clauses) + " ORDER BY 1"
@@ -85,6 +88,8 @@ def runsource(self, source, filename="<input>", symbol="single"):
8588
t = theme.syntax
8689
print(f"Enter SQL code or one of the below commands, and press enter.\n\n"
8790
f"{t.builtin}.tables{t.reset} Print names of tables\n"
91+
f"{t.builtin}.indexes{t.reset} Print names of indexes\n"
92+
f"{t.builtin}.indices{t.reset} Print names of indices\n"
8893
f"{t.builtin}.version{t.reset} Print underlying SQLite library version\n"
8994
f"{t.builtin}.help{t.reset} Print this help message\n"
9095
f"{t.builtin}.quit{t.reset} Exit the CLI, equivalent to CTRL-D\n")

Lib/sqlite3/_completer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
except ImportError:
66
SQLITE_KEYWORDS = ()
77

8-
CLI_COMMANDS = ('.quit', '.help', '.version', '.tables')
8+
CLI_COMMANDS = ('.quit', '.help', '.version', '.tables', '.indices', '.indexes')
99

1010
_completion_matches = []
1111

Lib/test/test_sqlite3/test_cli.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ def test_interact_tables(self):
129129
out, err = self.run_cli(commands=(
130130
"CREATE TABLE table_ (id INTEGER);",
131131
"CREATE TABLE sqlitee (id INTEGER);",
132-
"CREATE TEMP TABLE temp_table (id INTEGER);",
132+
"CREATE TEMP TABLE table_ (id INTEGER);",
133133
"CREATE VIEW view_ AS SELECT 1;",
134-
"CREATE TEMP VIEW temp_view AS SELECT 1;",
134+
"CREATE TEMP VIEW view_ AS SELECT 1;",
135135
"ATTACH ':memory:' AS attach_;",
136136
"CREATE TABLE attach_.table_ (id INTEGER);",
137137
"CREATE VIEW attach_.view_ AS SELECT 1;",
@@ -150,10 +150,34 @@ def test_interact_tables(self):
150150
"attach_.view_",
151151
"sqlitee",
152152
"table_",
153-
"temp.temp_table",
154-
"temp.temp_view",
153+
"temp.table_",
154+
"temp.view_",
155155
"view_")
156-
self.assertIn("\n".join(tables), out)
156+
self.assertEqual("\n".join(tables), out.replace(self.PS1, "").strip())
157+
158+
def test_interact_indexes(self):
159+
out, err = self.run_cli(commands=(
160+
"CREATE TABLE table_ (id INTEGER);",
161+
"CREATE INDEX idx_table_ ON table_ (id);",
162+
"CREATE TEMP TABLE temp_table (id INTEGER);",
163+
"CREATE INDEX temp.idx_temp_table_ ON temp_table (id);",
164+
"ATTACH ':memory:' AS attach_;",
165+
"CREATE TABLE attach_.attach_table (id INTEGER);",
166+
"CREATE INDEX attach_.idx_attach_table ON attach_table (id);",
167+
".indexes",
168+
".indices",
169+
))
170+
self.assertIn(self.MEMORY_DB_MSG, err)
171+
self.assertEndsWith(out, self.PS1)
172+
self.assertEqual(out.count(self.PS1), 10)
173+
self.assertEqual(out.count(self.PS2), 0)
174+
expected = ("attach_.idx_attach_table",
175+
"idx_table_",
176+
"temp.idx_temp_table_",
177+
"attach_.idx_attach_table",
178+
"idx_table_",
179+
"temp.idx_temp_table_")
180+
self.assertEqual("\n".join(expected), out.replace(self.PS1, "").strip())
157181

158182
def test_interact_empty_source(self):
159183
out, err = self.run_cli(commands=("", " "))

0 commit comments

Comments
 (0)