Skip to content

Commit 3713f14

Browse files
T-SantosSantos, Tyler (Boston)macbre
authored
Feature/support for truncate table query parsing (#1) (#521)
* Feature/support for truncate table query parsing (#1) * failing test for truncate table * fix failing truncate table test --------- Co-authored-by: Santos, Tyler (Boston) <tyler.santos@man.com> * Update test/test_truncate_table.py Co-authored-by: Maciej Brencz <maciej.brencz@gmail.com> * Update test_truncate_table.py Add whitespace --------- Co-authored-by: Santos, Tyler (Boston) <tyler.santos@man.com> Co-authored-by: Maciej Brencz <maciej.brencz@gmail.com>
1 parent 9a78fbc commit 3713f14

3 files changed

Lines changed: 12 additions & 1 deletion

File tree

sql_metadata/keywords_lists.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class QueryType(str, Enum):
8181
CREATE = "CREATE TABLE"
8282
ALTER = "ALTER TABLE"
8383
DROP = "DROP TABLE"
84+
TRUNCATE = "TRUNCATE TABLE"
8485

8586

8687
class TokenType(str, Enum):
@@ -109,6 +110,7 @@ class TokenType(str, Enum):
109110
"ALTERTABLE": QueryType.ALTER,
110111
"DROPTABLE": QueryType.DROP,
111112
"CREATEFUNCTION": QueryType.CREATE,
113+
"TRUNCATETABLE": QueryType.TRUNCATE,
112114
}
113115

114116
# all the keywords we care for - rest is ignored in assigning

sql_metadata/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def query_type(self) -> str:
116116
)
117117
if tokens[index].normalized == "CREATE":
118118
switch = self._get_switch_by_create_query(tokens, index)
119-
elif tokens[index].normalized in ("ALTER", "DROP"):
119+
elif tokens[index].normalized in ("ALTER", "DROP", "TRUNCATE"):
120120
switch = tokens[index].normalized + tokens[index + 1].normalized
121121
else:
122122
switch = tokens[index].normalized

test/test_truncate_table.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from sql_metadata import Parser
2+
from sql_metadata.keywords_lists import QueryType
3+
4+
5+
def test_truncate_table():
6+
parser = Parser("TRUNCATE TABLE foo")
7+
assert parser.query_type == QueryType.TRUNCATE
8+
assert parser.tables == ["foo"]
9+
assert parser.columns == []

0 commit comments

Comments
 (0)