将 Postgres 聚合函数与 NHibernate 结合使用

发布于 2024-10-28 20:12:18 字数 1891 浏览 2 评论 0原文

我有以下查询:

SELECT title_id, title, array_agg(g.name)
FROM title t
INNER JOIN title_genre tg USING(title_id)
INNER JOIN genre g USING (genre_id)
GROUP BY title_id, title
ORDER BY title_id
LIMIT 10

此查询的示例输出:

5527;"The Burbs";"{Suspense,"Dark Humor & Black Comedies",Comedy,"Cult Comedies"}"
5528;"20,000 Leagues Under the Sea";"{"Family Adventures","Children & Family","Ages 5-7","Book Characters","Family Animation"}"
5529;"2001: A Space Odyssey";"{"Classic Sci-Fi & Fantasy","Sci-Fi Thrillers",Classics}"
5530;"2010: The Year We Make Contact";"{"Sci-Fi Dramas","Alien Sci-Fi","Sci-Fi & Fantasy","Dramas Based on Contemporary Literature","Psychological Thrillers","Dramas Based on the Book"}"
5531;"The 39 Steps";"{"Dramas Based on the Book","United Kingdom",Thrillers,"Espionage Thrillers","Dramas Based on Classic Literature",Suspense}"
5532;"4D Man";"{"Classic Sci-Fi & Fantasy","Sci-Fi & Fantasy","Sci-Fi Horror"}"
5533;"8 Seconds";"{Drama,"Romantic Dramas",Biographies,"Indie Dramas","Sports Dramas","Miscellaneous Sports","Sports Stories","Other Sports"}"
5534;"9 1/2 Weeks";"{"Steamy Romance",Romance,"Romantic Dramas"}"
5535;"About Last Night...";"{"Romantic Dramas","Romantic Comedies",Romance}"
5536;"Above the Law";"{"Action & Adventure","Action Thrillers","Martial Arts"}"

(1) 如何围绕 array_agg 函数创建 NHibernate 标准?我是否需要以任何方式扩展 PostgreSQL 方言来适应这种情况?

(2) 我使用 SQLite 作为我的集成测试数据库,使用 PostgreSQL 作为我的测试/产品数据库。 SQLite 没有 array_agg 函数,但有一个 group_concat 函数可以执行类似的操作。是否可以设置一些东西,让我能够在测试中使用 SQLite,并在测试/产品中使用 PostgreSQL?

(3) array_agg 以数组形式返回数据。我在 nhibernate.info 上发现了一篇很棒的文章解释如何扩展 NHibernate 来处理 PostgreSQL 数组。我如何将其纳入我的标准?例如,假设我想找到一个属于戏剧类型但不是浪漫剧的标题。

预先感谢您的任何帮助!

I have the following query:

SELECT title_id, title, array_agg(g.name)
FROM title t
INNER JOIN title_genre tg USING(title_id)
INNER JOIN genre g USING (genre_id)
GROUP BY title_id, title
ORDER BY title_id
LIMIT 10

Sample output from this query:

5527;"The Burbs";"{Suspense,"Dark Humor & Black Comedies",Comedy,"Cult Comedies"}"
5528;"20,000 Leagues Under the Sea";"{"Family Adventures","Children & Family","Ages 5-7","Book Characters","Family Animation"}"
5529;"2001: A Space Odyssey";"{"Classic Sci-Fi & Fantasy","Sci-Fi Thrillers",Classics}"
5530;"2010: The Year We Make Contact";"{"Sci-Fi Dramas","Alien Sci-Fi","Sci-Fi & Fantasy","Dramas Based on Contemporary Literature","Psychological Thrillers","Dramas Based on the Book"}"
5531;"The 39 Steps";"{"Dramas Based on the Book","United Kingdom",Thrillers,"Espionage Thrillers","Dramas Based on Classic Literature",Suspense}"
5532;"4D Man";"{"Classic Sci-Fi & Fantasy","Sci-Fi & Fantasy","Sci-Fi Horror"}"
5533;"8 Seconds";"{Drama,"Romantic Dramas",Biographies,"Indie Dramas","Sports Dramas","Miscellaneous Sports","Sports Stories","Other Sports"}"
5534;"9 1/2 Weeks";"{"Steamy Romance",Romance,"Romantic Dramas"}"
5535;"About Last Night...";"{"Romantic Dramas","Romantic Comedies",Romance}"
5536;"Above the Law";"{"Action & Adventure","Action Thrillers","Martial Arts"}"

