Re: [GENERAL] Convert Datum to an user-defined data type?

From: Amit Langote <amitlangote09(at)gmail(dot)com>
To: Anh Pham <atpham(dot)wpi(at)gmail(dot)com>
Cc: "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>, PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: [GENERAL] Convert Datum to an user-defined data type?
Date: 2014-03-10 15:02:35
Message-ID: CA+HiwqEnBsv+H+5zMO0DgXDTaZXceKpuLQ9Da9mvzSQb_H-BAA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice

On Mon, Mar 10, 2014 at 11:09 PM, Anh Pham <atpham(dot)wpi(at)gmail(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tim Kane 2014-03-10 15:04:44 Playing with 9.4devel - unnest
Previous Message Anh Pham 2014-03-10 14:09:57 [NOVICE]Convert Datum to an user-defined data type?

Browse pgsql-novice by date

  From Date Subject
Next Message Simon ... 2014-03-11 10:00:35 Best method for storing recoverable passwords
Previous Message Anh Pham 2014-03-10 14:09:57 [NOVICE]Convert Datum to an user-defined data type?