]> git.phdru.name Git - sqlconvert.git/commitdiff
Unescape strings
authorOleg Broytman <phd@phdru.name>
Sat, 10 Sep 2016 17:35:02 +0000 (20:35 +0300)
committerOleg Broytman <phd@phdru.name>
Sat, 10 Sep 2016 17:37:45 +0000 (20:37 +0300)
ChangeLog
README.txt
docs/mysql2sql.rst
sqlconvert/process_mysql.py
tests/test_tokens.py

index 6d0e1944899aff915987f7beeb58c3f95c1f1db2..ec6b82f7953467d0bf1a43b54def6b6177b0d18d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -4,6 +4,8 @@ Version 0.0.6 (2016-09-??)
 
     Rename remove_directives -> remove_directive_tokens.
 
 
     Rename remove_directives -> remove_directive_tokens.
 
+    Unescape strings.
+
 Version 0.0.5 (2016-09-07)
 
     Remove /*! directives */; and newlines after them.
 Version 0.0.5 (2016-09-07)
 
     Remove /*! directives */; and newlines after them.
index 86b1b0513690252b34a648759d32039955bd8109..2e444746fcdebf7a61844fa24ed2e606970e4bba 100644 (file)
@@ -5,7 +5,8 @@ License: GPL
 This is sqlconvert, a a library to perform SQL convertions. It uses
 sqlparse to parse SQL.
 
 This is sqlconvert, a a library to perform SQL convertions. It uses
 sqlparse to parse SQL.
 
-The library is in its initial phase and currently cannot do much.
+The library is in the early stage of development and currently cannot do
+much.
 
 The first goal is to implemet mysq2sql, a script intended primarily to
 convert mysqldump output (especially with extended INSERT syntax) to
 
 The first goal is to implemet mysq2sql, a script intended primarily to
 convert mysqldump output (especially with extended INSERT syntax) to
index 9c1892d62a7054d88bf410624e5fb5144f25b1e9..12e574b3676702d30b42ac3f37c18b5bd43c5049 100644 (file)
@@ -5,8 +5,9 @@ This is mysql2sql, a mysql to sql converter. It is primary intended to
 convert mysqldump (especially with extended INSERT syntax) to standard
 SQL to load at least to PostgreSQL or SQLite.
 
 convert mysqldump (especially with extended INSERT syntax) to standard
 SQL to load at least to PostgreSQL or SQLite.
 
-The program is in its initial phase and currently cannot do much. It only
-removes /\*! directives \*/ and passes everything else unmodified.
+The program is in the early stage of development and currently cannot do much.
+It removes /\*! directives \*/, unescapes strings and passes everything else
+unmodified.
 
 
 .. highlight:: none
 
 
 .. highlight:: none
index f91f516d2d9236f53dd320f5eb2a958545d54c9d..d72479e7111bfc0db29b12c7f6bdb1d8aa0561ed 100644 (file)
@@ -49,6 +49,21 @@ def requote_names(token_list):
                 token.normalized = token.value = '"%s"' % value
 
 
                 token.normalized = token.value = '"%s"' % value
 
 
+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 (
+                ('\\"', '"'),
+                ("\\'", "''"),
+                ('\\\032', '\032'),
+            ):
+                value = value.replace(orig, repl)
+            token.normalized = token.value = value
+
+
 def process_statement(statement):
     remove_directive_tokens(statement)
     requote_names(statement)
 def process_statement(statement):
     remove_directive_tokens(statement)
     requote_names(statement)
+    unescape_strings(statement)
index 24866548cfa3b9abbbbd77d2ef35803badacb5b8..de11485d1f70bcb7bbc042c52829cbc4d27f9c13 100755 (executable)
@@ -5,8 +5,9 @@ import unittest
 from sqlparse import parse
 
 from sqlconvert.print_tokens import tlist2str
 from sqlparse import parse
 
 from sqlconvert.print_tokens import tlist2str
-from sqlconvert.process_mysql import remove_directive_tokens, requote_names, \
-        is_directive_statement, process_statement
+from sqlconvert.process_mysql import remove_directive_tokens, \
+        is_directive_statement, requote_names, unescape_strings, \
+        process_statement
 from tests import main
 
 
 from tests import main
 
 
@@ -22,17 +23,11 @@ class TestTokens(unittest.TestCase):
         query = tlist2str(parsed)
         self.assertEqual(query, u"INSERT INTO test (1, 'тест')")
 
         query = tlist2str(parsed)
         self.assertEqual(query, u"INSERT INTO test (1, 'тест')")
 
-    def test_requote(self):
-        parsed = parse("select * from `T`")[0]
-        requote_names(parsed)
-        query = tlist2str(parsed)
-        self.assertEqual(query, 'SELECT * FROM "T"')
-
     def test_directive(self):
         parsed = parse("select /*! test */ * from /* test */ `T`")[0]
         remove_directive_tokens(parsed)
         query = tlist2str(parsed)
     def test_directive(self):
         parsed = parse("select /*! test */ * from /* test */ `T`")[0]
         remove_directive_tokens(parsed)
         query = tlist2str(parsed)
-        self.assertEqual(query, 'SELECT * FROM /* test */ `T`')
+        self.assertEqual(query, u'SELECT * FROM /* test */ `T`')
 
     def test_directive_statement(self):
         parsed = parse("/*! test */ test ;")[0]
 
     def test_directive_statement(self):
         parsed = parse("/*! test */ test ;")[0]
@@ -40,11 +35,23 @@ class TestTokens(unittest.TestCase):
         parsed = parse("/*! test */ ;")[0]
         self.assertTrue(is_directive_statement(parsed))
 
         parsed = parse("/*! test */ ;")[0]
         self.assertTrue(is_directive_statement(parsed))
 
+    def test_requote(self):
+        parsed = parse("select * from `T`")[0]
+        requote_names(parsed)
+        query = tlist2str(parsed)
+        self.assertEqual(query, u'SELECT * FROM "T"')
+
+    def test_string(self):
+        parsed = parse("insert into test values ('\"test\\\"')")[0]
+        unescape_strings(parsed)
+        query = tlist2str(parsed)
+        self.assertEqual(query, u"INSERT INTO test VALUES ('\"test\"')")
+
     def test_process(self):
         parsed = parse("select /*! test */ * from /* test */ `T`")[0]
         process_statement(parsed)
         query = tlist2str(parsed)
     def test_process(self):
         parsed = parse("select /*! test */ * from /* test */ `T`")[0]
         process_statement(parsed)
         query = tlist2str(parsed)
-        self.assertEqual(query, 'SELECT * FROM /* test */ "T"')
+        self.assertEqual(query, u'SELECT * FROM /* test */ "T"')
 
 
 if __name__ == "__main__":
 
 
 if __name__ == "__main__":