我正在将现有的 Java 项目从 Oracle 10 移植到 PostgreSQL 9.1.8(Cayenne 真的支持这个版本吗?)
返回游标时,OUT 参数有问题。返回类型应该是
类型.OTHER
但它看起来像一个空对象。与游标不同时,OUT 参数没有问题。
这是我的一张数据图,有一个对 postgreSQL 存储函数 ShowValidSource 的调用:
<procedure name="ShowValidSource" schema="voting_x">
<procedure-parameter name="i_n_sourceid" type="BIGINT" direction="in" />
<procedure-parameter name="i_s_description" type="VARCHAR" direction="in" />
<procedure-parameter name="i_n_active" type="BIGINT" direction="in" />
<procedure-parameter name="cur_ref" type="OTHER" direction="out" />
<procedure-parameter name="o_n_exitflag" type="BIGINT" direction="out" />
<procedure-parameter name="o_s_exitmsg" type="VARCHAR" direction="out" />
</procedure>
<db-entity name="cur_ref">
<db-attribute name="SOURCE_ID" type="DOUBLE" isPrimaryKey="true" />
<db-attribute name="DESCRIPTION" type="VARCHAR" />
<db-attribute name="ACTIVE" type="DOUBLE" />
</db-entity>
<obj-entity name="SourceByDescription" readOnly="true" className="it.mxx.vote.dao.types.SourceByDescriptionImpl" dbEntityName="cur_ref">
<obj-attribute name="sourceId" type="java.lang.Double" db-attribute-path="SOURCE_ID" />
<obj-attribute name="description" type="java.lang.String" db-attribute-path="DESCRIPTION" />
<obj-attribute name="active" type="java.lang.Double" db-attribute-path="ACTIVE" />
</obj-entity>
这是我的程序声明:
CREATE OR REPLACE FUNCTION showvalidsource(IN i_n_sourceid bigint, IN i_s_description character varying, IN i_n_active bigint, OUT o_rc_source refcursor, OUT o_n_exitflag bigint, OUT o_s_exitmsg character varying)
RETURNS record AS
$BODY$
DECLARE
--
V_S_Description varchar(255);
V_N_Active bigint;
V_N_ExitFlag bigint:= NULL;
v_rc_source refcursor := 'cur_ref';
--
BEGIN
--
IF I_N_Active = -1 THEN
V_N_Active := null;
ELSE
V_N_Active :=I_N_Active;
END IF;
--
V_S_Description := trim(lower(I_S_Description));
--
BEGIN
--
IF coalesce(V_N_Active,0) IN (0,1) THEN
NULL;
ELSE
--
V_N_ExitFlag := -10;
O_S_exitMsg := 'WARNING! I_N_Active accepted values : 0, 1, -1, null';
RAISE EXCEPTION USING MESSAGE = O_S_exitMsg;
--
END IF;
END;
--
-- IF V_N_SourceId IS NOT NULL THEN
IF coalesce(I_N_SourceId, -1) <> -1 THEN
--
OPEN v_rc_source FOR
SELECT vsd.source_id,
vsd.description,
vsd.active
FROM voting_system_detail vsd
WHERE vsd.source_id = I_N_SourceId
AND vsd.Description = coalesce(V_S_Description, vsd.Description)
AND vsd.active = coalesce(V_N_Active, vsd.active)
ORDER BY vsd.source_id;
--
o_rc_source := v_rc_source;
O_N_exitFlag := 0;
O_S_exitMsg := 'Ok';
--
ELSE
--
OPEN v_rc_source FOR
SELECT vsd.source_id,
vsd.description,
vsd.active
FROM voting_system_detail vsd
WHERE vsd.Description = coalesce(V_S_Description, vsd.Description)
AND vsd.active = coalesce(V_N_Active, vsd.active)
ORDER BY vsd.source_id;
--
o_rc_source := v_rc_source;
O_N_exitFlag := 0;
O_S_exitMsg := 'Ok';
--
END IF; --IF V_N_SourceId IS NOT NULL THEN
--
EXCEPTION
WHEN OTHERS THEN
O_N_exitFlag := coalesce(V_N_ExitFlag,-1);
O_S_exitMsg := SQLERRM;
--
OPEN v_rc_source FOR
SELECT -1 source_id,
'x' description,
-1 active
WHERE 1=2;
o_rc_source := v_rc_source;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION showvalidsource(bigint, character varying, bigint)
OWNER TO dbvoting;
注意:pg 客户端的这个游标返回 3 列类型的更多记录(SOURCE_ID double, DESCRIPTION varchar, ACTIVE double)
有什么问题?谢谢