X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=sqlconvert%2Fprocess_mysql.py;h=34565316e93b62f0f6092df7950fb597ef405f93;hb=e8beeb55405cb5b29d424d247d2e06a13d907597;hp=9e342c2e04ec7c5ad4ba1796731932320762e5f3;hpb=d33677c7a57a955f59e46f2abe03e9f5a192d883;p=sqlconvert.git diff --git a/sqlconvert/process_mysql.py b/sqlconvert/process_mysql.py index 9e342c2..3456531 100644 --- a/sqlconvert/process_mysql.py +++ b/sqlconvert/process_mysql.py @@ -1,5 +1,6 @@ -from sqlparse.sql import Comment +from sqlparse.sql import Comment, Function, Identifier, Parenthesis, \ + Statement, Token from sqlparse import tokens as T from .process_tokens import escape_strings, is_comment_or_space @@ -28,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): @@ -77,10 +78,73 @@ def is_insert(statement): return (token.ttype is T.DML) and (token.normalized == 'INSERT') +def split_ext_insert(statement): + """Split extended INSERT into multiple standard INSERTs""" + insert_tokens = [] + values_tokens = [] + end_tokens = [] + expected = 'INSERT' + for token in statement.tokens: + if is_comment_or_space(token): + if expected == 'END': + end_tokens.append(token) + else: + insert_tokens.append(token) + continue + elif expected == 'INSERT': + if (token.ttype is T.DML) and (token.normalized == 'INSERT'): + insert_tokens.append(token) + expected = 'INTO' + continue + elif expected == 'INTO': + if (token.ttype is T.Keyword) and (token.normalized == 'INTO'): + insert_tokens.append(token) + expected = 'TABLE_NAME' + continue + elif expected == 'TABLE_NAME': + if isinstance(token, (Function, Identifier)): + insert_tokens.append(token) + expected = 'VALUES' + continue + elif expected == 'VALUES': + if (token.ttype is T.Keyword) and (token.normalized == 'VALUES'): + insert_tokens.append(token) + expected = 'VALUES_OR_SEMICOLON' + continue + elif expected == 'VALUES_OR_SEMICOLON': + if isinstance(token, Parenthesis): + values_tokens.append(token) + continue + elif token.ttype is T.Punctuation: + if token.value == ',': + continue + elif token.value == ';': + end_tokens.append(token) + expected = 'END' + continue + raise ValueError( + 'SQL syntax error: expected "%s", got %s "%s"' % ( + expected, token.ttype, token.normalized)) + new_line = Token(T.Newline, '\n') + new_lines = [new_line] # Insert newlines between split statements + for i, values in enumerate(values_tokens): + if i == len(values_tokens) - 1: # Last but one statement + # Insert newlines only between split statements but not after + new_lines = [] + # The statemnt sets `parent` attribute of the every token to self + # but we don't care. + statement = Statement(insert_tokens + [values] + + end_tokens + new_lines) + yield statement + + def process_statement(statement, quoting_style='sqlite'): requote_names(statement) unescape_strings(statement) remove_directive_tokens(statement) escape_strings(statement, quoting_style) - yield statement - return + if is_insert(statement): + for statement in split_ext_insert(statement): + yield statement + else: + yield statement