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