implement sqlite3_column_text_not_null in datastore/sqlite.nim

consolidate calls re: `SQLiteDatastore.new` and `idCol`
This commit is contained in:
Michael Bradley, Jr 2022-07-14 14:25:20 -05:00
parent bf6724b30a
commit 96695fed47
No known key found for this signature in database
GPG Key ID: D0307DBCF21A9A58
2 changed files with 22 additions and 33 deletions

View File

@ -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

View File

@ -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,