From d33677c7a57a955f59e46f2abe03e9f5a192d883 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 11 Mar 2017 23:42:01 +0300 Subject: [PATCH] Implement is_insert() --- sqlconvert/process_mysql.py | 9 ++++++++- tests/test_process_mysql.py | 11 ++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) 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)) -- 2.39.5