haskell - 使用 esqueleto 计算行数

标签 haskell yesod persistent esqueleto

我正在尝试使用 Esqueleto(版本 2.1.2.1)计算内部联接的行数。不幸的是,我的代码无法编译,我不明白为什么。我查看了以下如何执行此操作的示例,但无法弄清楚我做错了什么:example1 , example2 .

我的架构如下所示(简化):

User
Game
  state
Player
  user UserId Maybe
  game GameId

用户可以在网站上注册来玩游戏。无需注册也可以玩。因此,有一个单独的表Player。游戏有状态。它可以是正在进行,也可以是某种形式的游戏结束。我现在想要统计用户正在玩的所有正在进行的游戏。

以下 SQL 查询可以很好地实现这一点(对于固定的 userId 为 1):

SELECT COUNT (*)
FROM Player INNER JOIN Game
ON Player.game = Game.id
WHERE Player.user = 1 AND game.state = "Ongoing"

但是,以下 Esqueleto 查询无法编译:

[count1] <- runDB $ E.select                                    -- Line 25
                  $ E.from $ \(player `E.InnerJoin` game) -> do
                      E.on $ player^.PlayerGame E.==. game^.GameId
                      E.where_ $
                          player^.PlayerUser E.==. E.just (E.val userId) E.&&.
                          game^.GameState E.==. E.val MyGame.Ongoing
                      return (game, player)
                      return E.countRows                        -- Line 32

错误消息如下所示:

Handler/ListUserGames.hs:25:23:
    No instance for (E.SqlSelect (expr0 (E.Value a0)) r0)
      arising from a use of ‘E.select’
    The type variables ‘r0’, ‘expr0’, ‘a0’ are ambiguous
    Note: there are several potential instances:
      instance (E.SqlSelect a ra, E.SqlSelect b rb) =>
               E.SqlSelect (a, b) (ra, rb)
        -- Defined in ‘Database.Esqueleto.Internal.Sql’
      instance (E.SqlSelect a ra, E.SqlSelect b rb, E.SqlSelect c rc) =>
               E.SqlSelect (a, b, c) (ra, rb, rc)
        -- Defined in ‘Database.Esqueleto.Internal.Sql’
      instance (E.SqlSelect a ra, E.SqlSelect b rb, E.SqlSelect c rc,
                E.SqlSelect d rd) =>
               E.SqlSelect (a, b, c, d) (ra, rb, rc, rd)
        -- Defined in ‘Database.Esqueleto.Internal.Sql’
      ...plus 13 others
    In the expression: E.select
    In the second argument of ‘($)’, namely
      ‘E.select
       $ E.from
         $ \ (player `E.InnerJoin` game)
             -> do { E.on $ player ^. PlayerGame E.==. game ^. GameId;
                     E.where_
                     $ player ^. PlayerUser E.==. E.just (E.val userId)
                       E.&&. game ^. GameState E.==. E.val MyGame.Ongoing;
                     .... }’
    In a stmt of a 'do' block:
      [count1] <- runDB
                  $ E.select
                    $ E.from
                      $ \ (player `E.InnerJoin` game)
                          -> do { E.on $ player ^. PlayerGame E.==. game ^. GameId;
                                  E.where_
                                  $ player ^. PlayerUser E.==. E.just (E.val userId)
                                    E.&&. game ^. GameState E.==. E.val MyGame.Ongoing;
                                  .... }

Handler/ListUserGames.hs:32:32:
    No instance for (E.Esqueleto query0 expr0 backend0)
      arising from a use of ‘E.countRows’
    The type variables ‘query0’, ‘expr0’, ‘backend0’ are ambiguous
    Note: there is a potential instance available:
      instance E.Esqueleto E.SqlQuery E.SqlExpr SqlBackend
        -- Defined in ‘Database.Esqueleto.Internal.Sql’
    In the first argument of ‘return’, namely ‘E.countRows’
    In a stmt of a 'do' block: return E.countRows
    In the expression:
      do { E.on $ player ^. PlayerGame E.==. game ^. GameId;
           E.where_
           $ player ^. PlayerUser E.==. E.just (E.val userId)
             E.&&. game ^. GameState E.==. E.val MyGame.Ongoing;
           return (game, player);
           return E.countRows }

但是,如果我删除 countRows,则完全相同的查询会起作用。 IE。下面的代码编译并执行我想要它执行的操作。

ongoing <- runDB $ E.select
                 $ E.from $ \(player `E.InnerJoin` game) -> do
                     E.on $ player^.PlayerGame E.==. game^.GameId
                     E.where_ $
                       player^.PlayerUser E.==. E.just (E.val userId) E.&&.
                       game^.GameState E.==. E.val MyGame.Ongoing
                     E.orderBy [E.desc $ game^.GameLastActionTime]
                     E.limit pagelen
                     E.offset $ max 0 $ (page1 - 1) * pagelen
                     return (game, player)

我做错了什么?

最佳答案

事实证明上面的Esqueleto代码是正确的。该错误出现在代码的另一部分,缺乏限制导致类型模糊。

关于haskell - 使用 esqueleto 计算行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28273922/

相关文章:

f# - F# 列表是否持久?

list - 在 Haskell 元组列表上压缩相同的值

mysql - Esqueleto 位于实体 ID 上

haskell - 在 Haskell 中构建(惰性)映射的有效方法

haskell - 为持久实体创建 ToJSON 实例

haskell - 如何使用多片

mysql - 使用 MySQL 和 Yesod 的持久语法错误

php - Redis PHP session 不会持续存在

haskell - 如何在haskell中编写恒等函数

xml - 如何使用 Haskell 查询 RDF 数据?