Repository: macbre/sql-metadata

Issue:
Can not handle correctly with mysql upsert statement
```sql
insert into user (id, name, age) 
  values ('user1', 'john doe', 20) 
  on duplicate key 
  update name='john doe', age=20;
```

The correct value of `Parser(sql).tables` is `['user']`, but it returns `['user', 'name', 'age']`.

I used `0.4.3`.

Discussion:

Patch:
diff --git a/sql_metadata/parser.py b/sql_metadata/parser.py
index 349f044..c422846 100644
--- a/sql_metadata/parser.py
+++ b/sql_metadata/parser.py
@@ -357,6 +357,14 @@ class Parser:  # pylint: disable=R0902
                     )
                 ):
                     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)


Test patch:
diff --git a/test/test_getting_tables.py b/test/test_getting_tables.py
index 76c8741..d90b0ce 100644
--- a/test/test_getting_tables.py
+++ b/test/test_getting_tables.py
@@ -658,3 +658,11 @@ 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"]