From 6d906e2f335699348bf44a653ca708b522a99d85 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 25 Sep 2016 17:46:50 +0300 Subject: [PATCH] Unescape all known escapes --- sqlconvert/process_mysql.py | 8 +++++++- tests/test_mysql2postgres.py | 9 +++++---- tests/test_tokens.py | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/sqlconvert/process_mysql.py b/sqlconvert/process_mysql.py index d72479e..218655a 100644 --- a/sqlconvert/process_mysql.py +++ b/sqlconvert/process_mysql.py @@ -56,8 +56,14 @@ def unescape_strings(token_list): 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 diff --git a/tests/test_mysql2postgres.py b/tests/test_mysql2postgres.py index b81c6bf..971970c 100644 --- a/tests/test_mysql2postgres.py +++ b/tests/test_mysql2postgres.py @@ -19,11 +19,12 @@ CREATE TABLE test ( def test_mysql2postgres_string(): connection.query(create_postgres_test_table) - parsed = parse("insert into test (id, test_str) " - "values (1, '\"te\\'st\\\"')")[0] + parsed = parse("insert into test (id, test_str) values " + "(1, '\"te\\'st\\\"\\n')")[0] unescape_strings(parsed) query = tlist2str(parsed) - assert query == u"INSERT INTO test (id, test_str) VALUES (1, '\"te''st\"')" + assert query == u"INSERT INTO test (id, test_str) VALUES " \ + u"(1, '\"te''st\"\n')" connection.query(query) test_str = connection.queryOne("SELECT test_str FROM test WHERE id=1")[0] - assert test_str == u"\"te'st\"" + assert test_str == u"\"te'st\"\n" diff --git a/tests/test_tokens.py b/tests/test_tokens.py index b514b59..d537d73 100644 --- a/tests/test_tokens.py +++ b/tests/test_tokens.py @@ -43,10 +43,10 @@ def test_requote(): def test_string(): - parsed = parse("insert into test values ('\"te\\'st\\\"')")[0] + parsed = parse("insert into test values ('\"te\\'st\\\"\\n')")[0] unescape_strings(parsed) query = tlist2str(parsed) - assert query == u"INSERT INTO test VALUES ('\"te''st\"')" + assert query == u"INSERT INTO test VALUES ('\"te''st\"\n')" def test_process(): -- 2.39.2