diff --git a/datastore/sqlite.nim b/datastore/sqlite.nim index 8a2ae71..e4c8f63 100644 --- a/datastore/sqlite.nim +++ b/datastore/sqlite.nim @@ -143,25 +143,11 @@ template journalModePragmaStmt*(env: SQLite): RawStmtPtr = return failure $sqlite3_errstr(x) let - x = sqlite3_column_text(s, 0).cstring + x = $sqlite3_column_text_not_null(s, 0) - # 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 - - # in order to detect an out-of-memory error check that the result is a null - # pointer and that the result code is an error code - if x.isNil: - let - code = sqlite3_errcode(sqlite3_db_handle(s)) - - if not (code in [SQLITE_OK, SQLITE_ROW, SQLITE_DONE]): - raise (ref Defect)(msg: $sqlite3_errstr(code)) - - if not ($x in ["memory", "wal"]): + if not (x in ["memory", "wal"]): s.dispose - return failure "Invalid pragma result: \"" & $x & "\"" + return failure "Invalid pragma result: \"" & x & "\"" s @@ -245,3 +231,21 @@ proc query*( proc release*[T](x: var AutoDisposed[T]): T = result = x.val x.val = nil + +proc sqlite3_column_text_not_null*( + s: RawStmtPtr, + index: cint): cstring = + + let + text = sqlite3_column_text(s, index).cstring + + if text.isNil: + # see the conversion table and final paragraph of: + # https://www.sqlite.org/c3ref/column_blob.html + # a null pointer here implies an out-of-memory error + let + code = sqlite3_errcode(sqlite3_db_handle(s)) + + raise (ref Defect)(msg: $sqlite3_errstr(code)) + + text diff --git a/datastore/sqlite_datastore.nim b/datastore/sqlite_datastore.nim index 893ab3f..161fa56 100644 --- a/datastore/sqlite_datastore.nim +++ b/datastore/sqlite_datastore.nim @@ -117,22 +117,7 @@ proc idCol*( checkColMetadata(s, index, idColName) return proc (): string = - let - text = sqlite3_column_text(s, index.cint) - - # detect out-of-memory error - # see the conversion table and final paragraph of: - # https://www.sqlite.org/c3ref/column_blob.html - - # the "id" column is NOT NULL PRIMARY KEY so an out-of-memory error can be - # inferred from a null pointer result - if text.isNil: - let - code = sqlite3_errcode(sqlite3_db_handle(s)) - - raise (ref Defect)(msg: $sqlite3_errstr(code)) - - $text.cstring + $sqlite3_column_text_not_null(s, index.cint) proc dataCol*( s: RawStmtPtr,