Thread: Question about how to handle numeric (decimal) data types while using libpq

Hello,

I am trying to use libpq for interfacing with PostgreSQL from a C/C++ based application.

I have tried to use binary format of data for both sending and receiving data to and from server (resultFormat = 1).

As I understand most binary types, int, float etc… are encoded in bing-endian byte order and they can be converted to
appropriatehost specific memory representations. 

However NUMERIC datatype is a more complex structure with multiple ints and pointers.

I would like to know if there are published library functions that convert (to and from) NUMERIC field to say, long
doubledatatype 


Any pointers in this regard is appreciated.

Regards,
Sudheer





Re: Question about how to handle numeric (decimal) data types while using libpq

From
Dmitry Igrishin
Date:


On Tue, Jul 6, 2021, 14:04 Sudheer H R <sudheer.hr@tekenlight.com> wrote:
Hello,

I am trying to use libpq for interfacing with PostgreSQL from a C/C++ based application.

I have tried to use binary format of data for both sending and receiving data to and from server (resultFormat = 1).

As I understand most binary types, int, float etc… are encoded in bing-endian byte order and they can be converted to appropriate host specific memory representations.

However NUMERIC datatype is a more complex structure with multiple ints and pointers.

I would like to know if there are published library functions that convert (to and from) NUMERIC field to say, long double datatype


Any pointers in this regard is appreciated.
Why not just use text format for NUMERIC?
It is not possible to use binary for some fields and text for some fields.

If text format has to be used, it will be applicable for all fields.

And this method involved converting to string format (sprint) on the server side and binary format from string (equivalent of sscanf), which is costly.



On 06-Jul-2021, at 5:18 PM, Dmitry Igrishin <dmitigr@gmail.com> wrote:



On Tue, Jul 6, 2021, 14:04 Sudheer H R <sudheer.hr@tekenlight.com> wrote:
Hello,

I am trying to use libpq for interfacing with PostgreSQL from a C/C++ based application.

I have tried to use binary format of data for both sending and receiving data to and from server (resultFormat = 1).

As I understand most binary types, int, float etc… are encoded in bing-endian byte order and they can be converted to appropriate host specific memory representations.

However NUMERIC datatype is a more complex structure with multiple ints and pointers.

I would like to know if there are published library functions that convert (to and from) NUMERIC field to say, long double datatype


Any pointers in this regard is appreciated.
Why not just use text format for NUMERIC?

Re: Question about how to handle numeric (decimal) data types while using libpq

From
Peter Eisentraut
Date:
On 06.07.21 13:04, Sudheer H R wrote:
> I am trying to use libpq for interfacing with PostgreSQL from a C/C++ based application.
> 
> I have tried to use binary format of data for both sending and receiving data to and from server (resultFormat = 1).
> 
> As I understand most binary types, int, float etc… are encoded in bing-endian byte order and they can be converted to
appropriatehost specific memory representations.
 
> 
> However NUMERIC datatype is a more complex structure with multiple ints and pointers.
> 
> I would like to know if there are published library functions that convert (to and from) NUMERIC field to say, long
doubledatatype
 

There is a third-party library called libpqtypes out there that adds 
support for handling specific types on the libpq side.  But I'm not sure 
how up to date it is.