Guest User

Untitled

a guest
May 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. import re, fileinput
  2.  
  3. print 'SET FOREIGN_KEY_CHECKS=0;'
  4.  
  5. def main():
  6. for line in fileinput.input():
  7. process = False
  8. for nope in ('BEGIN TRANSACTION','COMMIT',
  9. 'sqlite_sequence','CREATE UNIQUE INDEX'):
  10. if nope in line: break
  11. else:
  12. process = True
  13. if not process: continue
  14. m = re.search('CREATE TABLE "([a-z_]*)"(.*)', line)
  15. if m:
  16. name, sub = m.groups()
  17. sub = sub.replace('"','`')
  18. line = '''DROP TABLE IF EXISTS %(name)s;
  19. CREATE TABLE IF NOT EXISTS %(name)s%(sub)s
  20. '''
  21. line = line % dict(name=name, sub=sub)
  22. else:
  23. m = re.search('INSERT INTO "([a-z_]*)"(.*)', line)
  24. if m:
  25. line = 'INSERT INTO %s%s\n' % m.groups()
  26. line = line.replace('"', r'\"')
  27. line = line.replace('"', "'")
  28.  
  29. line = line.strip()
  30. line = line.replace(' +00:00', "")
  31. line = line[0:len(line)-1].replace(';', '\;') + line[-1]
  32. line = re.sub(r'INSERT INTO "([^"]+)"', r"INSERT INTO \1", line)
  33. line = re.sub(r"([^'])'t'(.)", "\\1THIS_IS_TRUE\\2", line)
  34. line = line.replace('THIS_IS_TRUE', '1')
  35. line = re.sub(r"([^'])'f'(.)", "\\1THIS_IS_FALSE\\2", line)
  36. line = line.replace('THIS_IS_FALSE', '0')
  37. line = line.replace('AUTOINCREMENT', 'AUTO_INCREMENT')
  38. if re.search('^CREATE INDEX', line):
  39. line = line.replace('"','`')
  40. print line,
  41. print ""
  42.  
  43. main()
  44. print 'SET FOREIGN_KEY_CHECKS=1;'
Add Comment
Please, Sign In to add comment