Domanda

I'm using JOOQ for half a year now and must say that it's quite superb :)

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.

However, when I'm using Field.sortAsc(Collection< T> sortList) method and passing Collection of Strings I get following order by clause in query:

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.

Partial solution to this problem was to pass Collection of Param<String> values obtained from original values using DSL.inline(T) method. However, in that case placeholders still exist and order by clause in query looks like:

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. 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.

Thanks in advance for any help!

È stato utile?

Soluzione

In order to extract all bind values from a query, use 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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top