This commit is contained in:
Jaremy Creechley 2023-09-20 22:43:56 -07:00
parent 9362fcbb07
commit 8b494907c6
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
4 changed files with 34 additions and 38 deletions

View File

@ -47,7 +47,7 @@ proc delete*(self: SQLiteDatastore, keys: openArray[DbKey]): ?!void =
return failure(err)
for key in keys:
if err =? self.db.deleteStmt.exec((key.id)).errorOption:
if err =? self.db.deleteStmt.exec((key)).errorOption:
if err =? self.db.rollbackStmt.exec().errorOption:
return failure err.msg

View File

@ -33,7 +33,7 @@ type
containsStmt*: ContainsStmt
deleteStmt*: DeleteStmt
env*: SQLite
getDataCol*: BoundDataCol
getDataCol*: (RawStmtPtr, int)
getStmt*: GetStmt
putStmt*: PutStmt
beginStmt*: BeginStmt
@ -158,38 +158,38 @@ proc idCol*(
return proc (): string =
$sqlite3_column_text_not_null(s, index.cint)
proc dataCol*(
s: RawStmtPtr,
index: int): BoundDataCol =
proc dataCol*(data: (RawStmtPtr, int)): DataBuffer =
let s = data[0]
let index = data[1]
checkColMetadata(s, index, DataColName)
return proc (): DataBuffer =
let
i = index.cint
blob = sqlite3_column_blob(s, i)
# detect out-of-memory error
# see the conversion table and final paragraph of:
# https://www.sqlite.org/c3ref/column_blob.html
# see also https://www.sqlite.org/rescode.html
# the "data" column can be NULL so in order to detect an out-of-memory error
# it is necessary to check that the result is a null pointer and that the
# result code is an error code
if blob.isNil:
let
i = index.cint
blob = sqlite3_column_blob(s, i)
v = sqlite3_errcode(sqlite3_db_handle(s))
# detect out-of-memory error
# see the conversion table and final paragraph of:
# https://www.sqlite.org/c3ref/column_blob.html
# see also https://www.sqlite.org/rescode.html
if not (v in [SQLITE_OK, SQLITE_ROW, SQLITE_DONE]):
raise (ref Defect)(msg: $sqlite3_errstr(v))
# the "data" column can be NULL so in order to detect an out-of-memory error
# it is necessary to check that the result is a null pointer and that the
# result code is an error code
if blob.isNil:
let
v = sqlite3_errcode(sqlite3_db_handle(s))
let
dataLen = sqlite3_column_bytes(s, i)
dataBytes = cast[ptr UncheckedArray[byte]](blob)
if not (v in [SQLITE_OK, SQLITE_ROW, SQLITE_DONE]):
raise (ref Defect)(msg: $sqlite3_errstr(v))
let
dataLen = sqlite3_column_bytes(s, i)
dataBytes = cast[ptr UncheckedArray[byte]](blob)
# copy data out, since sqlite will free it
DataBuffer.new(toOpenArray(dataBytes, 0, dataLen - 1))
# copy data out, since sqlite will free it
DataBuffer.new(toOpenArray(dataBytes, 0, dataLen - 1))
proc timestampCol*(
s: RawStmtPtr,
@ -306,9 +306,9 @@ proc open*(
# "SQL logic error"
let
getDataCol = dataCol(RawStmtPtr(getStmt), GetStmtDataCol)
getDataCol = (RawStmtPtr(getStmt), GetStmtDataCol)
success T(
success SQLiteDsDb(
readOnly: readOnly,
dbPath: path,
containsStmt: containsStmt,

View File

@ -61,10 +61,10 @@ suite "Test Basic SQLiteDatastore":
test "delete batch":
var
batch: seq[Key]
batch: seq[string]
for k in 0..<100:
batch.add(Key.init(key, $k).tryGet)
batch.add(Key.init(key, $k).tryGet().id())
ds.delete(batch).tryGet
@ -72,7 +72,7 @@ suite "Test Basic SQLiteDatastore":
check: not ds.has(k).tryGet
test "handle missing key":
let key = Key.init("/missing/key").tryGet()
let key = Key.init("/missing/key").tryGet().id()
expect(DatastoreKeyNotFound):
discard ds.get(key).tryGet() # non existing key

View File

@ -111,12 +111,10 @@ suite "Test SQLite Datastore DB operations":
dsDb.putStmt.exec((key.id, data, timestamp())).tryGet()
test "Should select key":
let
dataCol = dsDb.getDataCol
var bytes: seq[byte]
proc onData(s: RawStmtPtr) =
bytes = dataCol()
bytes = dataCol(dsDb.getDataCol)
check:
dsDb.getStmt.query((key.id), onData).tryGet()
@ -129,12 +127,10 @@ suite "Test SQLite Datastore DB operations":
dsDb.putStmt.exec((key.id, otherData, timestamp())).tryGet()
test "Should select updated key":
let
dataCol = dsDb.getDataCol
var bytes: seq[byte]
proc onData(s: RawStmtPtr) =
bytes = dataCol()
bytes = dataCol(dsDb.getDataCol)
check:
dsDb.getStmt.query((key.id), onData).tryGet()