这是我的代码:
std::unique_ptr<pqxx::connection> conn = connect();
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
try{
conn->prepare("add_pr_file", "select * from add_pr_file($1, $2, $3)");
conn->prepare("entire_add_pr", " select * from entire_add_pr($1, $2, $3, $4, $5)");
pqxx::work tr(*conn.get(), "add_pr");
pqxx::binarystring data((pr.input_data.empty() ? nullptr : (const void *)pr.input_data.data()), pr.input_data.size());
tr.prepared("entire_add_pr")(pr.guid.to_str())(conv.to_bytes(pr.name))(data)(std::to_string(pr.rate))(conv.to_bytes(pr.type));
for (auto file : pr.files) {
tr.prepared("add_pr_file")(pr.guid.to_str())(conv.to_bytes(file.path))(type_cnv::type(file.module)).exec();
}
tr.commit();
}
catch (const pqxx::pqxx_exception &exc) {
THROW(exc.base().what());
}
我无法执行entire_add_pr
函数。此函数必须在表pr
中添加一行。没有执行任何exeption,但提交时pr
表中没有行。然而,add_pr_file
语句在提交后得到结果。这些是entire_add_pr
函数:
create or replace function entire_add_pr( _guid uuid, _name character varying, _input_data bytea, _rate integer, _type_name character varying )
returns void as $$
begin
insert into pr (guid, name, input_data, rate_priority, pr_type_id) values (_guid, _name, _input_data, _rate, (select id from pr_type where type_name = _type_name));
end;
$$
language plpgsql;
如果我从postgresql命令行执行查询,该函数会很好地工作:
select * from entire_add_pr('1aaa9aaa-aaaa-aaaa-aaaa-aaaaaaaaa111', 'pr_name', '123', 10, 'test type');
我忘记执行exec
方法:
tr.prepared("entire_add_pr")(pr.guid.to_str())(conv.to_bytes(pr.name))(data)(std::to_string(pr.rate))(conv.to_bytes(pr.type)).exec();