Database.createCollation

Creates and registers a collation function in the database.

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

Parameters

name
Type: string

The name that the function will have in the database.

fun
Type: T

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.

Examples

1 // The implementation of the collation
2 int my_collation(string s1, string s2) nothrow
3 {
4     import std.uni : icmp;
5     import std.exception : assumeWontThrow;
6 
7     return assumeWontThrow(icmp(s1, s2));
8 }
9 
10 auto db = Database(":memory:");
11 db.createCollation("my_coll", &my_collation);
12 db.run("CREATE TABLE test (word TEXT);
13         INSERT INTO test (word) VALUES ('straße');
14         INSERT INTO test (word) VALUES ('strasses');");
15 
16 auto word = db.execute("SELECT word FROM test ORDER BY word COLLATE my_coll")
17               .oneValue!string;
18 assert(word == "straße");

See Also

Meta