简体   繁体   English

在jooq中检索selectCount的值

[英]Retrieving the value of selectCount in jooq

I have some code that looks like this: 我有一些看起来像这样的代码:

Record record = jooq
    .selectCount()
    .from(USERS)
    .fetchOne();

Currently I'm doing the following to get the count: 目前我正在做以下事情以获得计数:

Integer count = (Integer) record.getValue(0);

But it seems like there must be a better solution (that's type-safe...since that's the whole point of using jooq). 但似乎必须有一个更好的解决方案(这是类型安全的......因为这是使用jooq的全部意义)。 Any suggestions? 有什么建议?

Unfortunately, for this particular query, there aren't many "better" ways to typesafely get the count() value. 不幸的是,对于这个特定的查询,没有很多“更好”的方法来类型安全地获取count()值。 What you could do, to add type-safety, is this: 你能做什么,增加类型安全性,是这样的:

Field<Integer> f = count();
Integer count = jooq.
    .select(f) // Or selectCount(). Replaced it to illustrate the case
    .from(USERS)
    .fetchOne(f);

The problem is that most of the type information about the projection has been "lost" to the Java compiler, by the time the fetch() methods are "reached". 问题是,当“到达” fetch()方法时,关于投影的大多数类型信息已经“丢失”到Java编译器。 There is no way that a ResultQuery.fetchXXX() method could recover it from the SELECT clause, and produce it to you. ResultQuery.fetchXXX()方法无法从SELECT子句中恢复它,并将其生成给您。

On the jOOQ user group, some users have argued to move the projection into the fetch() methods, entirely, the way C#'s LINQ, or Scala's SLICK do it. 在jOOQ用户组中,一些用户主张将投影移动到fetch()方法,完全是C#的LINQ或Scala的SLICK的方式。 This would greatly complicate the expression of more advanced SELECT statements. 这会使更高级的SELECT语句的表达变得非常复杂。 An more elaborate explanation is documented here . 这里记录了更详细的解释

With jOOQ 3.0, additional record-level typesafety has been introduced. 使用jOOQ 3.0,引入了额外的记录级类型安全性。 In jOOQ 3.3, it will thus be possible to fetch a single value as such (has been registered as #2246 ): 在jOOQ 3.3中,因此可以获取单个值(已注册为#2246 ):

<T> T fetchValue(Select<Record1<T>> select);

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

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