简体   繁体   English

Play 2.0 Complex join查询如何解析(Anorm)

[英]Play 2.0 Complex join query how to parse (Anorm )

I am writing website using play 2.0 framework. 我正在使用play 2.0框架编写网站。 And I have a problem when parsing results. 解析结果时遇到问题。 This request to mysql db gets all the links(can be several per episode) added to the database per episode together with all the information about episode and anime. 这个对mysql db的请求获得了每集添加到数据库的所有链接(可以是每集几个)以及有关剧集和动画的所有信息。

  def lastReleasedLink(limit:Long=5):List[(((Episode,Anime),Link),Genre)] = {
    DB.withConnection { implicit c =>
      SQL(
        """
          select * from yas_episodes as a
          inner join anime as b on a.ep_anime_id=b.id
          left outer join yas_links as c on a.ep_id=c.ep_id
          LEFT JOIN yas_animes_genres AS d ON a.ep_anime_id = d.ag_anime_id
          INNER JOIN yas_genres AS e ON e.g_id = d.ag_genre_id
          where c.ep_id IS NOT NULL group by c.ep_id order by c.date desc limit {limit}
        """).on('limit ->limit)as(Episode.simple~Anime.simple~Link.simple~Genre.simple map{
        case episode~anime~link~genre => episode -> anime -> link -> Genre
      } *)
    }
  }

The return value is type of List[(((Episode,Anime),Link),Genre)] but how can I form output to the list of let say List[episode,anime,Seq[links]] or List[episode,anime,Seq[Genres],Seq[links]] don't know where to put genres. 返回值是List[(((Episode,Anime),Link),Genre)]但是如何将输出形成列表,例如List[episode,anime,Seq[links]]List[episode,anime,Seq[Genres],Seq[links]]不知道在哪里放类型。

You can imagine that when you have two links per one episode information from table anime and yas_episodes will be copied for every row. 您可以想象,当每个剧集中有两个链接来自表格动画时,将会为每一行复制yas_episodes。 So I need somehow stack them together(group) by episode record. 所以我需要通过剧集记录以某种方式将它们叠加在一起(组)。 Then it will be possible to iterate list and access to all objects. 然后,可以迭代列表并访问所有对象。

As you can see, in the request there is many-to-many relation of anime with genres. 正如您所看到的,在请求中,动画与流派有多对多的关系。 I have no idea how can I put all together to one list to be able to access it in view. 我不知道如何将所有内容放在一个列表中以便能够在视图中访问它。 Should Genre be part of Anime model? 类型应该是动漫模型的一部分吗?

It seems that the preferred way to achieve this is using the Scala collection API, see this post by Guillaume Bort. 似乎实现这一点的首选方法是使用Scala集合API,请参阅Guillaume Bort的这篇文章

In your case, I think you could do something like 在你的情况下,我认为你可以做类似的事情

lastReleasedLink groupBy { case (((e, a), l), g) => (e, a) }

to group by (Episode, Anime). 分组(Episode,Anime)。 Generally speaking, those manipulations are probably a little easier if you change your function to return a List[(Episode, Anime, Link, Genre)] , ie 一般来说,如果你改变你的函数来返回List[(Episode, Anime, Link, Genre)] ,那些操作可能会更容易一点,即

case episode~anime~link~genre => (episode, anime, link, genre)

then you could achieve the same with this code: 然后你可以用这个代码实现相同的目标:

lastReleasedLink groupBy ((_._1, _._2))

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

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