diff options
| author | 2021-01-02 00:06:57 +0100 | |
|---|---|---|
| committer | 2021-01-02 00:32:20 +0100 | |
| commit | 54b39121f007aecab530ce2e38e850571c408b58 (patch) | |
| tree | d77a69859f508b10fd05a4c4205948d050cc0f9d /sqlite.zig | |
| parent | fix the comment on Stmt.one and Stmt.all (diff) | |
| download | zig-sqlite-54b39121f007aecab530ce2e38e850571c408b58.tar.gz zig-sqlite-54b39121f007aecab530ce2e38e850571c408b58.tar.xz zig-sqlite-54b39121f007aecab530ce2e38e850571c408b58.zip | |
add bindField
Diffstat (limited to 'sqlite.zig')
| -rw-r--r-- | sqlite.zig | 55 |
1 files changed, 31 insertions, 24 deletions
| @@ -711,34 +711,41 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 711 | .Untyped => {}, | 711 | .Untyped => {}, |
| 712 | } | 712 | } |
| 713 | 713 | ||
| 714 | const i = @as(usize, _i); | ||
| 715 | const field_type_info = @typeInfo(struct_field.field_type); | ||
| 716 | const field_value = @field(values, struct_field.name); | 714 | const field_value = @field(values, struct_field.name); |
| 717 | const column = i + 1; | ||
| 718 | 715 | ||
| 719 | switch (struct_field.field_type) { | 716 | self.bindField(struct_field.field_type, struct_field.name, _i, field_value); |
| 720 | []const u8, []u8 => { | 717 | } |
| 721 | _ = c.sqlite3_bind_text(self.stmt, column, field_value.ptr, @intCast(c_int, field_value.len), null); | 718 | } |
| 719 | |||
| 720 | fn bindField(self: *Self, comptime FieldType: type, comptime field_name: []const u8, i: c_int, field: FieldType) void { | ||
| 721 | const field_type_info = @typeInfo(FieldType); | ||
| 722 | const column = i + 1; | ||
| 723 | |||
| 724 | switch (FieldType) { | ||
| 725 | []const u8, []u8 => { | ||
| 726 | _ = c.sqlite3_bind_text(self.stmt, column, field.ptr, @intCast(c_int, field.len), null); | ||
| 727 | }, | ||
| 728 | Text => _ = c.sqlite3_bind_text(self.stmt, column, field.data.ptr, @intCast(c_int, field.data.len), null), | ||
| 729 | Blob => _ = c.sqlite3_bind_blob(self.stmt, column, field.data.ptr, @intCast(c_int, field.data.len), null), | ||
| 730 | else => switch (field_type_info) { | ||
| 731 | .Int, .ComptimeInt => _ = c.sqlite3_bind_int64(self.stmt, column, @intCast(c_longlong, field)), | ||
| 732 | .Float, .ComptimeFloat => _ = c.sqlite3_bind_double(self.stmt, column, field), | ||
| 733 | .Bool => _ = c.sqlite3_bind_int64(self.stmt, column, @boolToInt(field)), | ||
| 734 | .Pointer => |ptr| switch (ptr.size) { | ||
| 735 | else => @compileError("cannot bind field " ++ field_name ++ " of type " ++ @typeName(FieldType)), | ||
| 722 | }, | 736 | }, |
| 723 | Text => _ = c.sqlite3_bind_text(self.stmt, column, field_value.data.ptr, @intCast(c_int, field_value.data.len), null), | 737 | .Array => |arr| { |
| 724 | Blob => _ = c.sqlite3_bind_blob(self.stmt, column, field_value.data.ptr, @intCast(c_int, field_value.data.len), null), | 738 | switch (arr.child) { |
| 725 | else => switch (field_type_info) { | 739 | u8 => { |
| 726 | .Int, .ComptimeInt => _ = c.sqlite3_bind_int64(self.stmt, column, @intCast(c_longlong, field_value)), | 740 | const data: []const u8 = field[0..field.len]; |
| 727 | .Float, .ComptimeFloat => _ = c.sqlite3_bind_double(self.stmt, column, field_value), | 741 | |
| 728 | .Bool => _ = c.sqlite3_bind_int64(self.stmt, column, @boolToInt(field_value)), | 742 | _ = c.sqlite3_bind_text(self.stmt, column, data.ptr, @intCast(c_int, data.len), null); |
| 729 | .Array => |arr| { | 743 | }, |
| 730 | switch (arr.child) { | 744 | else => @compileError("cannot bind field " ++ field_name ++ " of type array of " ++ @typeName(arr.child)), |
| 731 | u8 => { | 745 | } |
| 732 | const data: []const u8 = field_value[0..field_value.len]; | ||
| 733 | |||
| 734 | _ = c.sqlite3_bind_text(self.stmt, column, data.ptr, @intCast(c_int, data.len), null); | ||
| 735 | }, | ||
| 736 | else => @compileError("cannot bind field " ++ struct_field.name ++ " of type array of " ++ @typeName(arr.child)), | ||
| 737 | } | ||
| 738 | }, | ||
| 739 | else => @compileError("cannot bind field " ++ struct_field.name ++ " of type " ++ @typeName(struct_field.field_type)), | ||
| 740 | }, | 746 | }, |
| 741 | } | 747 | else => @compileError("cannot bind field " ++ field_name ++ " of type " ++ @typeName(FieldType)), |
| 748 | }, | ||
| 742 | } | 749 | } |
| 743 | } | 750 | } |
| 744 | 751 | ||