[PATCH hare-sqlite] Fix bind_str and add a test for it to demo
Export this patch
This requires my previous patch that updates the code to the latest
harec, at least I didn't test it on any earlier version.
---
cmd/demo/main.ha | 22 ++++++++++++++++++++--
sqlite/statement.ha | 9 +++++++--
2 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/cmd/demo/main.ha b/cmd/demo/main.ha
index e361a8b..1870487 100644
--- a/cmd/demo/main.ha
+++ b/cmd/demo/main.ha
@@ -8,7 +8,7 @@ export fn main() void = {
sqlite::exec(db, "CREATE TABLE users (name text, age integer, bank_account real)")!;
- sqlite::exec(db, "INSERT INTO users (name, age, bank_account) values ('Blain Smith', 40, 1233.54)")!;
+ insert_into(db);
let stmt = sqlite::prepare(db, "SELECT * FROM users")!;
@@ -42,4 +42,22 @@ export fn main() void = {
sqlite::finalize(stmt)!;
sqlite::close(db)!;
-};
\ No newline at end of file
+};
+
+fn insert_into(db: sqlite::handle) void = {
+ let stmt = sqlite::prepare(db, "INSERT INTO users (name, age, bank_account) values (?, ?, ?)")!;
+ 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));
+ };
+ };
+ sqlite::finalize(stmt)!;
+};
diff --git a/sqlite/statement.ha b/sqlite/statement.ha
index daff4aa..6470cef 100644
--- a/sqlite/statement.ha
+++ b/sqlite/statement.ha
@@ -70,12 +70,17 @@ 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, bytelen: int, callback: nullable *void) int;
+@symbol("sqlite3_bind_text") fn _bind_str(sqlite: nullable *void, col: int, val: const *c::char, bytelen: int, callback: nullable *fn(*void) 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) = {
- return wrapvoid(_bind_str(s.stmt, col, val, -1, null));
+ const val = c::fromstr(val);
+ return wrapvoid(_bind_str(s.stmt, col, val, -1, &freecstr: *fn(*void) void));
+};
+
+fn freecstr(cstr: *const c::char) void = {
+ free(cstr);
};
@symbol("sqlite3_bind_null") fn _bind_null(sqlite: nullable *void, col: int) int;
--
2.41.0