简体   繁体   English

使用Esqueleto处理List类型

[英]Handling List-types with Esqueleto

I have data types defined as: 我将数据类型定义为:

data ComitteeView = CommitteeView { committeeId :: CommitteeId
                                  , committeeMembers :: [Person] 
                                  }

data CommitteesView = CommitteesView { committeeView :: [CommitteeView] }

Now, as it stands, I have a Persistent model defined as: 现在,就目前而言,我将Persistent模型定义为:

Person
  name  Text

Committee
  name  Text

CommitteePerson
  personId    PersonId
  committeeId CommitteeId

I can pretty easily create a query to populate a CommitteeView, using Esqueleto. 我可以很容易地使用Esqueleto创建一个填充委员会视图的查询。 It would go something like this: 它会是这样的:

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

Now, consider the problem of populating CommitteesView . 现在,考虑填充CommitteesView的问题。 In principle, we get enough data to populate by running subquery in the above query. 原则上,我们通过在上面的查询中运行子查询来获取足够的数据来填充。 Okay, fair enough. 好的,公平的。 Now how can I use "group by Haskell-list" like group by in SQL? 现在我如何在SQL中使用像group by Haskell-list这样的“group by Haskell-list”? How can I fold rows so that I can end up with a list of lists of people? 如何折叠行以便最终得到人员列表?

I get the impression that esqueleto can't handle the case as such (ie, it doesn't have a combinator that would do it). 我得到的结论是, esqueleto无法处理这种情况(即,它没有组合器会这样做)。 And my underlying database obviously doesn't support Haskell lists as a column. 而我的底层数据库显然不支持Haskell列表作为列。 But, surely, I can't be the only person to face this issue. 但是,当然,我不能成为唯一一个面对这个问题的人。 What is an effective strategy? 什么是有效的策略? Folding an n-list of lists into a n-list? 将n列表列表折叠到n列表中? Or running n+1 queries? 或者运行n+1查询? Are there any other options? 还有其他选择吗?

Esqueleto is NOT meant to handle list of sublists (multidimensional list) out of the box yet! Esqueleto 并不意味着处理子列表的列表(多维列表)开箱呢! Data.List.groupBy that 'cdk' advised to you can group only list itself, but not what you was asking for. Data.List.groupBy '建议你可以分组只列出自己,但不是你要求的。

For your case I would insistently advise you to use a classic SQL queries. 对于您的情况,我会坚持建议您使用经典的SQL查询。 You can run n+1 queries, but do that only if it is rare and not often usable function, that for example prepares cached data (based on your variables names I suppose that it may not be heavy used and it worth a try). 您可以运行n + 1个查询,但只有在它很少且通常不可用的函数时才这样做,例如准备缓存数据(基于您的变量名称,我认为它可能不会被大量使用,值得一试)。 For heavy usage you should consider using classic SQL without any doubt. 对于大量使用,您应该毫无疑问地考虑使用经典SQL。

If you will go to https://github.com/prowdsponsor/esqueleto you will find that: 如果您将访问https://github.com/prowdsponsor/esqueleto,您会发现:

Not all SQL features are available, but most of them can be easily added (especially functions). 并非所有SQL功能都可用,但大多数功能都可以轻松添加(尤其是功能)。

so you can try to ask for a new feature. 所以你可以试着要求一个新功能。 Good luck! 祝好运!

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

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