databuffer type

This commit is contained in:
Jaremy Creechley 2023-09-20 20:04:44 -07:00
parent b388d242bb
commit 29ff227d37
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
2 changed files with 8 additions and 5 deletions

View File

@ -29,11 +29,12 @@ proc timestamp*(t = epochTime()): int64 =
proc has*(self: SQLiteDatastore, key: DbKey): ?!bool =
var
exists = false
key = $key
proc onData(s: RawStmtPtr) =
exists = sqlite3_column_int64(s, ContainsStmtExistsCol.cint).bool
if err =? self.db.containsStmt.query((key.id), onData).errorOption:
if err =? self.db.containsStmt.query((key), onData).errorOption:
return failure err
return success exists

View File

@ -4,13 +4,14 @@ import pkg/questionable
import pkg/questionable/results
import pkg/upraises
import ../backend
import ./sqliteutils
export sqliteutils
type
BoundIdCol* = proc (): string {.closure, gcsafe, upraises: [].}
BoundDataCol* = proc (): seq[byte] {.closure, gcsafe, upraises: [].}
BoundDataCol* = proc (): DataBuffer {.closure, gcsafe, upraises: [].}
BoundTimestampCol* = proc (): int64 {.closure, gcsafe, upraises: [].}
# feels odd to use `void` for prepared statements corresponding to SELECT
@ -19,7 +20,7 @@ type
ContainsStmt* = SQLiteStmt[(string), void]
DeleteStmt* = SQLiteStmt[(string), void]
GetStmt* = SQLiteStmt[(string), void]
PutStmt* = SQLiteStmt[(string, seq[byte], int64), void]
PutStmt* = SQLiteStmt[(string, DataBuffer, int64), void]
QueryStmt* = SQLiteStmt[(string), void]
BeginStmt* = NoParamsStmt
EndStmt* = NoParamsStmt
@ -162,7 +163,7 @@ proc dataCol*(
checkColMetadata(s, index, DataColName)
return proc (): seq[byte] =
return proc (): DataBuffer =
let
i = index.cint
blob = sqlite3_column_blob(s, i)
@ -186,7 +187,8 @@ proc dataCol*(
dataLen = sqlite3_column_bytes(s, i)
dataBytes = cast[ptr UncheckedArray[byte]](blob)
@(toOpenArray(dataBytes, 0, dataLen - 1))
# copy data out, since sqlite will free it
DataBuffer.new(toOpenArray(dataBytes, 0, dataLen - 1))
proc timestampCol*(
s: RawStmtPtr,