]> git.phdru.name Git - sqlconvert.git/blobdiff - sqlconvert/process_mysql.py
Add MySQL-specific remove_directives() and process_statement()
[sqlconvert.git] / sqlconvert / process_mysql.py
index b11eccb38f4376f526a7b8b3130e618f7ff58c8f..5c63d0d1bc1de9c39c67455cd422ffc838a4220f 100644 (file)
@@ -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)