Re: Returning Vector of Pairs with a PostgreSQL C Extension Function

From: TalGloz <glozmantal(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Returning Vector of Pairs with a PostgreSQL C Extension Function
Date: 2018-08-25 15:57:07
Message-ID: 1535212627744-0.post@n3.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

OK so I think I'm on the right way with this code and now my goal is to
return a text array (text[ ]) with 2 fields

extern "C" {
Datum text_array(PG_FUNCTION_ARGS){

// For text aka. character varying parameter
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);

ArrayType *array;
Datum* elements[2];
int16 typlen;
bool typbyval;
char typalign;

elements[0] = CStringGetDatum(localT1.c_str());
elements[1] = CStringGetDatum(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);
}

But I'm getting this:

g++ -std=c++17 -fPIC -Wall -Werror -g -O0 -pthread
-I/usr/pgsql-10/include/server -I/usr/local/include
-I/usr/local/include/cppcodec -o seal_diff_cpp.o -c seal_diff_cpp.cpp
In file included from seal_diff_cpp.cpp:2:
seal_diff_cpp.cpp: In function ‘Datum seal_diff_cpp(FunctionCallInfo)’:
/usr/pgsql-10/include/server/postgres.h:562:29: error: invalid conversion
from ‘Datum’ {aka ‘long unsigned int’} to ‘Datum*’ {aka ‘long unsigned
int*’} [-fpermissive]
#define PointerGetDatum(X) ((Datum) (X))
~^~~~~~~~~~~~
/usr/pgsql-10/include/server/postgres.h:584:28: note: in expansion of macro
‘PointerGetDatum’
#define CStringGetDatum(X) PointerGetDatum(X)
^~~~~~~~~~~~~~~
seal_diff_cpp.cpp:168:23: note: in expansion of macro ‘CStringGetDatum’
elements[0] = CStringGetDatum(localT1.c_str());
^~~~~~~~~~~~~~~
/usr/pgsql-10/include/server/postgres.h:562:29: error: invalid conversion
from ‘Datum’ {aka ‘long unsigned int’} to ‘Datum*’ {aka ‘long unsigned
int*’} [-fpermissive]
#define PointerGetDatum(X) ((Datum) (X))
~^~~~~~~~~~~~
/usr/pgsql-10/include/server/postgres.h:584:28: note: in expansion of macro
‘PointerGetDatum’
#define CStringGetDatum(X) PointerGetDatum(X)
^~~~~~~~~~~~~~~
seal_diff_cpp.cpp:169:16: note: in expansion of macro ‘CStringGetDatum’
elements[1] = CStringGetDatum(localT2.c_str());
^~~~~~~~~~~~~~~
seal_diff_cpp.cpp:171:26: error: cannot convert ‘Datum**’ {aka ‘long
unsigned int**’} to ‘Datum*’ {aka ‘long unsigned int*’}
array = construct_array(elements, 2, TEXTOID, typlen, typbyval, typalign);
^~~~~~~~
In file included from /usr/pgsql-10/include/server/utils/acl.h:38,
from
/usr/pgsql-10/include/server/catalog/objectaddress.h:18,
from
/usr/pgsql-10/include/server/catalog/pg_publication.h:21,
from /usr/pgsql-10/include/server/utils/rel.h:21,
from seal_diff_cpp.cpp:6:
/usr/pgsql-10/include/server/utils/array.h:370:42: note: initializing
argument 1 of ‘ArrayType* construct_array(Datum*, int, Oid, int, bool,
char)’
extern ArrayType *construct_array(Datum *elems, int nelems,
~~~~~~~^~~~~
make: *** [Makefile:19: seal_diff_cpp.o] Error 1

Best regards,
Tal

--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2018-08-25 16:31:22 Re: Returning Vector of Pairs with a PostgreSQL C Extension Function
Previous Message Tom Lane 2018-08-25 15:44:24 Re: Returning Vector of Pairs with a PostgreSQL C Extension Function