postgresql libpqxx - 事务后没有结果

时间:2016-01-14 12:42:38

标签: postgresql transactions libpqxx

这些是我的代码:

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中添加一行。没有发生exeptions,但是在提交结果中,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');

1 个答案:

答案 0 :(得分:1)

我忘了执行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();