Function Database.createCollation

Creates and registers a collation function in the database.

void createCollation(T) (
  string name,
  T fun
)
if (isFunctionPointer!T || isDelegate!T);

Parameters

NameDescription
name The name that the function will have in the database.
fun a delegate or function that implements the collation. The function fun must be nothrow` and satisfy these criteria:
  • Takes two string arguments (s1 and s2). These two strings are slices of C-style strings that SQLite manages internally, so there is no guarantee that they are still valid when the function returns.
  • Returns an integer (ret).
  • If s1 is less than s2, ret < 0.
  • If s1 is equal to s2, ret == 0.
  • If s1 is greater than s2, ret > 0.
  • If s1 is equal to s2, then s2 is equal to s1.
  • If s1 is equal to s2 and s2 is equal to s3, then s1 is equal to s3.
  • If s1 is less than s2, then s2 is greater than s1.
  • If s1 is less than s2 and s2 is less than s3, then s1 is less than s3.

See Also

http://www.sqlite.org/lang_aggfunc.html

Example

// The implementation of the collation
int my_collation(string s1, string s2) nothrow
{
    import std.uni : icmp;
    import std.exception : assumeWontThrow;

    return assumeWontThrow(icmp(s1, s2));
}

auto db = Database(":memory:");
db.createCollation("my_coll", &my_collation);
db.run("CREATE TABLE test (word TEXT);
        INSERT INTO test (word) VALUES ('straße');
        INSERT INTO test (word) VALUES ('strasses');");

auto word = db.execute("SELECT word FROM test ORDER BY word COLLATE my_coll")
              .oneValue!string;
assert(word == "straße");