~blainsmith/public-inbox

hare-sqlite: free with defer in demo v1 APPLIED

: 1
 free with defer in demo

 1 files changed, 3 insertions(+), 5 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~blainsmith/public-inbox/patches/54995/mbox | git am -3
Learn more about email & git

[PATCH hare-sqlite] free with defer in demo Export this patch

From: wackbyte <wackbyte@protonmail.com>

---
 cmd/demo/main.ha | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/cmd/demo/main.ha b/cmd/demo/main.ha
index 1870487..9618355 100644
--- a/cmd/demo/main.ha
+++ b/cmd/demo/main.ha
@@ -5,12 +5,14 @@ use sqlite;

export fn main() void = {
	let db = sqlite::open("./my_app.db")!;
	defer sqlite::close(db)!;

	sqlite::exec(db, "CREATE TABLE users (name text, age integer, bank_account real)")!;

	insert_into(db);

	let stmt = sqlite::prepare(db, "SELECT * FROM users")!;
	defer sqlite::finalize(stmt)!;

	for (true) {
		match(sqlite::step(stmt)) {
@@ -38,14 +40,11 @@ export fn main() void = {
			fmt::fatalf("step error {}", sqlite::strerror(err));
		};
	};

	sqlite::finalize(stmt)!;

	sqlite::close(db)!;
};

fn insert_into(db: sqlite::handle) void = {
	let stmt = sqlite::prepare(db, "INSERT INTO users (name, age, bank_account) values (?, ?, ?)")!;
	defer sqlite::finalize(stmt)!;
	sqlite::bind_str(stmt, 1, "Blain Smith")!;
	sqlite::bind_int(stmt, 2, 40)!;
	sqlite::bind_float(stmt, 3, 1233.54)!;
@@ -59,5 +58,4 @@ fn insert_into(db: sqlite::handle) void = {
			fmt::fatalf("step error {}", sqlite::strerror(err));
		};
	};
	sqlite::finalize(stmt)!;
};
-- 
2.46.0