Extending NHibernate to support aggregate functions
Is it possible to extend NHibernate to add support for aggregate functions that is unique to a database system?开发者_StackOverflow社区 I have a handful of queries that use the array_agg() function (and others) in PostgreSQL that I'd like to convert to HQL/ICriteria.
Thanks!
Yes. It's possible.
Take a look: http://ayende.com/Blog/archive/2006/10/01/UsingSQLFunctionsInNHibernate.aspx
You could do something like this:
public class MyDialect : PostgreSQLDialect
{
public MyDialect()
{
RegisterFunction("dbo.myfunction", new
StandardSQLFunction(NHibernateUtil.String));
}
}
Then you can use myfunction
in your HQL statements. You just need to register the Dialect in your NH Configuration.
Edit:
Good news, this should work for Criteria queries as well.
This is code (from Reflector) in PostgreSQLDialect
base.RegisterFunction("iif",
new SQLFunctionTemplate(null, "case when ?1 then ?2 else ?3 end"));
So in you ICriteria, you could do:
Projections.SqlFunction("iif", NHibernateUtil.Boolean, foo, bar, baz...
All this should work because you're extending a NH Dialect.
HTH
精彩评论