X-Git-Url: https://git.phdru.name/?p=sqlconvert.git;a=blobdiff_plain;f=sqlconvert%2Fprocess_mysql.py;h=5c63d0d1bc1de9c39c67455cd422ffc838a4220f;hp=b11eccb38f4376f526a7b8b3130e618f7ff58c8f;hb=86cace63e9bc60457f310bc77aba8fc54b748bbe;hpb=dbc9220a2b29725f94637607f8d8b00c762deb67 diff --git a/sqlconvert/process_mysql.py b/sqlconvert/process_mysql.py index b11eccb..5c63d0d 100644 --- a/sqlconvert/process_mysql.py +++ b/sqlconvert/process_mysql.py @@ -1,11 +1,12 @@ -from sqlparse.tokens import Name +from sqlparse.sql import Comment +from sqlparse import tokens as T def requote_names(token_list): """Remove backticks, quote non-lowercase identifiers""" for token in token_list.flatten(): - if token.ttype is Name: + if token.ttype is T.Name: value = token.value if (value[0] == "`") and (value[-1] == "`"): value = value[1:-1] @@ -13,3 +14,23 @@ def requote_names(token_list): token.normalized = token.value = value else: token.normalized = token.value = '"%s"' % value + + +def remove_directives(statement): + """Remove /*! directives */ from the first-level""" + new_tokens = [] + for token in statement.tokens: + if isinstance(token, Comment): + subtokens = token.tokens + if subtokens: + comment = subtokens[0] + if comment.ttype is T.Comment.Multiline and \ + comment.value.startswith('/*!'): + continue + new_tokens.append(token) + statement.tokens = new_tokens + + +def process_statement(statement): + requote_names(statement) + remove_directives(statement)