0

I need extend Like restriction to support accent insensitive, but I don't know where tinkering. Anyone know how I do this? I can't change collate in all customer databases and I'm using criteria and can't change this because I create a infrastructure that wrap criteria in my domain.

Diego Dias
  • 904
  • 3
  • 15
  • 23

2 Answers2

0

Approach 1

You can do this with an SQL expression, i.e.

Expression.Sql(
      "your_field COLLATE Latin1_general_CI_AI LIKE ? COLLATE Latin1_general_CI_AI", 
      String.Format("%{0}%", your_field),
      NHibernateUtil.String)

Please see my answer to a similar question for further details. Also please see the answer to this question for details on the correct SQL format.

This, of course, is limited to a specific DBMS, in the example case SQL Server.

Approach 2

Another approach is to sub-class the NHibernate dialect for each DBMS the system will be used with, and add a custom function to each of these custom dialects. For example,

public class CustomMsSqlDialect : MsSql2005Dialect
{
  public CustomMsSqlDialect()
  {
      RegisterFunction("accentinsensitivelike",
        new SQLFunctionTemplate(NHibernateUtil.String,
            "?1 COLLATE Latin1_general_CI_AI like ?2 collate Latin1_general_CI_AI"));
  }
}

Then to call it use something like

var user = session.CreateQuery("from User u 
                                where accentinsensitivelike(u.name, :name)")
                  .SetParameter("name", name)
                  .UniqueResult<User>();

This has the advantage of working across multiple DBMS but is quite a bit more work than the first approach.

For further details please the following

http://blog.schuager.com/2009/06/case-sensitive-queries-in-nhibernate.html

Can I customize collation of query results in nHibernate?

Hope this helps.

Community
  • 1
  • 1
mickfold
  • 2,003
  • 1
  • 14
  • 20
0

I resolved my problem creating a custom criterion where I check dialect to identify what's the RDBMS that NHibernate are using, and then I create custom expression to it.

My code is here: source code

Diego Dias
  • 904
  • 3
  • 15
  • 23