如何在jooq中生成to_date

时间:2014-11-07 07:35:09

标签: java sql jooq

如何在jooq中使用to_date生成sql:

select * from mytable where TIME_ACTIVE >= 
    to_date('2011-01-02 00:00:00.000', 'yyyy-MM-dd HH:mm:ss.SSS')

1 个答案:

答案 0 :(得分:1)

没关系,想通了

创建自定义jooq字段:

static class ToDate extends CustomField<String> {
           String arg0=null;
           String arg1=null;
           ToDate(String arg0, String arg1) {
               super("to_date", SQLDataType.VARCHAR);
               this.arg0 = arg0;
               this.arg1 = arg1;
           }

           @Override
           public void toSQL(RenderContext context) {
               context.visit(delegate(context.configuration()));
           }
           @Override
           public void bind(BindContext context) {
               context.visit(delegate(context.configuration()));
           }
           private QueryPart delegate(Configuration configuration) {
                       return DSL.field("TO_DATE({0}, {1})",
                                String.class, arg0, arg1);
           }
        }

然后用类似的东西来调用它:

case "DATE":
    return new ToDate((String)object,"yyyy-MM-dd");
case "TIMESTAMP":
    return new ToDate((String)object,"yyyy-MM-dd HH:mm:ss.SSS");

这个答案很有帮助:How to Handle Date in Jooq?