summaryrefslogtreecommitdiff
path: root/sqlite.zig
diff options
context:
space:
mode:
Diffstat (limited to 'sqlite.zig')
-rw-r--r--sqlite.zig41
1 files changed, 4 insertions, 37 deletions
diff --git a/sqlite.zig b/sqlite.zig
index 98f59bc..0f9078c 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -19,6 +19,8 @@ pub const DetailedError = errors.DetailedError;
19const getLastDetailedErrorFromDb = errors.getLastDetailedErrorFromDb; 19const getLastDetailedErrorFromDb = errors.getLastDetailedErrorFromDb;
20const getDetailedErrorFromResultCode = errors.getDetailedErrorFromResultCode; 20const getDetailedErrorFromResultCode = errors.getDetailedErrorFromResultCode;
21 21
22const helpers = @import("helpers.zig");
23
22const logger = std.log.scoped(.sqlite); 24const 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,