summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vincent Rischmann2021-10-23 17:09:45 +0200
committerGravatar Vincent Rischmann2021-10-23 18:32:34 +0200
commitc96ed59924bf21d753ef513ccbfda0c8af35029d (patch)
tree006afceccb72050dbdfd0c6cf32f27c741af9ced
parentci: remove the debian amd64 build (diff)
downloadzig-sqlite-c96ed59924bf21d753ef513ccbfda0c8af35029d.tar.gz
zig-sqlite-c96ed59924bf21d753ef513ccbfda0c8af35029d.tar.xz
zig-sqlite-c96ed59924bf21d753ef513ccbfda0c8af35029d.zip
use explicit error sets everywhere
-rw-r--r--sqlite.zig36
1 files changed, 25 insertions, 11 deletions
diff --git a/sqlite.zig b/sqlite.zig
index 4bbc650..b1a903e 100644
--- a/sqlite.zig
+++ b/sqlite.zig
@@ -72,7 +72,7 @@ pub const Blob = struct {
72 size: c_int = 0, 72 size: c_int = 0,
73 73
74 /// close closes the blob. 74 /// close closes the blob.
75 pub fn close(self: *Self) !void { 75 pub fn close(self: *Self) Error!void {
76 const result = c.sqlite3_blob_close(self.handle); 76 const result = c.sqlite3_blob_close(self.handle);
77 if (result != c.SQLITE_OK) { 77 if (result != c.SQLITE_OK) {
78 return errors.errorFromResultCode(result); 78 return errors.errorFromResultCode(result);
@@ -139,10 +139,14 @@ pub const Blob = struct {
139 self.offset = 0; 139 self.offset = 0;
140 } 140 }
141 141
142 pub const ReopenError = error{
143 CannotReopenBlob,
144 };
145
142 /// reopen moves this blob to another row of the same table. 146 /// reopen moves this blob to another row of the same table.
143 /// 147 ///
144 /// See https://sqlite.org/c3ref/blob_reopen.html. 148 /// See https://sqlite.org/c3ref/blob_reopen.html.
145 pub fn reopen(self: *Self, row: i64) !void { 149 pub fn reopen(self: *Self, row: i64) ReopenError!void {
146 const result = c.sqlite3_blob_reopen(self.handle, row); 150 const result = c.sqlite3_blob_reopen(self.handle, row);
147 if (result != c.SQLITE_OK) { 151 if (result != c.SQLITE_OK) {
148 return error.CannotReopenBlob; 152 return error.CannotReopenBlob;
@@ -152,8 +156,12 @@ pub const Blob = struct {
152 self.offset = 0; 156 self.offset = 0;
153 } 157 }
154 158
159 pub const OpenError = error{
160 CannotOpenBlob,
161 };
162
155 /// open opens a blob for incremental i/o. 163 /// open opens a blob for incremental i/o.
156 fn open(db: *c.sqlite3, db_name: DatabaseName, table: [:0]const u8, column: [:0]const u8, row: i64, comptime flags: OpenFlags) !Blob { 164 fn open(db: *c.sqlite3, db_name: DatabaseName, table: [:0]const u8, column: [:0]const u8, row: i64, comptime flags: OpenFlags) OpenError!Blob {
157 comptime if (!flags.read and !flags.write) { 165 comptime if (!flags.read and !flags.write) {
158 @compileError("must open a blob for either read, write or both"); 166 @compileError("must open a blob for either read, write or both");
159 }; 167 };
@@ -318,8 +326,12 @@ pub const Db = struct {
318 create: bool = false, 326 create: bool = false,
319 }; 327 };
320 328
329 pub const InitError = error{
330 SQLiteBuildNotThreadSafe,
331 } || Error;
332
321 /// init creates a database with the provided options. 333 /// init creates a database with the provided options.
322 pub fn init(options: InitOptions) !Self { 334 pub fn init(options: InitOptions) InitError!Self {
323 var dummy_diags = Diagnostics{}; 335 var dummy_diags = Diagnostics{};
324 var diags = options.diags orelse &dummy_diags; 336 var diags = options.diags orelse &dummy_diags;
325 337
@@ -484,7 +496,7 @@ pub const Db = struct {
484 } 496 }
485 497
486 /// prepareWithDiags is like `prepare` but takes an additional options argument. 498 /// prepareWithDiags is like `prepare` but takes an additional options argument.
487 pub fn prepareWithDiags(self: *Self, comptime query: []const u8, options: QueryOptions) !blk: { 499 pub fn prepareWithDiags(self: *Self, comptime query: []const u8, options: QueryOptions) DynamicStatement.PrepareError!blk: {
488 @setEvalBranchQuota(100000); 500 @setEvalBranchQuota(100000);
489 break :blk StatementType(.{}, query); 501 break :blk StatementType(.{}, query);
490 } { 502 } {
@@ -494,7 +506,7 @@ pub const Db = struct {
494 } 506 }
495 507
496 /// prepareDynamicWithDiags is like `prepareDynamic` but takes an additional options argument. 508 /// prepareDynamicWithDiags is like `prepareDynamic` but takes an additional options argument.
497 pub fn prepareDynamicWithDiags(self: *Self, query: []const u8, options: QueryOptions) !DynamicStatement { 509 pub fn prepareDynamicWithDiags(self: *Self, query: []const u8, options: QueryOptions) DynamicStatement.PrepareError!DynamicStatement {
498 return try DynamicStatement.prepare(self, query, options, 0); 510 return try DynamicStatement.prepare(self, query, options, 0);
499 } 511 }
500 512
@@ -512,7 +524,7 @@ pub const Db = struct {
512 /// This is done because we type check the bind parameters when executing the statement later. 524 /// This is done because we type check the bind parameters when executing the statement later.
513 /// 525 ///
514 /// If you want additional error information in case of failures, use `prepareWithDiags`. 526 /// If you want additional error information in case of failures, use `prepareWithDiags`.
515 pub fn prepare(self: *Self, comptime query: []const u8) !blk: { 527 pub fn prepare(self: *Self, comptime query: []const u8) DynamicStatement.PrepareError!blk: {
516 @setEvalBranchQuota(100000); 528 @setEvalBranchQuota(100000);
517 break :blk StatementType(.{}, query); 529 break :blk StatementType(.{}, query);
518 } { 530 } {
@@ -527,7 +539,7 @@ pub const Db = struct {
527 /// That means such statements does not support comptime type-checking. 539 /// That means such statements does not support comptime type-checking.
528 /// 540 ///
529 /// Dynamic statement supports host parameter names. See `DynamicStatement`. 541 /// Dynamic statement supports host parameter names. See `DynamicStatement`.
530 pub fn prepareDynamic(self: *Self, query: []const u8) !DynamicStatement { 542 pub fn prepareDynamic(self: *Self, query: []const u8) DynamicStatement.PrepareError!DynamicStatement {
531 return try self.prepareDynamicWithDiags(query, .{}); 543 return try self.prepareDynamicWithDiags(query, .{});
532 } 544 }
533 545
@@ -562,7 +574,7 @@ pub const Db = struct {
562 /// 574 ///
563 /// See https://sqlite.org/c3ref/blob_open.html for more details on incremental i/o. 575 /// See https://sqlite.org/c3ref/blob_open.html for more details on incremental i/o.
564 /// 576 ///
565 pub fn openBlob(self: *Self, db_name: Blob.DatabaseName, table: [:0]const u8, column: [:0]const u8, row: i64, comptime flags: Blob.OpenFlags) !Blob { 577 pub fn openBlob(self: *Self, db_name: Blob.DatabaseName, table: [:0]const u8, column: [:0]const u8, row: i64, comptime flags: Blob.OpenFlags) Blob.OpenError!Blob {
566 return Blob.open(self.db, db_name, table, column, row, flags); 578 return Blob.open(self.db, db_name, table, column, row, flags);
567 } 579 }
568}; 580};
@@ -1080,7 +1092,9 @@ pub const DynamicStatement = struct {
1080 1092
1081 const Self = @This(); 1093 const Self = @This();
1082 1094
1083 fn prepare(db: *Db, queryStr: []const u8, options: QueryOptions, flags: c_uint) !Self { 1095 pub const PrepareError = error{} || Error;
1096
1097 fn prepare(db: *Db, queryStr: []const u8, options: QueryOptions, flags: c_uint) PrepareError!Self {
1084 var dummy_diags = Diagnostics{}; 1098 var dummy_diags = Diagnostics{};
1085 var diags = options.diags orelse &dummy_diags; 1099 var diags = options.diags orelse &dummy_diags;
1086 var stmt = blk: { 1100 var stmt = blk: {
@@ -1441,7 +1455,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: ParsedQuery) t
1441 1455
1442 dynamic_stmt: DynamicStatement, 1456 dynamic_stmt: DynamicStatement,
1443 1457
1444 fn prepare(db: *Db, options: QueryOptions, flags: c_uint) !Self { 1458 fn prepare(db: *Db, options: QueryOptions, flags: c_uint) DynamicStatement.PrepareError!Self {
1445 return Self{ 1459 return Self{
1446 .dynamic_stmt = try DynamicStatement.prepare(db, query.getQuery(), options, flags), 1460 .dynamic_stmt = try DynamicStatement.prepare(db, query.getQuery(), options, flags),
1447 }; 1461 };