我正在动态地构建createCriteria。到现在为止还挺好:
obj是我想要返回的域对象rulesList是一张 map 列表,其中包含要搜索的字段,要使用的运算符以及要搜索的值

def c = obj.createCriteria()
l = c.list (max: irows, offset: offset) {
    switch(obj){           //constrain results to those relevant to the user
        case Vehicle:
            eq("garage", usersGarage)
            break
        case Garage:
            users {
                idEq(user.id)
            }
            break
    }
    rulesList.each { rule ->
        switch(rule['op']){
            case 'eq':
                eq("${rule['field']}", rule['value'])
                break
            case 'ne':
                ne("${rule['field']}", rule['value'])
                break
            case 'gt':
                gt("${rule['field']}", rule['value'])
                break;
            case 'ge':
                ge("${rule['field']}", rule['value'])
                break
            case 'lt':
                lt("${rule['field']}", rule['value'])
                break
            case 'le':
                le("${rule['field']}", rule['value'])
                break
            case 'bw':
                ilike("${rule['field']}", "${rule['value']}%")
                break
            case 'bn':
                not{ilike("${rule['field']}", "${rule['value']}%")}
                break
            case 'ew':
                ilike("${rule['field']}", "%${rule['value']}")
                break
            case 'en':
                not{ilike("${rule['field']}", "%${rule['value']}")}
                break
            case 'cn':
                ilike("${rule['field']}", "%${rule['value']}%")
                break
            case 'nc':
                not{ilike("${rule['field']}", "%${rule['value']}%")}
                break
            }
        }
    }
}

上面的代码工作正常,并且在switch语句中看起来有些冗长。但是,如果我想添加功能以选择匹配任何规则或全部规则,该怎么办?我需要有条件地将规则放在or{}中。我不能做这样的事情
if(groupOp == 'or'){
    or{
}

在我通过rulesList然后
if(groupOp == 'or'){
    }
}

之后。我想做的就是为每个条件重复执行以下代码:
if(groupOp == 'or'){
    or{
        rulesList.each { rule ->
            switch(rule['op']){
                ...
            }
        }
    }
}
else{
    rulesList.each { rule ->
        switch(rule['op']){
            ...
        }
    }

现在,代码看起来很草率和重复。假设我要搜索域对象的属性的属性? (例如:我想退回轮胎是某个品牌的车辆; vehicle.tires.brand,或者其驾驶员匹配名称的车辆; vehicle.driver.name)。我需要做类似的事情:
switch(rule['op']){
    case 'eq':
        switch(thePropertiesProperty){
            case Garage:
                garage{
                    eq("${rule['field']}", rule['value'])
                }
                break
            case Driver:
                driver{
                     eq("${rule['field']}", rule['value'])
                }
                break
        }
        break
    case 'ne':
        ...
}

最佳答案

首先,您可以通过使用GString作为方法名称来简化您的大开关:

case ~/^(?:eq|ne|gt|ge|lt|le)$/:
  "${rule['op']}"("${rule['field']}", rule['value'])
  break

相同的技巧适用于和/或:
"${(groupOp == 'or') ? 'or' : 'and'}"() {
  rulesList.each { rule ->
    switch(rule['op']){
        ...
    }
  }
}

或者您可以先将闭包分配给变量,然后根据需要调用or(theClosure)and(theClosure)。最后,如果要添加“属性的属性”搜索,
createAlias('driver', 'drv')
createAlias('garage', 'grg')

在条件闭包的顶部,您可以查询诸如eq('drv.name', 'Fred')之类的内容,而不必添加中间的driver {...}garage {...}节点。

09-08 06:46