简体   繁体   English

Hasql Session的返回值变量被特定类型卡住了吗?

[英]Hasql Session's return value variable getting stuck with specific type?

I am attempting to connect to Postgres once, outside of my Spock application, but I can't seem to use a Hasql session handle with multiple return types. 我试图在我的Spock应用程序之外一次连接到Postgres,但是我似乎无法使用具有多个返回类型的Hasql会话句柄。

My main application is fairly straightforward, but fails to compile. 我的主要应用程序相当简单,但是无法编译。

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

If I comment out printAHasqlString or printAccountCount , it compiles and works (both work when running without the other). 如果我注释掉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 does a query returning an Int, and printAHasqlString runs a query for a Text. printAccountCount进行返回Int的查询,而printAHasqlString对Text进行查询。 Both just print the result, and returns an IO () . 两者都只打印结果,并返回IO ()

But when I attempt to run both queries in the same application, the r type variable in the Session data type gets locked down, and fails to compile with the second one. 但是,当我尝试在同一应用程序中运行两个查询时, Session数据类型中的r类型变量被锁定,并且无法与第二个一起编译。

Error Message: 错误信息:

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

Updated Error 更新错误

After some help below - I got to a new error: 经过下面的一些帮助-我遇到了一个新错误:

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

How do I let that type variable remain flexible between calls? 如何让类型变量在两次调用之间保持灵活性?

Full (updated) Code: https://gist.github.com/cschneid/4174addefb254a517f35 完整的(更新的)代码: https : //gist.github.com/cschneid/4174addefb254a517f35

Since version 0.6.0 there no longer is any sessionUnlifter fuss. 0.6.0版本开始 ,不再存在任何sessionUnlifter麻烦。 Pool is directly exposed and you can execute Session on it as many times as you want without any performance penalty. 池是直接公开的,您可以根据需要在其上执行Session多次,而不会影响性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM