利用正则表达式对比数据库

前端之家收集整理的这篇文章主要介绍了利用正则表达式对比数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

不是直接对比数据库,而是把数据库创建的sql导出来,对比sql,其实就是对比两个文本文件

  1. import re
  2.  
  3. def getPowerFile():
  4. f = open("C:\\Users\\Administrator\\Desktop\\sql对比\\Z_T.sql","r")
  5. return f
  6.  
  7. def get主干数据库File():
  8. f = open("C:\\Users\\Administrator\\Desktop\\sql对比\\I_T.sql","r")
  9. return f
  10.  
  11. def getDict(file,pattern,offset):
  12. match = pattern.findall(file.read().lower())
  13. retDict = {}
  14. if match:
  15. for index in range(len(match)):
  16. #print(match[index])
  17. wordList = match[index].replace('\n',' ').replace(';',' ').split(' ')
  18. retDict[wordList[offset]] = match[index]
  19. return retDict
  20.  
  21. def dumpDict(tmpDict):
  22. for key in tmpDict:
  23. print("-------------------------------")
  24. print("key:",key)
  25. print("value:\n",tmpDict[key])
  26. #print("dict num : ",len(tmpDict))
  27.  
  28. def dumpList(tmpList):
  29. if not tmpList:
  30. print("空")
  31. tmpList.sort()
  32. for index in range(len(tmpList)):
  33. print(tmpList[index])
  34. #print("list num : ",len(tmpList))
  35.  
  36. def getNew(powerDict,主干数据库Dict):
  37. newList = []
  38. for key in powerDict:
  39. if key not in 主干数据库Dict:
  40. newList.append(key)
  41. return newList
  42.  
  43. def getChange(powerDict,主干数据库Dict):
  44. changeList = []
  45. for key in powerDict:
  46. if key in 主干数据库Dict:
  47. 主干数据库tmp = re.sub('--.*?\n|\s+|,','',主干数据库Dict[key])
  48. 主干数据库tmp = re.sub('/\*.*\*/',主干数据库tmp)
  49. powertmp = re.sub('--.*?\n|\s+|,powerDict[key])
  50. powertmp = re.sub('/\*.*\*/',powertmp)
  51. if 主干数据库tmp != powertmp:
  52. changeList.append(key)
  53. return changeList
  54. #---------------------------generator----------------------------------
  55. def handleGenerator():
  56. pattern = re.compile('create generator .*;\n.*;',re.M|re.I)
  57. powerDict = getDict(getPowerFile(),2)
  58. 主干数据库Dict = getDict(get主干数据库File(),2)
  59. newList = getNew(powerDict,主干数据库Dict)
  60. changeList = getChange(powerDict,主干数据库Dict)
  61. print("目前Generator数目:",len(powerDict),"主干数据库目前Generator数目:",len(主干数据库Dict))
  62. print("新建Generator数目:",len(newList),"修改Generator数目:",len(changeList))
  63. print("新建Generator名列表:")
  64. dumpList(newList)
  65. print("\n修改Generator列表:")
  66. dumpList(changeList)
  67. print("\n修改Generator详细信息:")
  68. for index in range(len(changeList)):
  69. print("主干数据库:\n",主干数据库Dict[changeList[index]])
  70. print(":\n",powerDict[changeList[index]])
  71. print("\n")
  72. #---------------------------procedure----------------------------------
  73. def handleProcedure():
  74. pattern = re.compile('create or alter procedure .*?^end;',re.M|re.I|re.S)
  75. powerDict = getDict(getPowerFile(),4)
  76. 主干数据库Dict = getDict(get主干数据库File(),4)
  77. newList = getNew(powerDict,主干数据库Dict)
  78. print("目前存储过程数目:","主干数据库目前存储过程数目:",len(主干数据库Dict))
  79. print("新建存储过程数目:","修改存储过程数目:",len(changeList))
  80. print("新建存储过程名列表:")
  81. dumpList(newList)
  82. print("\n修改存储过程列表:")
  83. dumpList(changeList)
  84. #for index in range(len(changeList)):
  85. #方便验证,不打印内容,只打印到returns,内容需要用beyondCampare比较
  86. #print("主干数据库:\n ",主干数据库match.group(0))
  87. #print(":\n ",powermatch.group(0))
  88. #print("主干数据库:\n ",主干数据库Dict[changeList[index]])
  89. #print(":\n ",powerDict[changeList[index]])
  90. #print("\n")
  91. print("\n")
  92. #---------------------------trigger----------------------------------
  93. def handleTrigger():
  94. pattern = re.compile('create or alter trigger .*?^end;',主干数据库Dict)
  95. print("目前触发器数目:","主干数据库目前触发器数目:",len(主干数据库Dict))
  96. print("新建触发器数目:","修改触发器数目:",len(changeList))
  97. print("新建触发器名列表:")
  98. dumpList(newList)
  99. print("\n修改触发器列表:")
  100. dumpList(changeList)
  101. print("\n修改触发器详细信息:")
  102. for index in range(len(changeList)):
  103. print("主干数据库:\n ",主干数据库Dict[changeList[index]])
  104. print(":\n ",powerDict[changeList[index]])
  105. print("\n")
  106. print("\n")
  107. #---------------------------data 目前只处理 system_paramete 表新增数据----------------------------------
  108. def handleData():
  109. pattern = re.compile('insert into system_parameter .*?;',9)
  110. 主干数据库Dict = getDict(get主干数据库File(),9)
  111. newList = getNew(powerDict,主干数据库Dict)
  112. print("目前system_parameter数目:","主干数据库目前system_parameter数目:",len(主干数据库Dict))
  113. print("新建system_parameter数目:","修改system_parameter数目:",len(changeList))
  114. print("新建system_parameter列表:")
  115. for index in range(len(newList)):
  116. print(powerDict[newList[index]])
  117. print("\n修改system_parameter列表:")
  118. for index in range(len(changeList)):
  119. print("主干数据库:\n ",powerDict[changeList[index]])
  120. print("\n")
  121. print("\n")
  122. #---------------------------table----------------------------------
  123. def handleTable():
  124. pattern = re.compile('create table .*?;',2)
  125. tmpList = []
  126. for key in 主干数据库Dict:
  127. tmpList.append(key)
  128. #dumpList(tmpList)
  129. #dumpDict(主干数据库Dict)
  130. newList = getNew(powerDict,主干数据库Dict)
  131. #sql导出的表5张临时表没有统计,2张系统表多于
  132. print("目前表数目:",len(powerDict) + 5 - 2,"主干数据库目前表数目:",len(主干数据库Dict) + 5 - 2)
  133. print("新建表数目:","修改表数目:",len(changeList))
  134. print("新建表名列表:")
  135. dumpList(newList)
  136. print("\n修改表名明列表:")
  137. dumpList(changeList)
  138. print("\n")
  139. #---------------------------table column----------------------------------
  140. def getTableColumnDict(createTablesql):
  141. pattern = re.compile('^ .*?\n',re.M|re.I)
  142. match = pattern.findall(createTablesql.lower())
  143. retDict = {}
  144. if match:
  145. #print("match num:",len(match))
  146. for index in range(len(match)):
  147. #print(match[index])
  148. p = re.compile(r'\S+')
  149. wordList = p.findall(match[index])
  150. #print("size of dict:",len(wordList))
  151. retDict[wordList[0]] = match[index].rstrip(',')
  152. return retDict
  153. def handleColumn():
  154. print("修改的表的详细信息:")
  155. pattern = re.compile('create table .*?;',2)
  156. for key in powerDict:
  157. if key in 主干数据库Dict and (powerDict[key] != 主干数据库Dict[key]):
  158. powerColumnDict = getTableColumnDict(powerDict[key])
  159. 主干数据库ColumnDict = getTableColumnDict(主干数据库Dict[key])
  160. newList = getNew(powerColumnDict,主干数据库ColumnDict)
  161. changeList = getChange(powerColumnDict,主干数据库ColumnDict)
  162. print("修改的表名:",key)
  163. #dumpDict(powerColumnDict)
  164. print("主干数据库创建语句:\n",主干数据库Dict[key])
  165. print("表创建语句:\n",powerDict[key])
  166. print("新增","列:")
  167. dumpList(newList)
  168. #dumpDict(主干数据库ColumnDict)
  169. print("修改",len(changeList),"列:")
  170. dumpList(changeList)
  171. print("\n")
  172. print("\n")
  173. return
  174. ''' print("<html>") print("<body>") print("<p>") '''
  175. handleTable()
  176. handleColumn()
  177. handleProcedure()
  178. handleTrigger()
  179. handleData()
  180. handleGenerator()
  181. ''' print("</p>") print("</body>") print("</html>") '''

猜你在找的正则表达式相关文章