1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "grd_document_api_inner.h"
16
17 #include "check_common.h"
18 #include "grd_api_manager.h"
19 #include "grd_base/grd_db_api.h"
20 #include "grd_base/grd_error.h"
21 #include "grd_resultset_inner.h"
22 #include "grd_type_inner.h"
23 #include "rd_log_print.h"
24 namespace DocumentDB {
GRD_CreateCollectionInner(GRD_DB * db,const char * collectionName,const char * optionStr,uint32_t flags)25 int32_t GRD_CreateCollectionInner(GRD_DB *db, const char *collectionName, const char *optionStr, uint32_t flags)
26 {
27 if (db == nullptr || db->store_ == nullptr) {
28 return GRD_INVALID_ARGS;
29 }
30
31 std::string name = (collectionName == nullptr ? "" : collectionName);
32 std::string option = (optionStr == nullptr ? "" : optionStr);
33 int ret = db->store_->CreateCollection(name, option, flags);
34 return TransferDocErr(ret);
35 }
36
GRD_DropCollectionInner(GRD_DB * db,const char * collectionName,uint32_t flags)37 int32_t GRD_DropCollectionInner(GRD_DB *db, const char *collectionName, uint32_t flags)
38 {
39 if (db == nullptr || db->store_ == nullptr) {
40 return GRD_INVALID_ARGS;
41 }
42
43 std::string name = (collectionName == nullptr ? "" : collectionName);
44 int ret = db->store_->DropCollection(name, flags);
45 return TransferDocErr(ret);
46 }
47
GRD_UpdateDocInner(GRD_DB * db,const char * collectionName,const char * filter,const char * update,uint32_t flags)48 int32_t GRD_UpdateDocInner(GRD_DB *db, const char *collectionName, const char *filter, const char *update,
49 uint32_t flags)
50 {
51 if (db == nullptr || db->store_ == nullptr || collectionName == nullptr || filter == nullptr || update == nullptr) {
52 return GRD_INVALID_ARGS;
53 }
54 int ret = db->store_->UpdateDocument(collectionName, filter, update, flags);
55 if (ret >= 0) {
56 return ret;
57 }
58 return TransferDocErr(ret);
59 }
60
GRD_UpsertDocInner(GRD_DB * db,const char * collectionName,const char * filter,const char * document,uint32_t flags)61 int32_t GRD_UpsertDocInner(GRD_DB *db, const char *collectionName, const char *filter, const char *document,
62 uint32_t flags)
63 {
64 if (db == nullptr || db->store_ == nullptr || collectionName == nullptr || filter == nullptr ||
65 document == nullptr) {
66 return GRD_INVALID_ARGS;
67 }
68 int ret = db->store_->UpsertDocument(collectionName, filter, document, flags);
69 if (ret >= 0) {
70 return ret;
71 }
72 return TransferDocErr(ret);
73 }
74
GRD_InsertDocInner(GRD_DB * db,const char * collectionName,const char * document,uint32_t flags)75 int32_t GRD_InsertDocInner(GRD_DB *db, const char *collectionName, const char *document, uint32_t flags)
76 {
77 if (db == nullptr || db->store_ == nullptr || collectionName == nullptr || document == nullptr) {
78 return GRD_INVALID_ARGS;
79 }
80 int ret = db->store_->InsertDocument(collectionName, document, flags);
81 return TransferDocErr(ret);
82 }
83
GRD_DeleteDocInner(GRD_DB * db,const char * collectionName,const char * filter,uint32_t flags)84 int32_t GRD_DeleteDocInner(GRD_DB *db, const char *collectionName, const char *filter, uint32_t flags)
85 {
86 if (db == nullptr || db->store_ == nullptr || filter == nullptr || collectionName == nullptr) {
87 return GRD_INVALID_ARGS;
88 }
89 int ret = db->store_->DeleteDocument(collectionName, filter, flags);
90 int errCode = TransferDocErr(ret);
91 switch (errCode) {
92 case GRD_OK:
93 return 1; // The amount of text deleted
94 case GRD_NO_DATA:
95 return 0;
96 default:
97 return errCode;
98 }
99 }
100
GRD_FindDocInner(GRD_DB * db,const char * collectionName,Query query,uint32_t flags,GRD_ResultSet ** resultSet)101 int32_t GRD_FindDocInner(GRD_DB *db, const char *collectionName, Query query, uint32_t flags, GRD_ResultSet **resultSet)
102 {
103 if (db == nullptr || db->store_ == nullptr || collectionName == nullptr || resultSet == nullptr ||
104 query.filter == nullptr || query.projection == nullptr) {
105 return GRD_INVALID_ARGS;
106 }
107 GRD_ResultSet *grdResultSet = new (std::nothrow) GRD_ResultSet();
108 if (grdResultSet == nullptr) {
109 GLOGE("Memory allocation failed!");
110 return -E_FAILED_MEMORY_ALLOCATE;
111 }
112 int ret = db->store_->FindDocument(collectionName, query.filter, query.projection, flags, grdResultSet);
113 if (ret != E_OK) {
114 delete grdResultSet;
115 return TransferDocErr(ret);
116 }
117 *resultSet = grdResultSet;
118 return TransferDocErr(ret);
119 }
120 } // namespace DocumentDB