porting query tests

This commit is contained in:
Jaremy Creechley 2023-09-21 18:54:42 -07:00
parent a339bb3c95
commit 3d7c3d2d2f
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300

View File

@ -163,58 +163,62 @@ proc query*(
if not (v == SQLITE_OK):
return failure newException(DatastoreError, $sqlite3_errstr(v))
let handle = DbQueryHandle()
let iter = iterator(): ?!DbQueryResponse {.closure.} =
try:
if handle.cancel:
return
let v = sqlite3_step(s)
case v
of SQLITE_ROW:
let
key = KeyId.new(sqlite3_column_text_not_null(s, QueryStmtIdCol))
blob: ?pointer =
if query.value: sqlite3_column_blob(s, QueryStmtDataCol).some
else: pointer.none
# 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.isSome and blob.get().isNil:
let v = sqlite3_errcode(sqlite3_db_handle(s))
if not (v in [SQLITE_OK, SQLITE_ROW, SQLITE_DONE]):
return failure newException(DatastoreError, $sqlite3_errstr(v))
let
dataLen = sqlite3_column_bytes(s, QueryStmtDataCol)
data =
if blob.isSome: DataBuffer.new(blob.get(), 0, dataLen - 1)
else: DataBuffer.new(0)
yield success (key.some, data)
of SQLITE_DONE:
return
else:
return failure newException(DatastoreError, $sqlite3_errstr(v))
finally:
proc doClose() =
echo "sqlite backend: query: finally close"
discard sqlite3_reset(s)
discard sqlite3_clear_bindings(s)
s.dispose()
return
success (handle, iter)
let handle = DbQueryHandle()
let iter = iterator(): ?!DbQueryResponse {.closure.} =
if handle.cancel:
doClose()
return
let v = sqlite3_step(s)
case v
of SQLITE_ROW:
echo "SQLITE ROW"
let
key = KeyId.new(sqlite3_column_text_not_null(s, QueryStmtIdCol))
blob: ?pointer =
if query.value: sqlite3_column_blob(s, QueryStmtDataCol).some
else: pointer.none
# 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.isSome and blob.get().isNil:
let v = sqlite3_errcode(sqlite3_db_handle(s))
if not (v in [SQLITE_OK, SQLITE_ROW, SQLITE_DONE]):
return failure newException(DatastoreError, $sqlite3_errstr(v))
let
dataLen = sqlite3_column_bytes(s, QueryStmtDataCol)
data =
if blob.isSome: DataBuffer.new(blob.get(), 0, dataLen - 1)
else: DataBuffer.new(0)
echo "SQLITE ROW: yield"
yield success (key.some, data)
of SQLITE_DONE:
echo "SQLITE DONE: return"
doClose()
return
else:
echo "SQLITE ERROR: return"
doClose()
return failure newException(DatastoreError, $sqlite3_errstr(v))
proc contains*(self: SQLiteBackend, key: DbKey): bool =