Database.createAggregate

Creates and registers a new aggregate function in the database.

struct Database
private public
void
createAggregate
(
T
)

Parameters

name string

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

agg T

The struct of type T implementing the aggregate. T must 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. These methods cannot be variadic.

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;

// The implementation of the aggregate function
struct Joiner
{
    private
    {
        Appender!(string[]) stringList;
        string separator;
    }

    this(string separator)
    {
        this.separator = separator;
    }

    void accumulate(string word)
    {
        stringList.put(word);
    }

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

auto db = Database(":memory:");
db.run("CREATE TABLE test (word TEXT);
        INSERT INTO test VALUES ('My');
        INSERT INTO test VALUES ('cat');
        INSERT INTO test VALUES ('is');
        INSERT INTO test VALUES ('black');");

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

See Also

Meta