Re: Initializing Datums for use with SPI_execute_plan

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Jack Orenstein <jack(dot)orenstein(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Initializing Datums for use with SPI_execute_plan
Date: 2006-09-13 05:35:57
Message-ID: 20060913053557.GC28182@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Sep 12, 2006 at 09:57:33PM -0400, Jack Orenstein wrote:
> I'm stuck on how to bind the values. The problem is that the values
> being bound are not present as Datums. I don't get them as function
> parameters, or from a table. I have data encoded, in a proprietary
> format in one bytea column of a table. My function decodes the bytea
> yielding the values that I need to bind to the INSERT statement.
>
> int, bigint: From looking at postgres.h, I realize that Datum is an
> unsigned long. I'm guessing that I should just be able to assign Datums
> carrying ints or bigints, e.g. using Int32GetDatum to cast the int to
> a Datum. Is that correct?

Yes, need to use the *GetDatum functions.

> varchar: I have a zero-terminated string that I need to turn into a
> Datum. Is CStringGetDatum the right thing to use?

CStringGetDatum will get you something of the right format for cstring,
if you want varchar, you need to have an object of "VarChar*" first.

> bytea: I have an unsigned char* (not zero-terminated). Can I use
> PointerGetDatum?

Similarly you should use "bytea*" here. Then you can use
PointerGetDatum.

> For the varchar and bytea cases, I assume that I simply pfree the
> palloc'ed data as usual, after the SPI_execute_plan call.

Pretty much.

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message J S B 2006-09-13 05:51:38 Re: berkley sockets
Previous Message Martijn van Oosterhout 2006-09-13 05:31:08 Re: berkley sockets