Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. import pymysql
  2. undo_trans = []
  3.  
  4. pkdict = {'course' :'course_id', 'department' : 'dept_id', 'employee' : 'ename', 'professor' : 'prof_id', 'student' :'stu_id'}
  5.  
  6. def connect_db(Table, Column, after, Primary_Key, Pri_Value):
  7. connection = pymysql.connect(
  8. host = '127.0.0.1',
  9. user = 'root',
  10. password = 'Spscene1104!',
  11. db = 'python_test',
  12. charset = 'utf8',
  13. cursorclass=pymysql.cursors.DictCursor)
  14. try:
  15. with connection.cursor() as cursor:
  16. print(Table, Column, after, Primary_Key, Pri_Value)
  17. sql = "UPDATE %s SET %s = '%s' WHERE %s = '%s'" % (Table, Column, after, Primary_Key, Pri_Value)
  18. cursor.execute(sql)
  19. result = cursor.fetchall()
  20. print(result)
  21. finally:
  22. connection.close()
  23.  
  24. def parse_log_1(): # 텍스트 파일에 담긴 기록들을 리스트에 담는거.
  25. chkpt_idx = 0
  26. global undo_trans
  27. with open("D:\\workspace(pycharm)\\Engineering2\\recovery.txt", "r", encoding="utf-8") as f:
  28. logs = []
  29. for line in f:
  30. if line[-1] == "\n":
  31. logs.append(line[:-1]) # \n을 빼고 logs 리스트에 넣으라는 얘기.
  32. # \n을 제거 안하면 리스트에 담길 때 \n이 같이 담겨서 print할 때 한칸 씩 띄어서 print됨(중요한건 아님)
  33. else:
  34. logs.append(line) # \n이 안쳐져있으면 바로 logs 리스트에 넣기.
  35. # print(logs)
  36.  
  37. for i in range(len(logs)): # logs에 담긴 log기록들을 하나씩 가져오는거
  38. if logs[i].startswith("checkpoint"): # Checkpoint는 기준점이 되고 Checkpoint에 있는 log기록들 담기
  39. chkpt_idx = i
  40. transs = logs[i][11:].split(", ") # checkpoint 글자 끝나는 지점부터 ,를 구분자로 해서 쪼개준다.
  41. undo_trans.extend(transs) # Undo List 만들어 줌.
  42. # print("last checkpoint idx is : ", i)
  43. for i in range(chkpt_idx+1, len(logs)):
  44. if logs[i].split()[1] == 'commit' or logs[i].split()[1] == 'abort':
  45. undo_trans.remove(logs[i].split()[0])
  46. elif logs[i].split()[1] == 'start':
  47. undo_trans.append(logs[i].split()[0])
  48. elif len(list(logs[i].split())) < 3:
  49. opers.append(logs[i].split()[1])
  50. elif len(list(logs[i].split())) == 3:
  51. connect_db(logs[i].split(',')[0].split('.')[0][5:], # Table
  52. logs[i].split(',')[0].split('.')[2], # Column
  53. logs[i].split(',')[-1][1:], # after
  54. pkdict[logs[i].split(',')[0].split('.')[0][5:]], # Primary_Key
  55. logs[i].split(',')[0].split('.')[1])# Pri_Value
  56. elif len(list(logs[i].split())) == 4:
  57. connect_db(logs[i].split(',')[0].split('.')[0][5:], # Table
  58. logs[i].split(',')[0].split('.')[2], # Column
  59. logs[i].split(',')[-1][1:], # after
  60. pkdict[logs[i].split(',')[0].split('.')[0][5:]], # Primary_Key
  61. logs[i].split(',')[0].split('.')[1]) # Pri_Value
  62.  
  63. # Undo 과정
  64. for i in range(len(logs)-1, -1, -1):
  65. if logs[i].split()[0] in undo_trans and logs[i].split()[1] == 'start':
  66. newlog = open("abort.txt", "a")
  67. newlog.write(logs[i].split()[0] + ' ' +'abort' + '\n')
  68. newlog.close()
  69. undo_trans.remove(logs[i].split()[0])
  70. elif logs[i].split()[0] in undo_trans and len(list(logs[i].split())) == 4:
  71. connect_db(logs[i].split(',')[0].split('.')[0][5:], # Table
  72. logs[i].split(',')[0].split('.')[2], # Column
  73. logs[i].split(',')[-2][1:], # before
  74. pkdict[logs[i].split(',')[0].split('.')[0][5:]], # Primary_Key
  75. logs[i].split(',')[0].split('.')[1]) # Pri_Value
  76. newlog = open("abort.txt", "a")
  77. newlog.write(logs[i].split()[0]
  78. + ' '
  79. + logs[i].split(',')[0].split('.')[0][5:] + '.' # # Table
  80. + logs[i].split(',')[0].split('.')[1] + '.' # Pri_Value
  81. + logs[i].split(',')[0].split('.')[2] + ' '# Column
  82. + logs[i].split(',')[-2][1:] # before
  83. + '\n')
  84. newlog.close()
  85. print('abort')
  86. parse_log_1()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement