【发布时间】:2020-07-02 07:20:30
【问题描述】:
我正在编写一个玩具示例来学习使用 Persistent 库访问 Haskell 数据库。为了玩,我想看看数据库中有什么(内存中的 SQLite):
import qualified Database.Persist.Sql as PSQL
import qualified Data.Conduit.List as CL
import Data.Conduit ( ($$) )
import Control.Monad.IO.Class (liftIO)
dumpTable :: Text -> IO ()
dumpTable tableName = PSQL.rawQuery "select * from " <> tableName [] $$ CL.mapM_ (liftIO . print)
(取自 Haskell 学院)
因为我想为我的应用程序使用 RIO 库,所以上述方法不起作用:我需要使用 RIO 日志记录函数之一而不是打印,并且该函数必须在 RIO monad 中运行。这是我的尝试:
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
[..]
import RIO
import qualified Database.Persist.Sql as PSQL
import Data.Conduit ( ($$) )
import qualified Data.Conduit.List as CL
dumpTable :: (HasLogFunc env) => Text -> RIO env ()
dumpTable tableName =
let query = "select * from " <> tableName
in PSQL.rawQuery query [] $$ CL.mapM_ (logInfo . displayShow)
但是,此代码不进行类型检查。我收到以下错误:
• Could not deduce (PSQL.BackendCompatible PSQL.SqlBackend env)
arising from a use of ‘PSQL.rawQuery’
from the context: HasLogFunc env
bound by the type signature for:
dumpTable :: forall env. HasLogFunc env => Text -> RIO env ()
at src/Persistence/DbInspect.hs:13:1-51
• In the first argument of ‘($$)’, namely ‘PSQL.rawQuery query []’
In the expression:
PSQL.rawQuery query [] $$ CL.mapM_ (logInfo . displayShow)
In the expression:
let query = "select * from " <> tableName
in PSQL.rawQuery query [] $$ CL.mapM_ (logInfo . displayShow)
|
16 | in PSQL.rawQuery query [] $$ CL.mapM_ (logInfo . displayShow)
| ^^^^^^^^^^^^^^^^^^^^^^
我不明白这个错误是什么意思。如果有人能给我一些关于如何继续分析这个错误的提示,那就太好了,从而提高我对所涉及的类型类和 monad 的理解。
【问题讨论】:
-
我也有同样的问题。让我们看看是否有人可以帮助我们解决这个问题
-
我发现有一个包 hackage.haskell.org/package/rio-orphans-0.1.1.0/docs/… 包含 RIO 的 monadlogger 实例。这应该可以解决问题
标签: haskell persistent conduit rio