mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-01-05 23:23:10 +00:00
implement sqlite3_column_text_not_null in datastore/sqlite.nim
consolidate calls re: `SQLiteDatastore.new` and `idCol`
This commit is contained in:
parent
bf6724b30a
commit
96695fed47
@ -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
|
||||
|
||||
@ -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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user