Function Database.createFunction
Creates and registers a new function in the database.
void createFunction(T)
(
string name,
T fun,
Deterministic det = Deterministic .yes
)
if (isFunctionPointer!T || isDelegate!T);
void createFunction(T)
(
string name,
T fun = null
)
if (is(T == typeof(null)));
If a function with the same name and the same arguments already exists, it is replaced by the new one.
The memory associated with the function will be released when the database connection is closed.
Parameters
Name | Description |
---|---|
name | The name that the function will have in the database. |
fun | a delegate or function that implements the function. fun
must satisfy the following criteria:
null function pointer to delete the function from the database connection.
|
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 otherwise. |
See Also
Example
string star(int count, string starSymbol = "*")
{
import std .range : repeat;
import std .array : join;
return starSymbol .repeat(count) .join;
}
auto db = Database(":memory:");
db .createFunction("star", &star);
assert(db .execute("SELECT star(5)") .oneValue!string == "*****");
assert(db .execute("SELECT star(3, '♥')") .oneValue!string == "♥♥♥");
Example
// The implementation of the new function
string myList(ColumnData[] args)
{
import std .array : appender;
import std .string : format, join;
auto app = appender!(string[]);
foreach (arg; args)
{
if (arg .type == SqliteType .TEXT)
app .put(`"%s"` .format(arg));
else
app .put("%s" .format(arg));
}
return app .data .join(", ");
}
auto db = Database(":memory:");
db .createFunction("my_list", &myList);
auto list = db .execute("SELECT my_list(42, 3.14, 'text', NULL)") .oneValue!string;
assert(list == `42, 3.14, "text", null`);