Re: Is there any ways to pass an array as parameter in libpq?

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: ChenXun <p(dot)smasher(at)hotmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Is there any ways to pass an array as parameter in libpq?
Date: 2009-10-27 12:15:11
Message-ID: b42b73150910270515v4e155973n8e7403e8fa2b1939@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2009/10/26 ChenXun <p(dot)smasher(at)hotmail(dot)com>:
> Hello,
>
> I'm starting to learn programming with libpq.
> In the main loop of my code, I will receive some data in the format of an
> array of a struct. The data will be inserted to the database, in different
> lines.
> I also need to update the last record in the table before the insertion. So
> I plan to write a pl/pgsql function (procedure) to perform the whole
> updating and inserting.
> But I don't know how to pass the array to the procedure through libpq. It
> seems the only way is to using libpq to do updating and inserting
> separately. Like this
>
> for(;;) {
> /* receive data */
>
> /* libpq updating */
>   PQexec(...);
>
>   PQprepare(...);
>   for (i=0; i<n; i++) {
>   /* libpq inserting */
>     PQexecPrepared()...
>  }
> }
>
> The PQprepare function has to be called in each loop.So my question is that
> whether there is a method to pass the array as parameter to libpq?

check out libpqtypes (http://libpqtypes.esilo.com/) It's exactly what
you want. We expose sending arrays/composites directly in
paramaterized fashion in binary (no escaping). You still have to loop
because a C array is not directly compatible to postgres array.

PGint4 i;
PGarray arr;
PGparam *param;

arr.ndims = 0;
arr.param = PQparamCreate(conn);

for(i=0; i < 1000; i++)
PQputf(arr.param, "%int4", i); // client side

param = PQparamCreate(conn);
PQputf(param, "%int[]", &arr); // client side

res = PQparamExec(conn, param, "insert into foo values ($1)", 0);

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2009-10-27 12:21:19 Re: Invalid Page Header Error
Previous Message Merlin Moncure 2009-10-27 12:06:38 Re: joining an array with a table or...?