Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/support for truncate table query parsing (#1) #521

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sql_metadata/keywords_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class QueryType(str, Enum):
CREATE = "CREATE TABLE"
ALTER = "ALTER TABLE"
DROP = "DROP TABLE"
TRUNCATE = "TRUNCATE TABLE"


class TokenType(str, Enum):
Expand Down Expand Up @@ -109,6 +110,7 @@ class TokenType(str, Enum):
"ALTERTABLE": QueryType.ALTER,
"DROPTABLE": QueryType.DROP,
"CREATEFUNCTION": QueryType.CREATE,
"TRUNCATETABLE": QueryType.TRUNCATE,
}

# all the keywords we care for - rest is ignored in assigning
Expand Down
2 changes: 1 addition & 1 deletion sql_metadata/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def query_type(self) -> str:
)
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
Expand Down
9 changes: 9 additions & 0 deletions test/test_truncate_table.py
Original file line number Diff line number Diff line change
@@ -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 == []