Unescape strings. Add a test for Postgres.
- Use pytest, coverage, tox and SQLObject for testing.
+ Use SQLObject for string quoting and connection handling for tests.
+
+ Use pytest, coverage and tox for testing.
Version 0.0.5 (2016-09-07)
argparse; python_version == '2.6'
sqlparse
+SQLObject>=2.2.1; python_version >= '2.6' and python_version < '3.0'
+SQLObject>=3.0.0; python_version >= '3.4'
m_lib>=2.0; python_version >= '2.6' and python_version < '3.0'
m_lib>=3.0; python_version >= '3.4'
pytest
pytest-cov
tox >= 1.8
-SQLObject>=2.2.1; python_version >= '2.6' and python_version < '3.0'
-SQLObject>=3.0.0; python_version >= '3.4'
packages=['sqlconvert'],
package_data={},
scripts=['scripts/mysql2sql'],
- requires=['sqlparse', 'm_lib'],
+ requires=['sqlparse', 'SQLObject', 'm_lib'],
)
+from sqlobject.converters import sqlrepr
from sqlparse import parse
from sqlparse.compat import PY3
from sqlparse import tokens as T
return True
+def escape_strings(token_list, dbname):
+ """Escape strings"""
+ for token in token_list.flatten():
+ if token.ttype is T.String.Single:
+ value = token.value[1:-1] # unquote by removing apostrophes
+ value = sqlrepr(value, dbname)
+ token.normalized = token.value = value
+
+
if PY3:
xrange = range
from sqlconvert.print_tokens import tlist2str
from sqlconvert.process_mysql import unescape_strings
+from sqlconvert.process_tokens import escape_strings
connection = getConnection()
pytestmark = pytest.mark.skipif(connection.dbName != "postgres",
parsed = parse("insert into test (id, test_str) values "
"(1, '\"te\\'st\\\"\\n')")[0]
unescape_strings(parsed)
+ escape_strings(parsed, 'postgres')
query = tlist2str(parsed)
assert query == u"INSERT INTO test (id, test_str) VALUES " \
- u"(1, '\"te''st\"\n')"
+ u"(1, E'\"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\"\n"
from sqlconvert.process_mysql import remove_directive_tokens, \
is_directive_statement, requote_names, unescape_strings, \
process_statement
+from sqlconvert.process_tokens import escape_strings
def test_encoding():
assert query == u'SELECT * FROM "T"'
-def test_string():
+def test_unescape_string():
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\"\n')"
+
+
+def test_escape_string_postgres():
+ parsed = parse("insert into test values ('\"te\\'st\\\"\\n')")[0]
+ unescape_strings(parsed)
+ escape_strings(parsed, 'postgres')
+ query = tlist2str(parsed)
+ assert query == u"INSERT INTO test VALUES (E'\"te''st\"\\n')"
+
+
+def test_escape_string_sqlite():
+ parsed = parse("insert into test values ('\"te\\'st\\\"\\n')")[0]
+ unescape_strings(parsed)
+ escape_strings(parsed, 'sqlite')
+ query = tlist2str(parsed)
assert query == u"INSERT INTO test VALUES ('\"te''st\"\n')"