可用版本:Dev (3.21) | 最新 (3.20) | 3.19 | 3.18

可能错误的表达式

适用于 ❌ 开源版   ✅ Express 版   ✅ 专业版   ✅ 企业版

处理此事件的SPI方法是possiblyWrongExpression()

某些表达式很可能出现错误,即使它们在大多数情况下是正确的,或者在编写表达式的开发人员所知的范围内是正确的。

为什么这不好?

墨菲定律指出,表达式出错的微小风险最终意味着最好尽快修复它。可能错误的表达式的各种例子包括

  • MOD(x, 2) = 1 用于奇数检查不适用于负数。请与零进行比较:MOD(x, 2) != 0

这里给出了一个例子

// A custom DiagnosticsListener SPI implementation
class PossiblyWrongExpression implements DiagnosticsListener {
    @Override
    public void possiblyWrongExpression(DiagnosticsContext ctx) {

        // The statement that is being executed and which has a possibly wrong expression.
        System.out.println("Actual statement: " + ctx.actualStatement());

        // The possibly wrong expression.
        System.out.println("Predicate       : " + ctx.part());
    }
}

然后

// Configuration is configured with the target DataSource, SQLDialect, etc. for instance Oracle.
try (
    Connection c = DSL.using(configuration.derive(new PossiblyWrongExpression()))
                      .diagnosticsConnection();
    Statement s = c.createStatement()
) {
    try (ResultSet a = s.executeQuery("SELECT id FROM author WHERE MOD(id, 2) = 1")) {
        while (a.next())
            println(a.getInt(1));
    }
}

引用此页

反馈

您对此页面有任何反馈吗? 我们很乐意听到您的反馈!

The jOOQ Logo