How to avoid quotes around table aliases in jOOQ

*爱你&永不变心* 提交于 2019-12-30 06:47:29

问题


I have the following select-query creation:

final DSLContext create = DSL.using(..., SQLDialect.POSTGRES);

create
 .select(DSL.field("identifier"), DSL.field("name"), 
         create.selectCount()
               .from(DSL.table("person"))
               .where(DSL.field("identifier").eq(DSL.field("personOuter.identifier")))
               .asField("count"))
 .from(DSL.table("person").as("personOuter"))

jOOQ generates the following query:

select 
    identifier, 
    name, 
   (select count(*) 
    from person 
    where identifier = personOuter.identifier) as "count" 
from person as "personOuter"

The query should be:

select 
    identifier, 
    name, 
   (select count(*) 
    from person 
    where identifier = personOuter.identifier) as "count" 
from person as personOuter

The latter query works perfectly in PostgreSQL. The table alias should not be surrounded by quotes.

Is this a bug?

(Note that the query is pretty dumb. I am playing around with jOOQ to evaluate.)

The following "hack" works:

create
 .select(DSL.field("identifier"), DSL.field("name"), 
         create.selectCount()
               .from(DSL.table("person"))
               .where(DSL.field("identifier").eq(DSL.field("personOuter.identifier")))
               .asField("count"))
 .from("person as personOuter")

回答1:


By default, jOOQ will wrap all your identifiers in quotes in order to be able to handle case-sensitivity correctly.

The confusing part is why this isn't done for DSL.field(String), but only for Field.as(String). The reason for this is that jOOQ re-uses the String type for both:

  • Plain SQL as in DSL.field(String), where the input String doesn't really represent an identifier, but an arbitrary SQL expression
  • Identifiers as in DSL.name(String), where the input String represents a name / identifier. There is also DSL.fieldByName(String) to create Field types composed of (schema) / table / column identifiers.

In order to remove the quotes from all generated identifiers, you can also change the Settings.renderNameStyle to RenderNameStyle.AS_IS.

More information about Settings can be found here.



来源:https://stackoverflow.com/questions/28138434/how-to-avoid-quotes-around-table-aliases-in-jooq

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!