如何使 jOOQ 引用 table 保留关键字的名称?

How to make jOOQ quote table names that are reserved keywords?

在使用 jOOQ 从名为 user 的 table 中进行选择时,出现以下异常:

jOOQ; bad SQL grammar [insert into user (user_id, account_type) values (?, ?)]; nested exception is org.postgresql.util.PSQLException: ERROR: syntax error at or near "user"

我的 jOOQ 设置是:

private static final Settings jooqSettings = new Settings()
    .withRenderSchema(false)
    .withRenderNameStyle(RenderNameStyle.LOWER);

我从中创建了一个 DSLContext 并在事务中构造了一个查询,如下所示:

ctx.insertInto(USER)
      .set(USER.USER_ID, userId)
      .set(USER.ACCOUNT_TYPE, "U")
      .execute()

USER 导入为 <jooq-generated-package>.tables.USER

jOOQ 是否有配置 属性 来转义 table 名称(全部或仅保留关键字)?我在文档或源代码中找不到任何内容。

好吧,您通过设置 RenderNameStyle.LOWER 关闭了引号...这就是它的工作原理:)

通过删除该设置或将其设置为 RenderNameStyle.QUOTED,jOOQ 将在所有标识符周围生成这些双引号。

来自specification

<simpleType name="RenderNameStyle">
  <restriction base="string">
    <!--
     Render object names quoted, as defined in the database. Use this
     to stay on the safe side with case-sensitivity and special
     characters. For instance:
     Oracle    : "SYS"."ALL_TAB_COLS"
     MySQL     : `information_schema`.`TABLES`
     SQL Server: [INFORMATION_SCHEMA].[TABLES] 
     -->
    <enumeration value="QUOTED"/>

    <!--
     Render object names, as defined in the database. For instance:
     Oracle    : SYS.ALL_TAB_COLS
     MySQL     : information_schema.TABLES
     SQL Server: INFORMATION_SCHEMA.TABLES 
     -->
    <enumeration value="AS_IS"/>

    <!--
     Force rendering object names in lower case. For instance:
     Oracle    : sys.all_tab_cols
     MySQL     : information_schema.tables
     SQL Server: information_schema.tables 
     -->
    <enumeration value="LOWER"/>

    <!--
     Force rendering object names in upper case. For instance:
     Oracle    : SYS.ALL_TAB_COLS
     MySQL     : INFORMATION_SCHEMA.TABLES
     SQL Server: INFORMATION_SCHEMA.TABLES 
     -->
    <enumeration value="UPPER"/>
  </restriction>
</simpleType>

请注意,有功能请求向 Javadoc 添加更多文档 (#2830) and the manual (#5231)