summaryrefslogtreecommitdiff
path: root/sqlite.zig
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2021-08-12 11:53:02 +0200
committerGravatar GitHub2021-08-12 11:53:02 +0200
commit4954c419d379ffbb637904b41d25ef910c6bb02b (patch)
treeb528338332a05a6db66c7c256e67c014e33fed37 /sqlite.zig
parentMerge pull request #37 from vrischmann/improve-doc (diff)
parentupdate README with new init syntax as well (diff)
downloadzig-sqlite-4954c419d379ffbb637904b41d25ef910c6bb02b.tar.gz
zig-sqlite-4954c419d379ffbb637904b41d25ef910c6bb02b.tar.xz
zig-sqlite-4954c419d379ffbb637904b41d25ef910c6bb02b.zip
Merge pull request #36 from nektro/master
Various fixes
Diffstat (limited to 'sqlite.zig')
-rw-r--r--sqlite.zig21
1 files changed, 10 insertions, 11 deletions
diff --git a/sqlite.zig b/sqlite.zig
index b5f7b72..bbf7043 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -312,7 +312,7 @@ pub const Db = struct {
312 }; 312 };
313 313
314 /// init creates a database with the provided options. 314 /// init creates a database with the provided options.
315 pub fn init(self: *Self, options: InitOptions) !void { 315 pub fn init(options: InitOptions) !Self {
316 var dummy_diags = Diagnostics{}; 316 var dummy_diags = Diagnostics{};
317 var diags = options.diags orelse &dummy_diags; 317 var diags = options.diags orelse &dummy_diags;
318 318
@@ -348,7 +348,7 @@ pub const Db = struct {
348 return errorFromResultCode(result); 348 return errorFromResultCode(result);
349 } 349 }
350 350
351 self.db = db.?; 351 return Self{ .db = db.? };
352 }, 352 },
353 .Memory => { 353 .Memory => {
354 logger.info("opening in memory", .{}); 354 logger.info("opening in memory", .{});
@@ -366,7 +366,7 @@ pub const Db = struct {
366 return errorFromResultCode(result); 366 return errorFromResultCode(result);
367 } 367 }
368 368
369 self.db = db.?; 369 return Self{ .db = db.? };
370 }, 370 },
371 } 371 }
372 } 372 }
@@ -599,7 +599,7 @@ pub fn Iterator(comptime Type: type) type {
599 }, 599 },
600 .Struct => { 600 .Struct => {
601 std.debug.assert(columns == TypeInfo.Struct.fields.len); 601 std.debug.assert(columns == TypeInfo.Struct.fields.len);
602 return try self.readStruct(.{}); 602 return try self.readStruct(options);
603 }, 603 },
604 else => @compileError("cannot read into type " ++ @typeName(Type) ++ " ; if dynamic memory allocation is required use nextAlloc"), 604 else => @compileError("cannot read into type " ++ @typeName(Type) ++ " ; if dynamic memory allocation is required use nextAlloc"),
605 } 605 }
@@ -1030,7 +1030,10 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t
1030 const StructTypeInfo = @typeInfo(StructType).Struct; 1030 const StructTypeInfo = @typeInfo(StructType).Struct;
1031 1031
1032 if (comptime query.nb_bind_markers != StructTypeInfo.fields.len) { 1032 if (comptime query.nb_bind_markers != StructTypeInfo.fields.len) {
1033 @compileError("number of bind markers not equal to number of fields"); 1033 @compileError(comptime std.fmt.comptimePrint("number of bind markers ({d}) not equal to number of fields ({d})", .{
1034 query.nb_bind_markers,
1035 StructTypeInfo.fields.len,
1036 }));
1034 } 1037 }
1035 1038
1036 inline for (StructTypeInfo.fields) |struct_field, _i| { 1039 inline for (StructTypeInfo.fields) |struct_field, _i| {
@@ -1915,8 +1918,7 @@ test "sqlite: blob open, reopen" {
1915test "sqlite: failing open" { 1918test "sqlite: failing open" {
1916 var diags: Diagnostics = undefined; 1919 var diags: Diagnostics = undefined;
1917 1920
1918 var db: Db = undefined; 1921 const res = Db.init(.{
1919 const res = db.init(.{
1920 .diags = &diags, 1922 .diags = &diags,
1921 .open_flags = .{}, 1923 .open_flags = .{},
1922 .mode = .{ .File = "/tmp/not_existing.db" }, 1924 .mode = .{ .File = "/tmp/not_existing.db" },
@@ -2010,16 +2012,13 @@ fn getTestDb() !Db {
2010 2012
2011 var mode = dbMode(&fba.allocator); 2013 var mode = dbMode(&fba.allocator);
2012 2014
2013 var db: Db = undefined; 2015 return try Db.init(.{
2014 try db.init(.{
2015 .open_flags = .{ 2016 .open_flags = .{
2016 .write = true, 2017 .write = true,
2017 .create = true, 2018 .create = true,
2018 }, 2019 },
2019 .mode = mode, 2020 .mode = mode,
2020 }); 2021 });
2021
2022 return db;
2023} 2022}
2024 2023
2025fn tmpDbPath(allocator: *mem.Allocator) ![:0]const u8 { 2024fn tmpDbPath(allocator: *mem.Allocator) ![:0]const u8 {