[NOVICE]Convert Datum to an user-defined data type?

Started by Anh Phamabout 12 years ago2 messagesgeneral
Jump to latest
#1Anh Pham
atpham.wpi@gmail.com

Hi everyone,
I have created a new data types using SQL in a database:

CREATE TYPE *type1* AS (
score integer,
time integer[],
);

I also created a server extension module (C code), and I was able to
retrieve a Datum value of *type1 *from the database (using SPI_ functions)
My question is: how can I convert that Datum value back to type1 data
structure (in C code), so I can easily deal with score and time values?

typedef struct *type1*{
int score;
int *time;
} type1;

PS: I really don't want to deal with cstring to in order to get the score
and time attributes.

Many thanks :)

#2Amit Langote
Langote_Amit_f8@lab.ntt.co.jp
In reply to: Anh Pham (#1)
Re: [GENERAL] Convert Datum to an user-defined data type?

On Mon, Mar 10, 2014 at 11:09 PM, Anh Pham <atpham.wpi@gmail.com> wrote:

Hi everyone,
I have created a new data types using SQL in a database:

CREATE TYPE type1 AS (
score integer,
time integer[],
);

I also created a server extension module (C code), and I was able to
retrieve a Datum value of type1 from the database (using SPI_ functions)
My question is: how can I convert that Datum value back to type1 data
structure (in C code), so I can easily deal with score and time values?

I wonder if you read section 35.9.8 about using "funcapi.h" here:
http://www.postgresql.org/docs/9.1/static/xfunc-c.html

I suggest you read more about the following functions and how they are
to be used (quoting brief descriptions from the page here):

* To get a TupleDesc based on a type OID, call:
TupleDesc TypeGetTupleDesc(Oid typeoid, List *colaliases)

* Once you have a TupleDesc, call:
TupleDesc BlessTupleDesc(TupleDesc tupdesc)

* With the blessed tupdesc, given user data in Datum form, to build a
HeapTuple, use:
HeapTuple heap_form_tuple(TupleDesc tupdesc, Datum *values, bool *isnull)

--
Amit

--
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice