use sqlite3 write-ahead logging

This commit is contained in:
Dustin Brody 2020-08-24 12:37:40 +02:00
parent 73c1bb817c
commit e2ccbbb418
No known key found for this signature in database
GPG Key ID: 3D7A11A0156519DC
1 changed files with 25 additions and 3 deletions

View File

@ -120,9 +120,7 @@ proc init*(
discard sqlite3_close(env)
s
template checkExec(q: string) =
let s = prepare(q): discard
template checkExec(s: ptr sqlite3_stmt) =
if (let x = sqlite3_step(s); x != SQLITE_DONE):
discard sqlite3_finalize(s)
discard sqlite3_close(env)
@ -132,9 +130,33 @@ proc init*(
discard sqlite3_close(env)
return err($sqlite3_errstr(x))
template checkExec(q: string) =
let s = prepare(q): discard
checkExec(s)
template checkWalPragmaResult(journalModePragma: ptr sqlite3_stmt) =
if (let x = sqlite3_step(journalModePragma); x != SQLITE_ROW):
discard sqlite3_finalize(journalModePragma)
discard sqlite3_close(env)
return err($sqlite3_errstr(x))
if (let x = sqlite3_column_type(journalModePragma, 0); x != SQLITE3_TEXT):
discard sqlite3_finalize(journalModePragma)
discard sqlite3_close(env)
return err($sqlite3_errstr(x))
if (let x = sqlite3_column_text(journalModePragma, 0); x != "wal"):
discard sqlite3_finalize(journalModePragma)
discard sqlite3_close(env)
return err("Invalid result from pramga")
# TODO: check current version and implement schema versioning
checkExec "PRAGMA user_version = 1;"
let journalModePragma = prepare("PRAGMA journal_mode = WAL;"): discard
checkWalPragmaResult(journalModePragma)
checkExec(journalModePragma)
checkExec """
CREATE TABLE IF NOT EXISTS kvstore(
key BLOB PRIMARY KEY,