JOOQ时间戳精度查询 [英] JOOQ Timestamp precision on query

查看:213
本文介绍了JOOQ时间戳精度查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要转换为JOOQ查询的PSQL原始查询:

  SELECT DISTINCT date_trunc('day ',ref_date)从收入
中引用
,而probos_id =:probosId

我能够创建的最佳版本是:

  Result< Record1< Timestamp>>。结果= createQueryBuilder()
.selectDistinct(incomeTable.REF_DATE.cast(Date.class).cast(Timestamp.class).as( refdate))
.from(incomeTable)
.where(incomeTable.PROBOS_ID.eq(probosId))
.fetch();

这是生成的查询:



<$ p $ p> 选择不同的
强制转换(以cast( public。 income。 ref_date作为日期)作为时间戳)作为 refdate
来自 public。收入
,其中 public。 income。 probos_id =?

我想找到一种更好的方法来设置精度(例如PSQL中的date_trunc)



如果可能的话,我想找到一个本机解决方案而不扩展DSL。



谢谢!

解决方案

使用 DSL.trunc(Field,DatePart) ,即

  Result< Record1< Timestamp>>结果= createQueryBuilder()
.selectDistinct(trunc(incomeTable.REF_DATE,DatePart.DAY).as( refdate))
.from(incomeTable)
.where(incomeTable.PROBOS_ID。 eq(probosId))
.fetch();


I have this raw query for PSQL that I would like to transform to JOOQ query:

SELECT DISTINCT date_trunc('day', ref_date) AS refdate
FROM income
WHERE probos_id = :probosId

The best version I was able to create is:

Result<Record1<Timestamp>> result = createQueryBuilder()
    .selectDistinct(incomeTable.REF_DATE.cast(Date.class).cast(Timestamp.class).as("refdate"))
    .from(incomeTable)
    .where(incomeTable.PROBOS_ID.eq(probosId))
    .fetch();

This is the generated query:

select distinct 
cast(cast("public"."income"."ref_date" as date) as timestamp) as "refdate" 
from "public"."income"
where "public"."income"."probos_id" = ?

I'd like to find a better way for set the precision (like date_trunc in PSQL) to get the necessary value without doulbe casting.

If it's possible I'd like to find a native solution without extending the DSL.

Thanks!

解决方案

Use DSL.trunc(Field, DatePart), i.e.

Result<Record1<Timestamp>> result = createQueryBuilder()
    .selectDistinct(trunc(incomeTable.REF_DATE, DatePart.DAY).as("refdate"))
    .from(incomeTable)
    .where(incomeTable.PROBOS_ID.eq(probosId))
    .fetch();

这篇关于JOOQ时间戳精度查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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