fix: handle comments in `has_table_query` (#23882)

This commit is contained in:
Beto Dealmeida 2023-05-01 11:06:54 -07:00 committed by GitHub
parent a994145e37
commit 2a1a061a3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 0 deletions

View File

@ -509,6 +509,10 @@ def has_table_query(token_list: TokenList) -> bool:
"""
state = InsertRLSState.SCANNING
for token in token_list.tokens:
# Ignore comments
if isinstance(token, sqlparse.sql.Comment):
continue
# Recurse into child token list
if isinstance(token, TokenList) and has_table_query(token):
return True

View File

@ -1217,6 +1217,14 @@ def test_sqlparse_issue_652():
("extract(HOUR from from_unixtime(hour_ts)", False),
("(SELECT * FROM table)", True),
("(SELECT COUNT(DISTINCT name) from birth_names)", True),
(
"(SELECT table_name FROM information_schema.tables WHERE table_name LIKE '%user%' LIMIT 1)",
True,
),
(
"(SELECT table_name FROM /**/ information_schema.tables WHERE table_name LIKE '%user%' LIMIT 1)",
True,
),
],
)
def test_has_table_query(sql: str, expected: bool) -> None: