C Tigger Function Tuples
| От | Kubat, Philip | 
|---|---|
| Тема | C Tigger Function Tuples | 
| Дата | |
| Msg-id | 31AAF4D40EEBD31197250008C74B668401E8BFC3@cbebosntex1.cbetech.com обсуждение исходный текст | 
| Список | pgsql-general | 
/*
 Echos new record to file
*/
#include <stdlib.h>
#include "executor/spi.h" /* this is what you need to work with SPI */ #include "commands/trigger.h" /* -"- and triggers */
// Dummy prototype for function, looking for the real thing!
int GetDatumType(Datum myDatum);
extern Datum cbe_echo(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(echo_echo);
Datum cbe_echo(PG_FUNCTION_ARGS) 
{
 TriggerData *trigdata = (TriggerData *) fcinfo->context;
 TupleDesc tupdesc;
 HeapTuple rettuple;
 bool isnull;
 char* str;
 int num;
 int numatts;
 int curattr;
 Datum retDatum;
 /* Make sure trigdata is pointing at what I expect */
 if (!CALLED_AS_TRIGGER(fcinfo))
  elog(ERROR, "pg_echo: not fired by trigger manager");
 
 elog(DEBUG,"cbe_echo: function called");
 /* tuple to return to Executor */
 if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
  rettuple = trigdata->tg_newtuple;
 else
  rettuple = trigdata->tg_trigtuple;
 
 tupdesc = trigdata->tg_relation->rd_att;
/*
 //str=(char*)palloc(40);
 //str = (char*) GetAttributeByName(rettuple->t_data, "name", &isnull);
        str=(char*)DatumGetCString(DirectFunctionCall1(textout,heap_getattr(rettuple,1,tupdesc,&isnull)));
        num=DatumGetInt32(heap_getattr(rettuple,2,tupdesc,&isnull));
 if (isnull)
  elog(DEBUG,"cbe_echo: NULL");
 else
  elog(DEBUG,"cbe_echo: not NULL");
 elog(DEBUG,"cbe_echo: output..");
 elog(DEBUG,"cbe_echo: num <%i> str <%s>",num,str);
 elog(DEBUG,"cbe_echo: t_data t_natts <%i>",rettuple->t_data->t_natts);
 elog(DEBUG,"cbe_echo: tuple lenght %i",rettuple->t_len);
 elog(DEBUG,"cbe_echo: end.");
*/
 
 // Sample of what I would like to do.
numatts=rettuple->t_data->t_natts;
 for ( curattr=1;curattr<=numatts;curattr++ ) {
      retDatum=heap_getattr(rettuple,curattr,tupdesc,&isnull);
  // ** WHAT IS THIS FUNTION ** //
  switch (GetDatumType(retDatum)) {
   // int4 type oid?
   case 23: elog(DEBUG,"cbe_echo: %i",DatumGetInt32(heap_getattr(rettuple,2,tupdesc,&isnull)));
   break;
   // text?
   case 25: elog(DEBUG,"cbe_echo: %s",(char*)DatumGetCString(
    DirectFunctionCall1(textout,heap_getattr(rettuple,1,tupdesc,&isnull))));
   break;
  } //switch
 } //for
 return PointerGetDatum(rettuple);
}
В списке pgsql-general по дате отправления: