[odb-users] PostgreSQL NUMERIC(dec,prec)

Boris Kolpackov boris at codesynthesis.com
Mon Aug 4 03:48:02 EDT 2014


Hi Sean,

Sean Clarke <sean.clarke at sec-consulting.co.uk> writes:

> I am kind of surprised there is not a built in conversion from
> NUMERIC(x,y) to double.

In the general case NUMERIC(x,y) doesn't map to double without loss.
Also, implementing this conversion from the PG's binary representation
of NUMERIC to double will probably be non-trivial, to say the least.


> I have played with the the "#pragma db map type("regex") as("subst")
> [to("subst")] [from("subst")]" substitution, but can't seem to get it to
> work.

The pgsql/custom test in the odb-tests package has a couple of examples,
including for NUMERIC. Something like this should work:

// Map NUMERIC PostgreSQL type to DOUBLE PRECISION. This will allow us
// to extract it to C++ double.
//
#pragma db map type("NUMERIC *(\\(.+\\))?")  \
               as("DOUBLE PRECISION")        \
               to("(?)::NUMERIC$1")          \
               from("(?)::DOUBLE PRECISION") 

Then in your persistent classes:

#pragma db type("NUMERIC (6, 4)")
double member;

Boris



More information about the odb-users mailing list