Allow Sqlite keystores to be used in read-only mode

This is useful for tools such as `ncli_db` that can work with the database
of a running Nimbus instances.
This commit is contained in:
Zahary Karadjov 2021-11-16 13:45:28 +02:00
parent 73d9bf4c80
commit fe1bb4c4e7
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
1 changed files with 7 additions and 5 deletions

View File

@ -29,6 +29,7 @@ type
# can be created # can be created
env: Sqlite env: Sqlite
managedStmts: seq[RawStmtPtr] managedStmts: seq[RawStmtPtr]
readOnly*: bool
SqStoreCheckpointKind* {.pure.} = enum SqStoreCheckpointKind* {.pure.} = enum
passive, full, restart, truncate passive, full, restart, truncate
@ -509,6 +510,7 @@ proc init*(
ok(SqStoreRef( ok(SqStoreRef(
env: env.release, env: env.release,
readOnly: readOnly
)) ))
proc openKvStore*(db: SqStoreRef, name = "kvstore", withoutRowid = false): KvResult[SqKeyspaceRef] = proc openKvStore*(db: SqStoreRef, name = "kvstore", withoutRowid = false): KvResult[SqKeyspaceRef] =
@ -519,15 +521,15 @@ proc openKvStore*(db: SqStoreRef, name = "kvstore", withoutRowid = false): KvRes
## rows (the row being the sum of key and value) - see ## rows (the row being the sum of key and value) - see
## https://www.sqlite.org/withoutrowid.html ## https://www.sqlite.org/withoutrowid.html
## ##
let
createSql = """ if not db.readOnly:
let createSql = """
CREATE TABLE IF NOT EXISTS """ & name & """ ( CREATE TABLE IF NOT EXISTS """ & name & """ (
key BLOB PRIMARY KEY, key BLOB PRIMARY KEY,
value BLOB value BLOB
)""" )"""
checkExec db.env,
checkExec db.env, if withoutRowid: createSql & " WITHOUT ROWID;" else: createSql & ";"
if withoutRowid: createSql & " WITHOUT ROWID;" else: createSql & ";"
var var
tmp: SqKeyspace tmp: SqKeyspace