Skip to content

Commit

Permalink
Handle INSERT INTO ON DUPLICATE KEY UPDATE queries when extracting ta…
Browse files Browse the repository at this point in the history
…ble names

Resolves #371
  • Loading branch information
macbre committed Apr 20, 2023
1 parent 1c20ba1 commit e8a8a01
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions sql_metadata/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,14 @@ def tables(self) -> List[str]:
)
):
continue

# handle INSERT INTO ON DUPLICATE KEY UPDATE queries
if (
token.last_keyword_normalized == "UPDATE"
and self.query_type == "INSERT"
):
continue

table_name = str(token.value.strip("`"))
token.token_type = TokenType.TABLE
tables.append(table_name)
Expand Down
7 changes: 7 additions & 0 deletions test/test_getting_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,10 @@ def test_cross_join_with_subquery():
assert parser.subqueries == {
"foobar": "SELECT * FROM bars",
}

def test_insert_into_on_duplicate_key_ipdate():
assert Parser(
"INSERT INTO user (id, name, age)"
" VALUES ('user1', 'john doe', 20)"
" ON DUPLICATE KEY UPDATE name='john doe', age=20"
).tables == ['user']

0 comments on commit e8a8a01

Please sign in to comment.