Grails / Groovy:修改一个“查询”运行时关闭 [英] Grails/Groovy: Modify a "query" closure at runtime

查看:125
本文介绍了Grails / Groovy:修改一个“查询”运行时关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个gorm域类中,我可以做

pre code def f = {
property1 {
eq('attr',0)
}
}

MyDomainClass.list(q)

我如何在运行时修改闭包'q'(或创建一个包含闭包'q'的限制的新闭包),例如我可以添加另一个属性限制?






更多详情

其实我的问题是如何在一个Domain Class Hierarchy中创建组合标准。

  class父类{
int pAttr

static def getCriteria(){
def dummyParentCriteria = {
eq('pAttr',0)
}
}
}

class Child extends Parent {
int cAttr

static def getCriteria(){
def dummyChildCriteria = {
//(1)
eq('cAttr',0)
}
}
}

<强在'dummyChildCriteria'中我想避免重复父母的限制。



我想以某种方式将Parent的getCriteria的结果合并到(1)






带有命名查询继承的解决方案

 class Parent {
int pAttr
static namedQueries = {
parentCriteria {
eq('pAttr',0)
}

}

class Child扩展父项{
int cAttr
static namedQueries = {
childCriteria {
parentCriteria()
eq('cAttr',0)
}
}
}

但是如果有人知道最初问题的答案,那么知道它会很高兴!

解决方案

由于Grails 2.0.x,您可以使用 分离标准查询 有m的包括允许您创建通用的可重用条件查询,执行子查询以及执行批量更新/删除。



使用Detached Criteria,您可以使用 Where Queries 进行查询组合。

  def parentCriteria = {
pAttr == 0
}

def childCriteria = {
cAttr = = 0
}

def parentQuery = Parent.where(parentCriteria)
def childQuery = Child.where(parentCriteria&& childCriteria)


In a gorm domain class, I can do

def q = {
  property1{
    eq('attr', 0)
  }
}

MyDomainClass.list(q)

How could I modify the closure 'q' (or create a new closure that would contain the restrictions that closure 'q' has) at runtime so for example I could add another property restriction?


More details

Actually my problem is how to create combined criteria in a Domain Class Hierarchy.

class Parent{
  int pAttr

  static def getCriteria(){
    def dummyParentCriteria = {
      eq('pAttr', 0)
    }
  }
}

class Child extends Parent{
  int cAttr

  static def getCriteria(){
    def dummyChildCriteria = {
      // (1) 
      eq('cAttr', 0)
    }
  }
}

In 'dummyChildCriteria' I want to avoid repeating parent's restrictions.

I would like to somehow combine the result of Parent's getCriteria there (1)


A solution with named queries inheritance

class Parent{
  int pAttr
  static namedQueries = {
     parentCriteria{
       eq('pAttr', 0)
     }
  }
}

class Child extends Parent {
  int cAttr
  static namedQueries = {
     childCriteria{
       parentCriteria() 
       eq('cAttr', 0)
     }
  }
}

But if someone knows the answer to the initial question it would be nice to know!

解决方案

Since Grails 2.0.x, you can use Detached Criteria queries that have many uses including allowing you to create common reusable criteria queries, execute subqueries and execute batch updates/deletes.

With Detached Criteria, you can use Where Queries doing query composition.

def parentCriteria = {
    pAttr == 0
}

def childCriteria = {
    cAttr == 0
}

def parentQuery = Parent.where(parentCriteria)
def childQuery = Child.where(parentCriteria && childCriteria)

这篇关于Grails / Groovy:修改一个“查询”运行时关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