summaryrefslogtreecommitdiff
path: root/sqlite.zig
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2022-08-04 09:45:32 +0200
committerGravatar Vincent Rischmann2022-08-04 12:46:16 +0200
commit00ac50a1b79ea5cec2e8ed2fc344bc2f546c171f (patch)
tree73d71ceafd82380cb04197e4699b1617892cc071 /sqlite.zig
parentuse a single cImport (diff)
downloadzig-sqlite-00ac50a1b79ea5cec2e8ed2fc344bc2f546c171f.tar.gz
zig-sqlite-00ac50a1b79ea5cec2e8ed2fc344bc2f546c171f.tar.xz
zig-sqlite-00ac50a1b79ea5cec2e8ed2fc344bc2f546c171f.zip
move DetailedError, do some cleanup
DetailedError uses getLastErrorOffset, which must check the SQLite version with versionGreaterThanOrEqualTo. Instead of publicly exposing versionGreaterThanOrEqualTo in sqlite.zig move DetailedError in errors.zig
Diffstat (limited to 'sqlite.zig')
-rw-r--r--sqlite.zig46
1 files changed, 3 insertions, 43 deletions
diff --git a/sqlite.zig b/sqlite.zig
index 2f8611d..fdc15a5 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -15,6 +15,9 @@ pub const ParsedQuery = @import("query.zig").ParsedQuery;
15const errors = @import("errors.zig"); 15const errors = @import("errors.zig");
16pub const errorFromResultCode = errors.errorFromResultCode; 16pub const errorFromResultCode = errors.errorFromResultCode;
17pub const Error = errors.Error; 17pub const Error = errors.Error;
18pub const DetailedError = errors.DetailedError;
19const getLastDetailedErrorFromDb = errors.getLastDetailedErrorFromDb;
20const getDetailedErrorFromResultCode = errors.getDetailedErrorFromResultCode;
18 21
19const logger = std.log.scoped(.sqlite); 22const logger = std.log.scoped(.sqlite);
20 23
@@ -255,53 +258,10 @@ pub const InitOptions = struct {
255 diags: ?*Diagnostics = null, 258 diags: ?*Diagnostics = null,
256}; 259};
257 260
258/// DetailedError contains a SQLite error code and error message.
259pub const DetailedError = struct {
260 code: usize,
261 near: i32,
262 message: []const u8,
263
264 pub fn format(self: @This(), comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
265 _ = fmt;
266 _ = options;
267
268 _ = try writer.print("{{code: {}, near: {d}, message: {s}}}", .{ self.code, self.near, self.message });
269 }
270};
271
272fn isThreadSafe() bool { 261fn isThreadSafe() bool {
273 return c.sqlite3_threadsafe() > 0; 262 return c.sqlite3_threadsafe() > 0;
274} 263}
275 264
276fn getDetailedErrorFromResultCode(code: c_int) DetailedError {
277 return .{
278 .code = @intCast(usize, code),
279 .near = -1,
280 .message = blk: {
281 const msg = c.sqlite3_errstr(code);
282 break :blk mem.sliceTo(msg, 0);
283 },
284 };
285}
286
287fn getErrorOffset(db: *c.sqlite3) i32 {
288 if (c.SQLITE_VERSION_NUMBER >= 3038000) {
289 return c.sqlite3_error_offset(db);
290 }
291 return -1;
292}
293
294fn getLastDetailedErrorFromDb(db: *c.sqlite3) DetailedError {
295 return .{
296 .code = @intCast(usize, c.sqlite3_extended_errcode(db)),
297 .near = getErrorOffset(db),
298 .message = blk: {
299 const msg = c.sqlite3_errmsg(db);
300 break :blk mem.sliceTo(msg, 0);
301 },
302 };
303}
304
305/// Db is a wrapper around a SQLite database, providing high-level functions for executing queries. 265/// Db is a wrapper around a SQLite database, providing high-level functions for executing queries.
306/// A Db can be opened with a file database or a in-memory database: 266/// A Db can be opened with a file database or a in-memory database:
307/// 267///