sqlite: quick exec with result (#361)

This commit is contained in:
Jacek Sieka 2021-05-27 11:31:34 +02:00 committed by GitHub
parent 68e6aadc29
commit 3514ee6484
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -226,6 +226,16 @@ proc exec*[Params: tuple](db: SqStoreRef,
if finalizeStatus != SQLITE_OK and result.isOk: if finalizeStatus != SQLITE_OK and result.isOk:
return err($sqlite3_errstr(finalizeStatus)) 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] = template exec*(db: SqStoreRef, stmt: string): KvResult[void] =
exec(db, stmt, ()) exec(db, stmt, ())

View File

@ -52,6 +52,15 @@ procSuite "SqStoreRef":
countRes.isOk and countRes.get == true countRes.isOk and countRes.get == true
totalRecords == 3 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( let selectRangeStmt = db.prepareStmt(
"SELECT value FROM records WHERE key >= ? and key < ?;", "SELECT value FROM records WHERE key >= ? and key < ?;",
(int64, int64), openarray[byte]).get (int64, int64), openarray[byte]).get