[英]JOOQ Ordering using CASE expressions
I'm using JOOQ for half a year now and must say that it's quite superb :) 我使用JOOQ已有半年了,必须说它非常出色:)
The problem I encountered is: I'm trying to generate plain SQL query containing order by
clause with case ... when
statements to achieve custom sorting order as described in this part of JOOQ tutorial . 我遇到的问题是:我正在尝试生成普通SQL查询,其中包含带
case ... when
order by
子句case ... when
语句以实现自定义排序顺序,如JOOQ教程的本部分所述。
However, when I'm using Field.sortAsc(Collection< T> sortList) method and passing Collection
of String
s I get following order by
clause in query: 但是,当我使用Field.sortAsc(Collection <T> sortList)方法并传递
String
的Collection
,在查询中得到以下order by
子句:
order by
case `type`
when ? then ?
when ? then ?
when ? then ?
when ? then ?
when ? then ? end asc
Because I'm not using JOOQ for executing queries, I need to specify these values myself which is quite inconvenient. 因为我没有使用JOOQ来执行查询,所以我需要自己指定这些值,这很不方便。
Partial solution to this problem was to pass Collection
of Param<String>
values obtained from original values using DSL.inline(T) method. 此问题的部分解决方案是使用DSL.inline(T)方法传递从原始值获得的
Param<String>
值的Collection
。 However, in that case placeholders still exist and order by
clause in query looks like: 但是,在那种情况下,占位符仍然存在,查询中的
order by
子句看起来像:
order by
case `type`
when 'type_a' then ?
when 'type_b' then ?
when 'type_c' then ?
when 'type_d' then ?
when null then ? end asc
Is this expected behaviour or this can be considered as bug and should be reported? 这是预期的行为,还是可以视为错误并应报告?
For now, I worked this problem around by using Field.sort(Map sortMap) method, passing Map<Param<String>, Param<Integer>>
to it with incremented integer values. 现在,我通过使用Field.sort(Map sortMap)方法来解决此问题,并使用递增的整数值将
Map<Param<String>, Param<Integer>>
传递给它。 There is minor problem with this method as well, because it returns SortField
instance which doesn't have methods like asc()
, desc()
or sort(SortOrder)
so in order to sort with different orders this map should be regenerated. 这个方法也有一个小问题,因为它返回的
SortField
实例没有asc()
, desc()
或sort(SortOrder)
因此为了以不同的顺序进行排序,应重新生成此映射。
Thanks in advance for any help! 在此先感谢您的帮助!
In order to extract all bind values from a query, use Query.getBindValues()
. 为了从查询中提取所有绑定值,请使用
Query.getBindValues()
。 The sort indirection values should be in there. 排序间接值应该在其中。 See also the relevant section of the manual .
另请参见手册的相关部分 。
Note, it might be generally useful to generate "inline" sort indirection values instead of bind values to prevent this in the future. 请注意,通常可能会生成“内联”排序间接值而不是绑定值,以防止将来发生这种情况。 Execution-plan-wise, I don't think there will be any difference.
就执行计划而言,我认为不会有任何区别。 I have registered Issue #3147 for this.
我已经为此注册了问题3147 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.