could some one give me an hint in this starting tentative for a custom function?
I need to construct the query with 2 parameters, a varchar and a unix timestamp (an integer) I've spent 3 hours with the few lines below and this result
a query test could be select * from pdc_posot_cf_anno('MRDDCU83C12C433F',2012); I just would like to construct a correct SQL query to pass to SPI_exec.
Thanks a lot.
CREATE FUNCTION pdc_posot_cf_anno(varchar,integer)
RETURNS integer
AS 'pdc','posot_cf_anno'
LANGUAGE C STABLE STRICT;
Datum
posot_cf_anno(PG_FUNCTION_ARGS) {
char timestring[1024] = "";
char qanno[1024];
Timestamp t;
time_t time = 0;
int tempo;
Datum td;
sprintf(timestring,"%d-01-01",PG_GETARG_INT32(1));
elog(INFO, "tutto bene %s !",timestring);
t = DatumGetTimestamp(DirectFunctionCall2(to_timestamp,
CStringGetTextDatum(timestring),
CStringGetTextDatum("YYYY-MM-DD")));
sprintf(qanno,"SELECT DISTINCT o.idot FROM sit.otpos o "
"WHERE btrim(o.codfis) = %s AND to_timestamp(validita) <= %s ORDER BY o.idot;",
PG_GETARG_CSTRING(0), t);
elog(INFO, "QUERY %s !",qanno);
// SPI_connect();
// res = SPI_exec(qanno,0);
return 0;
}