gpt4 book ai didi

Kotlin 推断 JOOQ 方法错误

转载 作者:行者123 更新时间:2023-12-02 09:16:33 28 4
gpt4 key购买 nike

给定一个使用 Kotlin 版本 1.3.61 和 JOOQ 版本 3.13.1 的系统,这样的方法会构建 union正常查询:

    val selectCommonPart = coalesce(sum(field(name("amount"), Long::class.java)), ZERO)
.`as`(field(name("totalAmount")))
var whereCommonPart: Condition = trueCondition().and(field(name("Id")).eq(Id)) // Id comes as a parameter

var query = dsl.selectQuery()
query.addSelect(selectCommonPart)
query.addFrom(table("${tableName(startDate)}")) // `startDate` is a `LocalDate`, `tableName()` generates the table name as String
query.addConditions(whereCommonPart)

// `endDate` is also a `LocalDate`, can be either equal to `startDate` or after
if (endDate.isAfter(startDate)) {
for (date in Stream.iterate(startDate.plusDays(1), { d: LocalDate -> d.plusDays(1) })
.limit(ChronoUnit.DAYS.between(startDate, endDate))
.collect(Collectors.toList())) {

val unionQuery = dsl.selectQuery()
unionQuery.addSelect(selectCommonPart)
unionQuery.addFrom(table("public.${tableName(date)}"))
unionQuery.addConditions(whereCommonPart)

// Here `union` is inferred correctly
query.union(dsl.select(selectCommonPart)
.from("public.public.${tableName(date)}")
.where(whereCommonPart))
}
}

但是,如果我隔离 dsl.select(...)参与如下方法:

private fun buildSelect(selectCommonPart: Field<*>, whereCommonPart: Condition, date: LocalDate): Select<*> {
var query = dsl.selectQuery()
query.addSelect(selectCommonPart)
query.addFrom(table("public.${tableName(date)}"))
query.addConditions(whereCommonPart)
return query
}

并修改循环以使用:

    // Remove this part
/* var query = dsl.selectQuery()
query.addSelect(selectCommonPart)
query.addFrom(table("${tableName(startDate)}")) // `startDate` is a `LocalDate`, `tableName()` generates the table name as String
query.addConditions(whereCommonPart) */

// Add this part
var query = buildSelect(selectCommonPart, whereCommonPart, startDate)

if (endDate.isAfter(startDate)) {
for (date in Stream.iterate(startDate.plusDays(1), { d: LocalDate -> d.plusDays(1) })
.limit(ChronoUnit.DAYS.between(startDate, endDate))
.collect(Collectors.toList())) {

// This gives an inference error
query.union(buildSelect(selectCommonPart, whereCommonPart, date))
}
}

我有一个推理错误。 Kotlin 解析 union如这个方法:

/**
* Returns a set containing all distinct elements from both collections.
*
* The returned set preserves the element iteration order of the original collection.
* Those elements of the [other] collection that are unique are iterated in the end
* in the order of the [other] collection.
*
* To get a set containing all elements that are contained in both collections use [intersect].
*/

public infix fun <T> Iterable<T>.union(other: Iterable<T>): Set<T> {
val set = this.toMutableSet()
set.addAll(other)
return set
}

我想使用JOOQ的Select<*>union相反:

public interface Select<R extends Record> extends ResultQuery<R>, TableLike<R>, FieldLike {

/**
* Apply the <code>UNION</code> set operation.
*/
@Support
Select<R> union(Select<? extends R> select);

我应该怎么做才能推断出正确的 union方法?

最佳答案

好的,我知道如何解决您的问题。

方法buildSelect应该返回Select ,而不是Select<*>

我的建议为什么会发生:

Select.union 方法具有以下签名

public interface Select<R extends Record> extends ResultQuery<R>, TableLike<R>, FieldLike {
@Support
Select<R> union(Select<? extends R> var1);
....

如您所见,var1 应该与调用方法的对象具有相同的通用(或扩展)类型。在您的第一个实现中,方法 dsl.selectQuery() 返回 SelectQuery< Record> 并且都是变量 queryunionQuery> 具有相同的泛型类型并且正确确定了联合方法。

在第二个实现中,queryquery.union(...) 的参数具有 Select< *> 类型。我猜编译器认为这两个变量具有不同的泛型(因为它尚未确定并且可以不同),并且编译器不能使用 Select 中的 union,但是两个变量都实现了 Iterable 并且编译器选择 < em>Iterable.union 因为它适合。

关于Kotlin 推断 JOOQ 方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60384147/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com