Commit Diff


commit - 59c79ed2e1020c14167f892d17bba03345630770
commit + 19e3939cb58be9fc3d590cfe91946c3455a3a6c7
blob - 5050aebb38e81125474670fc53b7013b80a1bf4b
blob + aae128f2e8a2b83ee5bd1d155da99fd91113fff8
--- sqlite3/sqlite3.ha
+++ sqlite3/sqlite3.ha
@@ -61,19 +61,36 @@ export def def_open_flags = OPEN_READWRITE | OPEN_CREA
 	vfs: nullable *const c::char,
 ) int;
 
-// XXX vfs support?
+// Opens an SQLite database file as specified by the filename
+// argument.  If no flags are provided, [[def_open_flags]] is assumed.
+// The OPEN_WAL flag is "abused" to implicitly enable the WAL journal
+// mode if specified, as it's not a valid open flag otherwise.
 export fn open(filename: const str, flags: int...) (conn | error) = {
+	// XXX VFS support?
+
 	let f = if (len(flags) == 0) def_open_flags else flags[0];
 
+	let use_wal: bool = (f & OPEN_WAL) != 0;
+	f &= ~OPEN_WAL;
+
 	let path = c::fromstr(filename);
 	defer free(path);
 
 	let addr: uintptr = 0;
 	let ret = libsqlite3_open_v2(path, &addr: *opaque, f, null);
-	if (ret == sqlite_ok) {
-		return (addr: *opaque): conn;
+	if (ret != sqlite_ok) {
+		return code2err(ret);
 	};
-	return code2err(ret);
+
+	let conn = addr: *opaque: conn;
+
+	if (use_wal) {
+		let stmt = prepare(conn, "PRAGMA journal_mode=wal;")?;
+		defer finalize(stmt)!; // cannot propagate error in defer
+		runstmt(stmt)?;
+	};
+
+	return conn;
 };
 
 @symbol("sqlite3_close_v2") fn libsqlite3_close_v2(sqlite3: *opaque) int;