]> git.phdru.name Git - sqlconvert.git/blobdiff - sqlconvert/process_mysql.py
Fix a bug: assign buffer even if encoding is None
[sqlconvert.git] / sqlconvert / process_mysql.py
index 3f2f6aa3c7dee8ad84f60c3a630050c8f3d413b6..91ef98d66c65cd86ce93baddec518ccb980eb7f6 100644 (file)
@@ -1,6 +1,7 @@
 
 from sqlparse.sql import Comment
 from sqlparse import tokens as T
+from .process_tokens import escape_strings
 
 
 def _is_directive_token(token):
@@ -26,7 +27,7 @@ def is_directive_statement(statement):
     return True
 
 
-def remove_directives(statement):
+def remove_directive_tokens(statement):
     """Remove /*! directives */ from the first-level"""
     new_tokens = []
     for token in statement.tokens:
@@ -49,6 +50,28 @@ def requote_names(token_list):
                 token.normalized = token.value = '"%s"' % value
 
 
-def process_statement(statement):
-    remove_directives(statement)
+def unescape_strings(token_list):
+    """Unescape strings"""
+    for token in token_list.flatten():
+        if token.ttype is T.String.Single:
+            value = token.value
+            for orig, repl in (
+                ('\\"', '"'),
+                ("\\'", "'"),
+                ("''", "'"),
+                ('\\b', '\b'),
+                ('\\n', '\n'),
+                ('\\r', '\r'),
+                ('\\t', '\t'),
+                ('\\\032', '\032'),
+                ('\\\\', '\\'),
+            ):
+                value = value.replace(orig, repl)
+            token.normalized = token.value = value
+
+
+def process_statement(statement, quoting_style='sqlite'):
+    remove_directive_tokens(statement)
     requote_names(statement)
+    unescape_strings(statement)
+    escape_strings(statement, quoting_style)