From: Oleg Broytman Date: Sat, 11 Mar 2017 20:42:01 +0000 (+0300) Subject: Implement is_insert() X-Git-Tag: 0.0.8~4 X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=d33677c7a57a955f59e46f2abe03e9f5a192d883;p=sqlconvert.git Implement is_insert() --- diff --git a/sqlconvert/process_mysql.py b/sqlconvert/process_mysql.py index 0320b7f..9e342c2 100644 --- a/sqlconvert/process_mysql.py +++ b/sqlconvert/process_mysql.py @@ -1,7 +1,7 @@ from sqlparse.sql import Comment from sqlparse import tokens as T -from .process_tokens import escape_strings +from .process_tokens import escape_strings, is_comment_or_space def _is_directive_token(token): @@ -70,6 +70,13 @@ def unescape_strings(token_list): token.normalized = token.value = value +def is_insert(statement): + for token in statement.tokens: + if is_comment_or_space(token): + continue + return (token.ttype is T.DML) and (token.normalized == 'INSERT') + + def process_statement(statement, quoting_style='sqlite'): requote_names(statement) unescape_strings(statement) diff --git a/tests/test_process_mysql.py b/tests/test_process_mysql.py index 500a35d..45e5020 100644 --- a/tests/test_process_mysql.py +++ b/tests/test_process_mysql.py @@ -5,7 +5,7 @@ from sqlparse import parse from sqlconvert.print_tokens import tlist2str from sqlconvert.process_mysql import remove_directive_tokens, \ is_directive_statement, requote_names, unescape_strings, \ - process_statement + is_insert, process_statement from sqlconvert.process_tokens import escape_strings @@ -53,6 +53,15 @@ def test_escape_string_sqlite(): assert query == u"INSERT INTO test VALUES ('\"te''st\"\n')" +def test_is_insert(): + parsed = parse("select /*! test */ * from /* test */ `T`")[0] + statement = next(process_statement(parsed)) + assert not is_insert(statement) + parsed = parse("insert into test values ('\"te\\'st\\\"\\n')")[0] + statement = next(process_statement(parsed)) + assert is_insert(statement) + + def test_process(): parsed = parse("select /*! test */ * from /* test */ `T`")[0] statement = next(process_statement(parsed))