(1) How do I create a NHibernate criteria around the array_agg function? Will I need to extend the PostgreSQL dialect in any way to accommodate this?

(2) I'm using SQLite as my integration test database and PostgreSQL as my test/prod database. SQLite does not have the array_agg function, but does have a group_concat function that does something similar. Is it possible to set something up where I'll be able to use SQLite in my tests and PostgreSQL in test/prod?

(3) array_agg returns data as an array. I found a great article on nhibernate.info that explains how to extend NHibernate to handle PostgreSQL arrays. How do I include this in my criteria? For example, let's say I want to find a title that is in the Drama genre that is not a romantic drama.

Thanks in advance for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉目亦如画i 2024-11-04 20:12:18

(1) 如何创建 NHibernate
array_agg 周围的标准
功能?我需要延长期限吗
PostgreSQL 方言以任何方式
适应这个?

我认为你不应该。假设您想按流派选择所有标题,您只需要一个 WHERE 子句将流派解析为其 id 号。出于一个原因,varchar 列上的子选择可以使用索引。出于另一个原因,我很确定通过这样做,你的问题 #3 就会消失。

SELECT title_id, title, array_agg(g.genre)
FROM title t
INNER JOIN title_genre tg USING(title_id)
INNER JOIN genre g USING (genre_id)
WHERE tg.title_id in (SELECT title_id 
                      FROM title_genre 
                      INNER JOIN genre ON genre.genre_id = title_genre.genre_id 
                                      AND genre.genre = 'Suspense'
                      )
GROUP BY title_id, title
ORDER BY title_id
LIMIT 10

也可以在同一子查询上使用内部联接来编写它。

SELECT t.title_id, t.title, array_agg(g.genre)
FROM title t
INNER JOIN title_genre tg USING(title_id)
INNER JOIN genre g USING (genre_id)
INNER JOIN (SELECT title_id 
            FROM title_genre 
            INNER JOIN genre ON genre.genre_id = title_genre.genre_id 
                            AND genre.genre = 'Suspense'
            ) gn
            ON gn.title_id = tg.title_id
GROUP BY t.title_id, t.title
ORDER BY t.title_id
LIMIT 10

(2) 是否可以设置一些东西
我将能够在我的应用程序中使用 SQLite
test/prod 中的测试和 PostgreSQL?

在开发中使用与生产中使用的相同平台是可能的,也是明智的。安装 PostgreSQL 并使用它代替 SQLite。

(1) How do I create a NHibernate
criteria around the array_agg
function? Will I need to extend the
PostgreSQL dialect in any way to
accommodate this?

I don't think you should. Assuming you want to select all the titles by genre, you just need a WHERE clause that resolves the genre to its id number. For one reason, a subselect on a varchar column can use an index. For another reason, I'm pretty sure that by doing it this way, your question #3 just goes away.

SELECT title_id, title, array_agg(g.genre)
FROM title t
INNER JOIN title_genre tg USING(title_id)
INNER JOIN genre g USING (genre_id)
WHERE tg.title_id in (SELECT title_id 
                      FROM title_genre 
                      INNER JOIN genre ON genre.genre_id = title_genre.genre_id 
                                      AND genre.genre = 'Suspense'
                      )
GROUP BY title_id, title
ORDER BY title_id
LIMIT 10

It could also be written using an inner join on that same subquery.

SELECT t.title_id, t.title, array_agg(g.genre)
FROM title t
INNER JOIN title_genre tg USING(title_id)
INNER JOIN genre g USING (genre_id)
INNER JOIN (SELECT title_id 
            FROM title_genre 
            INNER JOIN genre ON genre.genre_id = title_genre.genre_id 
                            AND genre.genre = 'Suspense'
            ) gn
            ON gn.title_id = tg.title_id
GROUP BY t.title_id, t.title
ORDER BY t.title_id
LIMIT 10

(2) Is it possible to set something up
where I'll be able to use SQLite in my
tests and PostgreSQL in test/prod?

It's possible--and advisable--to use the same platform in development that you use in production. Install PostgreSQL and use it instead of SQLite.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文