Implemented select clause support.
h3rald h3rald@h3rald.com
Sat, 24 Feb 2018 12:40:25 +0100
3 files changed,
33 insertions(+),
5 deletions(-)
M
lib/core.nim
→
lib/core.nim
@@ -163,7 +163,7 @@ var raw_document = store.db.getRow(select.sql, id)
if raw_document[0] == "": return "" else: - return $store.prepareJsonDocument(raw_document, options.select) + return $store.prepareJsonDocument(raw_document, options) proc createDocument*(store: Datastore, id="", rawdata = "", contenttype = "text/plain", binary = -1, searchable = 1): string = let singleOp = not LS_TRANSACTION@@ -286,7 +286,7 @@ else:
raw_documents = store.db.getAllRows(select.sql) var documents = newSeq[JsonNode](0) for doc in raw_documents: - documents.add store.prepareJsonDocument(doc, options.select) + documents.add store.prepareJsonDocument(doc, options) return %documents proc countDocuments*(store: Datastore): int64 =
M
lib/utils.nim
→
lib/utils.nim
@@ -37,7 +37,7 @@
proc prepareSelectDocumentsQuery*(options: var QueryOptions): string = var tables = options.tables result = "SELECT " - if options.jsonFilter.len > 0: + if options.jsonFilter.len > 0 or options.jsonSelect.len > 0: if not options.tags.contains("$subtype:json"): options.tags = options.tags.split(",").concat(@["$subtype:json"]).join(",") if options.search.len > 0:@@ -107,7 +107,7 @@ if options.limit > 0:
result = result & "LIMIT " & $options.limit & " " LOG.debug(result.replace("$", "$$")) -proc prepareJsonDocument*(store:Datastore, doc: Row, cols:seq[string]): JsonNode = +proc prepareJsonDocument*(store:Datastore, doc: Row, options: QueryOptions): JsonNode = var raw_tags = store.db.getAllRows(SQL_SELECT_DOCUMENT_TAGS, doc[0]) var tags = newSeq[JsonNode](0) for tag in raw_tags:@@ -118,7 +118,7 @@ return %(doc[0].parseInt)
result = newJObject() var count = 0 var jsondoc = false - for s in cols: + for s in options.select: var key = s count.inc var rawvalue = doc[count-1]@@ -143,6 +143,16 @@ value = %doc[count-1]
result[key] = value if jsondoc: result["data"] = result["data"].getStr().parseJson() + if options.jsonSelect.len > 0: + var obj = newJObject() + for field in options.jsonSelect: + let keys = field.path.replace("$.", "").split(".") + let res = result["data"]{keys} + if res.isNil: + obj[field.alias] = newJNull() + else: + obj[field.alias] = %res + result["data"] = obj result["tags"] = %tags proc toPlainText*(s: string): string =
M
test/http_api.nim
→
test/http_api.nim
@@ -159,3 +159,21 @@ rget = jget("docs/?filter=$.age%20eq%2034%20or%20$.age%20eq%2036%20or%20$.eyeColor%20eq%20\"brown\"")
check(rget.body.parseJson["total"] == %5) rget = jget("docs/?filter=$.name.first%20eq%20\"Jensen\"") check(rget.body.parseJson["total"] == %1) + + test "GET documents selecting fields": + var rget = jget("docs/?select=$.age%20as%20age,$.email%20as%20email") + var json = rget.body.parseJson + var testdata = %*{ + "age": 36, + "email": "lawson.logan@trasola.co.uk" + } + check(json["total"] == %8) + check(json["results"][3]["data"] == testdata) + rget = jget("docs/" & ids[2] & "?select=$.age%20as%20age&raw=true") + json = rget.body.parseJson + testdata = %*{ + "age": 31 + } + check(json["data"] == testdata) + +