From 3d7c3d2d2fe9c7d0e30424b00df7ba28fec0889c Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Thu, 21 Sep 2023 18:54:42 -0700 Subject: [PATCH] porting query tests --- datastore/sql/sqliteds.nim | 98 ++++++++++++++++++++------------------ 1 file changed, 51 insertions(+), 47 deletions(-) diff --git a/datastore/sql/sqliteds.nim b/datastore/sql/sqliteds.nim index 09ce348..2565a43 100644 --- a/datastore/sql/sqliteds.nim +++ b/datastore/sql/sqliteds.nim @@ -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 =