C++ 返回成对向量或文本数组的PostgreSQL C扩展函数
我想用C扩展返回一个向量对。这是一个简单的例子 我有:C++ 返回成对向量或文本数组的PostgreSQL C扩展函数,c++,postgresql,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::st
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<std::string, std::string>> ret;
ret.emplace_back(encodedLocalT1, encodedLocalT2);
PG_RETURN_ARRAYTYPE_P(ret);
};
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>
extern“C”{//C头必须在extern“C”{}块内。
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
PG_模块_魔术;
}
//CPP头必须位于外部“C”{}块之外。
#包括
#包括
#包括
#包括
#包括
#包括//用于ostream_迭代器
#包含//以进行一些代码基准测试
//外部项目C++库编译并运行运行“make”链接。
#包括
#包括
#包括
上面的代码示例对这个问题的帮助很小。所有的include都用于代码的长版本中
我似乎不知道如何实现我的目标,我更喜欢返回带有两个字符串作为元素的
text[]
。这段代码适合我
extern“C”{
基准文本数组(PG函数参数){
//获取文本类型变量的输入
text*t1=PG_GETARG_text_PP(0);
text*t2=PG_GETARG_text_PP(1);
//在cpp字符串中转换
std::string localT1=TextDatumGetCString(t1);
std::string localT2=TextDatumGetCString(t2);
//返回包含2个元素的文本数组
ArrayType*数组;
基准要素[2];
int16型;
布尔·提比瓦尔;
字符类型对齐;
//将cpp字符串强制转换回pgSQL数据类型
元素[0]=cstringgettextdatam(localT1.c_str());
元素[1]=cstringgettextdatam(localT2.c_str());
//为pgSQL创建返回数组
获取_typlenbyvalalign(TEXTOID,&typlen,&typbyval,&typallign);
数组=构造数组(元素,2,TEXTOID,typlen,typbyval,typallign);
PG_返回_数组类型_P(数组);
};
PG_功能_信息_V1(文本_数组);
}
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>