Re: Initializing Datums for use with SPI_execute_plan

From: "Jack Orenstein" <jack(dot)orenstein(at)gmail(dot)com>
To: "Martijn van Oosterhout" <kleptog(at)svana(dot)org>, "Jack Orenstein" <jack(dot)orenstein(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Initializing Datums for use with SPI_execute_plan
Date: 2006-09-14 21:45:07
Message-ID: 7ecd811f0609141445r7d024d53icb73cb47164c4565@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 9/13/06, Martijn van Oosterhout <kleptog(at)svana(dot)org> wrote:
> On Tue, Sep 12, 2006 at 09:57:33PM -0400, Jack Orenstein wrote:
> > ...
> > 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.

I don't think I explained myself clearly. I have a C string (char*,
terminating zero) and a byte array (char*, possibly containing zeros,
and I know the length). I want to obtain Datums wrapping these values
that will be used to bind varchar and bytea columns, and I'm trying to
find out how to generate these Datums.

This doesn't seem right:

void* plan = SPI_prepare(...);
char* string = ...;
char* byte_array = ...;
int byte_array_length = ...;
Datum* values = (Datum*) palloc(sizeof(Datum) * 2);
values[0] = CStringGetDatum(string);
values[1] = PointerGetDatum(byte_array);
SPI_execute_plan(plan, values, ...);

because the code does not communicate byte_array_length to
SPI_execute_plan.

I suspect I'm missing something important, because I don't know how to
get a bytea* from byte_array and byte_array_length.

Jack Orenstein

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joshua D. Drake 2006-09-14 21:51:40 Re: PostgreSQL slammed by PHP creator
Previous Message Steve Crawford 2006-09-14 21:33:54 Feature request (was psql: absolutes and toggles)