A row type
type User { id: Int, name: Str }
query findUser(name: Str): User[dyn] {
SELECT id, name FROM users WHERE name = {name}
}
proc lookUp(db: hive.sql.SqlConnection, name: Str): Str {
if using db run findUser(name) is Result.Ok(rows) {
if rows bounds 0 {
return rows[0].name
}
}
return "?"
}
proc main(): void {
// A plain ":memory:" would give each of these eight connections a
// private, empty database of its own. The shared cache is what makes
// them all the same one.
opened := hive.sql.pool(hive.sql.DatabaseDriver.SQLite(), "file::memory:?cache=shared", 8, 1)
if opened is Result.Ok(db) {
using db run raw "CREATE TABLE users (id INTEGER, name TEXT)"
using db run raw "INSERT INTO users (id, name) VALUES (1, 'ada'), (2, 'grace')"
echo await [lookUp(db, "ada"), lookUp(db, "grace")]
hive.sql.close(db)
}
}