2011-04-02 72 views
2

我有以下查询:使用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,在test/prod中使用PostgreSQL?

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

在此先感谢您的帮助!

回答

1

(1)如何在array_agg 函数周围创建一个NHibernate 条件?我需要将 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在我 测试和PostgreSQL?

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

+0

我同意:随着您在生产中使用,始终使用相同的环境来测试(和开发) – 2011-04-15 06:59:34