不是直接对比数据库,而是把数据库创建的sql导出来,对比sql,其实就是对比两个文本文件。
@H_404_4@import re
@H_404_4@def getPowerFile():
f = open("C:\\Users\\Administrator\\Desktop\\sql对比\\Z_T.sql","r")
@H_404_4@return f
@H_404_4@def get主干数据库File():
f = open("C:\\Users\\Administrator\\Desktop\\sql对比\\I_T.sql","r")
@H_404_4@return f
@H_404_4@def getDict(file,pattern,offset):
match = pattern.findall(file.read().lower())
retDict = {}
@H_404_4@if match:
@H_404_4@for index @H_404_4@in range(len(match)):
#print(match[index])
wordList = match[index].replace('\n',' ').replace(';',' ').split(' ')
retDict[wordList[offset]] = match[index]
@H_404_4@return retDict
@H_404_4@def dumpDict(tmpDict):
@H_404_4@for key @H_404_4@in tmpDict:
print("-------------------------------")
print("key:",key)
print("value:\n",tmpDict[key])
#print("dict num : ",len(tmpDict))
@H_404_4@def dumpList(tmpList):
@H_404_4@if @H_404_4@not tmpList:
print("空")
tmpList.sort()
@H_404_4@for index @H_404_4@in range(len(tmpList)):
print(tmpList[index])
#print("list num : ",len(tmpList))
@H_404_4@def getNew(powerDict,主干数据库Dict):
newList = []
@H_404_4@for key @H_404_4@in powerDict:
@H_404_4@if key @H_404_4@not @H_404_4@in 主干数据库Dict:
newList.append(key)
@H_404_4@return newList
@H_404_4@def getChange(powerDict,主干数据库Dict):
changeList = []
@H_404_4@for key @H_404_4@in powerDict:
@H_404_4@if key @H_404_4@in 主干数据库Dict:
主干数据库tmp = re.sub('--.*?\n|\s+|,','',主干数据库Dict[key])
主干数据库tmp = re.sub('/\*.*\*/',主干数据库tmp)
powertmp = re.sub('--.*?\n|\s+|,powerDict[key])
powertmp = re.sub('/\*.*\*/',powertmp)
@H_404_4@if 主干数据库tmp != powertmp:
changeList.append(key)
@H_404_4@return changeList
#---------------------------generator----------------------------------
@H_404_4@def handleGenerator():
pattern = re.compile('create generator .*;\n.*;',re.M|re.I)
powerDict = getDict(getPowerFile(),2)
主干数据库Dict = getDict(get主干数据库File(),2)
newList = getNew(powerDict,主干数据库Dict)
changeList = getChange(powerDict,主干数据库Dict)
print("目前Generator数目:",len(powerDict),"主干数据库目前Generator数目:",len(主干数据库Dict))
print("新建Generator数目:",len(newList),"修改Generator数目:",len(changeList))
print("新建Generator名列表:")
dumpList(newList)
print("\n修改Generator列表:")
dumpList(changeList)
print("\n修改Generator详细信息:")
@H_404_4@for index @H_404_4@in range(len(changeList)):
print("主干数据库:\n",主干数据库Dict[changeList[index]])
print(":\n",powerDict[changeList[index]])
print("\n")
#---------------------------procedure----------------------------------
@H_404_4@def handleProcedure():
pattern = re.compile('create or alter procedure .*?^end;',re.M|re.I|re.S)
powerDict = getDict(getPowerFile(),4)
主干数据库Dict = getDict(get主干数据库File(),4)
newList = getNew(powerDict,主干数据库Dict)
print("目前存储过程数目:","主干数据库目前存储过程数目:",len(主干数据库Dict))
print("新建存储过程数目:","修改存储过程数目:",len(changeList))
print("新建存储过程名列表:")
dumpList(newList)
print("\n修改存储过程列表:")
dumpList(changeList)
#for index in range(len(changeList)):
#方便验证,不打印内容,只打印到returns,内容需要用beyondCampare比较
#print("主干数据库:\n ",主干数据库match.group(0))
#print(":\n ",powermatch.group(0))
#print("主干数据库:\n ",主干数据库Dict[changeList[index]])
#print(":\n ",powerDict[changeList[index]])
#print("\n")
print("\n")
#---------------------------trigger----------------------------------
@H_404_4@def handleTrigger():
pattern = re.compile('create or alter trigger .*?^end;',主干数据库Dict)
print("目前触发器数目:","主干数据库目前触发器数目:",len(主干数据库Dict))
print("新建触发器数目:","修改触发器数目:",len(changeList))
print("新建触发器名列表:")
dumpList(newList)
print("\n修改触发器列表:")
dumpList(changeList)
print("\n修改触发器详细信息:")
@H_404_4@for index @H_404_4@in range(len(changeList)):
print("主干数据库:\n ",主干数据库Dict[changeList[index]])
print(":\n ",powerDict[changeList[index]])
print("\n")
print("\n")
#---------------------------data 目前只处理 system_paramete 表新增数据----------------------------------
@H_404_4@def handleData():
pattern = re.compile('insert into system_parameter .*?;',9)
主干数据库Dict = getDict(get主干数据库File(),9)
newList = getNew(powerDict,主干数据库Dict)
print("目前system_parameter数目:","主干数据库目前system_parameter数目:",len(主干数据库Dict))
print("新建system_parameter数目:","修改system_parameter数目:",len(changeList))
print("新建system_parameter列表:")
@H_404_4@for index @H_404_4@in range(len(newList)):
print(powerDict[newList[index]])
print("\n修改system_parameter列表:")
@H_404_4@for index @H_404_4@in range(len(changeList)):
print("主干数据库:\n ",powerDict[changeList[index]])
print("\n")
print("\n")
#---------------------------table----------------------------------
@H_404_4@def handleTable():
pattern = re.compile('create table .*?;',2)
tmpList = []
@H_404_4@for key @H_404_4@in 主干数据库Dict:
tmpList.append(key)
#dumpList(tmpList)
#dumpDict(主干数据库Dict)
newList = getNew(powerDict,主干数据库Dict)
#sql导出的表5张临时表没有统计,2张系统表多于
print("目前表数目:",len(powerDict) + 5 - 2,"主干数据库目前表数目:",len(主干数据库Dict) + 5 - 2)
print("新建表数目:","修改表数目:",len(changeList))
print("新建表名列表:")
dumpList(newList)
print("\n修改表名明列表:")
dumpList(changeList)
print("\n")
#---------------------------table column----------------------------------
@H_404_4@def getTableColumnDict(createTablesql):
pattern = re.compile('^ .*?\n',re.M|re.I)
match = pattern.findall(createTablesql.lower())
retDict = {}
@H_404_4@if match:
#print("match num:",len(match))
@H_404_4@for index @H_404_4@in range(len(match)):
#print(match[index])
p = re.compile(r'\S+')
wordList = p.findall(match[index])
#print("size of dict:",len(wordList))
retDict[wordList[0]] = match[index].rstrip(',')
@H_404_4@return retDict
@H_404_4@def handleColumn():
print("修改的表的详细信息:")
pattern = re.compile('create table .*?;',2)
@H_404_4@for key @H_404_4@in powerDict:
@H_404_4@if key @H_404_4@in 主干数据库Dict @H_404_4@and (powerDict[key] != 主干数据库Dict[key]):
powerColumnDict = getTableColumnDict(powerDict[key])
主干数据库ColumnDict = getTableColumnDict(主干数据库Dict[key])
newList = getNew(powerColumnDict,主干数据库ColumnDict)
changeList = getChange(powerColumnDict,主干数据库ColumnDict)
print("修改的表名:",key)
#dumpDict(powerColumnDict)
print("主干数据库创建语句:\n",主干数据库Dict[key])
print("表创建语句:\n",powerDict[key])
print("新增","列:")
dumpList(newList)
#dumpDict(主干数据库ColumnDict)
print("修改",len(changeList),"列:")
dumpList(changeList)
print("\n")
print("\n")
@H_404_4@return
''' print("<html>") print("<body>") print("<p>") '''
handleTable()
handleColumn()
handleProcedure()
handleTrigger()
handleData()
handleGenerator()
''' print("</p>") print("</body>") print("</html>") '''