我一直在使用 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 " 它通过在中间添加引号,希望它会尝试将变量包含在引号中,一切都得到引号。空间似乎没有帮助。
有没有人遇到过这个?我想知道我说错了还是错误。