diff options
| author | 2020-12-31 22:04:08 +0100 | |
|---|---|---|
| committer | 2020-12-31 22:06:15 +0100 | |
| commit | 188373fd0a0031c159935444afd601ee4218de1b (patch) | |
| tree | c4c8699b30ff4601918ecab97e8dd099e4b69565 /sqlite.zig | |
| parent | Merge pull request #9 from vrischmann/split-alloc (diff) | |
| download | zig-sqlite-188373fd0a0031c159935444afd601ee4218de1b.tar.gz zig-sqlite-188373fd0a0031c159935444afd601ee4218de1b.tar.xz zig-sqlite-188373fd0a0031c159935444afd601ee4218de1b.zip | |
add proper error types
* Add SQLiteError and various SQLiteExtendedXYZError
* Replace old errors with the SQLite ones where appropriate
Fixes #8
Diffstat (limited to 'sqlite.zig')
| -rw-r--r-- | sqlite.zig | 15 |
1 files changed, 7 insertions, 8 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 | } |