Re: Initializing Datums for use with SPI_execute_plan - Mailing list pgsql-general

From Jack Orenstein
Subject Re: Initializing Datums for use with SPI_execute_plan
Date
Msg-id 7ecd811f0609141445r7d024d53icb73cb47164c4565@mail.gmail.com
Whole thread Raw
In response to Re: Initializing Datums for use with SPI_execute_plan  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general
On 9/13/06, Martijn van Oosterhout <kleptog@svana.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

pgsql-general by date:

Previous
From: Steve Crawford
Date:
Subject: Feature request (was psql: absolutes and toggles)
Next
From: "Joshua D. Drake"
Date:
Subject: Re: PostgreSQL slammed by PHP creator