簡體   English   中英

用esqueleto計數行

[英]Counting rows with esqueleto

我正在嘗試計算Esqueleto(版本2.1.2.1)的內部聯接的行。 不幸的是,我的代碼無法編譯,我也不明白為什么。 我查看了以下示例,了解如何執行此操作,但無法弄清自己做錯了什么: example1example2

我的架構如下所示(簡化):

User
Game
  state
Player
  user UserId Maybe
  game GameId

用戶可以在該網站上注冊玩游戲。 您也可以不注冊而玩。 因此,有一個單獨的表Player 游戲具有狀態。 它可以是Ongoing中或某種形式的游戲結束。 我現在要計算用戶正在玩的所有正在進行的游戲。

下面的SQL查詢可以很好地做到這一點(對於固定的用戶標識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 ,則完全相同的查詢有效。 即下面的代碼可以編譯並執行我想要的操作。

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代碼是正確的。 錯誤發生在代碼的另一部分,其中缺少限制導致類型歧義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM