libpq binary composite

From: Konstantin Kuzvesov <kuzvesov(at)list(dot)ru>
To: pgsql-novice(at)postgresql(dot)org
Subject: libpq binary composite
Date: 2010-03-31 12:16:30
Message-ID: E1NwwqU-0007Qo-00.kuzvesov-list-ru@f278.mail.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi,

I'm trying to post a composite value to database using C libpq interface.
There's a test datatype

CREATE TYPE testtype AS ( i integer, d double precision )

and a test table

CREATE TABLE test ( t testtype ) WITH (OIDS=FALSE);

There's how I'm trying to do that:

#pragma pack(push,1)
struct {
int i;
double d;
} v = { -1, 0};
#pragma pack(pop)

const int nparams=1;
void *values[nparams] = { &v };
int lengths[nparams] = { sizeof(v) };
int binary[nparams] = { 1 };

PGresult *res;

res = PQprepare( dbh, "teststmt", "insert into test(t) values ($1::testtype)", 1, NULL );
if (PQresultStatus(res) != PGRES_COMMAND_OK) { PQclear(res); return 0; }
PQclear(res);

res = PQexecPrepared( dbh, "teststmt", nparams, (char**)values, lengths, binary, 1 );
if (PQresultStatus(res) != PGRES_COMMAND_OK) { PQclear(res); return 0; }
PQclear(res);

Postgres returns error: 'wrong number of columns: -1, expected 2'.
There can be arbitrary value instead of -1, and it coincides with value of field 'i' in the structure.
So, afais, there must be some ROW structure in PQexecPrepared parameters, not the one I pass to, but I don't know how to make it.

And let me answer some questions I suppose to appear:
1. Prepared statements and binary data format are chosen due to performance requirements.
2. I know that I can go this way: "insert into test(t) values ($1::integer, $2::double precision)::testtype", but I don't want to, since in real database there're too many fields in the composite and there's actually an array of the composites, which elements may be null, so I want pass a single C NULL to indicate that whole composite is null.

--
Regards,
Konstantin

Browse pgsql-novice by date

  From Date Subject
Next Message Jonathan Mast 2010-03-31 19:09:41 Need Help Enabling Remote Connections
Previous Message Sean Davis 2010-03-31 10:40:00 Re: [NOVICE] Connect to postgresql database using Perl