2

我有以下架构:

share [ mkPersist sqlSettings, mkMigrate "migrateAll" ] [persistLowerCase|
AdminChan
  timestamp T.Text
  name      T.Text
  msg       T.Text
BanHost
  timestamp T.Text
  host      T.Text
  isBanned  Bool
  reason    T.Text
|]

现在说我想admin_chan[AdminChan]. 我怎样才能做到这一点?我有这个功能:

dumpDbTbl :: SqlPersistT IO [AdminChan]
dumpDbTbl = map entityVal <$> (select . from $ return)

1)但是我如何[AdminChan]SqlPersistT IO堆栈中取出?(请注意,我没有使用yesod.)

2)另外,是否可以重写上述函数,使其成为多态并且也可以与BanHost表一起使用?

3) 我如何编写一个函数,使用esqueleto,将返回表中的记录数?

谢谢!

4

1 回答 1

1
  1. 您使用类似runSqlPersistMor的函数在monadrunSqlPersistMPool中评估您的查询。IO例如:

    dumpDbTbl :: (MonadIO m) => SqlPersistT m [AdminChan]
    dumpDbTbl = map entityVal <$> (select . from $ return)
    
    main :: IO ()
    main = runStderrLoggingT $ withSqlitePool ":memory:" 4 $ \pool -> liftIO $ do
        flip runSqlPersistMPool pool $ do
            runMigration migrateAll
    
            admins <- dumpDbTbl
    
            liftIO $ print (admins :: [AdminChan])
    
  2. 是的,你可以,但你必须包括一些额外的限制:

    dumpDbTbl :: (PersistEntity b, MonadIO m,
                  PersistEntityBackend b ~ SqlBackend) => SqlPersistT m [b]
    dumpDbTbl = map entityVal <$> (select . from $ return)
    
  3. 您使用 esqueleto 功能countRows

    countHostBans :: (MonadIO m) => SqlPersistT m [(T.Text, Int)]
    countHostBans =
        map (\(Value v, Value c) -> (v, c)) <$> (select $ from $ \banHost -> do
            let c = countRows
            groupBy (banHost ^. BanHostHost)
            return (banHost ^. BanHostHost, c))
    
于 2015-08-28T22:48:01.580 回答