jOOQ - 带有 IN 谓词的查询生成器

jOOQ - query builder with IN predicate

我正在尝试构建这样的查询:

List<Integer> ids = ...

String query = DSL.select(TABLE.SOMETHING).from(TABLE).where(TABLE.ID.in(ids)).
getSQL();

但我无法获得生成的带有值的查询,只有占位符。 我试过 DSL.inline(ids) 但它不起作用。

我该怎么做?

我正在使用 jOOQ 3.4.2。

感谢您的帮助。

更新:

看来我可以这样做:

    Configuration configuration = new DefaultConfiguration();
    configuration.set(SQLDialect.DERBY);
    Settings settings = new Settings()
    .withStatementType(StatementType.STATIC_STATEMENT);
    configuration.set(settings);
    DSLContext create = DSL.using(configuration);

    String query = create.select(TABLE.SOMETHING).from(TABLE).where(TABLE.ID.in(ids)).getSQL();

如果有人能确认这是正确的方法,谢谢。

你不能用 jOOQ 的 DSL.inline() 内联一个列表,因为如果你可以,这样一个值的语义将是数据库中 list/array 文字的语义,而不是单个值列表的语义.

正确的使用方法DSL.inline()

这是将内联值列表传递给 Field.in(Field<?>...) 的一种正确方法:

List<Integer> ids = ...

String query = DSL.using(configuration) // Use a Configuration or at least a SQLDialect!
                  .select(TABLE.SOMETHING)
                  .from(TABLE)
                  .where(TABLE.ID.in(ids.stream().map(DSL::inline).collect(toList())))
                  .getSQL();

在每个 getSQL() 基础上内联所有绑定值:

使用Query.getSQL(ParamType)

List<Integer> ids = ...

String query = DSL.using(configuration)
                  .select(TABLE.SOMETHING)
                  .from(TABLE)
                  .where(TABLE.ID.in(ids))
                  .getSQL(ParamType.INLINED);

在每个 Configuration 基础上内联所有绑定值:

你在问题编辑中提到的解决方案当然也是有效的:

List<Integer> ids = ...
Configuration configuration = new DefaultConfiguration();
configuration.set(new Settings().withStatementType(StatementType.STATIC_STATEMENT));

String query = DSL.using(configuration)
                  .select(TABLE.SOMETHING)
                  .from(TABLE)
                  .where(TABLE.ID.in(ids))
                  .getSQL();