X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=sqlconvert%2Fprocess_mysql.py;h=c89d6d190940dad18230043b6ae77da89bfababf;hb=HEAD;hp=1834855dc9b59aee5261cf065afcab1c58310fac;hpb=72abf4a136b1a2d164259a4ac300e6a5a4762432;p=sqlconvert.git diff --git a/sqlconvert/process_mysql.py b/sqlconvert/process_mysql.py index 1834855..c89d6d1 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 @@ -29,7 +29,7 @@ def is_directive_statement(statement): def remove_directive_tokens(statement): - """Remove /\*! directives \*/ from the first-level""" + """Remove /*! directives */ from the first-level""" new_tokens = [] for token in statement.tokens: if _is_directive_token(token): @@ -71,11 +71,14 @@ def unescape_strings(token_list): token.normalized = token.value = value -def is_insert(statement): +def get_DML_type(statement): for token in statement.tokens: if is_comment_or_space(token): continue - return (token.ttype is T.DML) and (token.normalized == 'INSERT') + if (token.ttype is T.DML): + return token.normalized + break + raise ValueError("Not a DML statement") def split_ext_insert(statement): @@ -107,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' @@ -138,12 +149,16 @@ def split_ext_insert(statement): yield statement -def process_statement(statement, quoting_style='sqlite'): +def process_statement(statement, dbname='sqlite'): requote_names(statement) unescape_strings(statement) remove_directive_tokens(statement) - escape_strings(statement, quoting_style) - if is_insert(statement): + escape_strings(statement, dbname) + try: + dml_type = get_DML_type(statement) + except ValueError: + dml_type = 'UNKNOWN' + if dml_type == 'INSERT': for statement in split_ext_insert(statement): yield statement else: