diff options
| author | 2021-09-23 22:12:24 -0700 | |
|---|---|---|
| committer | 2021-09-23 22:12:24 -0700 | |
| commit | 84bc9db37557d5887f1c31908d750c4e77a8c448 (patch) | |
| tree | c2c42486def45efae37b8590da553d64290ca51d /sqlite.zig | |
| parent | Merge pull request #48 from vrischmann/clarify-options (diff) | |
| download | zig-sqlite-84bc9db37557d5887f1c31908d750c4e77a8c448.tar.gz zig-sqlite-84bc9db37557d5887f1c31908d750c4e77a8c448.tar.xz zig-sqlite-84bc9db37557d5887f1c31908d750c4e77a8c448.zip | |
add options parameter to bind/bindField
Diffstat (limited to 'sqlite.zig')
| -rw-r--r-- | sqlite.zig | 18 |
1 files changed, 9 insertions, 9 deletions
| @@ -1100,7 +1100,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1100 | /// } | 1100 | /// } |
| 1101 | /// | 1101 | /// |
| 1102 | /// The types are checked at comptime. | 1102 | /// The types are checked at comptime. |
| 1103 | fn bind(self: *Self, values: anytype) void { | 1103 | fn bind(self: *Self, options: anytype, values: anytype) !void { |
| 1104 | const StructType = @TypeOf(values); | 1104 | const StructType = @TypeOf(values); |
| 1105 | const StructTypeInfo = @typeInfo(StructType).Struct; | 1105 | const StructTypeInfo = @typeInfo(StructType).Struct; |
| 1106 | 1106 | ||
| @@ -1129,7 +1129,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1129 | 1129 | ||
| 1130 | const field_value = @field(values, struct_field.name); | 1130 | const field_value = @field(values, struct_field.name); |
| 1131 | 1131 | ||
| 1132 | self.bindField(struct_field.field_type, struct_field.name, _i, field_value); | 1132 | try self.bindField(struct_field.field_type, options, struct_field.name, _i, field_value); |
| 1133 | } | 1133 | } |
| 1134 | } | 1134 | } |
| 1135 | 1135 | ||
| @@ -1139,7 +1139,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1139 | } | 1139 | } |
| 1140 | } | 1140 | } |
| 1141 | 1141 | ||
| 1142 | fn bindField(self: *Self, comptime FieldType: type, comptime field_name: []const u8, i: c_int, field: FieldType) void { | 1142 | fn bindField(self: *Self, comptime FieldType: type, options: anytype, comptime field_name: []const u8, i: c_int, field: FieldType) !void { |
| 1143 | const field_type_info = @typeInfo(FieldType); | 1143 | const field_type_info = @typeInfo(FieldType); |
| 1144 | const column = i + 1; | 1144 | const column = i + 1; |
| 1145 | 1145 | ||
| @@ -1152,7 +1152,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1152 | .Float, .ComptimeFloat => _ = c.sqlite3_bind_double(self.stmt, column, field), | 1152 | .Float, .ComptimeFloat => _ = c.sqlite3_bind_double(self.stmt, column, field), |
| 1153 | .Bool => _ = c.sqlite3_bind_int64(self.stmt, column, @boolToInt(field)), | 1153 | .Bool => _ = c.sqlite3_bind_int64(self.stmt, column, @boolToInt(field)), |
| 1154 | .Pointer => |ptr| switch (ptr.size) { | 1154 | .Pointer => |ptr| switch (ptr.size) { |
| 1155 | .One => self.bindField(ptr.child, field_name, i, field.*), | 1155 | .One => try self.bindField(ptr.child, options, field_name, i, field.*), |
| 1156 | .Slice => switch (ptr.child) { | 1156 | .Slice => switch (ptr.child) { |
| 1157 | u8 => { | 1157 | u8 => { |
| 1158 | _ = c.sqlite3_bind_text(self.stmt, column, field.ptr, @intCast(c_int, field.len), null); | 1158 | _ = c.sqlite3_bind_text(self.stmt, column, field.ptr, @intCast(c_int, field.len), null); |
| @@ -1172,17 +1172,17 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1172 | } | 1172 | } |
| 1173 | }, | 1173 | }, |
| 1174 | .Optional => |opt| if (field) |non_null_field| { | 1174 | .Optional => |opt| if (field) |non_null_field| { |
| 1175 | self.bindField(opt.child, field_name, i, non_null_field); | 1175 | try self.bindField(opt.child, options, field_name, i, non_null_field); |
| 1176 | } else { | 1176 | } else { |
| 1177 | _ = c.sqlite3_bind_null(self.stmt, column); | 1177 | _ = c.sqlite3_bind_null(self.stmt, column); |
| 1178 | }, | 1178 | }, |
| 1179 | .Null => _ = c.sqlite3_bind_null(self.stmt, column), | 1179 | .Null => _ = c.sqlite3_bind_null(self.stmt, column), |
| 1180 | .Enum => { | 1180 | .Enum => { |
| 1181 | if (comptime std.meta.trait.isZigString(FieldType.BaseType)) { | 1181 | if (comptime std.meta.trait.isZigString(FieldType.BaseType)) { |
| 1182 | return self.bindField(FieldType.BaseType, field_name, i, @tagName(field)); | 1182 | return try self.bindField(FieldType.BaseType, options, field_name, i, @tagName(field)); |
| 1183 | } | 1183 | } |
| 1184 | if (@typeInfo(FieldType.BaseType) == .Int) { | 1184 | if (@typeInfo(FieldType.BaseType) == .Int) { |
| 1185 | return self.bindField(FieldType.BaseType, field_name, i, @enumToInt(field)); | 1185 | return try self.bindField(FieldType.BaseType, options, field_name, i, @enumToInt(field)); |
| 1186 | } | 1186 | } |
| 1187 | @compileError("enum column " ++ @typeName(FieldType) ++ " must have a BaseType of either string or int to bind"); | 1187 | @compileError("enum column " ++ @typeName(FieldType) ++ " must have a BaseType of either string or int to bind"); |
| 1188 | }, | 1188 | }, |
| @@ -1199,7 +1199,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1199 | /// in the input query string. | 1199 | /// in the input query string. |
| 1200 | /// | 1200 | /// |
| 1201 | pub fn exec(self: *Self, options: QueryOptions, values: anytype) !void { | 1201 | pub fn exec(self: *Self, options: QueryOptions, values: anytype) !void { |
| 1202 | self.bind(values); | 1202 | try self.bind({}, values); |
| 1203 | 1203 | ||
| 1204 | var dummy_diags = Diagnostics{}; | 1204 | var dummy_diags = Diagnostics{}; |
| 1205 | var diags = options.diags orelse &dummy_diags; | 1205 | var diags = options.diags orelse &dummy_diags; |
| @@ -1233,7 +1233,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 1233 | /// | 1233 | /// |
| 1234 | /// The iterator _must not_ outlive the statement. | 1234 | /// The iterator _must not_ outlive the statement. |
| 1235 | pub fn iterator(self: *Self, comptime Type: type, values: anytype) !Iterator(Type) { | 1235 | pub fn iterator(self: *Self, comptime Type: type, values: anytype) !Iterator(Type) { |
| 1236 | self.bind(values); | 1236 | try self.bind({}, values); |
| 1237 | 1237 | ||
| 1238 | var res: Iterator(Type) = undefined; | 1238 | var res: Iterator(Type) = undefined; |
| 1239 | res.db = self.db; | 1239 | res.db = self.db; |