change to generics

This commit is contained in:
Jaremy Creechley 2023-09-25 17:59:18 -07:00
parent 269aec68f3
commit 24ca5f96c9
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
4 changed files with 15 additions and 12 deletions

View File

@ -42,6 +42,9 @@ proc `$`*(id: KeyId): string = $(id.data)
proc toKey*(tp: typedesc[KeyId], id: cstring): KeyId = KeyId.new(id)
proc toKey*(tp: typedesc[string], id: cstring): string = $(id)
template toVal*(tp: typedesc[DataBuffer], id: openArray[byte]): DataBuffer = DataBuffer.new(id)
template toVal*(tp: typedesc[seq[byte]], id: openArray[byte]): seq[byte] = @(id)
proc new*(tp: typedesc[KeyId], id: cstring): KeyId =
## copy cstring including null terminator
KeyId(data: DataBuffer.new(id.toOpenArray(0, id.len()-1)))
@ -50,15 +53,15 @@ proc new*(tp: typedesc[KeyId], id: string): KeyId =
## copy cstring including null terminator
KeyId(data: DataBuffer.new(id))
proc toCString*(key: KeyId): cstring =
## copy cstring including null terminator
cast[cstring](baseAddr key.data)
# proc toCString*(key: KeyId): cstring =
# ## copy cstring including null terminator
# cast[cstring](baseAddr key.data)
proc toDb*(key: Key): DbKey {.inline, raises: [].} =
let id: string = key.id()
let db = DataBuffer.new(id.len()+1) # include room for null for cstring compat
db.setData(id)
DbKey(data: db)
# proc toDb*(key: Key): DbKey {.inline, raises: [].} =
# let id: string = key.id()
# let db = DataBuffer.new(id.len()+1) # include room for null for cstring compat
# db.setData(id)
# DbKey(data: db)
proc toKey*(key: KeyId): Key {.inline, raises: [].} =
Key.init(key.data).expect("expected valid key here for but got `" & $key.data & "`")

View File

@ -67,7 +67,7 @@ proc get*[K,V](self: SQLiteBackend[K,V], key: K): ?!seq[byte] =
bytes: seq[byte]
proc onData(s: RawStmtPtr) =
bytes = dataCol(self.db.getDataCol)
bytes = dataCol[V](self.db.getDataCol)
if err =? self.db.getStmt.query((key), onData).errorOption:
return failure(err)

View File

@ -157,7 +157,7 @@ proc idCol*(
return proc (): string =
$sqlite3_column_text_not_null(s, index.cint)
proc dataCol*(data: (RawStmtPtr, int)): DataBuffer =
proc dataCol*[V: DbVal](data: (RawStmtPtr, int)): V =
let s = data[0]
let index = data[1]
@ -188,7 +188,7 @@ proc dataCol*(data: (RawStmtPtr, int)): DataBuffer =
dataBytes = cast[ptr UncheckedArray[byte]](blob)
# copy data out, since sqlite will free it
DataBuffer.new(toOpenArray(dataBytes, 0, dataLen - 1))
V.toVal(toOpenArray(dataBytes, 0, dataLen - 1))
proc timestampCol*(
s: RawStmtPtr,

View File

@ -72,7 +72,7 @@ proc bindParam(
sqlite3_bind_text(s, n.cint, val.cstring, val.len().cint, SQLITE_TRANSIENT)
elif val is KeyId:
# same as previous
sqlite3_bind_text(s, n.cint, val.toCString(), val.data.len().cint, SQLITE_TRANSIENT)
sqlite3_bind_text(s, n.cint, cast[cstring](baseAddr val.data), val.data.len().cint, SQLITE_TRANSIENT)
else:
{.fatal: "Please add support for the '" & $typeof(val) & "' type".}