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