PostgreSQL C扩展函数返回向量对或文本数组

时间:2018-08-25 14:53:10

标签: c++ postgresql

我想使用C扩展名返回向量对。这很简单 我有的代码:

extern "C" { 
Datum pair(PG_FUNCTION_ARGS){ 

        // get the input
        text *t1 = PG_GETARG_TEXT_PP(0); 
        text *t2 = PG_GETARG_TEXT_PP(1); 
        std::string localT1 = text_to_cstring(t1); 
        std::string localT2 = text_to_cstring(t2); 

        // Return vector of pairs 
        std::vector<std::pair&lt;std::string, std::string>> ret; 
        ret.emplace_back(encodedLocalT1, encodedLocalT2); 
        PG_RETURN_ARRAYTYPE_P(ret); 

}; 
PG_FUNCTION_INFO_V1(pair); 
} 

但是它不能像它一样工作,我也不知道是否有可能。因此,我尝试返回一个text[],但它也不起作用:

extern "C" { 
Datum pair(PG_FUNCTION_ARGS){ 

        // Get the input 
        text *t1 = PG_GETARG_TEXT_PP(0); 
        text *t2 = PG_GETARG_TEXT_PP(1); 
        std::string localT1 = text_to_cstring(t1); 
        std::string localT2 = text_to_cstring(t2); 

        // Return array with 2 strings 
        ArrayType *array;
        Datum elements[2];
        int16 typlen;
        bool typbyval;
        char typalign;

        elements[0] = cstring_to_text(localT1.c_str());
        elements[1] = cstring_to_text(localT2.c_str());

        get_typlenbyvalalign(TEXTOID, &typlen, &typbyval, &typalign);
        array = construct_array(elements, 2, TEXTOID, typlen, typbyval, typalign);

        PG_RETURN_ARRAYTYPE_P(array);

}; 
PG_FUNCTION_INFO_V1(pair); 
} 

我使用扩展名中的那些内容:

extern "C" { // C Headers must be inside exter "C" { } block.
#include <postgres.h>
#include <fmgr.h>
#include <utils/builtins.h>
#include <catalog/pg_type.h>
#include <utils/rel.h>
#include <utils/array.h>
#include <stdlib.h>
#include <stdint.h>
#include <utils/lsyscache.h>

PG_MODULE_MAGIC;
}

// CPP Header must be outside extern "C" { } block.
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iterator> // For the ostream_iterator
#include <chrono> // For some code benchmarking

// External projects c++ libraries compiled and linked on running 'make'.
#include <seal/seal.h>
#include <thread>
#include <cppcodec/base64_rfc4648.hpp>

上面的代码示例对解决问题的帮助很小。所有包含的内容都在代码的长版中使用。

我似乎无法弄清楚如何实现自己的目标,我更喜欢以两个字符串作为元素返回text[]

1 个答案:

答案 0 :(得分:1)

这段代码对我有用。

extern "C" { 
Datum text_array(PG_FUNCTION_ARGS){ 

        // Get the input 
        text *t1 = PG_GETARG_TEXT_PP(0); 
        text *t2 = PG_GETARG_TEXT_PP(1); 
        std::string localT1 = TextDatumGetCString(t1); 
        std::string localT2 = TextDatumGetCString(t2); 

        // Return a text array with 2 elemsnts
        ArrayType *array;
        Datum elements[2];
        int16 typlen;
        bool typbyval;
        char typalign;

        elements[0] = CStringGetTextDatum(localT1.c_str());
        elements[1] = CStringGetTextDatum(localT2.c_str());

        get_typlenbyvalalign(TEXTOID, &typlen, &typbyval, &typalign);
        array = construct_array(elements, 2, TEXTOID, typlen, typbyval, typalign);

        PG_RETURN_ARRAYTYPE_P(array);

}; 
PG_FUNCTION_INFO_V1(text_array); 
}