【问题标题】:Is setFixedCHAR() implemented in JOOQsetFixedCHAR() 是否在 JOOQ 中实现
【发布时间】:2016-04-25 18:29:35
【问题描述】:

我们的 oracle 数据库无法更改,并且有很多以 CHARS 表示的列。目前,我在进行比较时手动将参数填充到字段的长度,例如给定 foo 是 db 'foo' 中的 char(5)

    String foo = "foo"

...    
        .where(FOO_TABLE.FOO.equal(StringUtils.rightPad(foo,FOO_TABLE.FOO.getDataType().length()))

有什么方法可以告诉 jooq foo 是一个字符,因此它会执行填充比较,例如在 rusty the robots answer here

【问题讨论】:

    标签: jooq


    【解决方案1】:

    您可以为所有CHAR 列实现data type binding,并在生成的SQL 字符串中强制转换绑定变量:

    @Override
    public final void sql(BindingSQLContext<String> ctx) throws SQLException {
        ctx.render().sql("CAST(? AS CHAR(5))");
    }
    

    (我刚刚注意到无法通过这种方式访问​​CHAR 长度...,这应该修复:https://github.com/jOOQ/jOOQ/issues/5223

    或者在将绑定变量绑定到准备好的语句之前缩短/填充绑定变量。或者您可以直接在数据类型绑定的变量绑定逻辑中use that logic you referenced to

    @Override
    public final void set(BindingSetStatementContext<String> ctx) throws SQLException {
        ctx.statement()
           .unwrap(OraclePreparedStatement.class)
           .setFixedChar(ctx.index(), ctx.value());
    }
    

    这些方法中的任何一种都只允许您实现此逻辑一次。

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 2021-06-26
      • 2019-04-17
      • 2020-01-02
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      相关资源
      最近更新 更多