summaryrefslogtreecommitdiff
path: root/sqlite.zig
diff options
context:
space:
mode:
Diffstat (limited to 'sqlite.zig')
-rw-r--r--sqlite.zig44
1 files changed, 11 insertions, 33 deletions
diff --git a/sqlite.zig b/sqlite.zig
index 3a659e8..2bc8a67 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -1359,9 +1359,15 @@ pub const DynamicStatement = struct {
1359 const StructType = @TypeOf(values); 1359 const StructType = @TypeOf(values);
1360 const StructTypeInfo = @typeInfo(StructType).Struct; 1360 const StructTypeInfo = @typeInfo(StructType).Struct;
1361 1361
1362 inline for (StructTypeInfo.fields) |struct_field, i| { 1362 inline for (StructTypeInfo.fields) |struct_field, struct_field_i| {
1363 const field_value = @field(values, struct_field.name); 1363 const field_value = @field(values, struct_field.name);
1364 try self.bindField(struct_field.field_type, options, struct_field.name, i, field_value); 1364
1365 const i = sqlite3BindParameterIndex(self.stmt, struct_field.name);
1366 if (i >= 0) {
1367 try self.bindField(struct_field.field_type, options, struct_field.name, i, field_value);
1368 } else {
1369 try self.bindField(struct_field.field_type, options, struct_field.name, struct_field_i, field_value);
1370 }
1365 } 1371 }
1366 } 1372 }
1367 1373
@@ -1374,34 +1380,6 @@ pub const DynamicStatement = struct {
1374 return -1; 1380 return -1;
1375 } 1381 }
1376 1382
1377 /// bind named structure
1378 fn bindNamedStruct(self: *Self, options: anytype, values: anytype) !void {
1379 const StructType = @TypeOf(values);
1380 const StructTypeInfo = @typeInfo(StructType).Struct;
1381
1382 inline for (StructTypeInfo.fields) |struct_field| {
1383 const i = sqlite3BindParameterIndex(self.stmt, struct_field.name);
1384 if (i >= 0) {
1385 try self.bindField(struct_field.field_type, options, struct_field.name, i, @field(values, struct_field.name));
1386 } else if (i == -1) {
1387 return errors.SQLiteError.SQLiteNotFound;
1388 // bug: do not put into a else block. reproduced in 0.8.1 and 0.9.0+dev.1193
1389 // title: broken LLVM module found: Operand is null.
1390 // TODO: fire an issue to ziglang/zig and place address here
1391 }
1392 }
1393 }
1394
1395 fn smartBind(self: *Self, options: anytype, values: anytype) !void {
1396 if (std.meta.fieldNames(@TypeOf(values)).len == 0) {
1397 return;
1398 } else if (std.meta.trait.isTuple(@TypeOf(values))) {
1399 try self.bind(options, values);
1400 } else {
1401 try self.bindNamedStruct(options, values);
1402 }
1403 }
1404
1405 /// exec executes a statement which does not return data. 1383 /// exec executes a statement which does not return data.
1406 /// 1384 ///
1407 /// The `options` tuple is used to provide additional state in some cases. 1385 /// The `options` tuple is used to provide additional state in some cases.
@@ -1414,7 +1392,7 @@ pub const DynamicStatement = struct {
1414 /// Possible errors: 1392 /// Possible errors:
1415 /// - SQLiteError.SQLiteNotFound if some fields not found 1393 /// - SQLiteError.SQLiteNotFound if some fields not found
1416 pub fn exec(self: *Self, options: QueryOptions, values: anytype) !void { 1394 pub fn exec(self: *Self, options: QueryOptions, values: anytype) !void {
1417 try self.smartBind(.{}, values); 1395 try self.bind(.{}, values);
1418 1396
1419 var dummy_diags = Diagnostics{}; 1397 var dummy_diags = Diagnostics{};
1420 var diags = options.diags orelse &dummy_diags; 1398 var diags = options.diags orelse &dummy_diags;
@@ -1453,7 +1431,7 @@ pub const DynamicStatement = struct {
1453 /// Possible errors: 1431 /// Possible errors:
1454 /// - SQLiteError.SQLiteNotFound if some fields not found 1432 /// - SQLiteError.SQLiteNotFound if some fields not found
1455 pub fn iterator(self: *Self, comptime Type: type, values: anytype) !Iterator(Type) { 1433 pub fn iterator(self: *Self, comptime Type: type, values: anytype) !Iterator(Type) {
1456 try self.smartBind(.{}, values); 1434 try self.bind(.{}, values);
1457 1435
1458 var res: Iterator(Type) = undefined; 1436 var res: Iterator(Type) = undefined;
1459 res.db = self.db; 1437 res.db = self.db;
@@ -1486,7 +1464,7 @@ pub const DynamicStatement = struct {
1486 /// Possible errors: 1464 /// Possible errors:
1487 /// - SQLiteError.SQLiteNotFound if some fields not found 1465 /// - SQLiteError.SQLiteNotFound if some fields not found
1488 pub fn iteratorAlloc(self: *Self, comptime Type: type, allocator: *std.mem.Allocator, values: anytype) !Iterator(Type) { 1466 pub fn iteratorAlloc(self: *Self, comptime Type: type, allocator: *std.mem.Allocator, values: anytype) !Iterator(Type) {
1489 try self.smartBind(.{ .allocator = allocator }, values); 1467 try self.bind(.{ .allocator = allocator }, values);
1490 1468
1491 var res: Iterator(Type) = undefined; 1469 var res: Iterator(Type) = undefined;
1492 res.db = self.db; 1470 res.db = self.db;