C Tigger Function Tuples

From: "Kubat, Philip" <philip(dot)kubat(at)cbetech(dot)com>
To: "'pgsql-general(at)postgresql(dot)org'" <pgsql-general(at)postgresql(dot)org>
Subject: C Tigger Function Tuples
Date: 2002-08-07 13:53:33
Message-ID: 31AAF4D40EEBD31197250008C74B668401E8BFC3@cbebosntex1.cbetech.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I am creating a C function which send inserted records to a file called via
a after insert trigger. I have attached my code. I have the basic
functionality working, but I need to understand working with heap tuples. I
am using heap_getattr to retrieve the attrs from the HeapTuple. I am
planning to loop through the attr and the send then to a file.

My question is how do you know what "type" an attr is? To convert from
Datum to C type i.e. DataGetInt32, dataGetCString, etc. (See my dummy
function GetDatumType.)

Thanks!
Philip Kubat

/*
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);
}

Responses

Browse pgsql-general by date

  From Date Subject
Next Message The Doctor What 2002-08-07 14:38:50 Re: inet <<= and indexes
Previous Message Elielson Fontanezi 2002-08-07 11:32:54 J2EE IDE - to help J2EE development