Database.createAggregate

Creates and registers a new aggregate function in the database.

struct Database
void
createAggregate
(
T
)

Parameters

name
Type: string

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

agg
Type: 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

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

1 import std.array : Appender, join;
2 
3 // The implementation of the aggregate function
4 struct Joiner
5 {
6     private
7     {
8         Appender!(string[]) stringList;
9         string separator;
10     }
11 
12     this(string separator)
13     {
14         this.separator = separator;
15     }
16 
17     void accumulate(string word)
18     {
19         stringList.put(word);
20     }
21 
22     string result()
23     {
24         return stringList.data.join(separator);
25     }
26 }
27 
28 auto db = Database(":memory:");
29 db.run("CREATE TABLE test (word TEXT);
30         INSERT INTO test VALUES ('My');
31         INSERT INTO test VALUES ('cat');
32         INSERT INTO test VALUES ('is');
33         INSERT INTO test VALUES ('black');");
34 
35 db.createAggregate("dash_join", Joiner("-"));
36 auto text = db.execute("SELECT dash_join(word) FROM test").oneValue!string;
37 assert(text == "My-cat-is-black");

See Also

Meta