简体   繁体   English

如何使用JOOQ Query Builder使用使用常量值的select语句构建Insert into

[英]How to build Insert into with select statement that uses constant values using JOOQ Query Builder

I have an insert statement that is inserting some constant values and it needs to select some reference keys from other tables by lookup. 我有一个插入语句,该语句正在插入一些常量值,并且需要通过查找从其他表中选择一些引用键。 The query looks something like this. 查询看起来像这样。

Insert into repository.buffer (
    b_external_id,
    b_buffer_type_id,
    b_entrypoints,
    b_site_id,
    b_state,
    b_uri)
select '100A',bt_id,'["/locations/100A"]'::jsonb,s_id,'ready','/buffers/100A'
from  repository.site, repository.buffer_type
where s_name = 'bar'
and bt_external_id = 'FOO';

My JOOQ Query Builder code looks like this 我的JOOQ查询生成器代码如下所示

dslContext
            .insertInto(
                table("repository.buffer"),
                field("b_external_id"),
                field("b_buffer_type_id"),
                field("b_entrypoints"),
                field("b_site_id"),
                field("b_state"),
                field("b_uri"))
            .select(select(
                    inline(null, String.class),
                    field("bt_id"),
                    inline(null, Object.class),
                    field("s_id"),
                    inline(null, String.class),
                    inline(null, String.class))
                    .from(table("repository.site"), table("repository.buffer_type"))
                    .where(field("s_name").eq(cast(null, String.class)))
                    .and(field("bt_external_id").eq(cast(null, Integer.class))))
                .onConflict().doNothing()
            .getSQL();

This statement fails to compile with following error 该语句无法编译,并出现以下错误

Error:(98, 25) java: incompatible types: org.jooq.SelectConditionStep<org.jooq.Record6<java.lang.String,java.lang.Object,java.lang.Object,java.lang.Object,java.lang.String,java.lang.String>> cannot be converted to org.jooq.Select<? extends org.jooq.Record6<java.lang.Object,java.lang.Object,java.lang.Object,java.lang.Object,java.lang.Object,java.lang.Object>>

Somehow as soon as I use inline in the select return type of select changes to SelectConditionStep instead of Select. 我以某种方式在选择的选择返回类型中使用内联将更改为SelectConditionStep而不是Select。

Any clue to solve this? 有什么解决办法吗?

The fact that the compiler infers SelectConditionStep is irrelevant here, as it is a subtype of Select , and as such, totally acceptable for the INSERT .. SELECT statement. 编译器推断SelectConditionStep的事实在这里无关紧要,因为它是Select的子类型,因此对于INSERT .. SELECT语句完全可以接受。 The problem is that when using plain SQL in your insertInto() clause and not providing any column data types, the compiler will infer Object for each individual column, instead of String . 问题是,当在insertInto()子句中使用普通SQL并且不提供任何列数据类型时,编译器将为每个单独的列而不是String推断Object

Remember that jOOQ is a very strongly typed API and this mostly helps you get your SQL right. 请记住,jOOQ是一个非常强类型的API,这主要可以帮助您正确使用SQL。 In your particular case, make sure you will specify a data type on each column reference: 在您的特定情况下,请确保在每个列引用上指定一种数据类型:

dslContext
    .insertInto(
        table("repository.buffer"),
        field("b_external_id", String.class), // Change here
        field("b_buffer_type_id"),
        field("b_entrypoints", Object.class), // Change here
        field("b_site_id"),
        field("b_state", String.class), // Change here
        field("b_uri", String.class)) // Change here
    .select(select(
            inline(null, String.class),
            field("bt_id"),
            inline(null, Object.class),
            field("s_id"),
            inline(null, String.class),
            inline(null, String.class))
            .from(table("repository.site"), table("repository.buffer_type"))
            .where(field("s_name").eq(cast(null, String.class)))
            .and(field("bt_external_id").eq(cast(null, Integer.class))))
        .onConflict().doNothing()
    .getSQL();

Or even better, use the code generator, in case of which all of this will be done for you automatically, improving readability. 甚至更好的是,使用代码生成器,在这种情况下,所有这些都将自动为您完成,从而提高了可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM