如何在Scala中组合数据库查询?

时间:2015-02-04 07:07:35

标签: mysql sql scala slick

假设我定义了类型Fetch[A],它封装了一个查询来从数据库中获取类型A的数据。 (它可能会为MySQL创建一个SQL查询,但这是实现细节。

假设我还有一些函数可以创建Fetch[A]来获取博客数据:

type BlogPosts = Seq[BlogPost] 

def recentPosts(blogId   : BlogId): Fetch[BlogPosts] = ...
def featuredPosts(blogId : BlogId): Fetch[BlogPosts] = ...
def popularPosts(blogId  : BlogId): Fetch[BlogPosts] = ... 

现在我需要一起获取所有recentfeaturedpopular个帖子。我想从上面的三个查询中创建单个查询。

def threePosts(recent   : Fetch[BlogPosts],
               featured : Fetch[BlogPosts],
               popular  : Fetch[BlogPosts]): Fetch[(BlogPosts, BlogPosts, BlogPosts)]  

有意义吗?它可行吗?

P.S。很抱歉,我不知道SQL因此我无法提供原始recentfeaturedpopular查询以及生成的查询的具体示例。

P.P.S Slick不是这样做的吗?

0 个答案:

没有答案