Hello hackers
At the moment I need to pass from a SQL array to a C array.
I have the following table:
CREATE TABLE emps
( name text, array int4[]
)
For example, array have this values: {4000,1,0,0}
I wrote this function for test in order to see something that could help me:
extern Datum vector(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(vector);
Datum
vector(PG_FUNCTION_ARGS)
{text *table_text = PG_GETARG_TEXT_P(0);TupleDesc tupdesc;bool isnull;char *table_char,
*type,*cmd;int n1, ret;
table_char = text_2_char(table_text); /* pass text to c string */cmd = (char *) palloc(14 + strlen(table_char) +
1);tupdesc= (TupleDesc) RelationNameGetTupleDesc(table_char);sprintf(cmd, "select * from %s", table_char);
SPI_connect();type = SPI_gettype(tupdesc, 2); /* to obtain the c data type of the
sql array?? */ret = SPI_exec(cmd, 0);
/* for this test I take only the first table row */n1 = DatumGetInt32(SPI_getbinval(SPI_tuptable->vals[0],
SPI_tuptable->tupdesc, 2, &isnull));SPI_finish();
elog(INFO, "The data type of the column no. 2 is %s and its value is
%d", type, n1);return PointerGetDatum(ret);
}
Then, the result of the "select vector('emps');" was
INFO: The data type of the column no. 2 is _int4 and its value is 137606808
I wanted that n1 collects the data of the SQL array. I really
appreciate any suggestions, thanks in advance, regards.
Juan P. Espino
PS. I ignore if the term I'm using SQL array is correct.