1

我一直在使用 JOOQ 来生成我的 SQL,但是我在使用“in”子句时遇到了问题,我写了一个简单的测试来说明我的意思:

@Test
public void testWhereInBehaviorJOOQ() {
    String expectedSingleObjectSQL = "select distinct \"Business Group\" \"Business_Group\" from \"SOME_TABLE\" where \"Business Group Process\" in ('SomeValue')";
    String expectedMultipleObjectSQL = "select distinct \"Business Group\" \"Business_Group\" from \"SOME_TABLE\" where \"Business Group Process\" in ('SomeValue','AnotherValue')";

    String actualSingleObjectSQL = generateSQLWithWhereInInline("SomeValue");
    String actualTwoObjectSQL = generateSQLWithWhereInInline("SomeValue,AnotherValue");
    String actualTwoObjectWithQuotesSQL = generateSQLWithWhereInInline("'SomeValue','AnotherValue'");
    String actualTwoObjectWithMiddleQuotesSQL = generateSQLWithWhereInInline("SomeValue','AnotherValue");


    LOGGER.info("JOOQ Generated query for single object \"someValue\" = {}",actualSingleObjectSQL );
    LOGGER.info("JOOQ Generated query for two objects without single quotes \"someValue,AnotherValue\" = {}",actualTwoObjectSQL );
    LOGGER.info("JOOQ Generated query for two objects with single quotes \"'someValue','AnotherValue'\" = {}",actualTwoObjectWithQuotesSQL);
    LOGGER.info("JOOQ Generated query for two objects with single quotes only at the middle \"someValue','AnotherValue\" = {}",actualTwoObjectWithMiddleQuotesSQL );

    Assert.assertEquals(expectedSingleObjectSQL,actualSingleObjectSQL);
    Assert.assertEquals(expectedMultipleObjectSQL,actualTwoObjectSQL);
    Assert.assertEquals(expectedMultipleObjectSQL,actualTwoObjectWithQuotesSQL);
    Assert.assertEquals(expectedMultipleObjectSQL,actualTwoObjectWithMiddleQuotesSQL);

}

private String generateSQLWithWhereInInline(String businessGroupProcess) {
    DSLContext create = DSL.using(SQLDialect.ORACLE);
    return create.
            selectDistinct(
                    DSL.fieldByName("Business Group").as("Business_Group"))
            .from(DSL.tableByName("SOME_TABLE")).where(DSL.fieldByName("Business Group Process").in(DSL.inline(businessGroupProcess))).getSQL();
}

但生成的值是:

JOOQ为单个对象“someValue”生成查询=

select distinct "Business Group" "Business_Group" 
from "SOME_TABLE" where "Business Group Process" in ('SomeValue')

JOOQ 为不带单引号的两个对象生成查询 "someValue,AnotherValue" =

select distinct "Business Group" "Business_Group" 
from "SOME_TABLE" where "Business Group Process" in ('SomeValue,AnotherValue')

JOOQ 为两个带有单引号的对象生成查询 "'someValue','AnotherValue'" =

select distinct "Business Group" "Business_Group" 
from "SOME_TABLE" 
where "Business Group Process" in ('''SomeValue'',''AnotherValue''')

JOOQ 只在中间用单引号生成两个对象的查询 "someValue','AnotherValue" =

select distinct "Business Group" "Business_Group" 
from "SOME_TABLE" 
where "Business Group Process" in ('SomeValue'',''AnotherValue')

因此,当只有一个对象时它可以正常工作,对于两个它只在开头和结尾添加引号,如果我在将引号添加到开头、结尾和每个对象之前添加引号,并且如果我尝试“trick " 它通过在中间添加引号,希望它会尝试将变量包含在引号中,一切都得到引号。空间似乎没有帮助。

有没有人遇到过这个?我想知道我说错了还是错误。

4

1 回答 1

4

jOOQ 在这里按预期工作。该Field.in(...)方法通过可变参数参数接受 IN 谓词的多个值。因此,如果您想用多个值表达一个谓词,您必须为该方法提供多个值。

因此,您的实用程序方法应为:

// I'm assuming this static import
import static org.jooq.impl.DSL.*;

private String generateSQLWithWhereInInline(String... businessGroupProcesses) {
    Field<String>[] fields = new Field[businessGroupProcesses.length];
    for (int i = 0; i < businessGroupProcesses.length; i++)
        fields[i] = inline(businessGroupProcesses[i]);

    return DSL.using(SQLDialect.ORACLE)
              .selectDistinct(
                    field(name("Business Group")).as("Business_Group"))
              .from(table(name("SOME_TABLE")))
              .where(field(name("Business Group Process")).in(fields))
              .getSQL();
}

一种更简单的方法可能是为 DSLContext 提供一个设置,告诉它直接内联所有绑定值。这记录在这里:

http://www.jooq.org/doc/latest/manual/sql-building/dsl-context/custom-settings

然后,您的方法将显示为:

private String generateSQLWithWhereInInline(String... businessGroupProcesses) {
    Settings settings = new Settings()
        .withStatementType(StatementType.STATIC_STATEMENT);

    return DSL.using(SQLDialect.ORACLE, settings);
              .selectDistinct(
                    field(name("Business Group")).as("Business_Group"))
              .from(table(name("SOME_TABLE")))
              .where(field(name("Business Group Process"))
                    .in(businessGroupProcesses))
              .getSQL();
}
于 2013-08-15T06:46:22.767 回答