Available in versions: Dev (3.18)

This is experimental functionality, and as such subject to change. Use at your own risk!

This documentation is for the unreleased development version of jOOQ. Click on the above version links to get this documentation for a supported version of jOOQ.

Possibly wrong expressions

Applies to ❌ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

The SPI method handling this event is possiblyWrongExpression()

Some expressions have a high likelyhood of being wrong, even if they're correct most of the time, or within the domain known by the developer who wrote the expression.

Why is it bad?

Murphy's Law dictates that the slight risk of the expression turning wrong eventually means that it should better be fixed as soon as possible. Various examples of possibly wrong expressions include:

  • MOD(x, 2) = 1 for oddness checks doesn't work for negative numbers. Compare with zero instead: MOD(x, 2) != 0

An example is given here:

// A custom DiagnosticsListener SPI implementation
class PossiblyWrongExpression extends DefaultDiagnosticsListener {
    @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));
    }
}

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo