6 #include "knowrob/storage/mongo/MongoTerm.h"
7 #include "knowrob/terms/ListTerm.h"
8 #include "knowrob/terms/Numeric.h"
9 #include "knowrob/terms/Variable.h"
17 const char *queryOperator,
19 bool includeVariables) {
20 bson_t queryOperatorDoc;
21 bson_t orArray, orCase1, orCase2;
22 bson_t *valueDocument;
25 if (matchNullValues) {
28 BSON_APPEND_ARRAY_BEGIN(doc,
"$or", &orArray);
30 BSON_APPEND_DOCUMENT_BEGIN(&orArray,
"0", &orCase1);
31 BSON_APPEND_NULL(&orCase1, key);
32 bson_append_document_end(&orArray, &orCase1);
34 BSON_APPEND_DOCUMENT_BEGIN(&orArray,
"1", &orCase2);
38 BSON_APPEND_DOCUMENT_BEGIN(matchNullValues ? &orCase2 : doc, key, &queryOperatorDoc);
39 valueDocument = &queryOperatorDoc;
40 valueKey = queryOperator;
42 valueDocument = (matchNullValues ? &orCase2 : doc);
47 auto atomic = std::static_pointer_cast<Atomic>(
term);
48 switch (
atomic->atomicType()) {
51 BSON_APPEND_UTF8(valueDocument, valueKey,
atomic->stringForm().data());
54 auto numeric = std::static_pointer_cast<Numeric>(
atomic);
55 switch (numeric->xsdType()) {
58 BSON_APPEND_DOUBLE(valueDocument, valueKey, numeric->asDouble());
63 BSON_APPEND_INT32(valueDocument, valueKey, numeric->asInteger());
67 BSON_APPEND_INT64(valueDocument, valueKey, numeric->asLong());
71 BSON_APPEND_INT32(valueDocument, valueKey, numeric->asShort());
74 BSON_APPEND_BOOL(valueDocument, valueKey, numeric->asBoolean());
84 append(valueDocument, valueKey,
89 BSON_APPEND_UTF8(valueDocument, valueKey, (varPrefix + varKey).data());
93 bson_append_document_end(matchNullValues ? &orCase2 : doc, &queryOperatorDoc);
95 if (matchNullValues) {
96 bson_append_document_end(&orArray, &orCase2);
97 bson_append_array_end(doc, &orArray);
102 bool matchNullValue) {
103 append(doc, key,
term, queryOperator, matchNullValue,
true);
109 const std::vector<TermPtr> &terms,
110 const char *arrayOperator) {
111 bson_t orOperator, orArray;
112 char arrIndexStr[16];
113 const char *arrIndexKey;
114 uint32_t arrIndex = 0;
116 BSON_APPEND_DOCUMENT_BEGIN(doc, key, &orOperator);
117 BSON_APPEND_ARRAY_BEGIN(&orOperator, arrayOperator, &orArray);
118 for (
auto &
term: terms) {
119 bson_uint32_to_string(arrIndex++,
120 &arrIndexKey, arrIndexStr,
sizeof arrIndexStr);
123 bson_append_array_end(&orOperator, &orArray);
124 bson_append_document_end(doc, &orOperator);
128 std::stringstream ss;
129 ss <<
"v_VARS." << varName <<
".val";
static void append(bson_t *doc, const char *key, const TermPtr &term, const char *queryOperator=nullptr, bool matchNullValue=false, bool includeVariables=false)
static void appendWithVars(bson_t *doc, const char *key, const TermPtr &term, const char *queryOperator=nullptr, bool matchNullValue=false)
static std::string variableKey(const std::string_view &varName)
std::shared_ptr< Term > TermPtr