如何制作保留关键字的jOOQ报价表名称?

时间:2017-02-24 12:18:24

标签: java sql postgresql jooq

在使用jOOQ从名为user的表中进行选择时,我得到以下异常:

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是否有配置属性来转义表名(全部或仅保留关键字)?我在文档或来源中找不到任何内容。

1 个答案:

答案 0 :(得分:2)

嗯,你通过设置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)和手册(#5231

添加更多文档