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

Handle leading comments in queries #164

Merged
merged 7 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 13 additions & 3 deletions sql_metadata/parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module provides SQL query parsing functions
"""
import logging
import re
from typing import Dict, List, Optional, Tuple, Union

Expand Down Expand Up @@ -29,6 +30,8 @@ class Parser: # pylint: disable=R0902
"""

def __init__(self, sql: str = "") -> None:
self._logger = logging.getLogger(self.__class__.__name__)

self._raw_query = sql
self._query = self._preprocess_query()
self._query_type = None
Expand Down Expand Up @@ -85,12 +88,19 @@ def query_type(self) -> str:
return self._query_type
if not self._tokens:
_ = self.tokens
if self._tokens[0].normalized in ["CREATE", "ALTER"]:
switch = self._tokens[0].normalized + self._tokens[1].normalized

# remove comment tokens to not confuse the logic below (see #163)
tokens: List[SQLToken] = list(
filter(lambda token: not token.is_comment, self._tokens)
)

if tokens[0].normalized in ["CREATE", "ALTER"]:
switch = tokens[0].normalized + tokens[1].normalized
else:
switch = self._tokens[0].normalized
switch = tokens[0].normalized
self._query_type = SUPPORTED_QUERY_TYPES.get(switch, "UNSUPPORTED")
if self._query_type == "UNSUPPORTED":
self._logger.error("Not supported query type: %s", self._raw_query)
raise ValueError("Not supported query type!")
return self._query_type

Expand Down
10 changes: 10 additions & 0 deletions test/test_getting_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,13 @@ def test_get_tables_with_leading_digits():
assert ["0020_big_table"] == Parser(
"SELECT t.val as value, count(*) FROM 0020_big_table"
).tables


def test_insert_ignore_with_comments():
assert ["bar"] == Parser(
"INSERT IGNORE /* foo */ INTO bar VALUES (1, '123', '2017-01-01');"
).tables

assert ["bar"] == Parser(
"/* foo */ INSERT IGNORE INTO bar VALUES (1, '123', '2017-01-01');"
).tables
macbre marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion test/test_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def test_getting_values():
}

parser = Parser(
"INSERT IGNORE INTO `0070_insert_ignore_table` VALUES (9, 2.15, '123', '2017-01-01');"
"/* method */ INSERT IGNORE INTO `0070_insert_ignore_table` VALUES (9, 2.15, '123', '2017-01-01');"
)
assert parser.query_type == "INSERT"
assert parser.values == [9, 2.15, "123", "2017-01-01"]
assert parser.values_dict == {
"column_1": 9,
Expand Down