带有纯文本模板!!''#{expr}'' 允许 Text、String、Int32 或 Int64 表达式作为Text.Shakespeare.Text.ToText的实例
{-# LANGUAGE OverloadedStrings, ConstraintKinds #-}
module Handler.Plain where
import Import
import qualified Data.List as List
import Database.Persist.Sqlite
import qualified Data.Text as Text
import Control.Monad.Logger (MonadLogger)
import Control.Monad.Trans.Resource (MonadResourceBase)
import Text.Shakespeare.Text -- for the plain text template
stmt :: Text
stmt = "SELECT ??, ?? FROM File, Conf ON Conf.file = File.id ORDER BY File.path, Conf.key"
getQryResult :: (PersistQuery SqlPersist m, MonadLogger m, MonadResourceBase m) => () -> SqlPersist m [(Text, Text, Text)]
getQryResult () = do
result <- rawSql stmt []
return $ List.map getMyData (result :: [(Entity File, Entity Conf)])
where
getMyData (Entity _ file, Entity _ conf) = (filePath file, confKey conf, confVal conf)
getPlainR :: Handler RepPlain
getPlainR = do
result <- runDB $ getQryResult ()
return $ RepPlain $ toContent $ Text.unlines $ List.map formatMyData result
where
-- formatMyData (a, b, c) = Text.intercalate ", " [a,b,c]
-- with a plain text template:
formatMyData (a, b, c) = [st|
#{a}, #{b}, #{c} |]