summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2020-12-30 15:23:37 +0100
committerGravatar Vincent Rischmann2020-12-30 15:27:13 +0100
commit6f7e035c5107b7672056ec0300c911e4386ea388 (patch)
tree0ad247669d2725137c81d8b75e77ba7674cda496
parentupdate readme for Db.init (diff)
downloadzig-sqlite-6f7e035c5107b7672056ec0300c911e4386ea388.tar.gz
zig-sqlite-6f7e035c5107b7672056ec0300c911e4386ea388.tar.xz
zig-sqlite-6f7e035c5107b7672056ec0300c911e4386ea388.zip
add Error, DetailedError
-rw-r--r--sqlite.zig33
1 files changed, 33 insertions, 0 deletions
diff --git a/sqlite.zig b/sqlite.zig
index 4a65461..fd0877d 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -42,10 +42,38 @@ pub const InitOptions = struct {
42 threading_mode: ThreadingMode = .Serialized, 42 threading_mode: ThreadingMode = .Serialized,
43}; 43};
44 44
45pub const Error = error{};
46
47/// DetailedError contains a SQLite error code and error message.
48pub const DetailedError = struct {
49 code: usize,
50 message: []const u8,
51};
52
45fn isThreadSafe() bool { 53fn isThreadSafe() bool {
46 return c.sqlite3_threadsafe() > 0; 54 return c.sqlite3_threadsafe() > 0;
47} 55}
48 56
57fn getDetailedErrorFromResultCode(code: c_int) DetailedError {
58 return .{
59 .code = @intCast(usize, code),
60 .message = blk: {
61 const msg = c.sqlite3_errstr(code);
62 break :blk mem.spanZ(msg);
63 },
64 };
65}
66
67fn getLastDetailedErrorFromDb(db: *c.sqlite3) DetailedError {
68 return .{
69 .code = @intCast(usize, c.sqlite3_extended_errcode(db)),
70 .message = blk: {
71 const msg = c.sqlite3_errmsg(db);
72 break :blk mem.spanZ(msg);
73 },
74 };
75}
76
49/// Db is a wrapper around a SQLite database, providing high-level functions for executing queries. 77/// Db is a wrapper around a SQLite database, providing high-level functions for executing queries.
50/// A Db can be opened with a file database or a in-memory database: 78/// A Db can be opened with a file database or a in-memory database:
51/// 79///
@@ -127,6 +155,11 @@ pub const Db = struct {
127 _ = c.sqlite3_close(self.db); 155 _ = c.sqlite3_close(self.db);
128 } 156 }
129 157
158 // getDetailedError returns the detailed error for the last API call if it failed.
159 pub fn getDetailedError(self: *Self) DetailedError {
160 return getLastDetailedErrorFromDb(self.db);
161 }
162
130 /// pragma is a convenience function to use the PRAGMA statement. 163 /// pragma is a convenience function to use the PRAGMA statement.
131 /// 164 ///
132 /// Here is how to set a pragma value: 165 /// Here is how to set a pragma value: