Repository: dbcli/litecli (language: python)

Issue:
Feature request for `.indexes` command support.



Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 06a4a9e..93b2a70 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
 ### Features
 
 * Add support for ANSI escape sequences for coloring the prompt.
+* Add support for `.indexes` command.
 * Add an option to turn off the auto-completion menu. Completion menu can be
   triggered by pressed the `<tab>` key when this option is set to False. Fixes
   [#105](https://github.com/dbcli/litecli/issues/105).
diff --git a/litecli/packages/completion_engine.py b/litecli/packages/completion_engine.py
index 0397857..0e2a30f 100644
--- a/litecli/packages/completion_engine.py
+++ b/litecli/packages/completion_engine.py
@@ -105,14 +105,14 @@ def suggest_special(text):
     if cmd in ["\\f", "\\fs", "\\fd"]:
         return [{"type": "favoritequery"}]
 
-    if cmd in ["\\d", "\\dt", "\\dt+", ".schema"]:
+    if cmd in ["\\d", "\\dt", "\\dt+", ".schema", ".indexes"]:
         return [
             {"type": "table", "schema": []},
             {"type": "view", "schema": []},
             {"type": "schema"},
         ]
 
-    if cmd in ["\\.", "source", ".open"]:
+    if cmd in ["\\.", "source", ".open", ".read"]:
         return [{"type": "file_name"}]
 
     if cmd in [".import"]:
diff --git a/litecli/packages/special/dbcommands.py b/litecli/packages/special/dbcommands.py
index 15db578..203e1a8 100644
--- a/litecli/packages/special/dbcommands.py
+++ b/litecli/packages/special/dbcommands.py
@@ -110,6 +110,41 @@ def list_databases(cur, **_):
         return [(None, None, None, "")]
 
 
+@special_command(
+    ".indexes",
+    ".indexes [tablename]",
+    "List indexes.",
+    arg_type=PARSED_QUERY,
+    case_sensitive=True,
+    aliases=("\\di",),
+)
+def list_indexes(cur, arg=None, arg_type=PARSED_QUERY, verbose=False):
+    if arg:
+        args = ("{0}%".format(arg),)
+        query = """
+            SELECT name FROM sqlite_master
+            WHERE type = 'index' AND tbl_name LIKE ? AND name NOT LIKE 'sqlite_%'
+            ORDER BY 1
+        """
+    else:
+        args = tuple()
+        query = """
+            SELECT name FROM sqlite_master
+            WHERE type = 'index' AND name NOT LIKE 'sqlite_%'
+            ORDER BY 1
+        """
+
+    log.debug(query)
+    cur.execute(query, args)
+    indexes = cur.fetchall()
+    status = ""
+    if cur.description:
+        headers = [x[0] for x in cur.description]
+    else:
+        return [(None, None, None, "")]
+    return [(None, indexes, headers, status)]
+
+
 @special_command(
     ".status",
     "\\s",
diff --git a/litecli/sqlexecute.py b/litecli/sqlexecute.py
index 2b8fce3..3f78d49 100644
--- a/litecli/sqlexecute.py
+++ b/litecli/sqlexecute.py
@@ -38,6 +38,13 @@ class SQLExecute(object):
         ORDER BY tableName, columnName
     """
 
+    indexes_query = """
+        SELECT name
+        FROM sqlite_master
+        WHERE type = 'index' AND name NOT LIKE 'sqlite_%'
+        ORDER BY 1
+    """
+
     functions_query = '''SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES
     WHERE ROUTINE_TYPE="FUNCTION" AND ROUTINE_SCHEMA = "%s"'''
 


Test patch:
diff --git a/tests/test_dbspecial.py b/tests/test_dbspecial.py
index c7065a9..5128b5b 100644
--- a/tests/test_dbspecial.py
+++ b/tests/test_dbspecial.py
@@ -63,3 +63,14 @@ def test_format_uptime():
 
     seconds = 522600
     assert "6 days 1 hour 10 min 0 sec" == format_uptime(seconds)
+
+
+def test_indexes():
+    suggestions = suggest_type(".indexes", ".indexes ")
+    assert sorted_dicts(suggestions) == sorted_dicts(
+        [
+            {"type": "table", "schema": []},
+            {"type": "view", "schema": []},
+            {"type": "schema"},
+        ]
+    )