: 1 update for hare 0.24.2 5 files changed, 43 insertions(+), 71 deletions(-)
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~blainsmith/public-inbox/patches/55000/mbox | git am -3Learn more about email & git
From: wackbyte <wackbyte@protonmail.com> replace the "ok", "row", and "done" types with void, void, and done respectively. because no function returns both SQLITE_OK and SQLITE_ROW (sqlite3_step is the only one to return SQLITE_ROW and should never return SQLITE_OK), it's okay for both "ok" and "row" to become void. consolidate error and result codes into codes.ha and unify usage of "wrapint" and "wrapvoid" into "wrapcode". --- I refactored the result code handling a bit--if the design is unsatisfactory, I'd be happy to submit a follow-up patch! I think it works quite nicely. cmd/demo/main.ha | 57 ++++++++++++++-------------------- sqlite/{errors.ha => codes.ha} | 14 ++++----- sqlite/handle.ha | 7 ++--- sqlite/results.ha | 15 --------- sqlite/statement.ha | 21 ++++++------- 5 files changed, 43 insertions(+), 71 deletions(-) rename sqlite/{errors.ha => codes.ha} (96%) delete mode 100644 sqlite/results.ha diff --git a/cmd/demo/main.ha b/cmd/demo/main.ha index 1870487..9306257 100644 --- a/cmd/demo/main.ha +++ b/cmd/demo/main.ha @@ -12,31 +12,25 @@ export fn main() void = { let stmt = sqlite::prepare(db, "SELECT * FROM users")!; - for (true) { - match(sqlite::step(stmt)) { - case let res: sqlite::result => - if (res is sqlite::done) { - break; - }; - - fmt::printfln( - "{}[{}]: {}, {}[{}]: {}, {}[{}]: {}", - - sqlite::column_name(stmt, 0), - sqlite::columntypestr(sqlite::column_type(stmt, 0)), - sqlite::column_str(stmt, 0), - - sqlite::column_name(stmt, 1), - sqlite::columntypestr(sqlite::column_type(stmt, 1)), - sqlite::column_int(stmt, 1), - - sqlite::column_name(stmt, 2), - sqlite::columntypestr(sqlite::column_type(stmt, 2)), - sqlite::column_float(stmt, 2), - )!; - case let err: sqlite::error => - fmt::fatalf("step error {}", sqlite::strerror(err)); - }; + for (let res => sqlite::step(stmt)) match (res) { + case let err: sqlite::error => + fmt::fatalf("step error {}", sqlite::strerror(err)); + case void => + fmt::printfln( + "{}[{}]: {}, {}[{}]: {}, {}[{}]: {}", + + sqlite::column_name(stmt, 0), + sqlite::columntypestr(sqlite::column_type(stmt, 0)), + sqlite::column_str(stmt, 0), + + sqlite::column_name(stmt, 1), + sqlite::columntypestr(sqlite::column_type(stmt, 1)), + sqlite::column_int(stmt, 1), + + sqlite::column_name(stmt, 2), + sqlite::columntypestr(sqlite::column_type(stmt, 2)), + sqlite::column_float(stmt, 2), + )!; }; sqlite::finalize(stmt)!; @@ -49,15 +43,10 @@ fn insert_into(db: sqlite::handle) void = { sqlite::bind_str(stmt, 1, "Blain Smith")!; sqlite::bind_int(stmt, 2, 40)!; sqlite::bind_float(stmt, 3, 1233.54)!; - for (true) { - match(sqlite::step(stmt)) { - case let res: sqlite::result => - if (res is sqlite::done) { - break; - }; - case let err: sqlite::error => - fmt::fatalf("step error {}", sqlite::strerror(err)); - }; + for (let res => sqlite::step(stmt)) match (res) { + case let err: sqlite::error => + fmt::fatalf("step error {}", sqlite::strerror(err)); + case void => void; }; sqlite::finalize(stmt)!; }; diff --git a/sqlite/errors.ha b/sqlite/codes.ha similarity index 96% rename from sqlite/errors.ha rename to sqlite/codes.ha index 8695ba0..b5bde3b 100644 --- a/sqlite/errors.ha +++ b/sqlite/codes.ha @@ -1,3 +1,7 @@ +def SQLITE_OK: int = 0; +def SQLITE_ROW: int = 100; +def SQLITE_DONE: int = 101; + def SQLITE_ERROR: int = 1; def SQLITE_INTERNAL: int = 2; def SQLITE_PERM: int = 3; @@ -134,16 +138,12 @@ export fn strerror(err: error) str = { }; }; -fn wrapvoid(ret: int) (void | error) = { - wrapint(ret)?; -}; - -fn wrapint(ret: int) (result | error) = { +fn wrapcode(ret: int) (void | error | done) = { switch (ret) { case SQLITE_OK => - return ok; + return void; case SQLITE_ROW => - return row; + return void; case SQLITE_DONE => return done; case SQLITE_INTERNAL => diff --git a/sqlite/handle.ha b/sqlite/handle.ha index 7178534..3f89ae9 100644 --- a/sqlite/handle.ha +++ b/sqlite/handle.ha @@ -15,7 +15,7 @@ export fn open(filename: str) (handle | error) = { defer free(_filename); let db: nullable *opaque = null; - wrapvoid(_open(_filename, &db))?; + wrapcode(_open(_filename, &db))?; return handle { sqlite = db as *opaque }; }; @@ -27,7 +27,7 @@ export fn exec(h: handle, sql: str) (void | error) = { let _sql = c::fromstr(sql); defer free(_sql); - return wrapvoid(_exec(h.sqlite, _sql, null, null, null)); + wrapcode(_exec(h.sqlite, _sql, null, null, null))?; }; @symbol("sqlite3_close") fn _close(sqlite: nullable *opaque) int; @@ -35,8 +35,7 @@ export fn exec(h: handle, sql: str) (void | error) = { // // https://sqlite.org/c3ref/close.html export fn close(h: handle) (void | error) = { - wrapvoid(_close(h.sqlite))?; - return; + wrapcode(_close(h.sqlite))?; }; @symbol("sqlite3_libversion") fn _version() *const c::char; diff --git a/sqlite/results.ha b/sqlite/results.ha deleted file mode 100644 index 2927db2..0000000 --- a/sqlite/results.ha @@ -1,15 +0,0 @@ -def SQLITE_OK: int = 0; -def SQLITE_ROW: int = 100; -def SQLITE_DONE: int = 101; - -// indicates a successful execution -export type ok = void; - -// indicates another row is available in the [[statement]] from [[step]] -export type row = void; - -// indcates there are no more rows available in the [[statement]] from [[step]] -export type done = void; - -// any successful result -export type result = (ok | row | done); \ No newline at end of file diff --git a/sqlite/statement.ha b/sqlite/statement.ha index 5d42a26..6ce7499 100644 --- a/sqlite/statement.ha +++ b/sqlite/statement.ha @@ -48,7 +48,7 @@ export fn prepare(h: handle, sql: str) (statement | error) = { let stmt: nullable *opaque = null; - wrapvoid(_prepare(h.sqlite, _sql, -1, &stmt, null))?; + wrapcode(_prepare(h.sqlite, _sql, -1, &stmt, null))?; return statement { stmt = stmt as *opaque }; }; @@ -58,7 +58,7 @@ export fn prepare(h: handle, sql: str) (statement | error) = { // // https://www.sqlite.org/c3ref/bind_blob.html export fn bind_float(s: statement, col: int, val: f64) (void | error) = { - return wrapvoid(_bind_float(s.stmt, col, val)); + wrapcode(_bind_float(s.stmt, col, val))?; }; @symbol("sqlite3_bind_int") fn _bind_int(sqlite: nullable *opaque, col: int, val: int) int; @@ -66,7 +66,7 @@ export fn bind_float(s: statement, col: int, val: f64) (void | error) = { // // https://www.sqlite.org/c3ref/bind_blob.html export fn bind_int(s: statement, col: int, val: int) (void | error) = { - return wrapvoid(_bind_int(s.stmt, col, val)); + wrapcode(_bind_int(s.stmt, col, val))?; }; @symbol("sqlite3_bind_text") fn _bind_str(sqlite: nullable *opaque, col: int, val: const *c::char, bytelen: int, callback: nullable *fn(*opaque) void) int; @@ -75,7 +75,7 @@ export fn bind_int(s: statement, col: int, val: int) (void | error) = { // https://www.sqlite.org/c3ref/bind_blob.html export fn bind_str(s: statement, col: int, val: str) (void | error) = { const val = c::fromstr(val); - return wrapvoid(_bind_str(s.stmt, col, val, -1, &freecstr: *fn(*opaque) void)); + wrapcode(_bind_str(s.stmt, col, val, -1, &freecstr: *fn(*opaque) void))?; }; fn freecstr(cstr: *const c::char) void = { @@ -87,23 +87,23 @@ fn freecstr(cstr: *const c::char) void = { // // https://www.sqlite.org/c3ref/bind_blob.html export fn bind_null(s: statement, col: int) (void | error) = { - return wrapvoid(_bind_null(s.stmt, col)); + wrapcode(_bind_null(s.stmt, col))?; }; @symbol("sqlite3_step") fn _step(stmt: nullable *opaque) int; // steps through a [[statement]] to iterate through rows which wraps: // // https://sqlite.org/c3ref/step.html -export fn step(s: statement) (result | error) = { - return wrapint(_step(s.stmt)); +export fn step(s: statement) (void | error | done) = { + return wrapcode(_step(s.stmt)); }; @symbol("sqlite3_reset") fn _reset(stmt: nullable *opaque) int; // resets a [[statement]] which wraps: // // https://sqlite.org/c3ref/step.html -export fn reset(s: statement) (result | error) = { - return wrapint(_reset(s.stmt)); +export fn reset(s: statement) (void | error) = { + wrapcode(_reset(s.stmt))?; }; @symbol("sqlite3_finalize") fn _finalize(stmt: nullable *opaque) int; @@ -111,8 +111,7 @@ export fn reset(s: statement) (result | error) = { // // https://sqlite.org/c3ref/prepare.html export fn finalize(s: statement) (void | error) = { - wrapvoid(_finalize(s.stmt))?; - return; + wrapcode(_finalize(s.stmt))?; }; @symbol("sqlite3_column_name") fn _column_name(stmt: nullable *opaque, col: int) const *c::char; -- 2.46.0