summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'README.md')
-rw-r--r--README.md30
1 files changed, 30 insertions, 0 deletions
diff --git a/README.md b/README.md
index 235c995..ae5c03c 100644
--- a/README.md
+++ b/README.md
@@ -503,3 +503,33 @@ const rows = try stmt.all(usize, .{}, .{
503}); 503});
504_ = rows; 504_ = rows;
505``` 505```
506
507## User defined functions
508
509sqlite supports [user-defined functions](https://www.sqlite.org/c3ref/create_function.html) which come in two types:
510* scalar functions
511* aggregate functions
512
513You can define a scalar function using `db.createScalarFunction`:
514```zig
515try db.createScalarFunction(
516 "blake3",
517 struct {
518 fn run(input: []const u8) [std.crypto.hash.Blake3.digest_length]u8 {
519 var hash: [std.crypto.hash.Blake3.digest_length]u8 = undefined;
520 std.crypto.hash.Blake3.hash(input, &hash, .{});
521 return hash;
522 }
523 }.run,
524 .{},
525);
526
527const hash = try db.one([std.crypto.hash.Blake3.digest_length]u8, "SELECT blake3('hello')", .{}, .{});
528```
529
530Each input arguments in the function call in the statement is passed on to the registered `run` function.
531Arguments are [sqlite3\_values](https://www.sqlite.org/c3ref/value_blob.html) and are converted to Zig values using the following rules:
532* TEXT values can be either `sqlite.Text` or `[]const u8`
533* BLOB values can be either `sqlite.Blob` or `[]const u8`
534* INTEGER values can be any Zig integer
535* REAL values can be any Zig float