diff options
| author | 2020-10-24 21:44:56 +0200 | |
|---|---|---|
| committer | 2020-10-24 21:44:56 +0200 | |
| commit | 6da2a5d43ed671419b51900acc72547aedb3aa64 (patch) | |
| tree | f03aa551e5f06aff635da4562e5b282cc2443c74 /src/main.zig | |
| parent | fix typo (diff) | |
| download | zig-sqlite-6da2a5d43ed671419b51900acc72547aedb3aa64.tar.gz zig-sqlite-6da2a5d43ed671419b51900acc72547aedb3aa64.tar.xz zig-sqlite-6da2a5d43ed671419b51900acc72547aedb3aa64.zip | |
take an options var in init()
Diffstat (limited to '')
| -rw-r--r-- | src/main.zig | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/src/main.zig b/src/main.zig index f83f316..2d6ca46 100644 --- a/src/main.zig +++ b/src/main.zig | |||
| @@ -15,11 +15,11 @@ const logger = std.log.scoped(.sqlite); | |||
| 15 | /// | 15 | /// |
| 16 | /// // File database | 16 | /// // File database |
| 17 | /// var db: sqlite.Db = undefined; | 17 | /// var db: sqlite.Db = undefined; |
| 18 | /// try db.init(allocator, .{ .File = "/tmp/data.db" }); | 18 | /// try db.init(allocator, .{ .mode = { .File = "/tmp/data.db" } }); |
| 19 | /// | 19 | /// |
| 20 | /// // In memory database | 20 | /// // In memory database |
| 21 | /// var db: sqlite.Db = undefined; | 21 | /// var db: sqlite.Db = undefined; |
| 22 | /// try db.init(allocator, .{ .Memory={} }); | 22 | /// try db.init(allocator, .{ .mode = { .Memory = {} } }); |
| 23 | /// | 23 | /// |
| 24 | pub const Db = struct { | 24 | pub const Db = struct { |
| 25 | const Self = @This(); | 25 | const Self = @This(); |
| @@ -34,9 +34,11 @@ pub const Db = struct { | |||
| 34 | }; | 34 | }; |
| 35 | 35 | ||
| 36 | /// init creates a database with the provided `mode`. | 36 | /// init creates a database with the provided `mode`. |
| 37 | pub fn init(self: *Self, allocator: *mem.Allocator, mode: Mode) !void { | 37 | pub fn init(self: *Self, allocator: *mem.Allocator, options: anytype) !void { |
| 38 | self.allocator = allocator; | 38 | self.allocator = allocator; |
| 39 | 39 | ||
| 40 | const mode = if (@hasField(@TypeOf(options), "mode")) options.mode else .Memory; | ||
| 41 | |||
| 40 | switch (mode) { | 42 | switch (mode) { |
| 41 | .File => |path| { | 43 | .File => |path| { |
| 42 | logger.info("opening {}", .{path}); | 44 | logger.info("opening {}", .{path}); |
| @@ -388,7 +390,7 @@ test "sqlite: statement exec" { | |||
| 388 | var allocator = &arena.allocator; | 390 | var allocator = &arena.allocator; |
| 389 | 391 | ||
| 390 | var db: Db = undefined; | 392 | var db: Db = undefined; |
| 391 | try db.init(testing.allocator, dbMode()); | 393 | try db.init(testing.allocator, .{ .mode = dbMode() }); |
| 392 | 394 | ||
| 393 | // Create the tables | 395 | // Create the tables |
| 394 | 396 | ||