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
数据加载中……

通过数据字典校验xml

def tablelist1 = new XmlParser().parse(new File(".\\fullConfig1.xml"))
def dbDef = new XmlParser().parse(new File("C:\\groovy\\DbDef.xml"))

/**
 * 正向校验
 */
tablelist1.DBTABLE.each {dbtable1 ->
    def tableDef = dbDef.Table.find {"${it.@Name}" == "${dbtable1.@name}"}
    if (tableDef == null)
    println "${dbtable1.@name} is not exist in dbdef" ; //通过数据字典,校验表个数
    else{
        if(dbtable1.DBRECORD.size() > new Integer(tableDef.@MaxRecNum))
        println "${dbtable1.@name} num is larger than ${tableDef.@MaxRecNum}"; //通过数据字典,校验记录个数

        dbtable1.DBRECORD.eachWithIndex{elem, i ->
            if(i==0)
            elem.FIELD.each{field ->
                if(tableDef.Field.find{"${it.@Name}" == "${field.@name}"} == null)
                println "can't find ${dbtable1.@name}:${field.@name}  in dbdef"
            }
        } //通过数据字典,校验字段名称

        checkTableRelation(tablelist1,tableDef,dbtable1)
    }
}

/**
 * 反向校验
 */
dbDef.Table.each{tableDef ->
    def tableData = tablelist1.DBTABLE.find {"${it.@name}" == "${tableDef.@Name}"}
    if (tableData == null)
    println "${tableDef.@Name} is not exist in data file" ; //校验少表的情况
    else if(tableData.DBRECORD.size()>0){
        tableDef.Field.each{ fieldInfo ->
            if(tableData.DBRECORD[0].FIELD.find{"${it.@name}" == "${fieldInfo.@Name}"} == null)
            println "can't find ${tableDef.@Name}:${fieldInfo.@Name} in data file" //校验少字段的情况
        }
    }
}
println 'finished'

 

/**------------------------------------------------------------------------------------------------------------------*/

def checkPriKeyRelation(tableDef,dbtable1,priKeyMap){
    def keyArray = []
    def key = priKeyMap
    dbtable1.DBRECORD.each{dbRecord ->
        def keyvalue  = ''
        key.each{ priKey ->
            def _field = dbRecord.FIELD.find{
                  "${priKey}" == "${it.@name}"
            }
            keyvalue += "${_field.@name}:${_field.@value} "
        }

        if(keyArray.contains(keyvalue))
          println "${dbtable1.@name}:${keyvalue} has exist"  //校验主键唯一
        else
          keyArray << keyvalue
    }
}

def checkRefKeyRelation(fullData,dbtable1,tableDef,refKeyMap,priKeyMap){
    if(refKeyMap==null || refKeyMap.size()==0) return
   
    for(dbRecord in dbtable1.DBRECORD){
        def refKeyInfos = refKeyMap
        def prikeyInfo = priKeyMap
        refKeyMap.each{refInfo ->       
          if(fullData.DBTABLE.find{"${it.@name}" == "${refInfo.key}"}==null)
           println "ref table (${it.key}) is not founded "
          else
          {
           def localTable = "${tableDef.@Name}"
           def refTable = "${refInfo.key}"
           def fieldsInfo = refInfo.value
           def keyInfo = [:]
           fieldsInfo.each{localField ->
               def fieldxml = dbRecord.FIELD.find{"${it.@name}" == "${localField[0]}"}
              if(fieldxml == null)
               println "${localField[0]} in ${tableDef.@Name} isn't found"
            else
               keyInfo.put("${localField[1]}","${fieldxml.@value}")
           }
             def dbRecords = fullData.DBTABLE.find{"${it.@name}" == "${refTable}"}.DBRECORD
             def sign = false
             for (_dbRecord in dbRecords){
              def _sign = true
              keyInfo.each{ attr->
               if(_dbRecord.FIELD.find{"${it.@name}" == "${attr.key}" && "${it.@value}" == "${attr.value}"}==null){
                _sign= false
               }              
              }
                if(_sign) sign = true              
             }
            
             if(!sign){
              println "can find : table Name ${dbtable1.@name} ref table ${refTable},value ${keyInfo} "
              //校验外键约束
             }
          }
        }
    }
}

def checkTableRelation(fullData,tableDef,dbtable1){
    def priKeyMap = getPriKeyInfo(tableDef)
    def refKeyMap = getRefKeyInfo(tableDef)
    checkPriKeyRelation(tableDef,dbtable1,priKeyMap)
    checkRefKeyRelation(fullData,dbtable1,tableDef,refKeyMap,priKeyMap)
}

def getPriKeyInfo(tableDef){
 tableDef.PriKey.PKI.collect{"${it.@Field}"}
}

def getRefKeyInfo(tableDef){
    def refKeyMap = [:]
    for(refKey in tableDef.RefKey){
        def map= refKey.RKI.collect{["${it.@Field}","${it.@RefField}"]}
        if(!refKeyMap.containsKey("${refKey.@RefTable}")) refKeyMap.put("${refKey.@RefTable}",map)
    }
    return refKeyMap
}

 

posted on 2008-10-02 23:54 大石头 阅读(385) 评论(0)  编辑  收藏


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


网站导航: