{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 800
{-# LANGUAGE TemplateHaskellQuotes #-}
#else
{-# LANGUAGE TemplateHaskell #-}
#endif
module Database.PostgreSQL.Simple.TypeInfo.Macro
( mkCompats
, inlineTypoid
) where
import Database.PostgreSQL.Simple.TypeInfo.Static
import Database.PostgreSQL.Simple.Types (Oid(..))
import Language.Haskell.TH
mkCompats :: [TypeInfo] -> ExpQ
mkCompats :: [TypeInfo] -> ExpQ
mkCompats [TypeInfo]
tys = do
Name
x <- forall (m :: * -> *). Quote m => String -> m Name
newName String
"x"
forall (m :: * -> *). Quote m => [m Pat] -> m Exp -> m Exp
lamE [forall (m :: * -> *). Quote m => Name -> [m Pat] -> m Pat
conP 'Oid [forall (m :: * -> *). Quote m => Name -> m Pat
varP Name
x]] forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). Quote m => m Exp -> [m Match] -> m Exp
caseE (forall (m :: * -> *). Quote m => Name -> m Exp
varE Name
x) (forall a b. (a -> b) -> [a] -> [b]
map TypeInfo -> Q Match
alt [TypeInfo]
tys forall a. [a] -> [a] -> [a]
++ [Q Match
catchAll])
where
alt :: TypeInfo -> MatchQ
alt :: TypeInfo -> Q Match
alt TypeInfo
ty = forall (m :: * -> *).
Quote m =>
m Pat -> m Body -> [m Dec] -> m Match
match (TypeInfo -> Q Pat
inlineTypoidP TypeInfo
ty) (forall (m :: * -> *). Quote m => m Exp -> m Body
normalB [| True |]) []
catchAll :: MatchQ
catchAll :: Q Match
catchAll = forall (m :: * -> *).
Quote m =>
m Pat -> m Body -> [m Dec] -> m Match
match forall (m :: * -> *). Quote m => m Pat
wildP (forall (m :: * -> *). Quote m => m Exp -> m Body
normalB [| False |]) []
inlineTypoid :: TypeInfo -> ExpQ
inlineTypoid :: TypeInfo -> ExpQ
inlineTypoid TypeInfo
ty = forall (m :: * -> *). Quote m => Name -> m Exp
conE 'Oid forall (m :: * -> *). Quote m => m Exp -> m Exp -> m Exp
`appE` forall (m :: * -> *). Quote m => Lit -> m Exp
litE (TypeInfo -> Lit
getTypoid TypeInfo
ty)
inlineTypoidP :: TypeInfo -> PatQ
inlineTypoidP :: TypeInfo -> Q Pat
inlineTypoidP TypeInfo
ty = forall (m :: * -> *). Quote m => Lit -> m Pat
litP (TypeInfo -> Lit
getTypoid TypeInfo
ty)
getTypoid :: TypeInfo -> Lit
getTypoid :: TypeInfo -> Lit
getTypoid TypeInfo
ty = let (Oid CUInt
x) = TypeInfo -> Oid
typoid TypeInfo
ty in Integer -> Lit
integerL (forall a b. (Integral a, Num b) => a -> b
fromIntegral CUInt
x)