lpsmith / postgresql-simple

Mid-level client library for accessing PostgreSQL from Haskell
Other
206 stars 71 forks source link

Support for PostgreSQL's Money type #205

Open ivan-m opened 7 years ago

ivan-m commented 7 years ago

None of the instances of FromField seem to support the money type; is this deliberate or an oversight?

It seems as if it should be able to convert to/from Double.

reiddraper commented 7 years ago

I might imagine something from Data.Fixed or Data.Scientific might be more appropriate. I'd avoid using Double for currency calculations.

lpsmith commented 7 years ago

I second Reid. Also, I prefer that FromField instances provided with postgresql-simple reflect the type(s) they support with as full fidelity as possible. IIRC, the money type is also tagged with a currency code. Adding support for Data.Fixed and/or Data.Scientific might be acceptable, but really you probably need a type specific to handling money.

lpsmith commented 7 years ago

Also keep in mind there is no type checking or anything on ToField instances in postgresql-simple.

ivan-m commented 7 years ago

OK, so I'm trying to use Fixed, but I can't seem to work out how to specify the proper precision on the Postgre side, even for NUMERIC(precision, scale) (specifically for the scale component to ensure it maps to the correct HasResolution instance).

lpsmith commented 7 years ago

Yeah, I looked at that issue once and run into trouble myself. I don't recall the specifics, but I recall I wasn't overly pleased with postgres's handling of the numeric type.

Superpat commented 6 years ago

Opaleye has a similar discussion going on https://github.com/tomjaguarpaw/haskell-opaleye/issues/230

I personally use Numeric(15,6) to represent money, from what I read Data.Decimal seems like a good choice for a haskell datatype but maybe Fixed could work as well.