mirror of https://github.com/status-im/nim-eth.git
sqlite: quick exec with result (#361)
This commit is contained in:
parent
68e6aadc29
commit
3514ee6484
|
@ -226,6 +226,16 @@ proc exec*[Params: tuple](db: SqStoreRef,
|
|||
if finalizeStatus != SQLITE_OK and result.isOk:
|
||||
return err($sqlite3_errstr(finalizeStatus))
|
||||
|
||||
proc exec*[Params: tuple, Res](db: SqStoreRef,
|
||||
stmt: string,
|
||||
params: Params,
|
||||
onData: ResultHandler[Res]): KvResult[bool] =
|
||||
let stmt = ? db.prepareStmt(stmt, Params, Res, managed = false)
|
||||
result = exec(stmt, params, onData)
|
||||
let finalizeStatus = sqlite3_finalize(RawStmtPtr stmt)
|
||||
if finalizeStatus != SQLITE_OK and result.isOk:
|
||||
return err($sqlite3_errstr(finalizeStatus))
|
||||
|
||||
template exec*(db: SqStoreRef, stmt: string): KvResult[void] =
|
||||
exec(db, stmt, ())
|
||||
|
||||
|
|
|
@ -52,6 +52,15 @@ procSuite "SqStoreRef":
|
|||
countRes.isOk and countRes.get == true
|
||||
totalRecords == 3
|
||||
|
||||
# Without prepare..
|
||||
totalRecords = 0
|
||||
check:
|
||||
(db.exec("SELECT COUNT(*) FROM records;", ()) do (res: int64):
|
||||
totalRecords = int res).get()
|
||||
|
||||
check:
|
||||
totalRecords == 3
|
||||
|
||||
let selectRangeStmt = db.prepareStmt(
|
||||
"SELECT value FROM records WHERE key >= ? and key < ?;",
|
||||
(int64, int64), openarray[byte]).get
|
||||
|
|
Loading…
Reference in New Issue