gpt4 book ai didi

c - 用户在 C 中定义类型 PostgreSQL

转载 作者:太空宇宙 更新时间:2023-11-04 04:22:58 24 4
gpt4 key购买 nike

我正在用 C 开发 postgreSQL 的扩展。我创建了两种新类型,一种是 var len size (vlen)。我不知道如何为 Geo_Polygon 类型创建输入和输出函数。有人有例子吗?这是我到目前为止所做的。

typedef struct Geo_Point{
double x;
double y;
} Geo_Point;

typedef struct Geo_Polygon{
int32 v_len;
int32 n_points;
Geo_Point point[FLEXIBLE_ARRAY_MEMBER];
} Geo_Polygon;


PG_FUNCTION_INFO_V1(geo_polygon_in);
Datum
geo_polygon_in(PG_FUNCTION_ARGS){


char *str = PG_GETARG_CSTRING(0);
char *new_position = NULL;
char aux[1024];
int i, dimension, len;
Geo_Polygon *result;
Geo_Point *point;
double x, y;



point = (Geo_Point *) palloc(sizeof(Geo_Point));

new_position = strchr(str, '(');

for (dimension = 0; *new_position != '\0'; dimension++){

memset(aux, 0, sizeof(aux));
for (i = 0; *new_position != ')'; i++, ++new_position) {
aux[i] = *new_position;
}
aux[i] = *new_position;
++new_position;
if (*new_position == ',') {
++new_position;
}

point = (Geo_Point *) repalloc(point, (dimension + 1) * sizeof(Geo_Point));
if(sscanf(aux, " ( %lf , %lf )", &x, &y )!= 2)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("entrada errada para o tipo geo_point: \"%s\"",
str)));

point->x = x;
point->y = y;

}

len = sizeof(Geo_Point) * (dimension + 1)+ VARHDRSZ;
result = (Geo_Polygon *) palloc0(len);
SET_VARSIZE(result, len);
// copy the coordinates to the data area destino arqu e quantos bytes
memcpy((void *) VARDATA(result),(void *) VARDATA (point), (dimension) * sizeof(Geo_Point));

result->n_points = dimension;

PG_RETURN_POINTER(result);

}


Datum
geo_polygon_out(PG_FUNCTION_ARGS){
Geo_Polygon *geo_polygon = (Geo_Polygon *) PG_GETARG_POINTER(0);
StringInfoData buf;
int dim = geo_polygon->n_points;

int i;
int ndig = 2;

initStringInfo(&buf);


appendStringInfoChar(&buf, '(');
for (i = 0; i < dim; i++){

appendStringInfo(&buf, "%.*g", geo_polygon->point[i].x, "%.*g", geo_polygon->point[i].y);
appendStringInfoChar(&buf, ')');

}
PG_FREE_IF_COPY(geo_polygon, 0);
PG_RETURN_CSTRING(buf.data);}

最佳答案

您应该查看类型现有实现的来源,例如 contrib/ltree/ltree_io.c或者(也许更好,因为它听起来更接近你想要的)postgis/lwgeom_inout.c来自 PostGIS。

关于c - 用户在 C 中定义类型 PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44507040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com