Re: libpq binary data - Mailing list pgsql-interfaces

From Daniel Verite
Subject Re: libpq binary data
Date
Msg-id 68c2c90f-2407-4cb0-a234-d38cb68e90d2@mm
Whole thread Raw
In response to libpq binary data  (thilo@riessner.de)
List pgsql-interfaces
     thilo@riessner.de wrote:

> I try to get the epoch value of a date via the 
> PQexecParams(conn, "SELECT extract(epoch from date + time) as epoch, content
> FROM daten .....);
> In that database, the timestamp ist stored in the two fields date and time.
> I want to get this data in binary form. The 
> PQfsize(res, 1); 
> tells me, that the size of the returned data is 8 byte (in contrast to the 
> standard size of epoch, which is meant to be 4 byte)
> I don't manage to get the epoch valule (seconds since 1970) from that
> returned value.

The doc about EXTRACT(field FROM source)  says:
"The extract function returns values of type double precision"

In C, that would presumably map to the "double" 64 bits floating point type.

For code converting the binary representation to a host variable, you may get
bits from  postgres itself in backend/libpq/pqformat.c

Otherwise, a working example could look like this
(without guarantee that it's suitable for your platform):

#include <stdint.h>
union {   uint64_t i;   double fp;
} swap;
uint64_t ibe = *((uint64_t*)PQgetvalue(result, row, column);
swap.i = be64toh(ibe);

And your result would be in swap.fp

Or you if prefer getting an int from postgres, cast the result of extract()
to an integer in the SQL query itself.

Best regards,
-- 
Daniel
PostgreSQL-powered mail user agent and storage: http://www.manitou-mail.org



pgsql-interfaces by date:

Previous
From: thilo@riessner.de
Date:
Subject: libpq binary data
Next
From: "frank ernest"
Date:
Subject: How to get int from select