Database.createAggregate

Creates and registers a new aggregate function in the database.

struct Database
void
createAggregate
(
T
string name
)

Parameters

T

The struct type implementing the aggregate. T must be default-construtible (a POD type) and implement at least these two methods: accumulate and result. Each parameter and the returned type of accumulate and result must be a boolean or numeric type, a string, an array, null, or a Nullable!T where T is any of the previous types.

name

The name that the aggregate function will have in the database.

det Deterministic

Tells SQLite whether the result of the function is deterministic, i.e. if the result is the same when called with the same parameters. Recent versions of SQLite perform optimizations based on this. Set to Deterministic.no otherwise.

Examples

import std.array : appender, join;

static struct Joiner
{
	Appender!(string[]) app;
	string separator;

	void accumulate(string word, string sep)
	{
		separator = sep;
		app.put(word);
	}

	string result()
	{
		return join(app.data, separator);
	}
}

auto db = Database(":memory:");
db.execute("CREATE TABLE test (word TEXT)");
db.createAggregate!(Joiner, "strjoin");

auto statement = db.prepare("INSERT INTO test VALUES (?)");
auto list = ["My", "cat", "is", "black"];
foreach (word; list)
{
	statement.bind(1, word);
	statement.execute();
	statement.reset();
}

auto text = db.execute("SELECT strjoin(word, '-') FROM test").oneValue!string;
assert(text == "My-cat-is-black");

See Also

Meta