Qualifying a temporary table column name in jOOQ

后端 未结 1 834
不思量自难忘°
不思量自难忘° 2021-01-21 00:31

I am using jOOQ with a temporary table:

Table TMP = DSL.table(\"tmp\");
Field TYPE = DSL.field(\"type\", String.class);
Field

        
相关标签:
1条回答
  • 2021-01-21 01:08

    There are two ways to interact with tables / columns dynamically (i.e. without using the code generator) in jOOQ:

    Using plain SQL (org.jooq.SQL)

    That's what you're doing. You can obviously qualify the columns directly in your plain SQL Field references in two ways:

    By repeating the "tmp" string in each field:

    Table<Record> TMP = DSL.table("tmp");
    Field<String> TYPE = DSL.field("tmp.type", String.class);
    Field<String> TOKEN = DSL.field("tmp.token", String.class);
    

    By embedding the "tmp" reference in the plain SQL template:

    Table<Record> TMP = DSL.table("tmp");
    Field<String> TYPE = DSL.field("{0}.type", String.class, TMP);
    Field<String> TOKEN = DSL.field("{0}.token", String.class, TMP);
    

    The plain SQL functionality is documented here in the manual

    Using qualified references (org.jooq.Name)

    That's probably what you want to be doing instead. You'll write:

    Table<Record> TMP = DSL.table(DSL.name("tmp"));
    Field<String> TYPE = DSL.field(DSL.name("tmp", "type"), String.class);
    Field<String> TOKEN = DSL.field(DSL.name("tmp", "token"), String.class);
    

    The naming functionality is described here in the manual.

    The advantages of this approach are:

    • No SQL injection risk
    • Case-sensitivity is taken care of
    • Table mapping and other AST transformations will work, too
    0 讨论(0)
提交回复
热议问题