agora inbox for pgsql-interfaces@postgresql.org  
help / color / mirror / Atom feed
From: Daniel Verite <daniel@manitou-mail.org>
To: thilo@riessner.de
Cc: pgsql-interfaces@postgresql.org
Subject: Re: libpq binary data
Date: Wed, 01 Oct 2014 21:45:50 +0200
Message-ID: <68c2c90f-2407-4cb0-a234-d38cb68e90d2@mm> (raw)
In-Reply-To: <3808965.tNLrlL6qTR@thilo.site>
List-Unsubscribe:  <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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


-- 
Sent via pgsql-interfaces mailing list (pgsql-interfaces@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-interfaces



view thread (3+ messages)  latest in thread

Message-ID: <68c2c90f-2407-4cb0-a234-d38cb68e90d2@mm>
Permalink:  ../68c2c90f-2407-4cb0-a234-d38cb68e90d2@mm/
Also on:    postgresql.org/message-id/68c2c90f-2407-4cb0-a234-d38cb68e90d2@mm

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: daniel@manitou-mail.org, thilo@riessner.de
  Subject: Re: libpq binary data
  In-Reply-To: <68c2c90f-2407-4cb0-a234-d38cb68e90d2@mm>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox