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