{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ConstraintKinds #-}
module Opaleye.Experimental.Enum
(
enumMapper,
EnumMapper,
enumFromField,
enumToFields,
fromFieldToFieldsEnum,
) where
import Opaleye.Column (Column)
import qualified Opaleye as O
import qualified Opaleye.Internal.Inferrable as I
import qualified Opaleye.Internal.RunQuery as RQ
import Data.ByteString.Char8 (unpack)
import qualified Data.Profunctor.Product.Default as D
data EnumMapper sqlEnum haskellSum = EnumMapper {
EnumMapper sqlEnum haskellSum -> FromField sqlEnum haskellSum
enumFromField :: RQ.FromField sqlEnum haskellSum
, EnumMapper sqlEnum haskellSum
-> ToFields haskellSum (Column sqlEnum)
enumToFields :: O.ToFields haskellSum (Column sqlEnum)
}
enumMapper :: String
-> (String -> Maybe haskellSum)
-> (haskellSum -> String)
-> EnumMapper sqlEnum haskellSum
enumMapper :: String
-> (String -> Maybe haskellSum)
-> (haskellSum -> String)
-> EnumMapper sqlEnum haskellSum
enumMapper String
type_ String -> Maybe haskellSum
from haskellSum -> String
to_ = EnumMapper :: forall sqlEnum haskellSum.
FromField sqlEnum haskellSum
-> ToFields haskellSum (Column sqlEnum)
-> EnumMapper sqlEnum haskellSum
EnumMapper {
enumFromField :: FromField sqlEnum haskellSum
enumFromField = FromField sqlEnum haskellSum
forall a. FromField a haskellSum
fromFieldEnum
, enumToFields :: ToFields haskellSum (Column sqlEnum)
enumToFields = ToFields haskellSum (Column sqlEnum)
forall b. ToFields haskellSum (Column b)
toFieldsEnum
}
where
toFieldsEnum :: ToFields haskellSum (Column b)
toFieldsEnum = (haskellSum -> Column b) -> ToFields haskellSum (Column b)
forall haskells fields.
(haskells -> fields) -> ToFields haskells fields
O.toToFields (String -> Column SqlText -> Column b
forall a b. String -> Column a -> Column b
O.unsafeCast String
type_ (Column SqlText -> Column b)
-> (haskellSum -> Column SqlText) -> haskellSum -> Column b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Column SqlText
String -> Field SqlText
O.sqlString (String -> Column SqlText)
-> (haskellSum -> String) -> haskellSum -> Column SqlText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. haskellSum -> String
to_)
fromFieldEnum :: FromField a haskellSum
fromFieldEnum = (((Field, Maybe ByteString) -> haskellSum)
-> FromField a (Field, Maybe ByteString) -> FromField a haskellSum)
-> FromField a (Field, Maybe ByteString)
-> ((Field, Maybe ByteString) -> haskellSum)
-> FromField a haskellSum
forall a b c. (a -> b -> c) -> b -> a -> c
flip ((Field, Maybe ByteString) -> haskellSum)
-> FromField a (Field, Maybe ByteString) -> FromField a haskellSum
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FromField a (Field, Maybe ByteString)
forall a. FromField a (Field, Maybe ByteString)
RQ.unsafeFromFieldRaw (((Field, Maybe ByteString) -> haskellSum)
-> FromField a haskellSum)
-> ((Field, Maybe ByteString) -> haskellSum)
-> FromField a haskellSum
forall a b. (a -> b) -> a -> b
$ \(Field
_, Maybe ByteString
mdata) -> case Maybe ByteString
mdata of
Maybe ByteString
Nothing -> String -> haskellSum
forall a. HasCallStack => String -> a
error String
"Unexpected NULL"
Just ByteString
s -> case String -> Maybe haskellSum
from (ByteString -> String
unpack ByteString
s) of
Just haskellSum
r -> haskellSum
r
Maybe haskellSum
Nothing -> String -> haskellSum
forall a. HasCallStack => String -> a
error (String
"Unexpected: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ByteString -> String
unpack ByteString
s)
fromFieldToFieldsEnum :: String
-> (String -> Maybe haskellSum)
-> (haskellSum -> String)
-> (RQ.FromField sqlEnum haskellSum,
O.ToFields haskellSum (Column sqlEnum))
fromFieldToFieldsEnum :: String
-> (String -> Maybe haskellSum)
-> (haskellSum -> String)
-> (FromField sqlEnum haskellSum,
ToFields haskellSum (Column sqlEnum))
fromFieldToFieldsEnum String
type_ String -> Maybe haskellSum
from haskellSum -> String
to_ = (EnumMapper sqlEnum haskellSum -> FromField sqlEnum haskellSum
forall sqlEnum haskellSum.
EnumMapper sqlEnum haskellSum -> FromField sqlEnum haskellSum
enumFromField EnumMapper sqlEnum haskellSum
forall sqlEnum. EnumMapper sqlEnum haskellSum
e, EnumMapper sqlEnum haskellSum
-> ToFields haskellSum (Column sqlEnum)
forall sqlEnum haskellSum.
EnumMapper sqlEnum haskellSum
-> ToFields haskellSum (Column sqlEnum)
enumToFields EnumMapper sqlEnum haskellSum
forall sqlEnum. EnumMapper sqlEnum haskellSum
e)
where e :: EnumMapper sqlEnum haskellSum
e = String
-> (String -> Maybe haskellSum)
-> (haskellSum -> String)
-> EnumMapper sqlEnum haskellSum
forall haskellSum sqlEnum.
String
-> (String -> Maybe haskellSum)
-> (haskellSum -> String)
-> EnumMapper sqlEnum haskellSum
enumMapper String
type_ String -> Maybe haskellSum
from haskellSum -> String
to_