diff options
| -rw-r--r-- | sqlite.zig | 14 |
1 files changed, 14 insertions, 0 deletions
| @@ -12,6 +12,16 @@ usingnamespace @import("query.zig"); | |||
| 12 | 12 | ||
| 13 | const logger = std.log.scoped(.sqlite); | 13 | const logger = std.log.scoped(.sqlite); |
| 14 | 14 | ||
| 15 | pub const ThreadingMode = enum { | ||
| 16 | SingleThread, | ||
| 17 | MultiThread, | ||
| 18 | Serialized, | ||
| 19 | }; | ||
| 20 | |||
| 21 | fn isThreadSafe() bool { | ||
| 22 | return c.sqlite3_threadsafe() > 0; | ||
| 23 | } | ||
| 24 | |||
| 15 | /// Db is a wrapper around a SQLite database, providing high-level functions for executing queries. | 25 | /// Db is a wrapper around a SQLite database, providing high-level functions for executing queries. |
| 16 | /// A Db can be opened with a file database or a in-memory database: | 26 | /// A Db can be opened with a file database or a in-memory database: |
| 17 | /// | 27 | /// |
| @@ -40,6 +50,10 @@ pub const Db = struct { | |||
| 40 | self.allocator = allocator; | 50 | self.allocator = allocator; |
| 41 | 51 | ||
| 42 | const mode: Mode = if (@hasField(@TypeOf(options), "mode")) options.mode else .Memory; | 52 | const mode: Mode = if (@hasField(@TypeOf(options), "mode")) options.mode else .Memory; |
| 53 | // Validate the threading mode | ||
| 54 | if (options.threading_mode != .SingleThread and !isThreadSafe()) { | ||
| 55 | return error.CannotUseSingleThreadedSQLite; | ||
| 56 | } | ||
| 43 | 57 | ||
| 44 | switch (mode) { | 58 | switch (mode) { |
| 45 | .File => |path| { | 59 | .File => |path| { |