Sunday, January 13, 2008

postgresql fetching data

Every time you want to fetch data from DB you use either PQexecParams or PQexec. Their syntax:

PGresult *PQexecParams(PGconn *conn,
                       const char *command,
                       int nParams,
                       const Oid *paramTypes,
                       const char * const *paramValues,
                       const int *paramLengths,
                       const int *paramFormats,
                       int resultFormat);
PGresult *PQexec(PGconn *conn, const char *command);
Yep, the difference is pretty big. PQexecParams has +6 arguments. The most interesting argument that makes SELECT request result different is resultFormat. PQexec doesn't have it. According to PostgreSQL's manual:

if resultFormat is set to zero it'll obtain results in text format, or if it is set to one it will obtain results in binary format.

Great! I want to get data in binary format. It'll be safe to get abyte(aka blob) data. Do you know what does 'obtain in binary format' mean? PosgreSQL manual hazily says:

There is not currently a provision to obtain different result columns in different formats, although that is possible in the underlying protocol.

That means that in 'binary mode' abyte would be binary, numeric would be numeric(int, long, short, ...), date will be returned as long and so on. Yes, I love postgreSQL that I can obtain data in it's native form.

My suggestion is to add resultFormat to PQexec

No comments: