我有如下定义的数据类型:
data ComitteeView = CommitteeView { committeeId :: CommitteeId
, committeeMembers :: [Person]
}
data CommitteesView = CommitteesView { committeeView :: [CommitteeView] }现在,我有一个持久化模型定义为:
Person
name Text
Committee
name Text
CommitteePerson
personId PersonId
committeeId CommitteeId使用Esqueleto,我可以很容易地创建一个查询来填充CommitteeView。它应该是这样的:
getCommitteeView cid =
CommitteeView <$> runDB $
select $
from (person `InnerJoin` pxc `InnerJoin` committee) -> do
on (committee ^. CommitteeId ==. pxc ^. CommitteePersonCommitteeId)
on (person ^. PersonId ==. pxc ^. CommitteePersonPersonId)
where_ (committee ^. CommitteePersonCommitteeId ==. val cid)
return person现在,考虑填充CommitteesView的问题。原则上,我们通过在上面的查询中运行子查询来获得足够的数据来填充。好的,很公平。现在如何像SQL中的group by一样使用"group by Haskell-list“?我如何折叠行,以得到一个人员列表?
我得到的印象是esqueleto不能处理这种情况(即,它没有一个可以做到这一点的组合器)。而且我的底层数据库显然不支持将Haskell列表作为列。但是,当然,我不可能是唯一面对这个问题的人。什么是有效的策略?将列表的n-list合并为n-list?或者运行n+1查询?还有没有别的选择?
发布于 2015-07-12 15:35:29
Esqueleto是而不是意味着要处理开箱即用的子列表(多维列表)!Data.List.groupBy 'cdk‘建议你只能分组列表本身,而不是你所要求的。
对于您的情况,我强烈建议您使用经典的SQL查询。您可以运行n+1查询,但仅当它是罕见且不经常使用的函数时才这样做,例如准备缓存数据(根据您的变量名称,我认为它可能不会大量使用,值得一试)。对于大量使用,毫无疑问,您应该考虑使用经典SQL。
如果你要去https://github.com/prowdsponsor/esqueleto,你会发现:
不是所有的SQL特性都可用,但它们中的大多数都可以很容易地添加(尤其是函数)。
因此,您可以尝试请求一个新功能。祝好运!
https://stackoverflow.com/questions/23751140
复制相似问题