diff options
Diffstat (limited to '')
| -rw-r--r-- | README.md | 47 |
1 files changed, 42 insertions, 5 deletions
| @@ -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 | ||
| 513 | In 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 | |||
| 513 | You can define a scalar function using `db.createScalarFunction`: | 521 | You can define a scalar function using `db.createScalarFunction`: |
| 514 | ```zig | 522 | ```zig |
| 515 | try db.createScalarFunction( | 523 | try db.createScalarFunction( |
| @@ -528,8 +536,37 @@ const hash = try db.one([std.crypto.hash.Blake3.digest_length]u8, "SELECT blake3 | |||
| 528 | ``` | 536 | ``` |
| 529 | 537 | ||
| 530 | Each input arguments in the function call in the statement is passed on to the registered `run` function. | 538 | 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: | 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 | 542 | You can define a scalar function using `db.createAggregateFunction`: |
| 535 | * REAL values can be any Zig float | 543 | ```zig |
| 544 | const MyContext = struct { | ||
| 545 | sum: u32, | ||
| 546 | }; | ||
| 547 | var my_ctx = MyContext{ .sum = 0 }; | ||
| 548 | |||
| 549 | try 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 | |||
| 565 | const result = try db.one(usize, "SELECT mySum(nb) FROM foobar", .{}, .{}); | ||
| 566 | ``` | ||
| 567 | |||
| 568 | Each input arguments in the function call in the statement is passed on to the registered `step` function. | ||
| 569 | The `finalize` function is called once at the end. | ||
| 570 | |||
| 571 | The context (2nd argument of `createAggregateFunction`) can be whatever you want; both `step` and `finalize` function must | ||
| 572 | have their first argument of the same type as the context. | ||