将 Postgres 聚合函数与 NHibernate 结合使用
我有以下查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不应该。假设您想按流派选择所有标题,您只需要一个 WHERE 子句将流派解析为其 id 号。出于一个原因,varchar 列上的子选择可以使用索引。出于另一个原因,我很确定通过这样做,你的问题 #3 就会消失。
也可以在同一子查询上使用内部联接来编写它。
在开发中使用与生产中使用的相同平台是可能的,也是明智的。安装 PostgreSQL 并使用它代替 SQLite。
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.
It could also be written using an inner join on that same subquery.
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.