Last active
June 16, 2018 12:51
-
-
Save rponte/1a437f9084f7c18847682eca437cbc16 to your computer and use it in GitHub Desktop.
Oracle PLSQL and jOOQ: converting Array of Record Types to Sys_refcursor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create or replace package jOOQ as | |
-- creates record type and its array | |
type Product_t is record( | |
code number, | |
name varchar2(100) | |
); | |
type Product_List_t is table of Product_t; | |
/** | |
* Common Pipelined function | |
*/ | |
function pipelined_function return Product_List_t PIPELINED; | |
end jOOQ; | |
create or replace package body jOOQ as | |
function pipelined_function return Product_List_t PIPELINED is | |
l_p Product_t; | |
begin | |
-- line 1 | |
l_p.code := 2020; l_p.name := 'iPad Mini Display'; | |
pipe row(l_p); | |
-- line 2 | |
l_p.code := 2021; l_p.name := 'iPhone 8 Plu'; | |
pipe row(l_p); | |
return; | |
end; | |
end jOOQ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set serveroutput on | |
declare | |
l_p jOOQ.Product_t; | |
l_cursor Sys_refcursor; | |
begin | |
-- converts array of record types to sys_refcursor | |
open l_cursor for | |
select pf.* | |
from table(jOOQ.pipelined_function) pf; | |
-- iterates over cursor | |
loop | |
fetch l_cursor into l_p; | |
exit when l_cursor%NOTFOUND; | |
DBMS_OUTPUT.PUT_LINE('code=' || l_p.code); | |
DBMS_OUTPUT.PUT_LINE('name=' || l_p.name); | |
end loop; | |
close l_cursor; | |
end; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Repository | |
public class ProductRepository { | |
@Autowired | |
private DSLContext jooq; | |
public List<Product> listAll() { | |
Result<Record> result = dsl.fetch("select * from table(jooq.pipelined_function)"); | |
return result.into(Product.class); // it works! <3 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment