mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-01-04 06:33:11 +00:00
commend out query for now
This commit is contained in:
parent
9901e71a88
commit
f9cfab5cbc
@ -37,10 +37,11 @@ method contains*(self: SQLiteDatastore, key: Key): Future[?!bool] {.async.} =
|
|||||||
proc onData(s: RawStmtPtr) =
|
proc onData(s: RawStmtPtr) =
|
||||||
exists = sqlite3_column_int64(s, ContainsStmtExistsCol.cint).bool
|
exists = sqlite3_column_int64(s, ContainsStmtExistsCol.cint).bool
|
||||||
|
|
||||||
let
|
if (
|
||||||
queryRes = self.db.containsStmt.query((key.id), onData)
|
let res = self.db.containsStmt.query((key.id), onData);
|
||||||
|
res.isErr):
|
||||||
|
return failure res.error.msg
|
||||||
|
|
||||||
if queryRes.isErr: return queryRes
|
|
||||||
return success exists
|
return success exists
|
||||||
|
|
||||||
method delete*(self: SQLiteDatastore, key: Key): Future[?!void] {.async.} =
|
method delete*(self: SQLiteDatastore, key: Key): Future[?!void] {.async.} =
|
||||||
@ -54,84 +55,80 @@ method get*(self: SQLiteDatastore, key: Key): Future[?!seq[byte]] {.async.} =
|
|||||||
var
|
var
|
||||||
bytes: seq[byte]
|
bytes: seq[byte]
|
||||||
|
|
||||||
let
|
|
||||||
dataCol = self.db.getDataCol
|
|
||||||
|
|
||||||
proc onData(s: RawStmtPtr) =
|
proc onData(s: RawStmtPtr) =
|
||||||
bytes = dataCol()
|
bytes = self.db.getDataCol()
|
||||||
|
|
||||||
let
|
if (
|
||||||
queryRes = self.db.getStmt.query((key.id), onData)
|
let res = self.db.getStmt.query((key.id), onData);
|
||||||
|
res.isErr):
|
||||||
if queryRes.isErr:
|
return failure res.error.msg
|
||||||
return failure queryRes.error.msg
|
|
||||||
|
|
||||||
return success bytes
|
return success bytes
|
||||||
|
|
||||||
method put*(self: SQLiteDatastore, key: Key, data: seq[byte]): Future[?!void] {.async.} =
|
method put*(self: SQLiteDatastore, key: Key, data: seq[byte]): Future[?!void] {.async.} =
|
||||||
return self.db.putStmt.exec((key.id, @data, timestamp()))
|
return self.db.putStmt.exec((key.id, @data, timestamp()))
|
||||||
|
|
||||||
iterator query*(
|
# iterator query*(
|
||||||
self: SQLiteDatastore,
|
# self: SQLiteDatastore,
|
||||||
query: Query): Future[QueryResponse] =
|
# query: Query): Future[QueryResponse] =
|
||||||
|
|
||||||
let
|
# let
|
||||||
queryStmt = QueryStmt.prepare(
|
# queryStmt = QueryStmt.prepare(
|
||||||
self.db.env, QueryStmtStr).expect("should not fail")
|
# self.db.env, QueryStmtStr).expect("should not fail")
|
||||||
|
|
||||||
s = RawStmtPtr(queryStmt)
|
# s = RawStmtPtr(queryStmt)
|
||||||
|
|
||||||
defer:
|
# defer:
|
||||||
discard sqlite3_reset(s)
|
# discard sqlite3_reset(s)
|
||||||
discard sqlite3_clear_bindings(s)
|
# discard sqlite3_clear_bindings(s)
|
||||||
s.dispose
|
# s.dispose
|
||||||
|
|
||||||
let
|
# let
|
||||||
v = sqlite3_bind_text(s, 1.cint, query.key.id.cstring, -1.cint,
|
# v = sqlite3_bind_text(s, 1.cint, query.key.id.cstring, -1.cint,
|
||||||
SQLITE_TRANSIENT_GCSAFE)
|
# SQLITE_TRANSIENT_GCSAFE)
|
||||||
|
|
||||||
if not (v == SQLITE_OK):
|
# if not (v == SQLITE_OK):
|
||||||
raise (ref Defect)(msg: $sqlite3_errstr(v))
|
# raise (ref Defect)(msg: $sqlite3_errstr(v))
|
||||||
|
|
||||||
while true:
|
# while true:
|
||||||
let
|
# let
|
||||||
v = sqlite3_step(s)
|
# v = sqlite3_step(s)
|
||||||
|
|
||||||
case v
|
# case v
|
||||||
of SQLITE_ROW:
|
# of SQLITE_ROW:
|
||||||
let
|
# let
|
||||||
key = Key.init($sqlite3_column_text_not_null(
|
# key = Key.init($sqlite3_column_text_not_null(
|
||||||
s, QueryStmtIdCol)).expect("should not fail")
|
# s, QueryStmtIdCol)).expect("should not fail")
|
||||||
|
|
||||||
blob = sqlite3_column_blob(s, QueryStmtDataCol)
|
# blob = sqlite3_column_blob(s, QueryStmtDataCol)
|
||||||
|
|
||||||
# detect out-of-memory error
|
# # detect out-of-memory error
|
||||||
# see the conversion table and final paragraph of:
|
# # see the conversion table and final paragraph of:
|
||||||
# https://www.sqlite.org/c3ref/column_blob.html
|
# # https://www.sqlite.org/c3ref/column_blob.html
|
||||||
# see also https://www.sqlite.org/rescode.html
|
# # see also https://www.sqlite.org/rescode.html
|
||||||
|
|
||||||
# the "data" column can be NULL so in order to detect an out-of-memory
|
# # the "data" column can be NULL so in order to detect an out-of-memory
|
||||||
# error it is necessary to check that the result is a null pointer and
|
# # error it is necessary to check that the result is a null pointer and
|
||||||
# that the result code is an error code
|
# # that the result code is an error code
|
||||||
if blob.isNil:
|
# if blob.isNil:
|
||||||
let
|
# let
|
||||||
v = sqlite3_errcode(sqlite3_db_handle(s))
|
# v = sqlite3_errcode(sqlite3_db_handle(s))
|
||||||
|
|
||||||
if not (v in [SQLITE_OK, SQLITE_ROW, SQLITE_DONE]):
|
# if not (v in [SQLITE_OK, SQLITE_ROW, SQLITE_DONE]):
|
||||||
raise (ref Defect)(msg: $sqlite3_errstr(v))
|
# raise (ref Defect)(msg: $sqlite3_errstr(v))
|
||||||
|
|
||||||
let
|
# let
|
||||||
dataLen = sqlite3_column_bytes(s, QueryStmtDataCol)
|
# dataLen = sqlite3_column_bytes(s, QueryStmtDataCol)
|
||||||
dataBytes = cast[ptr UncheckedArray[byte]](blob)
|
# dataBytes = cast[ptr UncheckedArray[byte]](blob)
|
||||||
data = @(toOpenArray(dataBytes, 0, dataLen - 1))
|
# data = @(toOpenArray(dataBytes, 0, dataLen - 1))
|
||||||
fut = newFuture[QueryResponse]()
|
# fut = newFuture[QueryResponse]()
|
||||||
|
|
||||||
fut.complete((key, data))
|
# fut.complete((key, data))
|
||||||
yield fut
|
# yield fut
|
||||||
of SQLITE_DONE:
|
# of SQLITE_DONE:
|
||||||
break
|
# break
|
||||||
else:
|
# else:
|
||||||
raise (ref Defect)(msg: $sqlite3_errstr(v))
|
# raise (ref Defect)(msg: $sqlite3_errstr(v))
|
||||||
|
|
||||||
proc new*(
|
proc new*(
|
||||||
T: type SQLiteDatastore,
|
T: type SQLiteDatastore,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user