def dbDefInfo = new XmlParser().parse(new File(".\\DbDef.xml"))
def file1Path = ".\\fullConfig1.xml"
def file2Path = ".\\fullConfig2.xml"
println "~~~~~~~~~~~~~~~~~~~~~~~~~~ $file1Path to $file2Path ~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"
compareXml(new File(file1Path),new File(file2Path),dbDefInfo)
println "\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~ $file2Path to $file1Path ~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"
compareXml(new File(file2Path),new File(file1Path),dbDefInfo)
println 'fineshed'
def compareXml(file1,file2,dbDef){
def tablelist1 = new XmlParser().parse(file1)
def tablelist2 = new XmlParser().parse(file2)
tablelist1.DBTABLE.each {dbtable1 ->
for(dbRecord in dbtable1.DBRECORD){
def otherDbRecord = findDbRecord(dbtable1,dbRecord,tablelist2,dbDef)
compareDbRecord(dbRecord,otherDbRecord)
}
}
}
def findDbRecord(dbtable1,dbRecord,tablelist2,dbDef){
def tableDef = dbDef.Table.find {"${it.@Name}" == "${dbtable1.@name}"}
if (tableDef == null){
println "${dbtable1.@name} is not exist in dbdef" ;
return null;
}
def priKeyMap = getPriKeyInfo(tableDef)
def prikeyInfo = priKeyMap["${dbtable1.@name}"]
def rdnMap = [:]
dbRecord.FIELD.findAll{prikeyInfo.contains("${it.@name}")}.each{rdnMap.put("${it.@name}","${it.@value}")}
def dbtable2 =
tablelist2.DBTABLE.find{"${it.@name}" == "${dbtable1.@name}"}
if(dbtable2 == null)
return null
else
return dbtable2.DBRECORD.find{dbRecord2 ->
def field2s = dbRecord2.FIELD.findAll{rdnMap.containsKey("${it.@name}") && rdnMap.get("${it.@name}") == "${it.@value}"}
field2s.size() == rdnMap.size()
}
}
def compareDbRecord(dbRecord1,dbRecord2){
if (dbRecord2==null){
printDbRecord(dbRecord1,dbRecord2)
return;
}
def sign1 = dbRecord1.FIELD.find{filed1 ->
dbRecord2.FIELD.find{filed2 ->
"${filed1.@name}"=="${filed2.@name}"?"${filed1.@value}"!="${filed2.@value}":false
}
}
def sign2 = dbRecord2.FIELD.find{filed1 ->
dbRecord1.FIELD.find{filed2 ->
"${filed1.@name}"=="${filed2.@name}"?"${filed1.@value}"!="${filed2.@value}":false
}
}
if (sign1 || sign2)
{
printDbRecord(dbRecord1,dbRecord2)
}
}
def printDbRecord(dbRecord1,dbRecord2){
dbRecord1.FIELD.each{field ->
field.attributes().each{print "$it.key $it.value "}
}
println ''
if (dbRecord2!=null)
dbRecord2.FIELD.each{field ->
field.attributes().each{print "$it.key $it.value "}
}
println '\n'
}
def getPriKeyInfo(tableDef){
def priKeyMap = [:]
for(priKey in tableDef.PriKey){
def tableName = "${priKey.@Name}"[0..("${priKey.@Name}".lastIndexOf("_PK")-1)]
priKeyMap.put(tableName,priKey.PKI.collect{"${it.@Field}"})
}
return priKeyMap
}