[database-devel] Postgresql enum type conversions

Leon Smith leon.p.smith at gmail.com
Thu Jun 21 18:15:59 CEST 2012


The issue is that the Haskell's Text type only supports conversions from
PostgreSQL's text, char, varchar,  and a few other types.   So this is why
converting to Text first doesn't work.

What you really want to do is something like the code that follows.   I'll
admit that your code is shorter and cleaner;  perhaps there is a sensible
way to change the FromField interface to make it (or something like it)
work.   I'm certainly not satisfied with the FromField/FromRow interface
yet,  but I haven't really worked on ways to improve it further.   It'd be
nice to develop a corpus of use cases that we want to cover.

Best,
Leon


data X = X Float
       | Y Float

buildX :: B.ByteString -> OK (Float -> X)
buildX "x"      = pure X
buildX "y"      = pure Y
buildX val      = returnError ConversionFailed (ellipsis val)

ellipsis :: B.ByteString -> String
ellipsis x | B.length x > 25 = take 20 (B.unpack x) ++ "[...]"
           | otherwise       = B.unpack x

instance FromField (Float -> X) where
    fromField f mv | typename f == "x"  =  case mv of
                                             Nothing -> returnError
UnexpectedNull "some possibly more informative messgage"
                                             Just v  -> buildX v
                   | otherwise          =  returnError TypesIncompatible
"some possibly more informative message"

instance FromRow X where
    fromRow = field <*> (field :: RowParser Float)


On Sat, Jun 16, 2012 at 3:02 PM, Luke Hoersten <luke at hoersten.org> wrote:

> I'm trying to use postgresql enum types to represent Haskell types like in the code snippet below but an receiving the following error:
>
> "Incompatible {errSQLType = "x", errHaskellType = "Text", errMessage = "types incompatible"}"
>
> In other areas of my code (not shown) I'm successfully using Text to convert to and from Postgersql enum types but it doesn't seem to work in the case below. Any ideas what I'm doing wrong?
>
> Thanks, Luke
>
>
>
>
> import           Database.PostgreSQL.Simple.FromField        (FromField, fromField)import           Database.PostgreSQL.Simple.FromRow          (FromRow, field, fromRow)import           Database.PostgreSQL.Simple.SqlQQ            (sql)
>
> data X = X Float
>        | Y Float
>
> buildX :: Text -> Float -> XbuildX "x"      = XbuildX "y"      = Y
>
> instance FromField (Float -> X) where
>     fromField f v = buildX <$> fromField f v
>
> instance FromRow X where
>     fromRow = field <*> (field :: RowParser Float)
>
>
>
>
> _______________________________________________
> database-devel mailing list
> database-devel at haskell.org
> http://www.haskell.org/mailman/listinfo/database-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/database-devel/attachments/20120621/23124ab1/attachment.htm>


More information about the database-devel mailing list