mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-05-27 20:19:39 +00:00
porting query tests
This commit is contained in:
parent
a339bb3c95
commit
3d7c3d2d2f
@ -163,58 +163,62 @@ proc query*(
|
|||||||
if not (v == SQLITE_OK):
|
if not (v == SQLITE_OK):
|
||||||
return failure newException(DatastoreError, $sqlite3_errstr(v))
|
return failure newException(DatastoreError, $sqlite3_errstr(v))
|
||||||
|
|
||||||
let handle = DbQueryHandle()
|
proc doClose() =
|
||||||
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:
|
|
||||||
echo "sqlite backend: query: finally close"
|
echo "sqlite backend: query: finally close"
|
||||||
discard sqlite3_reset(s)
|
discard sqlite3_reset(s)
|
||||||
discard sqlite3_clear_bindings(s)
|
discard sqlite3_clear_bindings(s)
|
||||||
s.dispose()
|
s.dispose()
|
||||||
return
|
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 =
|
proc contains*(self: SQLiteBackend, key: DbKey): bool =
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user