Jooq在自定义例程中引用函数参数

时间:2018-03-15 17:03:24

标签: java sql jooq

我在postgresql中定义了两个例程,array_distinct(anyarray)array_remove_all(anyarray, anyarray)。这确实是在jooq的Routines类中正确生成的。

除此之外,我还使用了一些函数来生成一些PostgreSQL数组运算符:

inline fun <reified T> Field<Array<T>>.concatenate(other: Array<T>) : Field<Array<T>> {
    return DSL.field("{0} || {1}", Array<T>::class.java, this, other)
}

这是在Kotlin中,因此java等价物将是:

Field<Array<T>> concatenate(Field<Array<T>> a, Array<T> b, Class<T[]> type) {
    return DSL.field("{0} || {1}", type, a, b);
}

现在我正在尝试在jooq中编写此查询:

UPDATE users
    SET groups = array_distinct(
        array_remove_all(groups, ARRAY['a', 'b']) || ARRAY['c']
    );

为了简洁起见,我删除了一些使这段代码有效所需的类型转换。

然而,当我这样做时:

    DSL.update(USERS)
        .set(USERS.GROUPS, Routines.arrayDistinct(
            Routines.arrayRemoveAll(
                USERS.GROUPS,
                arrayOf("a", "b")
            )
            .asTypedField<Array<String>>()
            .concatenate(arrayOf("c"))
        ).asTypedField())

asTypedField是我需要做的事情,以便我可以concatenate使用Field<Array<T>>作为update "public"."users" set "public"."users"."groups" = "public"."array_distinct"('"public"."array_remove_all"( ''"public"."users"."groups"'', ARRAY[''a'', ''b''] ) || ARRAY[''c'']' ) 的扩展功能;在内部它只不过是一个类型转换。

生成的查询是:

'

这整个事情到处都是额外的引用(')标记。我需要做什么来生成我上面提到的查询?

即使是连接的最终数组,其所有元素都在两个地方使用引用''a''两次(''b''SELECT a.id,a.name,a.value,a.status_id,last_modified_dt x.max_date FROM table1 a ( select max(max_date) as max_date from ( SELECT MAX(last_modified_dt) as max_date FROM table1 t1 WHERE t1.id = a.id union SELECT nvl(MAX(last_modified_ts),sysdate-90) as max_date FROM table2 t2 WHERE t2.table2_id=a.id ... ) y ) x where a.status_id ='Active' order by status_id desc,last_modified_dt desc; )。

1 个答案:

答案 0 :(得分:0)

jOOQ代码生成器当前(从版本3.10开始,即将发布的3.11)不支持PostgreSQL中的anyarray类型。待处理的功能请求他在这里: https://github.com/jOOQ/jOOQ/issues/5479

您需要应用以下任何变通办法:

相关问题