diff options
| author | 2022-08-15 21:52:02 +0200 | |
|---|---|---|
| committer | 2022-09-11 17:12:37 +0200 | |
| commit | 309105c7e8f43f21237dd243e714db2f16f71a6f (patch) | |
| tree | ea124dced982543b5725fa7323019f71a6b740f0 /sqlite.zig | |
| parent | gitignore: explicitly ignore only the zig output dirs (diff) | |
| download | zig-sqlite-309105c7e8f43f21237dd243e714db2f16f71a6f.tar.gz zig-sqlite-309105c7e8f43f21237dd243e714db2f16f71a6f.tar.xz zig-sqlite-309105c7e8f43f21237dd243e714db2f16f71a6f.zip | |
move setFunctionResult to helpers.setResult
This function will also be used by the virtual table implementation.
Diffstat (limited to '')
| -rw-r--r-- | sqlite.zig | 41 |
1 files changed, 4 insertions, 37 deletions
| @@ -19,6 +19,8 @@ pub const DetailedError = errors.DetailedError; | |||
| 19 | const getLastDetailedErrorFromDb = errors.getLastDetailedErrorFromDb; | 19 | const getLastDetailedErrorFromDb = errors.getLastDetailedErrorFromDb; |
| 20 | const getDetailedErrorFromResultCode = errors.getDetailedErrorFromResultCode; | 20 | const getDetailedErrorFromResultCode = errors.getDetailedErrorFromResultCode; |
| 21 | 21 | ||
| 22 | const helpers = @import("helpers.zig"); | ||
| 23 | |||
| 22 | const logger = std.log.scoped(.sqlite); | 24 | const logger = std.log.scoped(.sqlite); |
| 23 | 25 | ||
| 24 | /// Text is used to represent a SQLite TEXT value when binding a parameter or reading a column. | 26 | /// Text is used to represent a SQLite TEXT value when binding a parameter or reading a column. |
| @@ -579,41 +581,6 @@ pub const Db = struct { | |||
| 579 | return value.?[0..size]; | 581 | return value.?[0..size]; |
| 580 | } | 582 | } |
| 581 | 583 | ||
| 582 | /// Sets the result of a function call in the context `ctx`. | ||
| 583 | /// | ||
| 584 | /// Determines at compile time which sqlite3_result_XYZ function to use based on the type of `result`. | ||
| 585 | fn setFunctionResult(ctx: ?*c.sqlite3_context, result: anytype) void { | ||
| 586 | const ResultType = @TypeOf(result); | ||
| 587 | |||
| 588 | switch (ResultType) { | ||
| 589 | Text => c.sqlite3_result_text(ctx, result.data.ptr, @intCast(c_int, result.data.len), c.SQLITE_TRANSIENT), | ||
| 590 | Blob => c.sqlite3_result_blob(ctx, result.data.ptr, @intCast(c_int, result.data.len), c.SQLITE_TRANSIENT), | ||
| 591 | else => switch (@typeInfo(ResultType)) { | ||
| 592 | .Int => |info| if ((info.bits + if (info.signedness == .unsigned) 1 else 0) <= 32) { | ||
| 593 | c.sqlite3_result_int(ctx, result); | ||
| 594 | } else if ((info.bits + if (info.signedness == .unsigned) 1 else 0) <= 64) { | ||
| 595 | c.sqlite3_result_int64(ctx, result); | ||
| 596 | } else { | ||
| 597 | @compileError("integer " ++ @typeName(ResultType) ++ " is not representable in sqlite"); | ||
| 598 | }, | ||
| 599 | .Float => c.sqlite3_result_double(ctx, result), | ||
| 600 | .Bool => c.sqlite3_result_int(ctx, if (result) 1 else 0), | ||
| 601 | .Array => |arr| switch (arr.child) { | ||
| 602 | u8 => c.sqlite3_result_blob(ctx, &result, arr.len, c.SQLITE_TRANSIENT), | ||
| 603 | else => @compileError("cannot use a result of type " ++ @typeName(ResultType)), | ||
| 604 | }, | ||
| 605 | .Pointer => |ptr| switch (ptr.size) { | ||
| 606 | .Slice => switch (ptr.child) { | ||
| 607 | u8 => c.sqlite3_result_text(ctx, result.ptr, @intCast(c_int, result.len), c.SQLITE_TRANSIENT), | ||
| 608 | else => @compileError("cannot use a result of type " ++ @typeName(ResultType)), | ||
| 609 | }, | ||
| 610 | else => @compileError("cannot use a result of type " ++ @typeName(ResultType)), | ||
| 611 | }, | ||
| 612 | else => @compileError("cannot use a result of type " ++ @typeName(ResultType)), | ||
| 613 | }, | ||
| 614 | } | ||
| 615 | } | ||
| 616 | |||
| 617 | /// Sets a function argument using the provided value. | 584 | /// Sets a function argument using the provided value. |
| 618 | /// | 585 | /// |
| 619 | /// Determines at compile time which sqlite3_value_XYZ function to use based on the type `ArgType`. | 586 | /// Determines at compile time which sqlite3_value_XYZ function to use based on the type `ArgType`. |
| @@ -773,7 +740,7 @@ pub const Db = struct { | |||
| 773 | 740 | ||
| 774 | const result = @call(.{}, finalize_func, args); | 741 | const result = @call(.{}, finalize_func, args); |
| 775 | 742 | ||
| 776 | setFunctionResult(ctx, result); | 743 | helpers.setResult(ctx, result); |
| 777 | } | 744 | } |
| 778 | }.xFinal, | 745 | }.xFinal, |
| 779 | null, | 746 | null, |
| @@ -830,7 +797,7 @@ pub const Db = struct { | |||
| 830 | 797 | ||
| 831 | const result = @call(.{}, func, fn_args); | 798 | const result = @call(.{}, func, fn_args); |
| 832 | 799 | ||
| 833 | setFunctionResult(ctx, result); | 800 | helpers.setResult(ctx, result); |
| 834 | } | 801 | } |
| 835 | }.xFunc, | 802 | }.xFunc, |
| 836 | null, | 803 | null, |