Repository: macbre/sql-metadata

Issue:
Issue with table extraction for delete and drop query type
hi, 

Do you know why or when the above query type (delete, drop) should be supported? Or why it's not supported by now?

thanks.

Discussion:

Patch:
diff --git a/sql_metadata/keywords_lists.py b/sql_metadata/keywords_lists.py
index b20e12d..3414f1c 100644
--- a/sql_metadata/keywords_lists.py
+++ b/sql_metadata/keywords_lists.py
@@ -76,6 +76,7 @@ class QueryType(str, Enum):
     SELECT = "SELECT"
     CREATE = "CREATE TABLE"
     ALTER = "ALTER TABLE"
+    DROP = "DROP TABLE"
 
 
 class TokenType(str, Enum):
@@ -102,6 +103,7 @@ SUPPORTED_QUERY_TYPES = {
     "WITH": QueryType.SELECT,
     "CREATETABLE": QueryType.CREATE,
     "ALTERTABLE": QueryType.ALTER,
+    "DROPTABLE": QueryType.DROP,
 }
 
 # all the keywords we care for - rest is ignored in assigning
diff --git a/sql_metadata/parser.py b/sql_metadata/parser.py
index 03f72d8..d4fd473 100644
--- a/sql_metadata/parser.py
+++ b/sql_metadata/parser.py
@@ -112,7 +112,7 @@ class Parser:  # pylint: disable=R0902
             )
             .position
         )
-        if tokens[index].normalized in ["CREATE", "ALTER"]:
+        if tokens[index].normalized in ["CREATE", "ALTER", "DROP"]:
             switch = tokens[index].normalized + tokens[index + 1].normalized
         else:
             switch = tokens[index].normalized


Test patch:
diff --git a/test/test_drop_table.py b/test/test_drop_table.py
new file mode 100644
index 0000000..8764020
--- /dev/null
+++ b/test/test_drop_table.py
@@ -0,0 +1,9 @@
+from sql_metadata import Parser
+from sql_metadata.keywords_lists import QueryType
+
+
+def test_drop_table():
+    parser = Parser("DROP TABLE foo")
+    assert parser.query_type == QueryType.DROP
+    assert parser.tables == ["foo"]
+    assert parser.columns == []
diff --git a/test/test_query_type.py b/test/test_query_type.py
index 15f9fae..44b8f33 100644
--- a/test/test_query_type.py
+++ b/test/test_query_type.py
@@ -38,6 +38,16 @@ def test_delete_query():
             assert "DELETE" == Parser(query.format(comment)).query_type
 
 
+def test_drop_table_query():
+    queries = [
+        "{0}DROP TABLE foo;{0}",
+    ]
+
+    for query in queries:
+        for comment in ["", "/* foo */", "\n--foo\n", "\n# foo\n"]:
+            assert "DROP TABLE" == Parser(query.format(comment)).query_type
+
+
 def test_unsupported_query():
     queries = [
         "FOO BAR",