Skip to content

Commit

Permalink
kvstore: simplified stmt exec iterator (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnetheduck authored Jan 28, 2022
1 parent 5791afc commit ce4acc1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
42 changes: 42 additions & 0 deletions eth/db/kvstore_sqlite3.nim
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,48 @@ proc exec*[Params, Res](s: SqliteStmt[Params, Res],
discard sqlite3_reset(s) # same return information as step
discard sqlite3_clear_bindings(s) # no errors possible

iterator exec*[Params, Res](s: SqliteStmt[Params, Res],
params: Params, item: var Res): KvResult[void] =
let s = RawStmtPtr s

# we use a mutable `res` variable here to avoid the code bloat that multiple
# `yield` statements cause when inlining the loop body
var res = KvResult[void].ok()
when params is tuple:
var i = 1
for param in fields(params):
if (let v = bindParam(s, i, param); v != SQLITE_OK):
res = KvResult[void].err($sqlite3_errstr(v))
break

inc i
else:
if (let v = bindParam(s, 1, params); v != SQLITE_OK):
res = KvResult[void].err($sqlite3_errstr(v))

defer:
# release implicit transaction
discard sqlite3_reset(s) # same return information as step
discard sqlite3_clear_bindings(s) # no errors possible

while res.isOk():
let v = sqlite3_step(s)
case v
of SQLITE_ROW:
item = readResult(s, Res)
yield KvResult[void].ok()
of SQLITE_DONE:
break
else:
res = KvResult[void].err($sqlite3_errstr(v))

if not res.isOk():
yield res

iterator exec*[Res](s: SqliteStmt[NoParams, Res], item: var Res): KvResult[void] =
for r in exec(s, (), item):
yield r

template exec*(s: SqliteStmt[NoParams, void]): KvResult[void] =
exec(s, ())

Expand Down
8 changes: 8 additions & 0 deletions tests/db/test_kvstore_sqlite3.nim
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,11 @@ procSuite "SqStoreRef":
check:
selectRes.isOk and selectRes.get == true
abc == val

var found = false
var row: selectStmt.Result
for rowRes in selectStmt.exec(row):
rowRes.expect("working db")
check abc == row
found = true
check found

0 comments on commit ce4acc1

Please sign in to comment.