Repository: macbre/sql-metadata

Issue:
parse does not support `truncate table <tablename>` queries
```
import sql_metadata
sql_metadata.parser.Parser('truncate table foo.bar.baz').tokens
```
 results in 
```
----> 1 sql_metadata.parser.Parser('truncate table foo.bar.baz').tokens

File /turbo/tsantos/pyenvs/ni.bau_38/lib/python3.8/site-packages/sql_metadata/parser.py:179, in Parser.tokens(self)
    174 self._tokens = tokens
    175 # since tokens are used in all methods required parsing (so w/o generalization)
    176 # we set the query type here (and not in init) to allow for generalization
    177 # but disallow any other usage for not supported queries to avoid unexpected
    178 # results which are not really an error
--> 179 _ = self.query_type
    180 return tokens

File /turbo/tsantos/pyenvs/ni.bau_38/lib/python3.8/site-packages/sql_metadata/parser.py:122, in Parser.query_type(self)
    120 if self._query_type == "UNSUPPORTED":
    121     self._logger.error("Not supported query type: %s", self._raw_query)
--> 122     raise ValueError("Not supported query type!")
    123 return self._query_type

ValueError: Not supported query type!
```

Discussion:
T-Santos: @macbre please let me know if there's anything else needed from me to move this forward. thank you 


Patch:
diff --git a/sql_metadata/keywords_lists.py b/sql_metadata/keywords_lists.py
index 8521c29..4abeaea 100644
--- a/sql_metadata/keywords_lists.py
+++ b/sql_metadata/keywords_lists.py
@@ -81,6 +81,7 @@ class QueryType(str, Enum):
     CREATE = "CREATE TABLE"
     ALTER = "ALTER TABLE"
     DROP = "DROP TABLE"
+    TRUNCATE = "TRUNCATE TABLE"
 
 
 class TokenType(str, Enum):
@@ -109,6 +110,7 @@ SUPPORTED_QUERY_TYPES = {
     "ALTERTABLE": QueryType.ALTER,
     "DROPTABLE": QueryType.DROP,
     "CREATEFUNCTION": QueryType.CREATE,
+    "TRUNCATETABLE": QueryType.TRUNCATE,
 }
 
 # all the keywords we care for - rest is ignored in assigning
diff --git a/sql_metadata/parser.py b/sql_metadata/parser.py
index 64f4021..d003c54 100644
--- a/sql_metadata/parser.py
+++ b/sql_metadata/parser.py
@@ -116,7 +116,7 @@ class Parser:  # pylint: disable=R0902
         )
         if tokens[index].normalized == "CREATE":
             switch = self._get_switch_by_create_query(tokens, index)
-        elif tokens[index].normalized in ("ALTER", "DROP"):
+        elif tokens[index].normalized in ("ALTER", "DROP", "TRUNCATE"):
             switch = tokens[index].normalized + tokens[index + 1].normalized
         else:
             switch = tokens[index].normalized


Test patch:
diff --git a/test/test_truncate_table.py b/test/test_truncate_table.py
new file mode 100644
index 0000000..3662dbb
--- /dev/null
+++ b/test/test_truncate_table.py
@@ -0,0 +1,9 @@
+from sql_metadata import Parser
+from sql_metadata.keywords_lists import QueryType
+
+
+def test_truncate_table():
+    parser = Parser("TRUNCATE TABLE foo")
+    assert parser.query_type == QueryType.TRUNCATE
+    assert parser.tables == ["foo"]
+    assert parser.columns == []