1

I am using jooq as an SQL generation engine, and then executing those queries using jdbi. To map queries to their arguments, I need to explicitly bind it in jdbi, but when i add a named param to contains clause, the param name gets lost.

This test case shows what i expect from getParams() call

@Test
public void testJooqContains() {
    DSLContext context = new DefaultDSLContext(SQLDialect.MYSQL);
    Param<String> param = param("P1", "test");
    Condition condition = field("Field1").contains(param);
    SelectQuery<Record> select = context.selectQuery();
    select.addSelect(field("Col1"));
    select.addConditions(Collections.singletonList(condition));
    select.addFrom(table("Table1").as("T1"));

    assertTrue(select.getParams().containsKey("p1"));
}
np-hard
  • 5,725
  • 6
  • 52
  • 76

1 Answers1

1

This is a bug in jOOQ's (#6223, fixed for 3.10, 3.9.3, 3.8.8) caused by a subtlety in the Java language specification (see also this question).

Your field("Field1").contains(param) call is linked by the compiler to Field.contains(T) instead of the expected Field.contains(Field<T>). This happens because your field("Field1") expression is of type Field<Object>, meaning that both methods are applicable.

A workaround for this problem would be to write:

Condition condition = field("Field1", String.class).contains(param);
Community
  • 1
  • 1
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509