Re: libpq binary data

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: thilo(at)riessner(dot)de
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: libpq binary data
Date: 2014-10-01 19:45:50
Message-ID: 68c2c90f-2407-4cb0-a234-d38cb68e90d2@mm
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

thilo(at)riessner(dot)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

In response to

Browse pgsql-interfaces by date

  From Date Subject
Next Message frank ernest 2014-10-01 20:14:00 How to get int from select
Previous Message thilo 2014-10-01 16:26:08 libpq binary data