diff options
| author | 2022-08-16 00:21:40 +0200 | |
|---|---|---|
| committer | 2022-09-11 17:12:37 +0200 | |
| commit | c72edf38dcdb0865b8086b853bafae07d4072b44 (patch) | |
| tree | 41181b1ca3f5858d885a427e37785240064ee5da /sqlite.zig | |
| parent | move setFunctionResult to helpers.setResult (diff) | |
| download | zig-sqlite-c72edf38dcdb0865b8086b853bafae07d4072b44.tar.gz zig-sqlite-c72edf38dcdb0865b8086b853bafae07d4072b44.tar.xz zig-sqlite-c72edf38dcdb0865b8086b853bafae07d4072b44.zip | |
move setFunctionArgument to helpers.setTypeFromValue
Will be used in the virtual table implementation
Diffstat (limited to 'sqlite.zig')
| -rw-r--r-- | sqlite.zig | 52 |
1 files changed, 2 insertions, 50 deletions
| @@ -570,54 +570,6 @@ pub const Db = struct { | |||
| 570 | return Savepoint.init(self, name); | 570 | return Savepoint.init(self, name); |
| 571 | } | 571 | } |
| 572 | 572 | ||
| 573 | // Helpers functions to implement SQLite functions. | ||
| 574 | |||
| 575 | fn sliceFromValue(sqlite_value: *c.sqlite3_value) []const u8 { | ||
| 576 | const size = @intCast(usize, c.sqlite3_value_bytes(sqlite_value)); | ||
| 577 | |||
| 578 | const value = c.sqlite3_value_text(sqlite_value); | ||
| 579 | debug.assert(value != null); // TODO(vincent): how do we handle this properly ? | ||
| 580 | |||
| 581 | return value.?[0..size]; | ||
| 582 | } | ||
| 583 | |||
| 584 | /// Sets a function argument using the provided value. | ||
| 585 | /// | ||
| 586 | /// Determines at compile time which sqlite3_value_XYZ function to use based on the type `ArgType`. | ||
| 587 | fn setFunctionArgument(comptime ArgType: type, arg: *ArgType, sqlite_value: *c.sqlite3_value) void { | ||
| 588 | switch (ArgType) { | ||
| 589 | Text => arg.*.data = sliceFromValue(sqlite_value), | ||
| 590 | Blob => arg.*.data = sliceFromValue(sqlite_value), | ||
| 591 | else => switch (@typeInfo(ArgType)) { | ||
| 592 | .Int => |info| if ((info.bits + if (info.signedness == .unsigned) 1 else 0) <= 32) { | ||
| 593 | const value = c.sqlite3_value_int(sqlite_value); | ||
| 594 | arg.* = @intCast(ArgType, value); | ||
| 595 | } else if ((info.bits + if (info.signedness == .unsigned) 1 else 0) <= 64) { | ||
| 596 | const value = c.sqlite3_value_int64(sqlite_value); | ||
| 597 | arg.* = @intCast(ArgType, value); | ||
| 598 | } else { | ||
| 599 | @compileError("integer " ++ @typeName(ArgType) ++ " is not representable in sqlite"); | ||
| 600 | }, | ||
| 601 | .Float => { | ||
| 602 | const value = c.sqlite3_value_double(sqlite_value); | ||
| 603 | arg.* = @floatCast(ArgType, value); | ||
| 604 | }, | ||
| 605 | .Bool => { | ||
| 606 | const value = c.sqlite3_value_int(sqlite_value); | ||
| 607 | arg.* = value > 0; | ||
| 608 | }, | ||
| 609 | .Pointer => |ptr| switch (ptr.size) { | ||
| 610 | .Slice => switch (ptr.child) { | ||
| 611 | u8 => arg.* = sliceFromValue(sqlite_value), | ||
| 612 | else => @compileError("cannot use an argument of type " ++ @typeName(ArgType)), | ||
| 613 | }, | ||
| 614 | else => @compileError("cannot use an argument of type " ++ @typeName(ArgType)), | ||
| 615 | }, | ||
| 616 | else => @compileError("cannot use an argument of type " ++ @typeName(ArgType)), | ||
| 617 | }, | ||
| 618 | } | ||
| 619 | } | ||
| 620 | |||
| 621 | /// CreateFunctionFlag controls the flags used when creating a custom SQL function. | 573 | /// CreateFunctionFlag controls the flags used when creating a custom SQL function. |
| 622 | /// See https://sqlite.org/c3ref/c_deterministic.html. | 574 | /// See https://sqlite.org/c3ref/c_deterministic.html. |
| 623 | /// | 575 | /// |
| @@ -725,7 +677,7 @@ pub const Db = struct { | |||
| 725 | const arg_ptr = &args[i + 1]; | 677 | const arg_ptr = &args[i + 1]; |
| 726 | 678 | ||
| 727 | const ArgType = arg.arg_type.?; | 679 | const ArgType = arg.arg_type.?; |
| 728 | setFunctionArgument(ArgType, arg_ptr, sqlite_args[i].?); | 680 | helpers.setTypeFromValue(ArgType, arg_ptr, sqlite_args[i].?); |
| 729 | } | 681 | } |
| 730 | 682 | ||
| 731 | @call(.{}, step_func, args); | 683 | @call(.{}, step_func, args); |
| @@ -792,7 +744,7 @@ pub const Db = struct { | |||
| 792 | var fn_args: ArgTuple = undefined; | 744 | var fn_args: ArgTuple = undefined; |
| 793 | inline for (fn_info.args) |arg, i| { | 745 | inline for (fn_info.args) |arg, i| { |
| 794 | const ArgType = arg.arg_type.?; | 746 | const ArgType = arg.arg_type.?; |
| 795 | setFunctionArgument(ArgType, &fn_args[i], sqlite_args[i].?); | 747 | helpers.setTypeFromValue(ArgType, &fn_args[i], sqlite_args[i].?); |
| 796 | } | 748 | } |
| 797 | 749 | ||
| 798 | const result = @call(.{}, func, fn_args); | 750 | const result = @call(.{}, func, fn_args); |