How to apply a custom collation to specific columns of an SQLite database?

我与影子孤独终老i 提交于 2019-12-12 01:55:26

问题


In a C# application that uses iBATIS.NET to map data to an SQLite database I want to be able to sort specific columns by an alphabet that slightly differs from the English alphabet (there are some special characters and the order of some characters is different). I want to get from the database the results that are already sorted, because sorting in the application itself is impossible due to pagination.

After some research I concluded that applying a custom collation may be the only option here. Is this conclusion correct?

However, I couldn't find out how this could be done in the described situation. iBATIS API seemingly has nothing to do with collations and I don't see an obvious way to achieve this tinkering with the database configuration. So, how could this be done?


回答1:


User-defined functions and collations are a feature of the ADO.NET driver, not the ORM that sits on top of it. The SQlite ADO.Net driver does allow you to define custom functions, check the second answer to this question :

/// <summary>
/// User-defined collating sequence using the current UI culture.
/// </summary>
[SQLiteFunction(Name = "MYSEQUENCE", FuncType = FunctionType.Collation)]
class MySequence : SQLiteFunction
{
  public override int Compare(string param1, string param2)
  {
    return String.Compare(param1, param2, true);
  }
}

You need to register the function in your code before using it with

SQLiteFunction.RegisterFunction(typeof(MySequence));

Performance may be an issue as you'll pay the price of an Interop call each time a comparison is made



来源:https://stackoverflow.com/questions/27420991/how-to-apply-a-custom-collation-to-specific-columns-of-an-sqlite-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!