Hello,
I just finished a new C trigger that updated a "modified" column with
the current time upon an UPDATE event. It seems to work OK but I just
wanted to bounce this off you guys to check for some non-kosher stuff or
better way of doing it. Thanks in advance.
HeapTuple update_modified() {TupleDesc tupdesc;HeapTuple rettuple;bool isnull;TriggerData *trigdata =
CurrentTriggerData;
/* Get the current datetime. */Timestamp *tstamp = timestamp_in("now");Datum newdt = Float32GetDatum(tstamp);
CurrentTriggerData = NULL;
if (!trigdata) elog(NOTICE, "bid_control.c: triggers are not initialized");
if (!TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event)) elog(ERROR, "bid_control.c: trigger should only be called on
INSERT");
if (!TRIGGER_FIRED_BEFORE(trigdata->tg_event)) elog(ERROR, "bid_control.c: trigger should only be called BEFORE");
rettuple = trigdata->tg_trigtuple;tupdesc = trigdata->tg_relation->rd_att;
if ((i = SPI_connect()) < 0) elog(NOTICE, "bid_control.c: SPI_connect returned %d", i);
i = SPI_fnumber(tupdesc, "modified");rettuple = SPI_modifytuple( trigdata->tg_relation, rettuple,
1, &i, &newdt, NULL);
SPI_finish();return rettuple;
}
--
Louis-David Mitterrand - ldm@apartia.org - http://www.apartia.org
Lord, protect me from your followers.