diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h index 6f88630c7eeee11a5353afdc15ddbe583a02d13c..5b4a9936289485f35db3e5f0b1280db84b41527a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/include/json_common.h @@ -42,7 +42,7 @@ public: static bool IsJsonNodeMatch(const JsonObject &src, const JsonObject &target, int &errCode); private: - static bool JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, const JsonObject &item, + static bool JsonEqualJudge(const JsonObject &srcItem, const JsonObject &item, int &isAlreadyMatched, bool &isCollapse, int &isMatchFlag); static bool CheckNode(JsonObject &Node, std::set filedSet, bool &errFlag); static bool CheckProjectionNode(JsonObject &Node, std::set filedSet, bool &errFlag, bool isFirstFloor); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp index c213f8588d04cbb460f24e504d5d40ac35a3e019..1145dcac30821aabe1bd259ee9c18a404fbe9d0a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/common/src/json_common.cpp @@ -578,11 +578,11 @@ bool JsonCommon::IsArrayMatch(const JsonObject &src, const JsonObject &target, i return isMatch; } -bool JsonCommon::JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, const JsonObject &item, +bool JsonCommon::JsonEqualJudge(const JsonObject &srcItem, const JsonObject &item, int &isAlreadyMatched, bool &isCollapse, int &isMatchFlag) { int errCode; - JsonObject srcItem = src.FindItemPowerMode(itemPath, errCode); + JsonFieldPath granpaPath = itemPath; std::string lastFiledName = granpaPath.back(); granpaPath.pop_back(); @@ -591,9 +591,8 @@ bool JsonCommon::JsonEqualJudge(JsonFieldPath &itemPath, const JsonObject &src, JsonObject fatherItem = granpaItem.GetChild(); while (!fatherItem.IsNull()) { int isEqual = true; - int compareRet = (fatherItem.GetObjectItem(lastFiledName, errCode).Print() == item.Print()); - if (errCode == E_OK) { - isEqual = compareRet; + if (!(fatherItem.GetObjectItem(lastFiledName, errCode).Print() == item.Print())) { + isEqual = false; } if (isEqual) { GLOGI("Filter value is equal with src"); @@ -649,14 +648,15 @@ bool JsonCommon::IsJsonNodeMatch(const JsonObject &src, const JsonObject &target return false; } JsonFieldPath itemPath = SplitePath(path, isCollapse); + JsonObject srcItem = src.FindItemPowerMode(itemPath, errCode); if (src.IsFieldExistsPowerMode(itemPath)) { if (isCollapse) { - return JsonEqualJudge(itemPath, src, item, isAlreadyMatched, isCollapse, isMatchFlag); + return JsonEqualJudge(srcItem, item, isAlreadyMatched, isCollapse, isMatchFlag); } else { JsonObject srcItem = src.FindItemPowerMode(itemPath, errCode); if (srcItem.GetType() == JsonObject::Type::JSON_ARRAY) { - return JsonEqualJudge(itemPath, src, item, isAlreadyMatched, isCollapse, isMatchFlag); + return JsonEqualJudge(srcItem, item, isAlreadyMatched, isCollapse, isMatchFlag); } if (srcItem.Print() == item.Print()) { isMatchFlag = true; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp index 285036fc4ba7d7e609305b3cefbd1ec2604efcc2..dba817841d1ef70b65bdb50ccd5e9cf05afd07e8 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/executor/document/document_check.cpp @@ -108,13 +108,16 @@ int CheckCommon::CheckFilter(JsonObject &filterObj, bool &isOnlyId, std::vector< isOnlyId = false; } for (int i = 0; i < filterPath.size(); i++) { - for (auto fieldName : filterPath[i]) { - for (int j = 0; j < fieldName.size(); j++) { - if (!((isalpha(fieldName[j])) || (isdigit(fieldName[j])) || ('_' == fieldName[j]))) { + for (int j = 0; j < filterPath[i].size(); j++) { + for (auto oneChar : filterPath[i][j]) { + if (!((isalpha(oneChar)) || (isdigit(oneChar)) || ('_' == oneChar))) { return -E_INVALID_ARGS; } } } + if (!filterPath[i].empty() && !filterPath[i][0].empty() && isdigit(filterPath[i][0][0])) { + return -E_INVALID_ARGS; + } } bool isIdExisit = false; int ret = CheckIdFormat(filterObj, isIdExisit); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp index ab098f736a871e9a63c63883f9062c560144ba5d..31d1858c74119b704fb92381bf457190ef36ad1a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/collection.cpp @@ -129,7 +129,6 @@ int Collection::UpsertDocument(const std::string &id, const std::string &documen GLOGD("Append value failed. %d", errCode); return errCode; } - // kkk std::string valStr = originValue.Print(); if (valStr.length() + 1 > JSON_LENS_MAX) { GLOGE("document's length is too long"); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp index 210316d84bd4920decaeb127408fd9f8110c97fe..5133deda553a00c0ecd47341f8a77e94c498a0b3 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/src/interface/src/document_store.cpp @@ -363,7 +363,10 @@ int DocumentStore::DeleteDocument(const std::string &collection, const std::stri return errCode; } std::string id; - resultSet.GetKey(id); + errCode = resultSet.GetKey(id); + if (errCode != E_OK) { + return errCode; + } Key key(id.begin(), id.end()); return coll.DeleteDocument(key); } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp index 17ff76b7b25f45d3f8ba2514a7fa7aeb6cac76fc..13426d409f2f8e6e5d1936c2018c2a5bff475925 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_data_test.cpp @@ -257,4 +257,38 @@ HWTEST_F(DocumentDBDataTest, UpdateDataTest006, TestSize.Level0) GLOGD("UpdateDataTest006: update data with flag: %u", flag); EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, filter.c_str(), document.c_str(), flag), GRD_INVALID_ARGS); } +} + +/** + * @tc.name: UpsertDataTest001 + * @tc.desc: Test upsert data into collection + * @tc.type: FUNC + * @tc.require: + * @tc.author: lianhuix + */ +HWTEST_F(DocumentDBDataTest, UpsertDataTest011, TestSize.Level0) +{ + std::string document = R""({"name":"Tmono","age":18,"addr":{"city":"shanghai","postal":200001}})""; + EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, R""({"_id":"1234"})"", document.c_str(), GRD_DOC_REPLACE), 1); + + // std::string update = R""({"CC":"AAAA", "DD":"BBBB"})""; + // EXPECT_EQ(GRD_UpdateDoc(g_db, g_coll, R""({"_id":"1234"})"", update.c_str(), GRD_DOC_REPLACE), 1); + + std::string replace = R""({"DD":"GGGG"})""; + EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, R""({"_id":"1234"})"", replace.c_str(), GRD_DOC_REPLACE), 1); + + std::string append = R""({"EE":"GGGG"})""; + EXPECT_EQ(GRD_UpsertDoc(g_db, g_coll, R""({"_id":"1234"})"", append.c_str(), GRD_DOC_APPEND), 1); + + const char *filter = "{\"_id\":\"1234\"}"; + GRD_ResultSet *resultSet = nullptr; + const char *projection = "{}"; + Query query = { filter, projection }; + EXPECT_EQ(GRD_FindDoc(g_db, g_coll, query, 1, &resultSet), GRD_OK); + EXPECT_EQ(GRD_Next(resultSet), GRD_OK); + char *value = NULL; + EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); + GLOGE("value is ======>%s", value); + EXPECT_EQ(GRD_FreeValue(value), GRD_OK); + EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); } \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp index 3691ff168c6049fc611fd1662713284f90557390..c39de113a8778a127603886e2867d118d68e620b 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_simple/test/unittest/api/documentdb_find_test.cpp @@ -1559,4 +1559,24 @@ HWTEST_F(DocumentFindApiTest, DocumentFindApiTest058, TestSize.Level1) // EXPECT_EQ(GRD_GetValue(resultSet, &value), GRD_OK); // CompareValue(value, g_document13); //EXPECT_EQ(GRD_FreeResultSet(resultSet), GRD_OK); +} + +/** + * @tc.name: DocumentFindApiTest059 + * @tc.desc: Test findDoc with invalid field + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(DocumentFindApiTest, DocumentFindApiTest059, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create filter with _id and get the record according to filter condition. + * @tc.expected: step1. Succeed to get the record, the matching record is g_document6. + */ + const char *filter = "{\"123a1\":123}"; + GRD_ResultSet *resultSet = nullptr; + const char *projection = "{}"; + Query query = { filter, projection }; + EXPECT_EQ(GRD_FindDoc(g_db, COLLECTION_NAME, query, 1, &resultSet), GRD_INVALID_ARGS); } \ No newline at end of file