From: Oleg Broytman Date: Wed, 26 Feb 2020 22:41:47 +0000 (+0300) Subject: Fix: Adapt to newer `sqlparse` X-Git-Tag: 0.3.0~19 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=9bfd0fb291e1ca197ece43bb728f65830289d826;p=sqlconvert.git Fix: Adapt to newer `sqlparse` `VALUES` is now a separate object that contains a lists of value tuples. --- diff --git a/sqlconvert/process_mysql.py b/sqlconvert/process_mysql.py index ad4d422..303dcf3 100644 --- a/sqlconvert/process_mysql.py +++ b/sqlconvert/process_mysql.py @@ -1,6 +1,6 @@ from sqlparse.sql import Comment, Function, Identifier, Parenthesis, \ - Statement, Token + Statement, Token, Values from sqlparse import tokens as T from .process_tokens import escape_strings, is_comment_or_space @@ -110,6 +110,14 @@ def split_ext_insert(statement): expected = 'VALUES' continue elif expected == 'VALUES': + if isinstance(token, Values): + for subtoken in token.tokens: + if isinstance(subtoken, Parenthesis): + values_tokens.append(subtoken) + insert_tokens.append(Token(T.Keyword, 'VALUES')) + insert_tokens.append(Token(T.Whitespace, ' ')) + expected = 'VALUES_OR_SEMICOLON' + continue if (token.ttype is T.Keyword) and (token.normalized == 'VALUES'): insert_tokens.append(token) expected = 'VALUES_OR_SEMICOLON' diff --git a/tests/mysql2sql/test.out b/tests/mysql2sql/test.out index 0026e62..1bd6b8f 100644 --- a/tests/mysql2sql/test.out +++ b/tests/mysql2sql/test.out @@ -14,10 +14,10 @@ SELECT * FROM mytable; -- line-comment" INSERT INTO "MyTable" ("Id", "Name") VALUES (1, 'one'); -INSERT INTO mytable VALUES (1, 'one'); -INSERT INTO mytable VALUES (2, 'two'); +INSERT INTO mytable VALUES (1, 'one'); +INSERT INTO mytable VALUES (2, 'two'); -INSERT INTO mytable (id, name) VALUES (1, 'one'); -INSERT INTO mytable (id, name) VALUES (2, 'two'); +INSERT INTO mytable (id, name) VALUES (1, 'one'); +INSERT INTO mytable (id, name) VALUES (2, 'two'); -- The end diff --git a/tests/test_process_mysql.py b/tests/test_process_mysql.py index 656292b..420a219 100644 --- a/tests/test_process_mysql.py +++ b/tests/test_process_mysql.py @@ -84,19 +84,19 @@ def test_split_ext_insert(): stiter = process_statement(parsed) statement = next(stiter) query = tlist2str(statement) - assert query == u"INSERT INTO test VALUES (1, 2);\n" + assert query == u"INSERT INTO test VALUES (1, 2);\n" statement = next(stiter) query = tlist2str(statement) - assert query == u"INSERT INTO test VALUES (3, 4);" + assert query == u"INSERT INTO test VALUES (3, 4);" parsed = parse("insert into test (age, salary) values (1, 2), (3, 4)")[0] stiter = process_statement(parsed) statement = next(stiter) query = tlist2str(statement) - assert query == u"INSERT INTO test (age, salary) VALUES (1, 2)\n" + assert query == u"INSERT INTO test (age, salary) VALUES (1, 2)\n" statement = next(stiter) query = tlist2str(statement) - assert query == u"INSERT INTO test (age, salary) VALUES (3, 4)" + assert query == u"INSERT INTO test (age, salary) VALUES (3, 4)" def test_process():