From 20af41a5ff58fa44b25557c15eddc7cfae6dd1a9 Mon Sep 17 00:00:00 2001 From: Dmitry Matveyev Date: Sun, 4 Jun 2023 16:58:24 +0600 Subject: [PATCH] Update for hare 2023-06-02 --- sqlite/handle.ha | 33 +++++++++++--------------- sqlite/statement.ha | 57 +++++++++++++++++++++------------------------ 2 files changed, 41 insertions(+), 49 deletions(-) diff --git a/sqlite/handle.ha b/sqlite/handle.ha index 5d0c544..2052450 100644 --- a/sqlite/handle.ha +++ b/sqlite/handle.ha @@ -1,32 +1,30 @@ use fmt; -use strings; +use types::c; // handle holds a reference to an underlying SQLite database export type handle = struct { sqlite: *void, }; -@symbol("sqlite3_open") fn _open(filename: const *char, sqlite: nullable **void) int; +@symbol("sqlite3_open") fn _open(filename: const *c::char, sqlite: *nullable *void) int; // opens a handle to a SQLite database file which wraps: // // https://sqlite.org/c3ref/open.html -export fn open(filename: str) (*handle | error) = { - let _filename = strings::to_c(filename); +export fn open(filename: str) (handle | error) = { + let _filename = c::fromstr(filename); defer free(_filename); - let h = alloc(handle { ... }); - - wrapvoid(_open(_filename, &h.sqlite))?; - - return h; + let db: nullable *void = null; + wrapvoid(_open(_filename, &db))?; + return handle { sqlite = db as *void }; }; -@symbol("sqlite3_exec") fn _exec(sqlite: nullable *void, sql: const *char, _: nullable *void, args: nullable *void, errs: nullable **char) int; +@symbol("sqlite3_exec") fn _exec(sqlite: nullable *void, sql: const *c::char, callback: nullable *void, args: nullable *void, errs: nullable **c::char) int; // executes a SQL statement which wraps: // // https://sqlite.org/c3ref/exec.html -export fn exec(h: *handle, sql: str) (void | error) = { - let _sql = strings::to_c(sql); +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)); @@ -36,18 +34,15 @@ export fn exec(h: *handle, sql: str) (void | error) = { // closes the SQLite database file and frees the [[handle]] which wraps: // // https://sqlite.org/c3ref/close.html -export fn close(h: *handle) (void | error) = { +export fn close(h: handle) (void | error) = { wrapvoid(_close(h.sqlite))?; - - free(h); - return; }; -@symbol("sqlite3_libversion") fn _version() *const char; +@symbol("sqlite3_libversion") fn _version() *const c::char; // returns the SQLite version of the libary used: // // https://sqlite.org/c3ref/libversion.html export fn version() const str = { - return strings::fromc(_version()); -}; \ No newline at end of file + return c::tostr(_version())!; +}; diff --git a/sqlite/statement.ha b/sqlite/statement.ha index 4a44201..daff4aa 100644 --- a/sqlite/statement.ha +++ b/sqlite/statement.ha @@ -1,7 +1,7 @@ use fmt; use hare::types; use slices; -use strings; +use types::c; def SQLITE_INTEGER: int = 1; def SQLITE_FLOAT: int = 2; @@ -39,26 +39,26 @@ export type statement = struct { stmt: *void, }; -@symbol("sqlite3_prepare_v2") fn _prepare(sqlite: nullable *void, sql: const *char, n: int, stmt: nullable *void, tail: nullable *void) int; +@symbol("sqlite3_prepare_v2") fn _prepare(sqlite: nullable *void, sql: const *c::char, n: int, stmt: nullable *void, tail: nullable *void) int; // prepares a new [[statement]] with the specified SQL statement which wraps: // // https://sqlite.org/c3ref/prepare.html -export fn prepare(h: *handle, sql: str) (*statement | error) = { - let _sql = strings::to_c(sql); +export fn prepare(h: handle, sql: str) (statement | error) = { + let _sql = c::fromstr(sql); defer free(_sql); - let stmt = alloc(statement { ... }); + let stmt: nullable *void = null; - wrapvoid(_prepare(h.sqlite, _sql, -1, &stmt.stmt, null))?; + wrapvoid(_prepare(h.sqlite, _sql, -1, &stmt, null))?; - return stmt; + return statement { stmt = stmt as *void }; }; @symbol("sqlite3_bind_double") fn _bind_float(sqlite: nullable *void, col: int, val: f64) int; // binds a `f64` to the parameter at `col` index which wraps: // // https://www.sqlite.org/c3ref/bind_blob.html -export fn bind_float(s: *statement, col: int, val: f64) (void | error) = { +export fn bind_float(s: statement, col: int, val: f64) (void | error) = { return wrapvoid(_bind_float(s.stmt, col, val)); }; @@ -66,15 +66,15 @@ export fn bind_float(s: *statement, col: int, val: f64) (void | error) = { // binds a `int` to the parameter at `col` index which wraps: // // https://www.sqlite.org/c3ref/bind_blob.html -export fn bind_int(s: *statement, col: int, val: int) (void | error) = { +export fn bind_int(s: statement, col: int, val: int) (void | error) = { return wrapvoid(_bind_int(s.stmt, col, val)); }; -@symbol("sqlite3_bind_text") fn _bind_str(sqlite: nullable *void, col: int, val: str, _: int, _: nullable *void) int; +@symbol("sqlite3_bind_text") fn _bind_str(sqlite: nullable *void, col: int, val: str, bytelen: int, callback: nullable *void) int; // binds a `str` to the parameter at `col` index which wraps: // // https://www.sqlite.org/c3ref/bind_blob.html -export fn bind_str(s: *statement, col: int, val: str) (void | error) = { +export fn bind_str(s: statement, col: int, val: str) (void | error) = { return wrapvoid(_bind_str(s.stmt, col, val, -1, null)); }; @@ -82,7 +82,7 @@ export fn bind_str(s: *statement, col: int, val: str) (void | error) = { // binds null to the parameter at `col` index which wraps: // // https://www.sqlite.org/c3ref/bind_blob.html -export fn bind_null(s: *statement, col: int) (void | error) = { +export fn bind_null(s: statement, col: int) (void | error) = { return wrapvoid(_bind_null(s.stmt, col)); }; @@ -90,7 +90,7 @@ export fn bind_null(s: *statement, col: int) (void | error) = { // steps through a [[statement]] to iterate through rows which wraps: // // https://sqlite.org/c3ref/step.html -export fn step(s: *statement) (result | error) = { +export fn step(s: statement) (result | error) = { return wrapint(_step(s.stmt)); }; @@ -98,7 +98,7 @@ export fn step(s: *statement) (result | error) = { // resets a [[statement]] which wraps: // // https://sqlite.org/c3ref/step.html -export fn reset(s: *statement) (result | error) = { +export fn reset(s: statement) (result | error) = { return wrapint(_reset(s.stmt)); }; @@ -106,27 +106,24 @@ export fn reset(s: *statement) (result | error) = { // finalizes a [[statement]] and frees [[statement]] to free the resourcs which wraps: // // https://sqlite.org/c3ref/prepare.html -export fn finalize(s: *statement) (void | error) = { +export fn finalize(s: statement) (void | error) = { wrapvoid(_finalize(s.stmt))?; - - free(s); - return; }; -@symbol("sqlite3_column_name") fn _column_name(stmt: nullable *void, col: int) const *char; +@symbol("sqlite3_column_name") fn _column_name(stmt: nullable *void, col: int) const *c::char; // gets the column's name at position `col` for the [[statement]] which wraps: // // https://sqlite.org/c3ref/column_name.html -export fn column_name(s: *statement, col: int) str = { - return strings::fromc(_column_name(s.stmt, col)); +export fn column_name(s: statement, col: int) str = { + return c::tostr(_column_name(s.stmt, col))!; }; @symbol("sqlite3_column_type") fn _column_type(stmt: nullable *void, col: int) int; // gets the column's type at position `col` for the [[statement]] which wraps: // // https://sqlite.org/c3ref/column_type.html -export fn column_type(s: *statement, col: int) columntype = { +export fn column_type(s: statement, col: int) columntype = { switch (_column_type(s.stmt, col)) { case SQLITE_INTEGER => return columntype::INTEGER; @@ -145,7 +142,7 @@ export fn column_type(s: *statement, col: int) columntype = { // return an int for position `col` of type `INTEGER` for the [[statement]] which wraps: // // https://sqlite.org/c3ref/column_blob.html -export fn column_int(s: *statement, col: int) int = { +export fn column_int(s: statement, col: int) int = { return _column_int(s.stmt, col); }; @@ -153,32 +150,32 @@ export fn column_int(s: *statement, col: int) int = { // return an f64 for position `col` of type `REAL` for the [[statement]] which wraps: // // https://sqlite.org/c3ref/column_blob.html -export fn column_float(s: *statement, col: int) f64 = { +export fn column_float(s: statement, col: int) f64 = { return _column_float(s.stmt, col); }; -@symbol("sqlite3_column_text") fn _column_str(stmt: nullable *void, col: int) const *char; +@symbol("sqlite3_column_text") fn _column_str(stmt: nullable *void, col: int) const *c::char; // return a str for position `col` of type `TEXT` for the [[statement]] which wraps: // // https://sqlite.org/c3ref/column_blob.html -export fn column_str(s: *statement, col: int) str = { - return strings::fromc(_column_str(s.stmt, col)); +export fn column_str(s: statement, col: int) str = { + return c::tostr(_column_str(s.stmt, col))!; }; @symbol("sqlite3_column_blob") fn _column_blob(stmt: nullable *void, col: int) const *void; // return a *void for position `col` of type `BLOB` for the [[statement]] which wraps: // // https://sqlite.org/c3ref/column_blob.html -export fn column_blob(s: *statement, col: int) nullable *void = _column_blob(s.stmt, col); +export fn column_blob(s: statement, col: int) nullable *void = _column_blob(s.stmt, col); @symbol("sqlite3_column_bytes") fn _column_bytes(stmt: nullable *void, col: int) int; // return the number of bytes as a size for position `col` for the [[statement]] which wraps: // // https://sqlite.org/c3ref/column_blob.html -export fn column_bytes(s: *statement, col: int) size = _column_bytes(s.stmt, col): size; +export fn column_bytes(s: statement, col: int) size = _column_bytes(s.stmt, col): size; // a convience function to return a []u8 for position `col` of type `BLOB` for the [[statement]] -export fn column_byte_slice(s: *statement, col: int) []u8 = { +export fn column_byte_slice(s: statement, col: int) []u8 = { let blob = column_blob(s, col): *[*]u8; let sz = column_bytes(s, col); -- 2.45.2