Hasql Session的返回值变量是否陷入特定类型?

时间:2014-12-23 22:50:03

标签: postgresql haskell hasql

我试图在我的Spock应用程序之外连接到Postgres一次,但我似乎无法使用具有多种返回类型的Hasql会话句柄。

我的主要应用程序非常简单,但无法编译。

mainApp :: IO Middleware
mainApp = do
  session pgConfig sessConfig $ do
    dbHandle <- sessionUnlifter
    liftIO $ do
      printAHasqlString dbHandle
      printAccountCount dbHandle
      spockT id (appMiddleware >> appRoutes dbHandle)

如果我注释掉printAHasqlStringprintAccountCount,它会编译并运行(两者都在没有其他运行时运行)。

printAccountCount :: (MonadBase IO m) => (Session Settings s m Int -> IO Int) -> IO ()
printAHasqlString :: (MonadBase IO m) => (Session Settings s m Text -> IO Text) -> IO ()

printAccountCount执行返回Int的查询,printAHasqlString运行查询文本。两者都只打印结果,并返回IO ()

但是当我尝试在同一个应用程序中运行两个查询时,r数据类型中的Session类型变量被锁定,并且无法使用第二个进行编译。

错误消息:

src/Main.hs:30:25:
  Couldn't match type ‘Text’ with ‘Int’
  Expected type: Session Settings s IO Int -> IO Int
    Actual type: Session Settings s IO Text -> IO Text
  In the first argument of ‘printAccountCount’, namely ‘dbHandle’
  In a stmt of a 'do' block: printAccountCount dbHandle

更新错误

在下面的一些帮助之后 - 我遇到了一个新的错误:

src/Main.hs:29:24:
    Couldn't match type ‘r0’ with ‘a’
      because type variable ‘a’ would escape its scope
    This (rigid, skolem) type variable is bound by
      a type expected by the context: Session Settings s IO a -> IO a
      at src/Main.hs:29:7-31
    Expected type: Session Settings s IO a -> IO a
      Actual type: Session Settings s IO r0 -> IO r0
    Relevant bindings include
      dbHandle :: Session Settings s IO r0 -> IO r0
        (bound at src/Main.hs:27:5)
    In the first argument of ‘printAHsqlString’, namely ‘dbHandle’
    In a stmt of a 'do' block: printAHsqlString dbHandle

如何让这个类型变量在调用之间保持灵活性?

完整(已更新)代码:https://gist.github.com/cschneid/4174addefb254a517f35

1 个答案:

答案 0 :(得分:2)

version 0.6.0以来,不再有任何sessionUnlifter大惊小怪。池直接暴露,您可以根据需要多次执行Session,而不会造成任何性能损失。