diff options
Diffstat (limited to '')
| -rw-r--r-- | sqlite.zig | 19 |
1 files changed, 9 insertions, 10 deletions
| @@ -9,6 +9,7 @@ const c = @cImport({ | |||
| 9 | }); | 9 | }); |
| 10 | 10 | ||
| 11 | usingnamespace @import("query.zig"); | 11 | usingnamespace @import("query.zig"); |
| 12 | usingnamespace @import("error.zig"); | ||
| 12 | 13 | ||
| 13 | const logger = std.log.scoped(.sqlite); | 14 | const logger = std.log.scoped(.sqlite); |
| 14 | 15 | ||
| @@ -42,8 +43,6 @@ pub const InitOptions = struct { | |||
| 42 | threading_mode: ThreadingMode = .Serialized, | 43 | threading_mode: ThreadingMode = .Serialized, |
| 43 | }; | 44 | }; |
| 44 | 45 | ||
| 45 | pub const Error = error{}; | ||
| 46 | |||
| 47 | /// DetailedError contains a SQLite error code and error message. | 46 | /// DetailedError contains a SQLite error code and error message. |
| 48 | pub const DetailedError = struct { | 47 | pub const DetailedError = struct { |
| 49 | code: usize, | 48 | code: usize, |
| @@ -128,7 +127,7 @@ pub const Db = struct { | |||
| 128 | var db: ?*c.sqlite3 = undefined; | 127 | var db: ?*c.sqlite3 = undefined; |
| 129 | const result = c.sqlite3_open_v2(path, &db, flags, null); | 128 | const result = c.sqlite3_open_v2(path, &db, flags, null); |
| 130 | if (result != c.SQLITE_OK or db == null) { | 129 | if (result != c.SQLITE_OK or db == null) { |
| 131 | return error.CannotOpenDatabase; | 130 | return errorFromResultCode(result); |
| 132 | } | 131 | } |
| 133 | 132 | ||
| 134 | self.db = db.?; | 133 | self.db = db.?; |
| @@ -141,7 +140,7 @@ pub const Db = struct { | |||
| 141 | var db: ?*c.sqlite3 = undefined; | 140 | var db: ?*c.sqlite3 = undefined; |
| 142 | const result = c.sqlite3_open_v2(":memory:", &db, flags, null); | 141 | const result = c.sqlite3_open_v2(":memory:", &db, flags, null); |
| 143 | if (result != c.SQLITE_OK or db == null) { | 142 | if (result != c.SQLITE_OK or db == null) { |
| 144 | return error.CannotOpenDatabase; | 143 | return errorFromResultCode(result); |
| 145 | } | 144 | } |
| 146 | 145 | ||
| 147 | self.db = db.?; | 146 | self.db = db.?; |
| @@ -292,7 +291,7 @@ pub fn Iterator(comptime Type: type) type { | |||
| 292 | return null; | 291 | return null; |
| 293 | } | 292 | } |
| 294 | if (result != c.SQLITE_ROW) { | 293 | if (result != c.SQLITE_ROW) { |
| 295 | return error.SQLiteStepError; | 294 | return errorFromResultCode(result); |
| 296 | } | 295 | } |
| 297 | 296 | ||
| 298 | const columns = c.sqlite3_column_count(self.stmt); | 297 | const columns = c.sqlite3_column_count(self.stmt); |
| @@ -332,7 +331,7 @@ pub fn Iterator(comptime Type: type) type { | |||
| 332 | return null; | 331 | return null; |
| 333 | } | 332 | } |
| 334 | if (result != c.SQLITE_ROW) { | 333 | if (result != c.SQLITE_ROW) { |
| 335 | return error.SQLiteStepError; | 334 | return errorFromResultCode(result); |
| 336 | } | 335 | } |
| 337 | 336 | ||
| 338 | const columns = c.sqlite3_column_count(self.stmt); | 337 | const columns = c.sqlite3_column_count(self.stmt); |
| @@ -649,7 +648,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 649 | null, | 648 | null, |
| 650 | ); | 649 | ); |
| 651 | if (result != c.SQLITE_OK) { | 650 | if (result != c.SQLITE_OK) { |
| 652 | return error.CannotPrepareStatement; | 651 | return errorFromResultCode(result); |
| 653 | } | 652 | } |
| 654 | break :blk tmp.?; | 653 | break :blk tmp.?; |
| 655 | }; | 654 | }; |
| @@ -754,7 +753,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t | |||
| 754 | const result = c.sqlite3_step(self.stmt); | 753 | const result = c.sqlite3_step(self.stmt); |
| 755 | switch (result) { | 754 | switch (result) { |
| 756 | c.SQLITE_DONE => {}, | 755 | c.SQLITE_DONE => {}, |
| 757 | c.SQLITE_BUSY => return error.SQLiteBusy, | 756 | c.SQLITE_BUSY => return errorFromResultCode(result), |
| 758 | else => std.debug.panic("invalid result {}", .{result}), | 757 | else => std.debug.panic("invalid result {}", .{result}), |
| 759 | } | 758 | } |
| 760 | } | 759 | } |
| @@ -1403,7 +1402,7 @@ test "sqlite: failing open" { | |||
| 1403 | .open_flags = .{}, | 1402 | .open_flags = .{}, |
| 1404 | .mode = .{ .File = "/tmp/not_existing.db" }, | 1403 | .mode = .{ .File = "/tmp/not_existing.db" }, |
| 1405 | }); | 1404 | }); |
| 1406 | testing.expectError(error.CannotOpenDatabase, res); | 1405 | testing.expectError(error.SQLiteCantOpen, res); |
| 1407 | } | 1406 | } |
| 1408 | 1407 | ||
| 1409 | test "sqlite: failing prepare statement" { | 1408 | test "sqlite: failing prepare statement" { |
| @@ -1411,7 +1410,7 @@ test "sqlite: failing prepare statement" { | |||
| 1411 | try db.init(initOptions()); | 1410 | try db.init(initOptions()); |
| 1412 | 1411 | ||
| 1413 | const result = db.prepare("SELECT id FROM foobar"); | 1412 | const result = db.prepare("SELECT id FROM foobar"); |
| 1414 | testing.expectError(error.CannotPrepareStatement, result); | 1413 | testing.expectError(error.SQLiteError, result); |
| 1415 | 1414 | ||
| 1416 | const detailed_err = db.getDetailedError(); | 1415 | const detailed_err = db.getDetailedError(); |
| 1417 | testing.expectEqual(@as(usize, 1), detailed_err.code); | 1416 | testing.expectEqual(@as(usize, 1), detailed_err.code); |