Database.createFunction

Creates and registers a new function in the database.

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.

struct Database
void
createFunction
(
string name
T
)

Parameters

name

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

fun T

a delegate or function that implements the function. fun must satisfy these criteria:

  • It must not be a variadic.
  • Its arguments must all have a type that is compatible with SQLite types: it must be a boolean or numeric type, a string, an array, null, or a Nullable!T where T is any of the previous types.
  • Its return value must also be of a compatible type.
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

string fmt = "Hello, %s!";
string my_msg(string name)
{
	return fmt.format(name);
}
auto db = Database(":memory:");
db.createFunction!"msg"(&my_msg);
auto msg = db.execute("SELECT msg('John')").oneValue!string;
assert(msg == "Hello, John!");

See Also

Meta