Search Postgresql Archives

Re: Initializing Datums for use with SPI_execute_plan

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 9/13/06, Martijn van Oosterhout <kleptog@xxxxxxxxx> 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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux