libpq binary composite - Mailing list pgsql-novice

From Konstantin Kuzvesov
Subject libpq binary composite
Date
Msg-id E1NwwqU-0007Qo-00.kuzvesov-list-ru@f278.mail.ru
Whole thread Raw
List 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
tomake 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'twant 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

pgsql-novice by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: [GENERAL] Connect to postgresql database using Perl
Next
From: "Wright, George"
Date:
Subject: Re: Need Help Enabling Remote Connections