summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2022-04-17 01:45:07 +0200
committerGravatar Vincent Rischmann2022-04-17 01:45:07 +0200
commit9f5ba703c36fc937ea22becf590baf26a3bdbd8c (patch)
tree16655d59a767b6711b8d874a95523c6afdc4b647 /README.md
parentfactor the CreateFunctionFlag to c_int code (diff)
parentdocument the aggregate function in the readme (diff)
downloadzig-sqlite-9f5ba703c36fc937ea22becf590baf26a3bdbd8c.tar.gz
zig-sqlite-9f5ba703c36fc937ea22becf590baf26a3bdbd8c.tar.xz
zig-sqlite-9f5ba703c36fc937ea22becf590baf26a3bdbd8c.zip
Merge branch 'document-functions'
Diffstat (limited to 'README.md')
-rw-r--r--README.md47
1 files changed, 42 insertions, 5 deletions
diff --git a/README.md b/README.md
index ae5c03c..6ed06f5 100644
--- a/README.md
+++ b/README.md
@@ -510,6 +510,14 @@ sqlite supports [user-defined functions](https://www.sqlite.org/c3ref/create_fun
510* scalar functions 510* scalar functions
511* aggregate functions 511* aggregate functions
512 512
513In both cases the arguments are [sqlite3\_values](https://www.sqlite.org/c3ref/value_blob.html) and are converted to Zig values using the following rules:
514* TEXT values can be either `sqlite.Text` or `[]const u8`
515* BLOB values can be either `sqlite.Blob` or `[]const u8`
516* INTEGER values can be any Zig integer
517* REAL values can be any Zig float
518
519## Scalar functions
520
513You can define a scalar function using `db.createScalarFunction`: 521You can define a scalar function using `db.createScalarFunction`:
514```zig 522```zig
515try db.createScalarFunction( 523try db.createScalarFunction(
@@ -528,8 +536,37 @@ const hash = try db.one([std.crypto.hash.Blake3.digest_length]u8, "SELECT blake3
528``` 536```
529 537
530Each input arguments in the function call in the statement is passed on to the registered `run` function. 538Each 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: 539
532* TEXT values can be either `sqlite.Text` or `[]const u8` 540## Aggregate functions
533* BLOB values can be either `sqlite.Blob` or `[]const u8` 541
534* INTEGER values can be any Zig integer 542You can define a scalar function using `db.createAggregateFunction`:
535* REAL values can be any Zig float 543```zig
544const MyContext = struct {
545 sum: u32,
546};
547var my_ctx = MyContext{ .sum = 0 };
548
549try db.createAggregateFunction(
550 "mySum",
551 &my_ctx,
552 struct {
553 fn step(ctx: *MyContext, input: u32) void {
554 ctx.sum += input;
555 }
556 }.step,
557 struct {
558 fn finalize(ctx: *MyContext) u32 {
559 return ctx.sum;
560 }
561 }.finalize,
562 .{},
563);
564
565const result = try db.one(usize, "SELECT mySum(nb) FROM foobar", .{}, .{});
566```
567
568Each input arguments in the function call in the statement is passed on to the registered `step` function.
569The `finalize` function is called once at the end.
570
571The context (2nd argument of `createAggregateFunction`) can be whatever you want; both `step` and `finalize` function must
572have their first argument of the same type as the context.