• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "grd_document/grd_document_api.h"
17 
18 #include "grd_base/grd_error.h"
19 #include "grd_resultset_inner.h"
20 #include "grd_type_inner.h"
21 #include "log_print.h"
22 using namespace DocumentDB;
23 
GRD_CreateCollection(GRD_DB * db,const char * collectionName,const char * optionStr,uint32_t flags)24 GRD_API int32_t GRD_CreateCollection(GRD_DB *db, const char *collectionName, const char *optionStr, uint32_t flags)
25 {
26     if (db == nullptr || db->store_ == nullptr) {
27         return GRD_INVALID_ARGS;
28     }
29 
30     std::string name = (collectionName == nullptr ? "" : collectionName);
31     std::string option = (optionStr == nullptr ? "" : optionStr);
32     int ret = db->store_->CreateCollection(name, option, flags);
33     return TransferDocErr(ret);
34 }
35 
GRD_DropCollection(GRD_DB * db,const char * collectionName,uint32_t flags)36 GRD_API int32_t GRD_DropCollection(GRD_DB *db, const char *collectionName, uint32_t flags)
37 {
38     if (db == nullptr || db->store_ == nullptr) {
39         return GRD_INVALID_ARGS;
40     }
41 
42     std::string name = (collectionName == nullptr ? "" : collectionName);
43     int ret = db->store_->DropCollection(name, flags);
44     return TransferDocErr(ret);
45 }
46 
GRD_UpdateDoc(GRD_DB * db,const char * collectionName,const char * filter,const char * update,uint32_t flags)47 GRD_API int32_t GRD_UpdateDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *update,
48     uint32_t flags)
49 {
50     if (db == nullptr || db->store_ == nullptr || collectionName == nullptr || filter == nullptr || update == nullptr) {
51         return GRD_INVALID_ARGS;
52     }
53     int ret = db->store_->UpdateDocument(collectionName, filter, update, flags);
54     if (ret >= 0) {
55         return ret;
56     }
57     return TransferDocErr(ret);
58 }
59 
GRD_UpsertDoc(GRD_DB * db,const char * collectionName,const char * filter,const char * document,uint32_t flags)60 GRD_API int32_t GRD_UpsertDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *document,
61     uint32_t flags)
62 {
63     if (db == nullptr || db->store_ == nullptr || collectionName == nullptr || filter == nullptr ||
64         document == nullptr) {
65         return GRD_INVALID_ARGS;
66     }
67     int ret = db->store_->UpsertDocument(collectionName, filter, document, flags);
68     if (ret >= 0) {
69         return ret;
70     }
71     return TransferDocErr(ret);
72 }
73 
GRD_InsertDoc(GRD_DB * db,const char * collectionName,const char * document,uint32_t flags)74 GRD_API int32_t GRD_InsertDoc(GRD_DB *db, const char *collectionName, const char *document, uint32_t flags)
75 {
76     if (db == nullptr || db->store_ == nullptr || collectionName == nullptr || document == nullptr) {
77         return GRD_INVALID_ARGS;
78     }
79     int ret = db->store_->InsertDocument(collectionName, document, flags);
80     return TransferDocErr(ret);
81 }
82 
GRD_DeleteDoc(GRD_DB * db,const char * collectionName,const char * filter,uint32_t flags)83 GRD_API int32_t GRD_DeleteDoc(GRD_DB *db, const char *collectionName, const char *filter, uint32_t flags)
84 {
85     if (db == nullptr || db->store_ == nullptr || filter == nullptr || collectionName == nullptr) {
86         return GRD_INVALID_ARGS;
87     }
88     int ret = db->store_->DeleteDocument(collectionName, filter, flags);
89     int errCode = TransferDocErr(ret);
90     switch (errCode) {
91         case GRD_OK:
92             return 1; // The amount of text deleted
93         case GRD_NO_DATA:
94             return 0;
95         default:
96             return errCode;
97     }
98 }
99 
GRD_FindDoc(GRD_DB * db,const char * collectionName,Query query,uint32_t flags,GRD_ResultSet ** resultSet)100 GRD_API int32_t GRD_FindDoc(GRD_DB *db, const char *collectionName, Query query, uint32_t flags,
101     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