[英]How to call overloaded remote procedure from pqxx
How to call overloaded remote procedure from pqxx? 如何从pqxx调用重载的远程过程?
Procedures eg: 程序,例如:
CREATE OR REPLACE FUNCTION foo(str text) RETURNS text AS $$
BEGIN
RETURN 'text';
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION foo(num integer) RETURNS text AS $$
BEGIN
RETURN 'int';
END;
$$ LANGUAGE plpgsql;
C++ code: C ++代码:
pqxx::connection connection(/* credentials */);
std::string query_mark = "test_procedure_mark";
connection.prepare(query_mark, "SELECT foo($1);");
//first case
pqxx::work work(connection);
pqxx::result res = work.prepared(query_mark)("text").exec();
work.commit();
std::string ans = res[0][0].as(std::string("")); //ans here "text"
//second case
pqxx::work work(connection);
pqxx::result res = work.prepared(query_mark)(1).exec();
work.commit();
std::string ans = res[0][0].as(std::string("")); //ans here "text"
How can i call "foo(num integer)" from c++ code? 我如何从C ++代码中调用“ foo(num integer)”? In example desired result "int" in "ans".
在示例中,“ ans”中的期望结果“ int”。
psql.exe output: psql.exe输出:
SELECT foo(1);
SELECT foo(1); returns "int"
返回“ int”
SELECT foo("test");
SELECT foo(“ test”); returns "text"
返回“文本”
Thanks in advance. 提前致谢。
I don't think you can solve it with a single prepared statement. 我认为您无法用一个准备好的语句解决它。
You can set each parameter's treatment in libpqxx 3.1 (see third example ), like: 您可以在libpqxx 3.1中设置每个参数的处理方式(请参见第三个示例 ),例如:
connection.prepare(query_mark_text, "SELECT foo($1);")("text", pqxx::prepare::treat_string);
connection.prepare(query_mark_int, "SELECT foo($1);")("integer");
But i cannot see that in the latest docs . 但是我看不到最新的文档 。
Or, you can cast your parameteres to the desired type on PostgreSQL's side, with: 或者,您可以使用以下命令将参数转换为PostgreSQL所需的类型:
connection.prepare(query_mark_text, "SELECT foo($1::text);");
connection.prepare(query_mark_int, "SELECT foo($1::int);");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.