mirror of https://github.com/status-im/nim-eth.git
sqlite: separate contains query
Using `select 1` for contains query tells sqlite not to actually load the data for simple contains queries
This commit is contained in:
parent
17586c05d7
commit
f730557baa
|
@ -12,7 +12,7 @@ export kvstore
|
|||
type
|
||||
SqStoreRef* = ref object of RootObj
|
||||
env: ptr sqlite3
|
||||
selectStmt, insertStmt, deleteStmt: ptr sqlite3_stmt
|
||||
getStmt, putStmt, delStmt, containsStmt: ptr sqlite3_stmt
|
||||
|
||||
template checkErr(op, cleanup: untyped) =
|
||||
if (let v = (op); v != SQLITE_OK):
|
||||
|
@ -26,16 +26,16 @@ proc bindBlob(s: ptr sqlite3_stmt, n: int, blob: openarray[byte]): cint =
|
|||
sqlite3_bind_blob(s, n.cint, unsafeAddr blob[0], blob.len.cint, nil)
|
||||
|
||||
proc get*(db: SqStoreRef, key: openarray[byte], onData: DataProc): KvResult[bool] =
|
||||
checkErr sqlite3_reset(db.selectStmt)
|
||||
checkErr sqlite3_clear_bindings(db.selectStmt)
|
||||
checkErr bindBlob(db.selectStmt, 1, key)
|
||||
checkErr sqlite3_reset(db.getStmt)
|
||||
checkErr sqlite3_clear_bindings(db.getStmt)
|
||||
checkErr bindBlob(db.getStmt, 1, key)
|
||||
|
||||
let v = sqlite3_step(db.selectStmt)
|
||||
let v = sqlite3_step(db.getStmt)
|
||||
case v
|
||||
of SQLITE_ROW:
|
||||
let
|
||||
p = cast[ptr UncheckedArray[byte]](sqlite3_column_blob(db.selectStmt, 0))
|
||||
l = sqlite3_column_bytes(db.selectStmt, 0)
|
||||
p = cast[ptr UncheckedArray[byte]](sqlite3_column_blob(db.getStmt, 0))
|
||||
l = sqlite3_column_bytes(db.getStmt, 0)
|
||||
onData(toOpenArray(p, 0, l-1))
|
||||
ok(true)
|
||||
of SQLITE_DONE:
|
||||
|
@ -44,44 +44,45 @@ proc get*(db: SqStoreRef, key: openarray[byte], onData: DataProc): KvResult[bool
|
|||
err($sqlite3_errstr(v))
|
||||
|
||||
proc put*(db: SqStoreRef, key, value: openarray[byte]): KvResult[void] =
|
||||
checkErr sqlite3_reset(db.insertStmt)
|
||||
checkErr sqlite3_clear_bindings(db.insertStmt)
|
||||
checkErr sqlite3_reset(db.putStmt)
|
||||
checkErr sqlite3_clear_bindings(db.putStmt)
|
||||
|
||||
checkErr bindBlob(db.insertStmt, 1, key)
|
||||
checkErr bindBlob(db.insertStmt, 2, value)
|
||||
checkErr bindBlob(db.putStmt, 1, key)
|
||||
checkErr bindBlob(db.putStmt, 2, value)
|
||||
|
||||
if (let v = sqlite3_step(db.insertStmt); v != SQLITE_DONE):
|
||||
if (let v = sqlite3_step(db.putStmt); v != SQLITE_DONE):
|
||||
err($sqlite3_errstr(v))
|
||||
else:
|
||||
ok()
|
||||
|
||||
proc contains*(db: SqStoreRef, key: openarray[byte]): KvResult[bool] =
|
||||
checkErr sqlite3_reset(db.selectStmt)
|
||||
checkErr sqlite3_clear_bindings(db.selectStmt)
|
||||
checkErr sqlite3_reset(db.containsStmt)
|
||||
checkErr sqlite3_clear_bindings(db.containsStmt)
|
||||
|
||||
checkErr bindBlob(db.selectStmt, 1, key)
|
||||
checkErr bindBlob(db.containsStmt, 1, key)
|
||||
|
||||
let v = sqlite3_step(db.selectStmt)
|
||||
let v = sqlite3_step(db.containsStmt)
|
||||
case v
|
||||
of SQLITE_ROW: ok(true)
|
||||
of SQLITE_DONE: ok(false)
|
||||
else: err($sqlite3_errstr(v))
|
||||
|
||||
proc del*(db: SqStoreRef, key: openarray[byte]): KvResult[void] =
|
||||
checkErr sqlite3_reset(db.deleteStmt)
|
||||
checkErr sqlite3_clear_bindings(db.deleteStmt)
|
||||
checkErr sqlite3_reset(db.delStmt)
|
||||
checkErr sqlite3_clear_bindings(db.delStmt)
|
||||
|
||||
checkErr bindBlob(db.deleteStmt, 1, key)
|
||||
checkErr bindBlob(db.delStmt, 1, key)
|
||||
|
||||
if (let v = sqlite3_step(db.deleteStmt); v != SQLITE_DONE):
|
||||
if (let v = sqlite3_step(db.delStmt); v != SQLITE_DONE):
|
||||
err($sqlite3_errstr(v))
|
||||
else:
|
||||
ok()
|
||||
|
||||
proc close*(db: SqStoreRef) =
|
||||
discard sqlite3_finalize(db.insertStmt)
|
||||
discard sqlite3_finalize(db.selectStmt)
|
||||
discard sqlite3_finalize(db.deleteStmt)
|
||||
discard sqlite3_finalize(db.putStmt)
|
||||
discard sqlite3_finalize(db.getStmt)
|
||||
discard sqlite3_finalize(db.delStmt)
|
||||
discard sqlite3_finalize(db.containsStmt)
|
||||
|
||||
discard sqlite3_close(db.env)
|
||||
|
||||
|
@ -142,17 +143,22 @@ proc init*(
|
|||
"""
|
||||
|
||||
let
|
||||
selectStmt = prepare "SELECT value FROM kvstore WHERE key = ?;":
|
||||
getStmt = prepare "SELECT value FROM kvstore WHERE key = ?;":
|
||||
discard
|
||||
insertStmt = prepare "INSERT OR REPLACE INTO kvstore(key, value) VALUES (?, ?);":
|
||||
discard sqlite3_finalize(selectStmt)
|
||||
deleteStmt = prepare "DELETE FROM kvstore WHERE key = ?;":
|
||||
discard sqlite3_finalize(selectStmt)
|
||||
discard sqlite3_finalize(insertStmt)
|
||||
putStmt = prepare "INSERT OR REPLACE INTO kvstore(key, value) VALUES (?, ?);":
|
||||
discard sqlite3_finalize(getStmt)
|
||||
delStmt = prepare "DELETE FROM kvstore WHERE key = ?;":
|
||||
discard sqlite3_finalize(getStmt)
|
||||
discard sqlite3_finalize(putStmt)
|
||||
containsStmt = prepare "SELECT 1 FROM kvstore WHERE key = ?;":
|
||||
discard sqlite3_finalize(getStmt)
|
||||
discard sqlite3_finalize(putStmt)
|
||||
discard sqlite3_finalize(delStmt)
|
||||
|
||||
ok(SqStoreRef(
|
||||
env: env,
|
||||
selectStmt: selectStmt,
|
||||
insertStmt: insertStmt,
|
||||
deleteStmt: deleteStmt
|
||||
getStmt: getStmt,
|
||||
putStmt: putStmt,
|
||||
delStmt: delStmt,
|
||||
containsStmt: containsStmt
|
||||
))
|
||||
|
|
Loading…
Reference in New Issue