我有一个postgresfunction返回类型tp_m_info,其中包含 成对的数组(请参见下文)。我正在使用libpqxx连接到 Postgres。
CREATE TYPE public.tp_m_set_id AS
(
m_id integer,
m_name text
);
CREATE TYPE public.tp_m_info AS
(
m_id integer,
m_name text,
m_value double precision,
m_is_true boolean,
original_ms tp_m_set_id[]
);
我可以从结果中读取int,double,str和boolean值:
iter[m_id].as<int>()
iter[m_name].c_str()
iter[m_value].as<double>()
// and bool like
std::string tmp_str = iter["m_is_true"].c_str();
if ("t" == tmp_str)
{
info.m_is_merged = true;
}
else
{
info.m_is_merged = false;
}
但是我不知道如何处理“ tp_m_set_id []” 它失败,并显示诸如“ std :: vector>”
iter[original_ms].as<std::vector<std::pair<uint32_t, std::string>>>()
有什么想法吗?
在“ libpq”中,二进制结果为: PQexecParams()和paramFormats = 1 参见:https://www.postgresql.org/docs/10/libpq-exec.html
现在“ libpqxx”中有二进制格式吗?有什么改变 最近十年? 参见:http://pqxx.org/development/libpqxx/wiki/BinaryTransfers
有没有一种快速的方法可以从libpqxx和 将其转换为C ++?
答案 0 :(得分:0)
我想解决方法是
例如使用:
std::pair<int, int> my_pair = row["m_ints"].as<std::pair<int, int> >();
写入strconv.hxx:
template<> struct PQXX_LIBEXPORT string_traits<std::pair<int, int> >
{
static constexpr const char *name() noexcept { return "std::pair<int, int>"; }
static constexpr bool has_null() noexcept { return false; }
static bool is_null(std::pair<int, int>) { return false; }
[[noreturn]] static std::pair<int, int> null()
{ internal::throw_null_conversion(name()); }
static void from_string(const char Str[], std::pair<int, int> &Obj);
static std::string to_string(std::pair<int, int> Obj);
};
以及在strconv.cxx中实现:
现在您必须重新编译libpqxx。
问题在于,“ from_string”的实现最终以字符串形式解析为“(9,5)”之类的字符串。而且Stringparsing并不是我想要的。
所以我要尝试libpq。