Dissecting Tuples in C

Поиск
Список
Период
Сортировка
Искать

Dissecting Tuples in C

От:
Clay Luther <claycle@cisco.com>
Дата:
 I am trying to get up to speed writing C triggers and I have a question about the relation between HeapTuples and other tuple-related types, like TupleTableSlots, and the routines and macros used to manipulate them. I am reading and re-reading the online docs, but still have some broad questions.

For example, suppose I just wanted a trigger to iterate through the HeapTuple pointed at by the tirgger data tg_trigtuple, printing out the column names, types, and values (lets say, only ints and strings for simplicity). How would I do this?

A pointer to a good document would be just as good as a detailed answer.

Thanks!

-- 
Clay
claycle@cisco.com

Re: Dissecting Tuples in C

От:
"Nigel J. Andrews" <nandrews@investsystems.co.uk>
Дата:
On 26 Jun 2003, Clay Luther wrote:

> I am trying to get up to speed writing C triggers and I have a question
> about the relation between HeapTuples and other tuple-related types,
> like TupleTableSlots, and the routines and macros used to manipulate
> them. I am reading and re-reading the online docs, but still have some
> broad questions.
> 
> For example, suppose I just wanted a trigger to iterate through the
> HeapTuple pointed at by the tirgger data tg_trigtuple, printing out the
> column names, types, and values (lets say, only ints and strings for
> simplicity). How would I do this?
> 
> A pointer to a good document would be just as good as a detailed answer.

Take a browse around the things in contrib. However, I'd say the easiest way to
do the specific thing you ask about is to look up the SPI utility functions in
the docs and then write something like the psuedo code:

i = 1;
while (i < tupdesc->natts)
{char *colname = SPI_fname(tg_trigtuple, i);char *coltype = SPI_gettype(tg_trigtuple, i);char *colvalue = SPI_getvalue(tg_trigtuple, i);
i++;
}

That's very much psuedo code. It's bad that I can't remember the exact names
and parameters considering I was doing such things only a week ago but, that's
what being busy does to my memory.

But really, you can't go wrong looking in chapter 9 (server side
programming) at the spi functions. Well you can just watch out that the column
numbering starts at 1 for spi.


-- 
Nigel J. Andrews


Re: Dissecting Tuples in C

От:
Clay Luther <claycle@cisco.com>
Дата:
 Hrmmmm....let me be more clear.

How do I get the TupleDesc for a HeapTuple, for example?  The Trigger structure has HeapTuples, but I need the TupleDesc as well to be able to use functions like SPI_getvalue().

Clay




On Thu, 2003-06-26 at 15:58, Nigel J. Andrews wrote:
On 26 Jun 2003, Clay Luther wrote:

> I am trying to get up to speed writing C triggers and I have a question
> about the relation between HeapTuples and other tuple-related types,
> like TupleTableSlots, and the routines and macros used to manipulate
> them. I am reading and re-reading the online docs, but still have some
> broad questions.
>
> For example, suppose I just wanted a trigger to iterate through the
> HeapTuple pointed at by the tirgger data tg_trigtuple, printing out the
> column names, types, and values (lets say, only ints and strings for
> simplicity). How would I do this?
>
> A pointer to a good document would be just as good as a detailed answer.

Take a browse around the things in contrib. However, I'd say the easiest way to
do the specific thing you ask about is to look up the SPI utility functions in
the docs and then write something like the psuedo code:

i = 1;
while (i < tupdesc->natts)
{
        char *colname = SPI_fname(tg_trigtuple, i);
        char *coltype = SPI_gettype(tg_trigtuple, i);
        char *colvalue = SPI_getvalue(tg_trigtuple, i);

        i++;
}

That's very much psuedo code. It's bad that I can't remember the exact names
and parameters considering I was doing such things only a week ago but, that's
what being busy does to my memory.

But really, you can't go wrong looking in chapter 9 (server side
programming) at the spi functions. Well you can just watch out that the column
numbering starts at 1 for spi.


--
Nigel J. Andrews

Re: Dissecting Tuples in C

От:
Joe Conway <mail@joeconway.com>
Дата:
Clay Luther wrote:
> Hrmmmm....let me be more clear.
> 
> How do I get the TupleDesc for a HeapTuple, for example?  The Trigger
> structure has HeapTuples, but I need the TupleDesc as well to be able to
> use functions like SPI_getvalue().
> 

Take a look at ttdummy() in src/test/regress/regress.c.

Here's a snippet of the specific lines you need:
  TriggerData *trigdata = (TriggerData *) fcinfo->context;  rel = trigdata->tg_relation;  tupdesc = rel->rd_att;


HTH,

Joe


FAQ