From e0a63dba62bfc11dc901e6f0eee18e4d38ecfdd3 Mon Sep 17 00:00:00 2001 From: Rodrigo Abt Date: Mon, 23 Dec 2024 11:06:36 -0300 Subject: [PATCH] db.sqlite: add an `exec_map` function, returning `[]map[string]string`, given an SQL query (#23246) --- vlib/db/sqlite/sqlite.c.v | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/vlib/db/sqlite/sqlite.c.v b/vlib/db/sqlite/sqlite.c.v index ab118cd7d40a83..b873a020ce2619 100644 --- a/vlib/db/sqlite/sqlite.c.v +++ b/vlib/db/sqlite/sqlite.c.v @@ -212,6 +212,42 @@ pub fn (db &DB) q_string(query string) !string { return if val != &u8(0) { unsafe { tos_clone(val) } } else { '' } } +// exec_map executes the query on the given `db`, and returns an array of maps of strings, or an error on failure +@[manualfree] +pub fn (db &DB) exec_map(query string) ![]map[string]string { + stmt := &C.sqlite3_stmt(unsafe { nil }) + defer { + C.sqlite3_finalize(stmt) + } + mut code := C.sqlite3_prepare_v2(db.conn, &char(query.str), query.len, &stmt, 0) + if code != sqlite_ok { + return db.error_message(code, query) + } + + nr_cols := C.sqlite3_column_count(stmt) + mut res := 0 + mut rows := []map[string]string{} + for { + res = C.sqlite3_step(stmt) + if res != 100 { + break + } + mut row := map[string]string{} + for i in 0 .. nr_cols { + val := unsafe { &u8(C.sqlite3_column_text(stmt, i)) } + col_char := unsafe { &u8(C.sqlite3_column_name(stmt, i)) } + col := unsafe { tos_clone(col_char) } + if val == &u8(0) { + row[col] = '' + } else { + row[col] = unsafe { tos_clone(val) } + } + } + rows << row + } + return rows +} + // exec executes the query on the given `db`, and returns an array of all the results, or an error on failure @[manualfree] pub fn (db &DB) exec(query string) ![]Row {