packages feed

hasql-th-0.5.1.3 / src / library / Hasql / TH / Construction / Exp.hs

-- |-- Expression construction.module Hasql.TH.Construction.Exp whereimport qualified Data.Text as Textimport qualified Data.Vector.Generic as Vectorimport qualified Hasql.Decoders as Decodersimport qualified Hasql.Encoders as Encodersimport qualified Hasql.Statement as Statementimport Hasql.TH.Prelude hiding (sequence_)import qualified Hasql.TH.Prelude as Preludeimport Language.Haskell.TH.Syntaximport qualified TemplateHaskell.Compat.V0208 as Compat-- * HelpersappList :: Exp -> [Exp] -> ExpappList = foldl' AppEtext :: Text -> Exptext x = AppE (VarE 'Text.pack) (LitE (StringL (Text.unpack x)))integral :: (Integral a) => a -> Expintegral x = LitE (IntegerL (fromIntegral x))list :: (a -> Exp) -> [a] -> Explist renderer x = ListE (map renderer x)string :: String -> Expstring x = LitE (StringL x)char :: Char -> Expchar x = LitE (CharL x)sequence_ :: [Exp] -> Expsequence_ = foldl' andThen pureUnitpureUnit :: ExppureUnit = AppE (VarE 'Prelude.pure) (TupE [])andThen :: Exp -> Exp -> ExpandThen exp1 exp2 = AppE (AppE (VarE '(*>)) exp1) exp2tuple :: Int -> Exptuple = ConE . tupleDataNamesplitTupleAt :: Int -> Int -> ExpsplitTupleAt arity position =  let nameByIndex index = Name (OccName ('_' : show index)) NameS      names = enumFromTo 0 (pred arity) & map nameByIndex      pats = names & map VarP      pat = TupP pats      exps = names & map VarE      body = splitAt position exps & \(a, b) -> Compat.tupE [Compat.tupE a, Compat.tupE b]   in LamE [pat] body-- |-- Given a list of divisible functor expressions,-- constructs an expression, which composes them together into-- a single divisible functor, parameterized by a tuple of according arity.contrazip :: [Exp] -> Expcontrazip = \case  hd : [] -> hd  hd : tl -> appList (VarE 'divide) [splitTupleAt (succ (length tl)) 1, hd, contrazip tl]  [] ->    SigE      (VarE 'conquer)      ( let fName = mkName "f"         in ForallT              [Compat.specifiedPlainTV fName]              [AppT (ConT ''Divisible) (VarT fName)]              (AppT (VarT fName) (TupleT 0))      )-- |-- Given a list of applicative functor expressions,-- constructs an expression, which composes them together into-- a single applicative functor, parameterized by a tuple of according arity.---- >>> $(return (cozip [])) :: Maybe ()-- Just ()---- >>> $(return (cozip (fmap (AppE (ConE 'Just) . LitE . IntegerL) [1,2,3]))) :: Maybe (Int, Int, Int)-- Just (1,2,3)cozip :: [Exp] -> Expcozip = \case  hd : [] -> hd  hd : tl ->    let len = length tl + 1     in foldl'          (\a b -> AppE (AppE (VarE '(<*>)) a) b)          (AppE (AppE (VarE 'fmap) (tuple len)) hd)          tl  [] -> AppE (VarE 'pure) (TupE [])-- |-- Lambda expression, which destructures 'Fold'.foldLam :: (Exp -> Exp -> Exp -> Exp) -> ExpfoldLam body =  let stepVarName = mkName "progress"      initVarName = mkName "start"      extractVarName = mkName "finish"   in LamE        [ Compat.conP            'Fold            [ VarP stepVarName,              VarP initVarName,              VarP extractVarName            ]        ]        (body (VarE stepVarName) (VarE initVarName) (VarE extractVarName))-- * Statementstatement :: Exp -> Exp -> Exp -> Expstatement sql encoder decoder =  appList (VarE 'Statement.preparable) [sql, encoder, decoder]noResultResultDecoder :: ExpnoResultResultDecoder = VarE 'Decoders.noResultrowsAffectedResultDecoder :: ExprowsAffectedResultDecoder = VarE 'Decoders.rowsAffectedsingleRowResultDecoder :: Exp -> ExpsingleRowResultDecoder = 'Decoders.singleRow & VarE & AppErowMaybeResultDecoder :: Exp -> ExprowMaybeResultDecoder = AppE (VarE 'Decoders.rowMaybe)rowVectorResultDecoder :: Exp -> ExprowVectorResultDecoder = AppE (VarE 'boxedRowVector)boxedRowVector :: Decoders.Row row -> Decoders.Result (Vector row)boxedRowVector = Decoders.rowVectorfoldStatement :: Exp -> Exp -> Exp -> ExpfoldStatement sql encoder rowDecoder' =  foldLam (\step init extract -> statement sql encoder (foldResultDecoder step init extract rowDecoder'))foldResultDecoder :: Exp -> Exp -> Exp -> Exp -> ExpfoldResultDecoder step init extract rowDecoder' =  appList (VarE 'fmap) [extract, appList (VarE 'Decoders.foldlRows) [step, init, rowDecoder']]unidimensionalParamEncoder :: Bool -> Exp -> ExpunidimensionalParamEncoder nullable =  applyParamToEncoder . applyNullabilityToEncoder nullablemultidimensionalParamEncoder :: Bool -> Int -> Bool -> Exp -> ExpmultidimensionalParamEncoder nullable dimensionality arrayNull =  applyParamToEncoder    . applyNullabilityToEncoder arrayNull    . AppE (VarE 'Encoders.array)    . applyArrayDimensionalityToEncoder dimensionality    . applyNullabilityToEncoder nullableapplyParamToEncoder :: Exp -> ExpapplyParamToEncoder = AppE (VarE 'Encoders.param)applyNullabilityToEncoder :: Bool -> Exp -> ExpapplyNullabilityToEncoder nullable = AppE (VarE (if nullable then 'Encoders.nullable else 'Encoders.nonNullable))applyArrayDimensionalityToEncoder :: Int -> Exp -> ExpapplyArrayDimensionalityToEncoder levels =  if levels > 0    then AppE (AppE (VarE 'Encoders.dimension) (VarE 'Vector.foldl')) . applyArrayDimensionalityToEncoder (pred levels)    else AppE (VarE 'Encoders.element)rowDecoder :: [Exp] -> ExprowDecoder = cozipunidimensionalColumnDecoder :: Bool -> Exp -> ExpunidimensionalColumnDecoder nullable =  applyColumnToDecoder . applyNullabilityToDecoder nullablemultidimensionalColumnDecoder :: Bool -> Int -> Bool -> Exp -> ExpmultidimensionalColumnDecoder nullable dimensionality arrayNull =  applyColumnToDecoder    . applyNullabilityToDecoder arrayNull    . AppE (VarE 'Decoders.array)    . applyArrayDimensionalityToDecoder dimensionality    . applyNullabilityToDecoder nullableapplyColumnToDecoder :: Exp -> ExpapplyColumnToDecoder = AppE (VarE 'Decoders.column)applyNullabilityToDecoder :: Bool -> Exp -> ExpapplyNullabilityToDecoder nullable = AppE (VarE (if nullable then 'Decoders.nullable else 'Decoders.nonNullable))applyArrayDimensionalityToDecoder :: Int -> Exp -> ExpapplyArrayDimensionalityToDecoder levels =  if levels > 0    then AppE (AppE (VarE 'Decoders.dimension) (VarE 'Vector.replicateM)) . applyArrayDimensionalityToDecoder (pred levels)    else AppE (VarE 'Decoders.element)