diff options
| author | 2020-12-30 15:23:37 +0100 | |
|---|---|---|
| committer | 2020-12-30 15:27:13 +0100 | |
| commit | 6f7e035c5107b7672056ec0300c911e4386ea388 (patch) | |
| tree | 0ad247669d2725137c81d8b75e77ba7674cda496 /sqlite.zig | |
| parent | update readme for Db.init (diff) | |
| download | zig-sqlite-6f7e035c5107b7672056ec0300c911e4386ea388.tar.gz zig-sqlite-6f7e035c5107b7672056ec0300c911e4386ea388.tar.xz zig-sqlite-6f7e035c5107b7672056ec0300c911e4386ea388.zip | |
add Error, DetailedError
Diffstat (limited to 'sqlite.zig')
| -rw-r--r-- | sqlite.zig | 33 |
1 files changed, 33 insertions, 0 deletions
| @@ -42,10 +42,38 @@ pub const InitOptions = struct { | |||
| 42 | threading_mode: ThreadingMode = .Serialized, | 42 | threading_mode: ThreadingMode = .Serialized, |
| 43 | }; | 43 | }; |
| 44 | 44 | ||
| 45 | pub const Error = error{}; | ||
| 46 | |||
| 47 | /// DetailedError contains a SQLite error code and error message. | ||
| 48 | pub const DetailedError = struct { | ||
| 49 | code: usize, | ||
| 50 | message: []const u8, | ||
| 51 | }; | ||
| 52 | |||
| 45 | fn isThreadSafe() bool { | 53 | fn isThreadSafe() bool { |
| 46 | return c.sqlite3_threadsafe() > 0; | 54 | return c.sqlite3_threadsafe() > 0; |
| 47 | } | 55 | } |
| 48 | 56 | ||
| 57 | fn 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 | |||
| 67 | fn 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: |