java - jOOQ - 使用 refcursor 执行 postgres 用户定义的函数
问题描述
我在执行refcursor
在 jooq 中获取和返回 postgres 的函数时遇到了一些问题。我不知道如何通过实例化来处理ref
,Result<Record>
以及如何遍历我应该从我想要执行的函数中获得的记录。
假设我在 postgres (postgres 9.5) 中有以下功能:
create or replace function foo_cursor(name character varying, ref refcursor)
returns refcursor
as $func$
begin
open ref for
select id, first_name, last_name
from students
where first_name = name;
return ref;
end
$func$
language plpgsql;
在 postgres 中,我是这样执行的:
begin;
select foo_cursor('Konrad', 'my_cursor');
fetch all in "my_cursor";
commit;
该函数必须保持不变 - 它返回refcursor
并获取refcursor
.
我想在 jOOQ 中执行它:
Routines.fooCursor(configuration, "Konrad", ____);
但我不知道在里面放什么____
,这需要Result<Record>
。我试过类似的东西:
Result<Record> records = DSL.using(configuration).newResult();
但它也没有用。
解决方案
jOOQ 支持 PostgreSQL 中的refcursor
结果类型(或OUT
参数),但不支持IN
参数类型,这确实有点怪癖,因为它伪装成标识符的伪类型。如果你可以重载你的函数到这个:
create or replace function foo_cursor(name character varying)
returns refcursor
as $func$
declare
ref refcursor;
begin
open ref for
select id, first_name, last_name
from students
where first_name = name;
return ref;
end
$func$
language plpgsql;
然后,jOOQ 将能够调用该函数。jOOQ(或者您,在使用 jOOQ 时)不需要定义生成的游标名称。
但是,您可能需要在事务内部运行函数调用。
推荐阅读
- ios - 实例化 Nib 并访问它的子视图
- php - Wordpress REST API 自定义帖子类型 DELETE 访问被禁止
- wordpress - 在 WordPress 中批准评论时重定向到 URL
- c# - 覆盖第二个可选参数,同时保留第一个可选参数
- c - C 程序卡在类似无限循环的 nautre 中,但没有打印任何变量
- java - 正在同步的字符串缓冲区有什么用
- python - 如何将一组数据框中的列值替换为与另一组数据框相比
- android - 可展开列表视图内的列表视图
- python - decorator to generate new classes into namespace
- python - pyqt attribute error when trying to emit integer signal