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

Sean Clarke sean.clarke at sec-consulting.co.uk
Tue Aug 5 15:05:38 EDT 2014


Thanks Boris, that works a treat!!

I was probably there with the pattern matching, but I didn't have the
numeric pragma.

Regards
Sean Clarke
---------------------------------------------
SEC Consulting Limited
Phone: +44 (0)23 8040 5599
Website: http://www.sec-consulting.co.uk
On 4 Aug 2014 08:53, "Boris Kolpackov" <boris at codesynthesis.com> wrote:

> 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