Postgres文本数组到hstore

时间:2018-01-24 05:54:10

标签: postgresql data-conversion hstore

我有一个偶数长度的数组,用逗号分隔的值(不一定是同质的):

'{"a", 10000, "b", 20000}'

我想将这个postgres数组解析成一个hstore列,将每个奇数条目(索引1,3,...)关联为一个键,并将每个偶数条目(索引2,4,...)关联为值。这可能吗?

1 个答案:

答案 0 :(得分:1)

正如萨米(Sami)所说,只需使用:

#include <iostream>

struct A { template<typename X, typename Y> static bool compare(const X&, const Y&) { return true; } };
struct B { };

template <typename Z, typename X, typename Y>
inline void Process(X x, Y y)
{    
    if constexpr (std::is_invocable_v<decltype(Z::compare), const X&, const Y&> )
    {
        Z::compare(x,y);
    }
    else { 
        std::cout << "hi";
    }
}

int main()
{
    Process<A>(3,2);
    Process<B>(3,2);    
}

输出:

SELECT hstore('{"a", 10000, "b", 20000}'::text[]);