module Hasql.QQ where
import Hasql.Prelude
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import qualified Hasql.QQ.Parser as Parser
import qualified Hasql.Backend as Backend
q :: QuasiQuoter
q =
QuasiQuoter
parseExp
(const $ fail "Pattern context is not supported")
(const $ fail "Type context is not supported")
(const $ fail "Declaration context is not supported")
parseExp :: String -> Q Exp
parseExp s =
do
n <- either (fail . showString "Parsing failure: ") return (Parser.parse (fromString s))
return $ statementF s n
statementF :: String -> Word -> Exp
statementF s n =
LamE pats exp
where
vars = map (mkName . ('_' :) . show) [1 .. n]
pats = map VarP vars
exp = AppE (AppE (ConE '(,)) (LitE (StringL s))) (ListE exps)
where
exps = map (AppE (VarE 'Backend.renderValue) . VarE) vars