JOOQ Timestamp precision on query

我是研究僧i 提交于 2020-01-05 03:09:13

问题


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!


回答1:


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();


来源:https://stackoverflow.com/questions/51681406/jooq-timestamp-precision-on-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!