JOOQ - 没有引号的查询

时间:2014-12-08 08:03:43

标签: oracle jooq

我使用JOOQ-3.1.0为Spring和4生成并执行Oracle和Postgresql的动态查询。在一个场景中,我有一个分区表,我需要使用JOOQ进行查询。我使用DSL.tableByName(vblTablename);其中vblTablename是在查询生成方法中收到的字符串,例如vbl_default partition(p_04-Dec-14)。 (vblTablename模式因不同的数据库而异,并在外部属性文件中配置。 JOOQ生成sql,但在表名周围有双引号。查询和错误如下所示

查询

SELECT COUNT(ID) COUNT FROM "vbl_default partition(p_04-Dec-14)" 
    where (rts between timestamp '2014-12-04 00:00:00.0' and timestamp '2014-12-05 00:00:00.0' and userid in (2))

错误

ORA-00972: identifier is too long
00972. 00000 -  "identifier is too long"
*Cause:    An identifier with more than 30 characters was specified.
*Action:   Specify at most 30 characters.
Error at Line: 4 Column: 29

虽然我在DefaultDSLContext

上设置了以下设置
Settings settings = new Settings();
        settings.setRenderNameStyle(RenderNameStyle.AS_IS);

如何删除表格周围的引用?我错过了其他任何设置吗?

1 个答案:

答案 0 :(得分:2)

DSL.tableByName(String...)背后的想法是你提供一个表......按名称: - )

您正在寻找的是一个纯SQL表,通过DSL.table(String)

你可以写:

// Assuming this import
import static org.jooq.impl.DSL.*;

DSL.using(configuration)
   .select(count(VBL_DEFAULT.ID))
   .from(table("vbl_default partition(p_04-Dec-14)"))
   .where(...);

或者使用方便的重载SelectFromStep.from(String)

DSL.using(configuration)
   .select(count(VBL_DEFAULT.ID))
   .from("vbl_default partition(p_04-Dec-14)")
   .where(...);

有关jOOQ中纯SQL的更多信息,请参阅本手册页:

http://www.jooq.org/doc/latest/manual/sql-building/plain-sql/

分区支持

请注意,路线图上支持Oracle分区:#2775。如果您希望更频繁地使用分区表,那么您也可以为此编写自己的函数:

// Beware of the risk of SQL injection, though!
public <R extends Record> Table<R> partition(Table<R> table, String partition) {
    return DSL.table("{0} partition(" + partition + ")", table);
}

......然后:

DSL.using(configuration)
   .select(count(VBL_DEFAULT.ID))
   .from(partition(VBL_DEFAULT, "p_04-Dec-14"))
   .where(...);