From b0db4f712cb045f772cc5491e626b1a6009a8607 Mon Sep 17 00:00:00 2001 From: Vincent Rischmann Date: Tue, 29 Dec 2020 23:32:37 +0100 Subject: add InitOptions --- sqlite.zig | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) (limited to 'sqlite.zig') diff --git a/sqlite.zig b/sqlite.zig index dd4469b..156edcd 100644 --- a/sqlite.zig +++ b/sqlite.zig @@ -18,6 +18,12 @@ pub const ThreadingMode = enum { Serialized, }; +pub const InitOptions = struct { + mode: Db.Mode = .Memory, + open_flags: Db.OpenFlags = .{}, + threading_mode: ThreadingMode = .SingleThread, +}; + fn isThreadSafe() bool { return c.sqlite3_threadsafe() > 0; } @@ -45,27 +51,38 @@ pub const Db = struct { Memory, }; + pub const OpenFlags = struct { + write: bool = false, + create: bool = false, + }; + /// init creates a database with the provided `mode`. - pub fn init(self: *Self, allocator: *mem.Allocator, options: anytype) !void { + pub fn init(self: *Self, allocator: *mem.Allocator, options: InitOptions) !void { self.allocator = allocator; - const mode: Mode = if (@hasField(@TypeOf(options), "mode")) options.mode else .Memory; // Validate the threading mode if (options.threading_mode != .SingleThread and !isThreadSafe()) { return error.CannotUseSingleThreadedSQLite; } - switch (mode) { + // Compute the flags + var flags: c_int = 0; + flags |= @as(c_int, if (options.open_flags.write) c.SQLITE_OPEN_READWRITE else c.SQLITE_OPEN_READONLY); + if (options.open_flags.create) { + flags |= c.SQLITE_OPEN_CREATE; + } + switch (options.threading_mode) { + .MultiThread => flags |= c.SQLITE_OPEN_NOMUTEX, + .Serialized => flags |= c.SQLITE_OPEN_FULLMUTEX, + else => {}, + } + + switch (options.mode) { .File => |path| { logger.info("opening {}", .{path}); var db: ?*c.sqlite3 = undefined; - const result = c.sqlite3_open_v2( - path, - &db, - c.SQLITE_OPEN_READWRITE | c.SQLITE_OPEN_CREATE, - null, - ); + const result = c.sqlite3_open_v2(path, &db, flags, null); if (result != c.SQLITE_OK or db == null) { logger.warn("unable to open database, result: {}", .{result}); return error.CannotOpenDatabase; @@ -76,13 +93,10 @@ pub const Db = struct { .Memory => { logger.info("opening in memory", .{}); + flags |= c.SQLITE_OPEN_MEMORY; + var db: ?*c.sqlite3 = undefined; - const result = c.sqlite3_open_v2( - ":memory:", - &db, - c.SQLITE_OPEN_READWRITE | c.SQLITE_OPEN_MEMORY, - null, - ); + const result = c.sqlite3_open_v2(":memory:", &db, flags, null); if (result != c.SQLITE_OK or db == null) { logger.warn("unable to open database, result: {}", .{result}); return error.CannotOpenDatabase; -- cgit v1.2.3