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 "ResultSetProxy"
16 #include "napi_result_set.h"
17
18 #include <functional>
19
20 #include "js_utils.h"
21 #include "logger.h"
22 #include "napi_rdb_error.h"
23 #include "napi_rdb_trace.h"
24 #include "rdb_errno.h"
25
26 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM)
27 #include "abs_shared_result_set.h"
28 #include "rdb_result_set_bridge.h"
29 #include "shared_block.h"
30 #include "string_ex.h"
31 #endif
32
33 using namespace OHOS::Rdb;
34 using namespace OHOS::NativeRdb;
35 using namespace OHOS::AppDataMgrJsKit;
36
37 namespace OHOS {
38 namespace RdbJsKit {
39 static napi_ref __thread ctorRef_ = nullptr;
40 static napi_ref __thread ctorRefV9_ = nullptr;
41 static const int E_OK = 0;
42
43 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM)
NewInstance(napi_env env,std::shared_ptr<AbsSharedResultSet> resultSet,int version)44 napi_value ResultSetProxy::NewInstance(napi_env env, std::shared_ptr<AbsSharedResultSet> resultSet, int version)
45 {
46 auto instance = NewInstance(env, std::static_pointer_cast<NativeRdb::ResultSet>(resultSet), version);
47 ResultSetProxy *proxy = nullptr;
48 auto status = napi_unwrap(env, instance, reinterpret_cast<void **>(&proxy));
49 if (proxy == nullptr) {
50 LOG_ERROR("NewInstance native instance is nullptr! code:%{public}d!", status);
51 return instance;
52 }
53
54 if (resultSet != nullptr && resultSet->GetBlock() != nullptr) {
55 proxy->sharedBlockName_ = resultSet->GetBlock()->Name();
56 proxy->sharedBlockAshmemFd_ = resultSet->GetBlock()->GetFd();
57 }
58 proxy->sharedResultSet_ = resultSet;
59 return instance;
60 }
61 #endif
62
NewInstance(napi_env env,std::shared_ptr<NativeRdb::ResultSet> resultSet,int version)63 napi_value ResultSetProxy::NewInstance(napi_env env, std::shared_ptr<NativeRdb::ResultSet> resultSet, int version)
64 {
65 napi_value cons = GetConstructor(env, version);
66 if (cons == nullptr) {
67 LOG_ERROR("NewInstance GetConstructor is nullptr!");
68 return nullptr;
69 }
70 napi_value instance = nullptr;
71 napi_status status = napi_new_instance(env, cons, 0, nullptr, &instance);
72 if (status != napi_ok) {
73 LOG_ERROR("NewInstance napi_new_instance failed! code:%{public}d!", status);
74 return nullptr;
75 }
76
77 ResultSetProxy *proxy = nullptr;
78 status = napi_unwrap(env, instance, reinterpret_cast<void **>(&proxy));
79 if (proxy == nullptr) {
80 LOG_ERROR("NewInstance native instance is nullptr! code:%{public}d!", status);
81 return instance;
82 }
83 *proxy = std::move(resultSet);
84 return instance;
85 }
86
87 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM)
GetNativeObject(napi_env const env,napi_value const arg)88 std::shared_ptr<NativeRdb::AbsSharedResultSet> ResultSetProxy::GetNativeObject(
89 napi_env const env, napi_value const arg)
90 {
91 if (arg == nullptr) {
92 LOG_ERROR("ResultSetProxy GetNativeObject arg is null.");
93 return nullptr;
94 }
95 ResultSetProxy *proxy = nullptr;
96 napi_unwrap(env, arg, reinterpret_cast<void **>(&proxy));
97 if (proxy == nullptr || proxy->sharedResultSet_ == nullptr) {
98 LOG_ERROR("ResultSetProxy GetNativeObject proxy or sharedResultSet_ is null.");
99 return nullptr;
100 }
101 return proxy->sharedResultSet_;
102 }
103
Create()104 std::shared_ptr<DataShare::ResultSetBridge> ResultSetProxy::Create()
105 {
106 auto instance = GetInstance();
107 if (instance == nullptr) {
108 LOG_ERROR("resultSet_ is null.");
109 return nullptr;
110 }
111 SetInstance(nullptr);
112 return std::make_shared<RdbDataShareAdapter::RdbResultSetBridge>(instance);
113 }
114 #endif
115
GetConstructor(napi_env env,int version)116 napi_value ResultSetProxy::GetConstructor(napi_env env, int version)
117 {
118 napi_value cons = nullptr;
119 if (version > APIVERSION_8 && ctorRefV9_ != nullptr) {
120 NAPI_CALL(env, napi_get_reference_value(env, ctorRefV9_, &cons));
121 return cons;
122 }
123 if (version == APIVERSION_8 && ctorRef_ != nullptr) {
124 NAPI_CALL(env, napi_get_reference_value(env, ctorRef_, &cons));
125 return cons;
126 }
127
128 napi_property_descriptor clzDes[] = {
129 DECLARE_NAPI_FUNCTION("goToRow", GoToRow),
130 DECLARE_NAPI_FUNCTION("getLong", GetLong),
131 DECLARE_NAPI_FUNCTION("getColumnType", GetColumnType),
132 DECLARE_NAPI_FUNCTION("goTo", GoTo),
133 DECLARE_NAPI_FUNCTION("getColumnIndex", GetColumnIndex),
134 DECLARE_NAPI_FUNCTION("getInt", GetInt),
135 DECLARE_NAPI_FUNCTION("getColumnName", GetColumnName),
136 DECLARE_NAPI_FUNCTION("close", Close),
137 DECLARE_NAPI_FUNCTION("goToFirstRow", GoToFirstRow),
138 DECLARE_NAPI_FUNCTION("goToLastRow", GoToLastRow),
139 DECLARE_NAPI_FUNCTION("goToNextRow", GoToNextRow),
140 DECLARE_NAPI_FUNCTION("goToPreviousRow", GoToPreviousRow),
141 DECLARE_NAPI_FUNCTION("getBlob", GetBlob),
142 DECLARE_NAPI_FUNCTION("getString", GetString),
143 DECLARE_NAPI_FUNCTION("getDouble", GetDouble),
144 DECLARE_NAPI_FUNCTION("isColumnNull", IsColumnNull),
145
146 DECLARE_NAPI_GETTER("columnNames", GetAllColumnNames),
147 DECLARE_NAPI_GETTER("columnCount", GetColumnCount),
148 DECLARE_NAPI_GETTER("isEnded", IsEnded),
149 DECLARE_NAPI_GETTER("isStarted", IsBegin),
150 DECLARE_NAPI_GETTER("isClosed", IsClosed),
151 DECLARE_NAPI_GETTER("rowCount", GetRowCount),
152 DECLARE_NAPI_GETTER("rowIndex", GetRowIndex),
153 DECLARE_NAPI_GETTER("isAtFirstRow", IsAtFirstRow),
154 DECLARE_NAPI_GETTER("isAtLastRow", IsAtLastRow),
155 };
156
157 if (version > APIVERSION_8) {
158 NAPI_CALL(env, napi_define_class(env, "ResultSetV9", NAPI_AUTO_LENGTH, InitializeV9, nullptr,
159 sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons));
160 NAPI_CALL(env, napi_create_reference(env, cons, 1, &ctorRefV9_));
161 return cons;
162 }
163
164 NAPI_CALL(env, napi_define_class(env, "ResultSet", NAPI_AUTO_LENGTH, Initialize, nullptr,
165 sizeof(clzDes) / sizeof(napi_property_descriptor), clzDes, &cons));
166 NAPI_CALL(env, napi_create_reference(env, cons, 1, &ctorRef_));
167 return cons;
168 }
169
InnerInitialize(napi_env env,napi_callback_info info,int version)170 napi_value ResultSetProxy::InnerInitialize(napi_env env, napi_callback_info info, int version)
171 {
172 napi_value self = nullptr;
173 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &self, nullptr));
174 auto *proxy = new (std::nothrow) ResultSetProxy();
175 if (proxy == nullptr) {
176 LOG_ERROR("ResultSetProxy::InnerInitialize new failed, proxy is nullptr.");
177 return nullptr;
178 }
179 proxy->apiversion = version;
180 auto finalize = [](napi_env env, void *data, void *hint) {
181 ResultSetProxy *proxy = reinterpret_cast<ResultSetProxy *>(data);
182 delete proxy;
183 };
184 napi_status status = napi_wrap(env, self, proxy, finalize, nullptr, nullptr);
185 if (status != napi_ok) {
186 LOG_ERROR("ResultSetProxy napi_wrap failed! code:%{public}d!, version:%{public}d.", status, version);
187 finalize(env, proxy, nullptr);
188 return nullptr;
189 }
190 return self;
191 }
192
Initialize(napi_env env,napi_callback_info info)193 napi_value ResultSetProxy::Initialize(napi_env env, napi_callback_info info)
194 {
195 return InnerInitialize(env, info, APIVERSION_8);
196 }
197
InitializeV9(napi_env env,napi_callback_info info)198 napi_value ResultSetProxy::InitializeV9(napi_env env, napi_callback_info info)
199 {
200 return InnerInitialize(env, info, APIVERSION_V9);
201 }
202
~ResultSetProxy()203 ResultSetProxy::~ResultSetProxy()
204 {
205 LOG_DEBUG("ResultSetProxy destructor!");
206 }
207
ResultSetProxy(std::shared_ptr<ResultSet> resultSet)208 ResultSetProxy::ResultSetProxy(std::shared_ptr<ResultSet> resultSet)
209 {
210 if (GetInstance() == resultSet) {
211 return;
212 }
213 SetInstance(std::move(resultSet));
214 }
215
operator =(std::shared_ptr<ResultSet> resultSet)216 ResultSetProxy &ResultSetProxy::operator=(std::shared_ptr<ResultSet> resultSet)
217 {
218 if (GetInstance() == resultSet) {
219 return *this;
220 }
221 SetInstance(std::move(resultSet));
222 return *this;
223 }
224
GetInnerResultSet(napi_env env,napi_callback_info info,int & version)225 ResultSetProxy *ResultSetProxy::GetInnerResultSet(napi_env env, napi_callback_info info, int &version)
226 {
227 ResultSetProxy *resultSetProxy = nullptr;
228 napi_value self = nullptr;
229 napi_get_cb_info(env, info, nullptr, nullptr, &self, nullptr);
230 napi_unwrap(env, self, reinterpret_cast<void **>(&resultSetProxy));
231 RDB_NAPI_ASSERT_FROMV9(env, resultSetProxy && resultSetProxy->GetInstance(), std::make_shared<ResultGotoError>(),
232 resultSetProxy->apiversion);
233
234 version = resultSetProxy->apiversion;
235 return resultSetProxy;
236 }
237
ParseInt32FieldByName(napi_env env,napi_callback_info info,int32_t & field,const std::string & name)238 ResultSetProxy *ResultSetProxy::ParseInt32FieldByName(
239 napi_env env, napi_callback_info info, int32_t &field, const std::string& name)
240 {
241 DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
242 napi_value self = nullptr;
243 size_t argc = 1;
244 napi_value args[1] = { 0 };
245 napi_get_cb_info(env, info, &argc, args, &self, nullptr);
246 ResultSetProxy *resultSetProxy = nullptr;
247 napi_unwrap(env, self, reinterpret_cast<void **>(&resultSetProxy));
248 RDB_NAPI_ASSERT_FROMV9(env, resultSetProxy && resultSetProxy->GetInstance(),
249 std::make_shared<ParamTypeError>("resultSet", "not null"), resultSetProxy->apiversion);
250 RDB_NAPI_ASSERT_FROMV9(
251 env, argc == 1, std::make_shared<ParamNumError>("1"), resultSetProxy->apiversion);
252
253 napi_status status = napi_get_value_int32(env, args[0], &field);
254 RDB_NAPI_ASSERT_FROMV9(
255 env, status == napi_ok, std::make_shared<ParamTypeError>(name, "a number."), resultSetProxy->apiversion);
256 return resultSetProxy;
257 }
258
ParseFieldByName(napi_env env,napi_callback_info info,std::string & field)259 ResultSetProxy *ResultSetProxy::ParseFieldByName(napi_env env, napi_callback_info info, std::string &field)
260 {
261 napi_value self = nullptr;
262 size_t argc = 1;
263 napi_value args[1] = { 0 };
264 napi_get_cb_info(env, info, &argc, args, &self, nullptr);
265 ResultSetProxy *resultSetProxy = nullptr;
266 napi_unwrap(env, self, reinterpret_cast<void **>(&resultSetProxy));
267 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
268 RDB_NAPI_ASSERT_FROMV9(
269 env, argc == 1, std::make_shared<ParamNumError>("1"), resultSetProxy->apiversion);
270
271 field = JSUtils::Convert2String(env, args[0]);
272 return resultSetProxy;
273 }
274
GetAllColumnNames(napi_env env,napi_callback_info info)275 napi_value ResultSetProxy::GetAllColumnNames(napi_env env, napi_callback_info info)
276 {
277 std::vector<std::string> colNames;
278 int version = 0;
279 auto resultSetProxy = GetInnerResultSet(env, info, version);
280 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
281
282 int errCode = resultSetProxy->GetInstance()->GetAllColumnNames(colNames);
283 if (errCode != E_OK) {
284 LOG_ERROR("GetAllColumnNames failed code:%{public}d, version:%{public}d.", errCode, version);
285 }
286 return JSUtils::Convert2JSValue(env, colNames);
287 }
288
GoToRow(napi_env env,napi_callback_info info)289 napi_value ResultSetProxy::GoToRow(napi_env env, napi_callback_info info)
290 {
291 int32_t position;
292 auto resultSetProxy = ParseInt32FieldByName(env, info, position, "position");
293 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
294
295 int errCode = resultSetProxy->GetInstance()->GoToRow(position);
296 return JSUtils::Convert2JSValue(env, (errCode == E_OK));
297 }
298
GetColumnCount(napi_env env,napi_callback_info info)299 napi_value ResultSetProxy::GetColumnCount(napi_env env, napi_callback_info info)
300 {
301 int32_t count = 0;
302 int version = 0;
303 auto resultSetProxy = GetInnerResultSet(env, info, version);
304 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
305
306 int errCode = resultSetProxy->GetInstance()->GetColumnCount(count);
307 if (errCode != E_OK) {
308 LOG_ERROR("GetColumnCount failed code:%{public}d, version:%{public}d.", errCode, version);
309 }
310 return JSUtils::Convert2JSValue(env, count);
311 }
312
GetLong(napi_env env,napi_callback_info info)313 napi_value ResultSetProxy::GetLong(napi_env env, napi_callback_info info)
314 {
315 int32_t columnIndex;
316 int64_t result;
317 auto resultSetProxy = ParseInt32FieldByName(env, info, columnIndex, "columnIndex");
318 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
319
320 int errCode = resultSetProxy->GetInstance()->GetLong(columnIndex, result);
321 int version = resultSetProxy->apiversion;
322 RDB_NAPI_ASSERT_FROMV9(env, errCode == E_OK, std::make_shared<ResultGetError>(), version);
323 return JSUtils::Convert2JSValue(env, result);
324 }
325
GetColumnType(napi_env env,napi_callback_info info)326 napi_value ResultSetProxy::GetColumnType(napi_env env, napi_callback_info info)
327 {
328 int32_t columnIndex;
329 ColumnType columnType;
330 auto resultSetProxy = ParseInt32FieldByName(env, info, columnIndex, "columnIndex");
331 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
332
333 int errCode = resultSetProxy->GetInstance()->GetColumnType(columnIndex, columnType);
334 if (errCode != E_OK) {
335 LOG_ERROR("GetColumnType failed code:%{public}d, version:%{public}d.", errCode, resultSetProxy->apiversion);
336 }
337 return JSUtils::Convert2JSValue(env, int32_t(columnType));
338 }
339
GoTo(napi_env env,napi_callback_info info)340 napi_value ResultSetProxy::GoTo(napi_env env, napi_callback_info info)
341 {
342 int32_t offset;
343 auto resultSetProxy = ParseInt32FieldByName(env, info, offset, "offset");
344 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
345
346 int errCode = resultSetProxy->GetInstance()->GoTo(offset);
347 return JSUtils::Convert2JSValue(env, (errCode == E_OK));
348 }
349
GetColumnIndex(napi_env env,napi_callback_info info)350 napi_value ResultSetProxy::GetColumnIndex(napi_env env, napi_callback_info info)
351 {
352 std::string input;
353 int32_t result = -1;
354 auto resultSetProxy = ParseFieldByName(env, info, input);
355 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
356
357 int errCode = resultSetProxy->GetInstance()->GetColumnIndex(input, result);
358 if (errCode != E_OK) {
359 LOG_ERROR("GetColumnIndex failed code:%{public}d, version:%{public}d.", errCode, resultSetProxy->apiversion);
360 }
361 return JSUtils::Convert2JSValue(env, result);
362 }
363
GetInt(napi_env env,napi_callback_info info)364 napi_value ResultSetProxy::GetInt(napi_env env, napi_callback_info info)
365 {
366 int32_t columnIndex;
367 int32_t result;
368 auto resultSetProxy = ParseInt32FieldByName(env, info, columnIndex, "columnIndex");
369 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
370
371 int errCode = resultSetProxy->GetInstance()->GetInt(columnIndex, result);
372 int version = resultSetProxy->apiversion;
373 RDB_NAPI_ASSERT_FROMV9(env, errCode == E_OK, std::make_shared<ResultGetError>(), version);
374 return JSUtils::Convert2JSValue(env, result);
375 }
376
GetColumnName(napi_env env,napi_callback_info info)377 napi_value ResultSetProxy::GetColumnName(napi_env env, napi_callback_info info)
378 {
379 int32_t columnIndex;
380 std::string result;
381 auto resultSetProxy = ParseInt32FieldByName(env, info, columnIndex, "columnIndex");
382 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
383
384 int errCode = resultSetProxy->GetInstance()->GetColumnName(columnIndex, result);
385 if (errCode != E_OK) {
386 LOG_ERROR("GetColumnName failed code:%{public}d, version:%{public}d.", errCode, resultSetProxy->apiversion);
387 }
388 return JSUtils::Convert2JSValue(env, result);
389 }
390
Close(napi_env env,napi_callback_info info)391 napi_value ResultSetProxy::Close(napi_env env, napi_callback_info info)
392 {
393 int version = 0;
394 auto resultSetProxy = GetInnerResultSet(env, info, version);
395 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
396
397 int errCode = resultSetProxy->GetInstance()->Close();
398 RDB_NAPI_ASSERT_FROMV9(env, errCode == E_OK, std::make_shared<ResultGotoError>(), version);
399 napi_value result = nullptr;
400 napi_get_null(env, &result);
401 return result;
402 }
403
GetRowCount(napi_env env,napi_callback_info info)404 napi_value ResultSetProxy::GetRowCount(napi_env env, napi_callback_info info)
405 {
406 int version = 0;
407 ResultSetProxy *resultSetProxy = GetInnerResultSet(env, info, version);
408 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
409
410 int32_t result;
411 int errCode = resultSetProxy->GetInstance()->GetRowCount(result);
412 if (errCode != E_OK) {
413 LOG_ERROR("GetRowCount failed code:%{public}d, version:%{public}d.", errCode, version);
414 }
415 return JSUtils::Convert2JSValue(env, result);
416 }
417
GetRowIndex(napi_env env,napi_callback_info info)418 napi_value ResultSetProxy::GetRowIndex(napi_env env, napi_callback_info info)
419 {
420 int version = 0;
421 auto resultSetProxy = GetInnerResultSet(env, info, version);
422 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
423
424 int32_t result;
425 int errCode = resultSetProxy->GetInstance()->GetRowIndex(result);
426 if (errCode != E_OK) {
427 LOG_ERROR("GetRowIndex failed code:%{public}d, version:%{public}d.", errCode, version);
428 }
429 return JSUtils::Convert2JSValue(env, result);
430 }
431
IsEnded(napi_env env,napi_callback_info info)432 napi_value ResultSetProxy::IsEnded(napi_env env, napi_callback_info info)
433 {
434 int version = 0;
435 auto resultSetProxy = GetInnerResultSet(env, info, version);
436 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
437
438 bool result = false;
439 int errCode = resultSetProxy->GetInstance()->IsEnded(result);
440 if (errCode != E_OK) {
441 LOG_ERROR("IsEnded failed code:%{public}d, version:%{public}d.", errCode, version);
442 result = true;
443 }
444 return JSUtils::Convert2JSValue(env, result);
445 }
446
IsBegin(napi_env env,napi_callback_info info)447 napi_value ResultSetProxy::IsBegin(napi_env env, napi_callback_info info)
448 {
449 int version = 0;
450 auto resultSetProxy = GetInnerResultSet(env, info, version);
451 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
452
453 bool result = false;
454 int errCode = resultSetProxy->GetInstance()->IsStarted(result);
455 if (errCode != E_OK) {
456 LOG_ERROR("IsBegin failed code:%{public}d, version:%{public}d.", errCode, version);
457 }
458 return JSUtils::Convert2JSValue(env, result);
459 }
460
GoToFirstRow(napi_env env,napi_callback_info info)461 napi_value ResultSetProxy::GoToFirstRow(napi_env env, napi_callback_info info)
462 {
463 int version = 0;
464 auto resultSetProxy = GetInnerResultSet(env, info, version);
465 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
466
467 int errCode = resultSetProxy->GetInstance()->GoToFirstRow();
468 return JSUtils::Convert2JSValue(env, (errCode == E_OK));
469 }
470
GoToLastRow(napi_env env,napi_callback_info info)471 napi_value ResultSetProxy::GoToLastRow(napi_env env, napi_callback_info info)
472 {
473 int version = 0;
474 auto resultSetProxy = GetInnerResultSet(env, info, version);
475 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
476
477 int errCode = resultSetProxy->GetInstance()->GoToLastRow();
478 return JSUtils::Convert2JSValue(env, (errCode == E_OK));
479 }
480
GoToNextRow(napi_env env,napi_callback_info info)481 napi_value ResultSetProxy::GoToNextRow(napi_env env, napi_callback_info info)
482 {
483 int version = 0;
484 auto resultSetProxy = GetInnerResultSet(env, info, version);
485 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
486
487 int errCode = resultSetProxy->GetInstance()->GoToNextRow();
488 return JSUtils::Convert2JSValue(env, (errCode == E_OK));
489 }
490
GoToPreviousRow(napi_env env,napi_callback_info info)491 napi_value ResultSetProxy::GoToPreviousRow(napi_env env, napi_callback_info info)
492 {
493 int version = 0;
494 auto resultSetProxy = GetInnerResultSet(env, info, version);
495 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
496
497 int errCode = resultSetProxy->GetInstance()->GoToPreviousRow();
498 return JSUtils::Convert2JSValue(env, (errCode == E_OK));
499 }
500
IsAtFirstRow(napi_env env,napi_callback_info info)501 napi_value ResultSetProxy::IsAtFirstRow(napi_env env, napi_callback_info info)
502 {
503 int version = 0;
504 auto resultSetProxy = GetInnerResultSet(env, info, version);
505 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
506
507 bool result = false;
508 int errCode = resultSetProxy->GetInstance()->IsAtFirstRow(result);
509 if (errCode != E_OK) {
510 LOG_ERROR("IsAtFirstRow failed code:%{public}d, version:%{public}d.", errCode, version);
511 }
512 return JSUtils::Convert2JSValue(env, result);
513 }
514
IsAtLastRow(napi_env env,napi_callback_info info)515 napi_value ResultSetProxy::IsAtLastRow(napi_env env, napi_callback_info info)
516 {
517 int version = 0;
518 auto resultSetProxy = GetInnerResultSet(env, info, version);
519 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
520
521 bool result = false;
522 int errCode = resultSetProxy->GetInstance()->IsAtLastRow(result);
523 if (errCode != E_OK) {
524 LOG_ERROR("IsAtLastRow failed code:%{public}d, version:%{public}d.", errCode, version);
525 }
526 return JSUtils::Convert2JSValue(env, result);
527 }
528
GetBlob(napi_env env,napi_callback_info info)529 napi_value ResultSetProxy::GetBlob(napi_env env, napi_callback_info info)
530 {
531 DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
532 int32_t columnIndex;
533 std::vector<uint8_t> result;
534 auto resultSetProxy = ParseInt32FieldByName(env, info, columnIndex, "columnIndex");
535 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
536
537 int errCode = resultSetProxy->GetInstance()->GetBlob(columnIndex, result);
538 int version = resultSetProxy->apiversion;
539 RDB_NAPI_ASSERT_FROMV9(env, errCode == E_OK, std::make_shared<ResultGetError>(), version);
540 return JSUtils::Convert2JSValue(env, result);
541 }
542
GetString(napi_env env,napi_callback_info info)543 napi_value ResultSetProxy::GetString(napi_env env, napi_callback_info info)
544 {
545 DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__));
546 int32_t columnIndex;
547 std::string result;
548 auto resultSetProxy = ParseInt32FieldByName(env, info, columnIndex, "columnIndex");
549 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
550
551 int errCode = resultSetProxy->GetInstance()->GetString(columnIndex, result);
552 int version = resultSetProxy->apiversion;
553 RDB_NAPI_ASSERT_FROMV9(env, errCode == E_OK, std::make_shared<ResultGetError>(), version);
554 return JSUtils::Convert2JSValue(env, result);
555 }
556
GetDouble(napi_env env,napi_callback_info info)557 napi_value ResultSetProxy::GetDouble(napi_env env, napi_callback_info info)
558 {
559 int32_t columnIndex;
560 double result = 0.0;
561 auto resultSetProxy = ParseInt32FieldByName(env, info, columnIndex, "columnIndex");
562 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
563
564 int errCode = resultSetProxy->GetInstance()->GetDouble(columnIndex, result);
565 int version = resultSetProxy->apiversion;
566 RDB_NAPI_ASSERT_FROMV9(env, errCode == E_OK, std::make_shared<ResultGetError>(), version);
567 return JSUtils::Convert2JSValue(env, result);
568 }
569
IsColumnNull(napi_env env,napi_callback_info info)570 napi_value ResultSetProxy::IsColumnNull(napi_env env, napi_callback_info info)
571 {
572 int32_t columnIndex;
573 bool result = false;
574 auto resultSetProxy = ParseInt32FieldByName(env, info, columnIndex, "columnIndex");
575 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
576
577 int errCode = resultSetProxy->GetInstance()->IsColumnNull(columnIndex, result);
578 int version = resultSetProxy->apiversion;
579 RDB_NAPI_ASSERT_FROMV9(env, errCode == E_OK, std::make_shared<ResultGetError>(), version);
580 napi_value output = nullptr;
581 napi_get_boolean(env, result, &output);
582 return output;
583 }
584
IsClosed(napi_env env,napi_callback_info info)585 napi_value ResultSetProxy::IsClosed(napi_env env, napi_callback_info info)
586 {
587 int version = 0;
588 auto resultSetProxy = GetInnerResultSet(env, info, version);
589 RDB_CHECK_RETURN_NULLPTR(resultSetProxy && resultSetProxy->GetInstance(), "Proxy or instance is nullptr");
590
591 int result = resultSetProxy->GetInstance()->IsClosed();
592 napi_value output = nullptr;
593 napi_get_boolean(env, result, &output);
594 return output;
595 }
596
GetSharedBlockName(napi_env env,napi_callback_info info)597 napi_value ResultSetProxy::GetSharedBlockName(napi_env env, napi_callback_info info)
598 {
599 napi_value thiz = nullptr;
600 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thiz, nullptr));
601
602 ResultSetProxy *proxy;
603 NAPI_CALL(env, napi_unwrap(env, thiz, reinterpret_cast<void **>(&proxy)));
604 RDB_CHECK_RETURN_NULLPTR(proxy != nullptr,
605 "ResultSetProxy::GetSharedBlockName proxy is nullptr");
606
607 return JSUtils::Convert2JSValue(env, proxy->sharedBlockName_);
608 }
609
GetSharedBlockAshmemFd(napi_env env,napi_callback_info info)610 napi_value ResultSetProxy::GetSharedBlockAshmemFd(napi_env env, napi_callback_info info)
611 {
612 napi_value thiz = nullptr;
613 NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thiz, nullptr));
614
615 ResultSetProxy *proxy;
616 NAPI_CALL(env, napi_unwrap(env, thiz, reinterpret_cast<void **>(&proxy)));
617 RDB_CHECK_RETURN_NULLPTR(proxy != nullptr,
618 "ResultSetProxy::GetSharedBlockAshmemFd proxy is nullptr");
619
620 return JSUtils::Convert2JSValue(env, proxy->sharedBlockAshmemFd_);
621 }
622 } // namespace RdbJsKit
623 } // namespace OHOS
624
625 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM)
626
627 __attribute__((visibility("default"))) napi_value NewCInstance(napi_env env,
628 napi_value arg) asm("NAPI_OHOS_Data_RdbJsKit_ResultSetProxy_NewInstance");
NewCInstance(napi_env env,std::shared_ptr<OHOS::NativeRdb::AbsSharedResultSet> resultSet)629 napi_value NewCInstance(napi_env env, std::shared_ptr<OHOS::NativeRdb::AbsSharedResultSet> resultSet)
630 {
631 return OHOS::RdbJsKit::ResultSetProxy::NewInstance(env, resultSet);
632 }
633
634 __attribute__((visibility("default"))) std::shared_ptr<OHOS::NativeRdb::AbsSharedResultSet> GetCObject(napi_env env,
635 napi_value arg) asm("NAPI_OHOS_Data_RdbJsKit_ResultSetProxy_GetNativeObject");
GetCObject(napi_env env,napi_value arg)636 std::shared_ptr<OHOS::NativeRdb::AbsSharedResultSet> GetCObject(napi_env env, napi_value arg)
637 {
638 return OHOS::RdbJsKit::ResultSetProxy::GetNativeObject(env, arg);
639 }
640 #endif