Search code examples
javapostgresqljooq

XOR comparison between two Conditions in JooQ


In my WHERE condition in PostgreSQL I have something in the following form:

a != b

where a and b are booleans.

However, JooQ decides that boolean is a Condition which only knows or and and. I can convert to DNF, but that greatly reduces readability (because then it becomes (a and !b) or (!a and b)).

Is there any other way to implement this use case?

Example

Say I have the following SQL code:

WHERE (field_a IS NOT NULL) != (field_b IS NULL)

I would like to convert it to JooQ code similar to this:

where(DSL.field("field_a", Integer.class).isNotNull().notEq(DSL.field("field_b", Integer.class).isNull()))

I cannot, because notEq is not a method on the Condition class.

Is there any way to do this?


Solution

  • I found I could wrap the Condition as a Field using DSL.field(Condition). The Field class does have a notEqual method. My previous example becomes as follows:

    where(DSL.field(DSL.field("field_a", Integer.class).isNotNull()).notEqual(DSL.field(DSL.field("field_b", Integer.class).isNull()))))
    

    I don't think it's the most elegant, but definitely more elegant than using the DNF.