Skip to content

Instantly share code, notes, and snippets.

@zhy0216
Created March 14, 2014 03:24
Show Gist options
  • Save zhy0216/9541608 to your computer and use it in GitHub Desktop.
Save zhy0216/9541608 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import codecs
STATE = "IDEL" # INSERT, SEQUENCE
f = open("1.sql", "r")
f_w = codecs.open("test.sql", 'w', 'utf-8')
f_w.write("DELETE FROM ACCOUNT_GROUP_NAMES;DELETE FROM ACCOUNT_GROUPS;" + \
# "ALTER TABLE changes ALTER COLUMN subject TYPE varchar(2047);" + \
"CREATE TABLE SYSTEM_LOB_STREAM(ID INT NOT NULL, PART INT NOT NULL, CDATA VARCHAR, BDATA BYTEA);")
for line in f:
line_strip = line.strip();
if STATE == "INSERT":
l = line.replace("STRINGDECODE(", "(E")\
.replace(r"\',","',")\
.replace(r"\\'),", r"\\\\'),")\
.replace("SYSTEM_COMBINE_CLOB", "")\
.replace("''''", '""')\
.replace("''',", '"\',')\
.replace("''')", '"\')')\
.replace("'''", '\'"')\
.replace(r"\0", "")
# unicode(l, "unicode-escape")
l = unicode(l, "unicode-escape").replace(r"\''", r'""').replace(r"\u2026", u"…").replace("''", '"').replace('\\', r"\\")
f_w.write(l)
if line_strip.startswith("INSERT") and "SCHEMA_VERSION" not in line_strip and "SYSTEM_CONFIG" not in line_strip:
STATE = "INSERT"
l = line.replace("STRINGDECODE(", "(E")\
.replace(r"\',","',")\
.replace(r"\\'),", r"\\\\'),")\
.replace("SYSTEM_COMBINE_CLOB", "")\
.replace("''''", '""')\
.replace("''',", '"\',')\
.replace("''')", '"\')')\
.replace("'''", '\'"')\
.replace(r"\0", "")
l = unicode(l, "unicode-escape").replace(r"\''", r'""').replace(r"\u2026", u"…").replace("''", '"').replace('\\', r"\\")
f_w.write(l)
if line_strip.endswith(";"):
STATE = "IDEL"
elif line_strip.startswith("CREATE SEQUENCE"):
STATE = "SEQUENCE"
f_w.write(line.replace("CREATE", "ALTER").replace("START", "RESTART"))
elif line_strip.endswith(";"):
STATE = "IDEL"
f.close()
f_w.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment