keep moving!

We must not cease from exploration. And the end of all our exploring will be to arrive where we began and to know the place for the first time.
随笔 - 37, 文章 - 2, 评论 - 3, 引用 - 0
数据加载中……

数据文件比较

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
}

posted on 2008-10-02 17:10 大石头 阅读(287) 评论(0)  编辑  收藏 所属分类: Groovy


只有注册用户登录后才能发表评论。


网站导航: