Database.createCollation

Creates and registers a collation function in the database.

struct Database
void
createCollation
(
string name
T
)
(
T fun
)

Parameters

fun T

An alias to the D implementation of the function. The function fun must satisfy these criteria:

  • 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.
name

The name that the function will have in the database; this name defaults to the identifier of fun.

Examples

static int my_collation(string s1, string s2)
{
	import std.uni;
	return icmp(s1, s2);
}

auto db = Database(":memory:");
db.createCollation!"my_coll"(&my_collation);
db.execute("CREATE TABLE test (word TEXT)");

auto statement = db.prepare("INSERT INTO test (word) VALUES (?)");
foreach (word; ["straße", "strasses"])
{
	statement.bind(1, word);
	statement.execute();
	statement.reset();
}

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

See Also

Meta