1 /*
2 * Copyright (c) 2022 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 #define LOG_TAG "JS_KVStoreResultSet"
16 #include "js_kv_store_resultset.h"
17 #include "js_util.h"
18 #include "log_print.h"
19 #include "napi_queue.h"
20 #include "uv_queue.h"
21 #include "kvstore_datashare_bridge.h"
22 #include "kv_utils.h"
23
24 using namespace OHOS::DistributedKv;
25 using namespace OHOS::DataShare;
26 namespace OHOS::DistributedKVStore {
SetKvStoreResultSetPtr(std::shared_ptr<KvStoreResultSet> resultSet)27 void JsKVStoreResultSet::SetKvStoreResultSetPtr(std::shared_ptr<KvStoreResultSet> resultSet)
28 {
29 resultSet_ = resultSet;
30 }
31
GetKvStoreResultSetPtr()32 std::shared_ptr<KvStoreResultSet> JsKVStoreResultSet::GetKvStoreResultSetPtr()
33 {
34 return resultSet_;
35 }
36
Constructor(napi_env env)37 napi_value JsKVStoreResultSet::Constructor(napi_env env)
38 {
39 const napi_property_descriptor properties[] = {
40 DECLARE_NAPI_FUNCTION("getCount", JsKVStoreResultSet::GetCount),
41 DECLARE_NAPI_FUNCTION("getPosition", JsKVStoreResultSet::GetPosition),
42 DECLARE_NAPI_FUNCTION("moveToFirst", JsKVStoreResultSet::MoveToFirst),
43 DECLARE_NAPI_FUNCTION("moveToLast", JsKVStoreResultSet::MoveToLast),
44 DECLARE_NAPI_FUNCTION("moveToNext", JsKVStoreResultSet::MoveToNext),
45 DECLARE_NAPI_FUNCTION("moveToPrevious", JsKVStoreResultSet::MoveToPrevious),
46 DECLARE_NAPI_FUNCTION("move", JsKVStoreResultSet::Move),
47 DECLARE_NAPI_FUNCTION("moveToPosition", JsKVStoreResultSet::MoveToPosition),
48 DECLARE_NAPI_FUNCTION("isFirst", JsKVStoreResultSet::IsFirst),
49 DECLARE_NAPI_FUNCTION("isLast", JsKVStoreResultSet::IsLast),
50 DECLARE_NAPI_FUNCTION("isBeforeFirst", JsKVStoreResultSet::IsBeforeFirst),
51 DECLARE_NAPI_FUNCTION("isAfterLast", JsKVStoreResultSet::IsAfterLast),
52 DECLARE_NAPI_FUNCTION("getEntry", JsKVStoreResultSet::GetEntry)
53 };
54 size_t count = sizeof(properties) / sizeof(properties[0]);
55 return JSUtil::DefineClass(env, "KVStoreResultSet", properties, count, JsKVStoreResultSet::New);
56 }
57
New(napi_env env,napi_callback_info info)58 napi_value JsKVStoreResultSet::New(napi_env env, napi_callback_info info)
59 {
60 ZLOGD("constructor JsKVStoreResultSet!");
61 auto ctxt = std::make_shared<ContextBase>();
62 ctxt->GetCbInfoSync(env, info);
63 NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!");
64
65 JsKVStoreResultSet* resultSet = new (std::nothrow) JsKVStoreResultSet();
66 NAPI_ASSERT(env, resultSet !=nullptr, "no memory for resultSet");
67
68 auto finalize = [](napi_env env, void* data, void* hint) {
69 ZLOGD("kvStoreResultSet finalize.");
70 auto* resultSet = reinterpret_cast<JsKVStoreResultSet*>(data);
71 ASSERT_VOID(resultSet != nullptr, "finalize null!");
72 delete resultSet;
73 };
74 ASSERT_CALL(env, napi_wrap(env, ctxt->self, resultSet, finalize, nullptr, nullptr), resultSet);
75 return ctxt->self;
76 }
77
GetCount(napi_env env,napi_callback_info info)78 napi_value JsKVStoreResultSet::GetCount(napi_env env, napi_callback_info info) /* number */
79 {
80 ZLOGD("KVStoreResultSet::GetCount()");
81 auto ctxt = std::make_shared<ContextBase>();
82 ctxt->GetCbInfoSync(env, info);
83 NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!");
84 ZLOGD("KVStoreResultSet::GetCount(status=%{public}d)", ctxt->status);
85
86 auto& resultSet = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->resultSet_;
87 int count = resultSet->GetCount();
88
89 napi_create_int32(env, count, &ctxt->output);
90 return ctxt->output;
91 }
92
GetPosition(napi_env env,napi_callback_info info)93 napi_value JsKVStoreResultSet::GetPosition(napi_env env, napi_callback_info info) /* number */
94 {
95 ZLOGD("KVStoreResultSet::GetPosition()");
96 auto ctxt = std::make_shared<ContextBase>();
97 ctxt->GetCbInfoSync(env, info);
98 NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!");
99
100 auto& resultSet = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->resultSet_;
101 int position = resultSet->GetPosition();
102
103 napi_create_int32(env, position, &ctxt->output);
104 return ctxt->output;
105 }
106
MoveToFirst(napi_env env,napi_callback_info info)107 napi_value JsKVStoreResultSet::MoveToFirst(napi_env env, napi_callback_info info) /* boolean */
108 {
109 ZLOGD("KVStoreResultSet::MoveToFirst()");
110 auto ctxt = std::make_shared<ContextBase>();
111 ctxt->GetCbInfoSync(env, info);
112 NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!");
113
114 auto& resultSet = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->resultSet_;
115 bool isMoved = resultSet->MoveToFirst();
116
117 napi_get_boolean(env, isMoved, &ctxt->output);
118 return ctxt->output;
119 }
120
MoveToLast(napi_env env,napi_callback_info info)121 napi_value JsKVStoreResultSet::MoveToLast(napi_env env, napi_callback_info info) /* boolean */
122 {
123 ZLOGD("KVStoreResultSet::MoveToLast()");
124 auto ctxt = std::make_shared<ContextBase>();
125 ctxt->GetCbInfoSync(env, info);
126 NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!");
127
128 auto& resultSet = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->resultSet_;
129 bool isMoved = resultSet->MoveToLast();
130
131 napi_get_boolean(env, isMoved, &ctxt->output);
132 return ctxt->output;
133 }
134
MoveToNext(napi_env env,napi_callback_info info)135 napi_value JsKVStoreResultSet::MoveToNext(napi_env env, napi_callback_info info) /* boolean */
136 {
137 ZLOGD("KVStoreResultSet::MoveToNext()");
138 auto ctxt = std::make_shared<ContextBase>();
139 ctxt->GetCbInfoSync(env, info);
140 NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!");
141
142 auto& resultSet = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->resultSet_;
143 bool isMoved = resultSet->MoveToNext();
144
145 napi_get_boolean(env, isMoved, &ctxt->output);
146 return ctxt->output;
147 }
148
MoveToPrevious(napi_env env,napi_callback_info info)149 napi_value JsKVStoreResultSet::MoveToPrevious(napi_env env, napi_callback_info info) /* boolean */
150 {
151 ZLOGD("KVStoreResultSet::MoveToPrevious()");
152 auto ctxt = std::make_shared<ContextBase>();
153 ctxt->GetCbInfoSync(env, info);
154 NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!");
155
156 auto& resultSet = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->resultSet_;
157 bool isMoved = resultSet->MoveToPrevious();
158
159 napi_get_boolean(env, isMoved, &ctxt->output);
160 return ctxt->output;
161 }
162
Move(napi_env env,napi_callback_info info)163 napi_value JsKVStoreResultSet::Move(napi_env env, napi_callback_info info) /* boolean */
164 {
165 ZLOGD("KVStoreResultSet::Move()");
166 int offset = 0;
167 auto ctxt = std::make_shared<ContextBase>();
168 auto input = [env, ctxt, &offset](size_t argc, napi_value* argv) {
169 // required 1 arguments :: <offset>
170 ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::INVALID_ARGUMENT, "The number of parameters is incorrect.");
171 ctxt->status = napi_get_value_int32(env, argv[0], reinterpret_cast<int32_t*>(&offset));
172 };
173 ctxt->GetCbInfoSync(env, info, input);
174 ASSERT_NULL(!ctxt->isThrowError, "Move exit");
175 ASSERT_ERR(env, ctxt->status == napi_ok, Status::INVALID_ARGUMENT, "The function MoveV9 parameter is incorrect.");
176
177 auto& resultSet = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->resultSet_;
178 bool isMoved = resultSet->Move(offset);
179
180 napi_get_boolean(env, isMoved, &ctxt->output);
181 return ctxt->output;
182 }
183
MoveToPosition(napi_env env,napi_callback_info info)184 napi_value JsKVStoreResultSet::MoveToPosition(napi_env env, napi_callback_info info) /* boolean */
185 {
186 int position = 0;
187 auto ctxt = std::make_shared<ContextBase>();
188 auto input = [env, ctxt, &position](size_t argc, napi_value* argv) {
189 // required 1 arguments :: <position>
190 ASSERT_BUSINESS_ERR(ctxt, argc >= 1, Status::INVALID_ARGUMENT, "The number of parameters is incorrect.");
191 ctxt->status = napi_get_value_int32(env, argv[0], reinterpret_cast<int32_t*>(&position));
192 };
193 ctxt->GetCbInfoSync(env, info, input);
194 ASSERT_NULL(!ctxt->isThrowError, "MoveToPosition exit");
195 ASSERT_ERR(env, ctxt->status == napi_ok, Status::INVALID_ARGUMENT,
196 "The function MoveToPositionV9 parameter is incorrect.");
197 ZLOGD("KVStoreResultSet::MoveToPosition(%{public}d)", position);
198
199 auto& resultSet = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->resultSet_;
200 bool isMoved = resultSet->MoveToPosition(position);
201
202 napi_get_boolean(env, isMoved, &ctxt->output);
203 return ctxt->output;
204 }
205
IsFirst(napi_env env,napi_callback_info info)206 napi_value JsKVStoreResultSet::IsFirst(napi_env env, napi_callback_info info) /* boolean */
207 {
208 ZLOGD("KVStoreResultSet::IsFirst()");
209 auto ctxt = std::make_shared<ContextBase>();
210 ctxt->GetCbInfoSync(env, info);
211 NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!");
212
213 auto& resultSet = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->resultSet_;
214 bool isFirst = resultSet->IsFirst();
215
216 napi_get_boolean(env, isFirst, &ctxt->output);
217 return ctxt->output;
218 }
219
IsLast(napi_env env,napi_callback_info info)220 napi_value JsKVStoreResultSet::IsLast(napi_env env, napi_callback_info info) /* boolean */
221 {
222 ZLOGD("KVStoreResultSet::IsLast()");
223 auto ctxt = std::make_shared<ContextBase>();
224 ctxt->GetCbInfoSync(env, info);
225 NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!");
226
227 auto& resultSet = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->resultSet_;
228 bool isLast = resultSet->IsLast();
229
230 napi_get_boolean(env, isLast, &ctxt->output);
231 return ctxt->output;
232 }
233
IsBeforeFirst(napi_env env,napi_callback_info info)234 napi_value JsKVStoreResultSet::IsBeforeFirst(napi_env env, napi_callback_info info) /* boolean */
235 {
236 ZLOGD("KVStoreResultSet::IsBeforeFirst()");
237 auto ctxt = std::make_shared<ContextBase>();
238 ctxt->GetCbInfoSync(env, info);
239 NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!");
240
241 auto& resultSet = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->resultSet_;
242 bool isBeforeFirst = resultSet->IsBeforeFirst();
243
244 napi_get_boolean(env, isBeforeFirst, &ctxt->output);
245 return ctxt->output;
246 }
247
IsAfterLast(napi_env env,napi_callback_info info)248 napi_value JsKVStoreResultSet::IsAfterLast(napi_env env, napi_callback_info info) /* boolean */
249 {
250 ZLOGD("KVStoreResultSet::IsAfterLast()");
251 auto ctxt = std::make_shared<ContextBase>();
252 ctxt->GetCbInfoSync(env, info);
253 NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!");
254
255 auto& resultSet = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->resultSet_;
256 bool isAfterLast = resultSet->IsAfterLast();
257
258 napi_get_boolean(env, isAfterLast, &ctxt->output);
259 return ctxt->output;
260 }
261
GetEntry(napi_env env,napi_callback_info info)262 napi_value JsKVStoreResultSet::GetEntry(napi_env env, napi_callback_info info) /* Entry */
263 {
264 ZLOGD("KVStoreResultSet::GetEntry()");
265 auto ctxt = std::make_shared<ContextBase>();
266 ctxt->GetCbInfoSync(env, info);
267 NAPI_ASSERT(env, ctxt->status == napi_ok, "invalid arguments!");
268
269 DistributedKv::Entry entry;
270 auto& resultSet = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->resultSet_;
271 bool isSchema = reinterpret_cast<JsKVStoreResultSet*>(ctxt->native)->isSchema_;
272 auto status = resultSet->GetEntry(entry);
273 if (status != Status::SUCCESS) {
274 return nullptr;
275 }
276
277 ctxt->status = JSUtil::SetValue(env, entry, ctxt->output, isSchema);
278 NAPI_ASSERT(env, ctxt->status == napi_ok, "GetEntry failed!");
279 return ctxt->output;
280 }
281
Create()282 std::shared_ptr<ResultSetBridge> JsKVStoreResultSet::Create()
283 {
284 return KvUtils::ToResultSetBridge(resultSet_);
285 }
286
SetSchema(bool isSchema)287 void JsKVStoreResultSet::SetSchema(bool isSchema)
288 {
289 isSchema_ = isSchema;
290 }
291 } // namespace OHOS::DistributedKVStore
292