gpt4 book ai didi

c++ - 如何从 pqxx 调用重载的远程过程

转载 作者:行者123 更新时间:2023-11-28 06:43:40 25 4
gpt4 key购买 nike

如何从pqxx调用重载的远程过程?

程序例如:

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++代码:

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"

如何从 C++ 代码中调用“foo(num integer)”?在示例中,所需结果为“ans”中的“int”。

psql.exe 输出:

SELECT foo(1); returns "int"

SELECT foo("test"); returns "text"

提前致谢。

最佳答案

我不认为你可以用一个准备好的语句来解决它。

您可以在 libpqxx 3.1 中设置每个参数的处理方式(参见 third example),例如:

connection.prepare(query_mark_text, "SELECT foo($1);")("text", pqxx::prepare::treat_string);
connection.prepare(query_mark_int, "SELECT foo($1);")("integer");

但我在 latest docs 中看不到它.

或者,您可以在 PostgreSQL 端将参数转换为所需的类型,方法是:

connection.prepare(query_mark_text, "SELECT foo($1::text);");
connection.prepare(query_mark_int, "SELECT foo($1::int);");

关于c++ - 如何从 pqxx 调用重载的远程过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25448432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com