Skip to content

Instantly share code, notes, and snippets.

@haruo31
Last active February 14, 2016 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haruo31/1f47dc5ff22ce5682e3a to your computer and use it in GitHub Desktop.
Save haruo31/1f47dc5ff22ce5682e3a to your computer and use it in GitHub Desktop.
the short tool will work if you cannot remake mysqldump without extended-insert option.
# -*- coding: utf8 -*-
import sys
BOL, LCOMMENT, INSTMT, INSERT, VALUES, QUOTE_VALUE = range(6)
CHUNK_SIZE = 1024 * 1024 * 1 / 2 # 512k in read
stock = ''
def next():
return sys.stdin.read(CHUNK_SIZE)
def write(buf):
sys.stdout.write(buf)
state_stack = [BOL]
insert_stmt = ''
stock = next()
rp = 0
np = 0
while True:
rp = np
if len(stock) - rp < CHUNK_SIZE or stock[-1] == "\\":
write(stock[:rp])
stock = stock[rp:] + next()
rp = 0
if not len(stock):
break
p = state_stack[-1]
if p == BOL:
if stock.startswith('INSERT INTO', rp):
state_stack.append(INSERT)
np = stock.find('(', rp)
write(stock[:rp])
insert_stmt = stock[rp:np]
stock = stock[np:]
np = 0
elif stock[rp] == "\n":
np = rp + 1
elif stock.startswith("--", rp):
state_stack.append(LCOMMENT)
else:
state_stack.append(INSTMT)
continue
if p == LCOMMENT:
i = stock.find("\n", rp)
if i >= 0:
np = i + 1
state_stack.pop()
continue
np = len(stock)
continue
if p == INSTMT:
i = stock.find(';', rp)
if i >= 0:
qp = stock.find("\\'", rp, i)
if qp >= 0:
i = qp
state_stack.append(QUOTE_VALUE)
else:
state_stack.pop()
np = i + 1
continue
i = len(stock)
qp = stock.find("\\'", rp, i)
if qp >= 0:
i = qp + 1
state_stack.append(QUOTE_VALUE)
np = i
continue
if p == INSERT or p == VALUES:
if p == VALUES:
state_stack.pop()
if p == INSERT:
write(insert_stmt)
i = stock.find(')', rp)
if i >= 0:
np = i + 1
q = stock.find("'", rp, np)
if q >= 0:
np = q + 1
state_stack.append(VALUES)
state_stack.append(QUOTE_VALUE)
continue
write(stock[:np])
if stock[np] == ',':
write(";\n")
elif stock[np] == ';':
state_stack.pop()
write(';')
np += 1
stock = stock[np:]
np = 0
continue
state_stack.append(VALUES)
q = stock.find("'", rp)
if q >= 0:
np = q + 1
state_stack.append(QUOTE_VALUE)
continue
np = len(stock)
continue
if p == QUOTE_VALUE:
i = stock.find("'", rp)
if i >= 0:
bs = stock.find("\\", rp, i)
if bs >= 0:
np = bs + 2
continue
np = i + 1
state_stack.pop()
continue
np = len(stock)
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment