Grails 中的重要数据查询

发布于 2024-11-09 02:01:59 字数 585 浏览 0 评论 0原文

想象一下 grails 中的以下问题,

您有某种带有数字属性的审计跟踪域类。例如,写入 Scrum 项目当前燃尽值的类:

class burndown {
    Date createDate
    int  value
}

每次更新任务时,您的项目都使用此类来存储当前燃尽值 - 这意味着每天多次。

现在您想要绘制一个图表,其中包含每天最后存储的值。

一个 SQL 语句可能看起来像

select 
  * 
from 
  table 
where 
  id in (
    select 
      max(id) 
    from 
      table 
    group by 
      TO_CHAR(create_date,'yyyyddmm')
  )

现在我的问题:如何在 grails 中执行这样的查询?

如果我必须使用这样的SQL语句,如何避免将表名和列名硬编码在语句中?

PS:此代码尚未经过测试。只是从我的脑海里写下来......但我想你明白我想问的是什么

imagine the following problem in grails

you have some kind of audit trail domain class with numeric properties. For instance a class in which the current burndown value of your scrum project is written:

class burndown {
    Date createDate
    int  value
}

Your projects uses this class to store the current burndown value each time you update a task - this means several times a day.

Now you want to plot a diagram with the last stored value for each day.

An SQL statement for this could look something like

select 
  * 
from 
  table 
where 
  id in (
    select 
      max(id) 
    from 
      table 
    group by 
      TO_CHAR(create_date,'yyyyddmm')
  )

Now my question: how do you do such a query in grails?

If I have to use such an SQL statement, how to I avoid to put the table and column names hard coded in the statement?

PS: this code hasn't been tested. just written down from my mind... but I guess you feel what I want to ask

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£烟消云散 2024-11-16 02:01:59

对于初学者,您很可能希望将 createDate 重命名为 dateCreated,因为如果您使用该名称,Grails 会自动为您设置它,因此您只需指定类的“真实”属性。不过这对这个问题来说并不重要。

如果您想要创建日期之前的最新项目,可以通过几种不同的方式来执行此操作,但我认为这是最有意义的:

def mostRecent = Burndown.listOrderByDateCreated(max: 1, order: 'desc')[0]

或者如果您保留您的姓名,

def mostRecent = Burndown.listOrderByCreateDate(max: 1, order: 'desc')[0]

这在 http://grails.org/doc/latest/ - 这是一本很好的手册,值得一读。

For starters you'll most likely want to rename createDate to dateCreated since it's automatically set for you by Grails if you use that name, so you only need to specify values for the 'real' properties of the class. It's not important to this issue though.

If you want the most recent item by created date, there are a few different ways you could do this but I think this makes the most sense:

def mostRecent = Burndown.listOrderByDateCreated(max: 1, order: 'desc')[0]

or if you retain your name

def mostRecent = Burndown.listOrderByCreateDate(max: 1, order: 'desc')[0]

This is described at http://grails.org/doc/latest/ - it's a fine manual, worthy of reading.

ㄖ落Θ余辉 2024-11-16 02:01:59

不是 Grails 用户,因此应由 Grails'ist 相应更正以下内容。

如果域相关,请尝试以下操作:

Foo.findAllInList(Bar.list([group:'create_date']))

Not a Grails user, so below should be corrected accordingly by a Grails'ist

If domains are related, try something like:

Foo.findAllInList(Bar.list([group:'create_date']))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文