我正在尝试构建与以下 SQL 匹配的 Opaleye 查询:
select * ,
(select array_agg(tags.tagname)
from articles_tags
inner join tags on tags.id = articles_tags.tag_fk
where articles_tags.article_fk = articles.id
)
from articles
涉及的表格(简化)是:
articles: (id, title, content)
articles_tags: (article_fk, tag_fk)
tags: (id, tagname)
我的目标是查询附加了一个或多个标签的文章,并将所有附加标签作为数组检索。
到目前为止,我得到了以下基本查询:
-- | Query all article-tag relations.
allTaggedArticlesQ :: OE.Select TaggedArticleR
allTaggedArticlesQ = OE.selectTable taggedArticlesTable
-- | Query article-tag relations for the given articles.
taggedArticlesQ :: OE.SelectArr PA.ArticleIdField TaggedArticleR
taggedArticlesQ = proc articleId -> do
ta <- allTaggedArticlesQ -< ()
OE.restrict -< articleFk ta .=== articleId
returnA -< ta
-- | Join article-ids and tag names for the given subset of articles.
articleTagNamesQ :: OE.SelectArr PA.ArticleIdField ArticleTagR
articleTagNamesQ = proc articleIds -> do
ta <- taggedArticlesQ -< articleIds
tags <- PT.allTagsQ -< ()
OE.restrict -< PT.tagKey tags .=== tagFk ta
returnA -< ArticleTag (articleFk ta) (PT.tagName tags)
但是,我无法使聚合工作:以下内容不进行类型检查,并且我不明白如何使用上述查询组合此聚合:
-- | Aggregate all tag names for all given articles
articleTagsQ :: PA.ArticleIdField -> OE.Select (PA.ArticleIdField, F (OE.SqlArray OE.SqlText))
articleTagsQ = OE.aggregate
( pArticleTag
ArticleTag
{ atArticleFk = OE.groupBy,
atTagname = OE.arrayAgg
}
) OE.selectTable articleTagNamesQ
在一些博客文章和 GitHub 问题中,我发现聚合不适用于 Product-Profunctors 和 Arrows,因此不能包含在箭头查询中。然而,我对 Haskell 比较陌生,并没有真正理解这两个库背后的理论(似乎没有适合初学者的文档);因此,我无法提出如何将查询与聚合相结合的一般结构。William Yao here有一些示例,但我不了解一般概念,因此无法将这些示例应用于我的问题。
如果有人可以提供有关如何在 Opaleye 中使用常规查询组合聚合的见解,我将不胜感激,谢谢!