ECPG 使用数组参数调用存储过程
我有一个 ECPG 客户端尝试获取数据。它使用带有子选择子句的准备好的语句,使用带有 4 个参数的存储函数“getsipid”。 我已经将第四个参数声明为简单的 varchar 了。我试图将第四个参数声明为数组,但是 ecpg 仅在第四个参数中传递数组的第一个参数,而不是全部参数。
传递整个 requsr(s) 数组的技巧是什么?此数组大小通常最多仅包含 5 个参数。我意识到我可以向函数添加更多参数来解决这个问题。我希望解决方案会更优雅。
感谢您的评论
Dave
EXEC SQL BEGIN DECLARE SECTION;
const char* cid = connid;
const char* tgrp; // group key ( trk )
const char* cca; // call control agent key ( trk )
const char* dhost; // dest host key (trk)
const char* regusr[MAX_USRS]; // Registration user (lin)
EXEC SQL END DECLARE SECTION;
pgc 文件准备了上述主机变量,并且有一个调用可以达到效果。
EXEC SQL AT :cid PREPARE ps_portdataviasip FROM
"SELECT * FROM port LEFT JOIN linesip USING (id)\
LEFT JOIN trunksip USING (id)\
LEFT JOIN customer USING (cid)\
WHERE port.id = (SELECT getsipid(?, ?, ?, ?))\
ORDER BY registersip.expiration DESC";
EXEC SQL AT :cid DECLARE cur_portsip CURSOR FOR ps_portdataviasip;
EXEC SQL AT :cid OPEN cur_portsip USING :cca, :tgrp, :dhost, :regusr;
EXEC SQL AT :cid FETCH NEXT FROM cur_portsip INTO DESCRIPTOR sqlda;
存储的函数声明为
CREATE OR REPLACE FUNCTION getsipid(cca character varying, tgrp character varying,
dhost character varying, usr character varying[]) RETURNS INTEGER AS
I have a ECPG client attempting to obtain data. It uses a a prepared statement with a subselect clause using a stored function "getsipid" that takes 4 arguments.
I have had this working with the 4th argument declared as a simple varchar. I have tried to declare the 4th argument as an array however, ecpg is only passing the first argument of the array in the 4th arg and not all of them.
What is the trick to passsing the entire arry of requsr(s). This array size would typically only contain upto 5 arguements. I realize I could just add more arguments to the function to overcome this issue. I was hoping the solution would be more elegant.
Thanks for your comments
Dave
EXEC SQL BEGIN DECLARE SECTION;
const char* cid = connid;
const char* tgrp; // group key ( trk )
const char* cca; // call control agent key ( trk )
const char* dhost; // dest host key (trk)
const char* regusr[MAX_USRS]; // Registration user (lin)
EXEC SQL END DECLARE SECTION;
The pgc file prepares the above host variables and the there is an invocation to the effect.
EXEC SQL AT :cid PREPARE ps_portdataviasip FROM
"SELECT * FROM port LEFT JOIN linesip USING (id)\
LEFT JOIN trunksip USING (id)\
LEFT JOIN customer USING (cid)\
WHERE port.id = (SELECT getsipid(?, ?, ?, ?))\
ORDER BY registersip.expiration DESC";
EXEC SQL AT :cid DECLARE cur_portsip CURSOR FOR ps_portdataviasip;
EXEC SQL AT :cid OPEN cur_portsip USING :cca, :tgrp, :dhost, :regusr;
EXEC SQL AT :cid FETCH NEXT FROM cur_portsip INTO DESCRIPTOR sqlda;
The stored function is declared as
CREATE OR REPLACE FUNCTION getsipid(cca character varying, tgrp character varying,
dhost character varying, usr character varying[]) RETURNS INTEGER AS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题通常是您需要将数组处理为 PostgreSQL 字符串表示形式。通常,这意味着将其转换为 CSV 并用适当的括号括起来。所以你会得到类似
'{"myuser2","myuser1","myuser321"}'
这实际上在很多环境中都很典型,而不仅仅是 ecpg。
第二种方法可能是使函数成为可变参数并以这种方式传递参数。然后它们将以数组的形式出现,您可以像这样使用它们,但您可以将它们作为单独的参数传递。
The problem typically is that you need to process the array to be in the PostgreSQL string representation. Typically this means converting it to CSV and surrounding it with appropriate brackets. So you get something like
'{"myuser2","myuser1","myuser321"}'
This is actually pretty typical in a lot of environments, not only ecpg.
A second approach might be to make the function variadic and just pass the args in that way. Then they will come in as an array and you get to use them as such, but you pass them as individual arguments.