CREATE OR REPLACE FUNCTION public.custom_order(anyarray, anyelement)
RETURNS INT AS
$$
SELECT i FROM (
SELECT generate_series(array_lower($1,1),array_upper($1,1))
) g(i)
WHERE $1[i] = $2
LIMIT 1;
$$ LANGUAGE SQL IMMUTABLE;
select
public.deck_history.id,
public.deck_history.foreign_id,
public.deck_history.title,
public.deck_history.deck_type,
public.deck_history.confidential,
public.deck_history.certified,
public.deck_history.deck_id,
public.deck_history.created,
public.deck_history.updated,
public.deck_history.original_created,
public.deck_history.original_updated,
public.deck_history.num_cards,
public.deck_history.num_images,
public.deck_history.edition,
public.deck_history.deleted
from public.deck_history
where public.deck_history.id in (
'c02615e9-2ff4-47a0-ab56-103e1be7a20a', 'ae0b0756-8d00-47c9-aa74-a73a57f5e021',
'58ef7c63-3473-43c4-9e84-42e1b337c7d7', 'e9c8f273-5010-41f9-8ad1-eb5f3d661bbc',
'7d4a9263-21ea-4427-8498-12ea5f992157', '1ec3cd36-514b-4684-920d-8cb23cea4e4e',
'a11fe2f2-07a2-4cb4-baf1-adf37206720f', '3cd7458f-dd1d-4d34-9bea-0c1664b4b571',
'db692534-2bad-4503-b1f2-030c684a6c37', '907875c2-00cc-47e9-bc89-1eda3055ae25'
)
order by public.custom_order(
cast('{c02615e9-2ff4-47a0-ab56-103e1be7a20a,ae0b0756-8d00-47c9-aa74-a73a57f5e021,58ef7c63-3473-43c4-9e84-42e1b337c7d7,e9c8f273-5010-41f9-8ad1-eb5f3d661bbc,7d4a9263-21ea-4427-8498-12ea5f992157,1ec3cd36-514b-4684-920d-8cb23cea4e4e,a11fe2f2-07a2-4cb4-baf1-adf37206720f,3cd7458f-dd1d-4d34-9bea-0c1664b4b571,db692534-2bad-4503-b1f2-030c684a6c37,907875c2-00cc-47e9-bc89-1eda3055ae25}' as uuid[]), public.deck_history.id)
UUID[] deckIdArray = deckIds.stream().toArray(UUID[]::new);Field<Integer> field = customOrder(deckIdArray, DECK_HISTORY.ID);SelectSeekStep1<DeckHistoryRecord, Integer> deckHistoryRecords = dsl.selectFrom(DECK_HISTORY).where(DECK_HISTORY.ID.in(deckIds)).orderBy(field);ResultSet rs = deckHistoryRecords.fetchResultSet();
org.jooq.exception.SQLDialectNotSupportedException: Type class org.jooq.impl.TableFieldImpl is not supported in dialect POSTGRES at org.jooq_3.11.8.POSTGRES.debug(Unknown Source) at org.jooq.impl.DefaultDataType.getDataType(DefaultDataType.java:884) at org.jooq.impl.DefaultDataType.getDataType(DefaultDataType.java:823) at org.jooq.impl.DefaultBinding$AbstractBinding.sqlCast(DefaultBinding.java:618) at org.jooq.impl.DefaultBinding$AbstractBinding.sql(DefaultBinding.java:707)
select "public"."deck_history"."id", "public"."deck_history"."foreign_id", "public"."deck_history"."title", "public"."deck_history"."deck_type", "public"."deck_history"."confidential", "public"."deck_history"."certified", "public"."deck_history"."deck_id", "public"."deck_history"."created", "public"."deck_history"."updated", "public"."deck_history"."original_created", "public"."deck_history"."original_updated", "public"."deck_history"."num_cards", "public"."deck_history"."num_images", "public"."deck_history"."edition", "public"."deck_history"."deleted"from "public"."deck_history"where "public"."deck_history"."id" in ( 'c02615e9-2ff4-47a0-ab56-103e1be7a20a', 'ae0b0756-8d00-47c9-aa74-a73a57f5e021', '58ef7c63-3473-43c4-9e84-42e1b337c7d7', 'e9c8f273-5010-41f9-8ad1-eb5f3d661bbc', '7d4a9263-21ea-4427-8498-12ea5f992157', '1ec3cd36-514b-4684-920d-8cb23cea4e4e', 'a11fe2f2-07a2-4cb4-baf1-adf37206720f', '3cd7458f-dd1d-4d34-9bea-0c1664b4b571', 'db692534-2bad-4503-b1f2-030c684a6c37', '907875c2-00cc-47e9-bc89-1eda3055ae25')order by "public"."custom_order"( cast('{"c02615e9-2ff4-47a0-ab56-103e1be7a20a","ae0b0756-8d00-47c9-aa74-a73a57f5e021","58ef7c63-3473-43c4-9e84-42e1b337c7d7","e9c8f273-5010-41f9-8ad1-eb5f3d661bbc","7d4a9263-21ea-4427-8498-12ea5f992157","1ec3cd36-514b-4684-920d-8cb23cea4e4e","a11fe2f2-07a2-4cb4-baf1-adf37206720f","3cd7458f-dd1d-4d34-9bea-0c1664b4b571","db692534-2bad-4503-b1f2-030c684a6c37","907875c2-00cc-47e9-bc89-1eda3055ae25"}' as uuid[]), '"public"."deck_history"."id"')
Hi Knut,I have a requirement where i wanted to return the result with sorted order of the IDs i am passing in to the query to fetch the data from DB.I wrote this POSTGRES query and able to get the result with same order as the IDs i am passing.So how can i convert this query into JOOQ format ? Can you please help me ?Thanks,
Deba
with
cta1 as (
select array[
'125d7988-14d7-4898-bf89-0b54eb8aa317' ,
'63028718-bf33-4e3f-a6bd-ef7c0e941205' ,
'a1266416-4e7d-4426-9b9d-e5c66d15ffbc' ,
'940b3c50-7a5e-49d5-8105-b3ddfdfde9c1' ,
'86a1647f-a24d-45c0-ae04-0e19c2fd10e2' ] as a),
cta2 as (
select
unnest(a)::uuid id,
generate_series(1, array_length(a, 1)) idx
from cta1)
select
idx,
d.id,
d.title
from cta2
join public.deck d on d.id = cta2.id order by idx;