diff options
| author | 2022-04-03 02:52:16 +0200 | |
|---|---|---|
| committer | 2022-04-03 03:15:38 +0200 | |
| commit | fb306fd4acd46ec50eb2516fae4de5ec96db4487 (patch) | |
| tree | 706cf5843308712ab0cca39d743795312bbb6016 | |
| parent | add createScalarFunction to create a user-defined scalar function (diff) | |
| download | zig-sqlite-fb306fd4acd46ec50eb2516fae4de5ec96db4487.tar.gz zig-sqlite-fb306fd4acd46ec50eb2516fae4de5ec96db4487.tar.xz zig-sqlite-fb306fd4acd46ec50eb2516fae4de5ec96db4487.zip | |
add documentation for user defined functions
| -rw-r--r-- | README.md | 30 |
1 files changed, 30 insertions, 0 deletions
| @@ -503,3 +503,33 @@ const rows = try stmt.all(usize, .{}, .{ | |||
| 503 | }); | 503 | }); |
| 504 | _ = rows; | 504 | _ = rows; |
| 505 | ``` | 505 | ``` |
| 506 | |||
| 507 | ## User defined functions | ||
| 508 | |||
| 509 | sqlite supports [user-defined functions](https://www.sqlite.org/c3ref/create_function.html) which come in two types: | ||
| 510 | * scalar functions | ||
| 511 | * aggregate functions | ||
| 512 | |||
| 513 | You can define a scalar function using `db.createScalarFunction`: | ||
| 514 | ```zig | ||
| 515 | try 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 | |||
| 527 | const hash = try db.one([std.crypto.hash.Blake3.digest_length]u8, "SELECT blake3('hello')", .{}, .{}); | ||
| 528 | ``` | ||
| 529 | |||
| 530 | Each input arguments in the function call in the statement is passed on to the registered `run` function. | ||
| 531 | Arguments 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 | ||