lpsmith / postgresql-simple

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

Char's FromField instance should support bpchar #210

Closed ivan-m closed 7 years ago

ivan-m commented 7 years ago

I have a table with CHARACTER fields. I tried having a Char type in Haskell and use that, but I kept getting errors that the type was bpchar instead of char.

My current workaround is:

newtype PGChar = PGChar { unPGChar :: Char }
  deriving (Eq, Ord, Show, Read)

instance FromField PGChar where
  fromField f mdata =
    if typeOid f `notElem` [typoid char, typoid bpchar]
       then returnError Incompatible f "Must be a single character"
       else case unpack <$> mdata of
              Nothing  -> returnError UnexpectedNull f ""
              Just [c] -> return (PGChar c)
              Just r   -> returnError ConversionFailed f r
lpsmith commented 7 years ago

Thanks!