1 /*
2 * Copyright (C) 2025-2025 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 #ifndef INTERFACES_KITS_JS_RDB_INCLUDE_RESULT_SET_NAPI_H_
17 #define INTERFACES_KITS_JS_RDB_INCLUDE_RESULT_SET_NAPI_H_
18
19 #include <vector>
20
21 #include "medialibrary_napi_utils.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "result_set.h"
25 #include "napi_remote_object.h"
26 #include "datashare_helper.h"
27 #include "napi_error.h"
28
29 namespace OHOS {
30 namespace Media {
31 #define EXPORT __attribute__ ((visibility ("default")))
32
33 using namespace OHOS::NativeRdb;
34
35 static const std::string RESULT_SET_NAPI_CLASS_NAME = "ResultSet";
36
37 class ResultSetNapi {
38 public:
39 std::shared_ptr<ResultSet> resultSetPtr = nullptr;
40 EXPORT static napi_value Init(napi_env env, napi_value exports);
41 EXPORT static napi_value CreateResultSetNapi(napi_env env, std::shared_ptr<ResultSet> &resultSet,
42 JSAsyncContextOutput &asyncContext);
43 explicit ResultSetNapi(std::shared_ptr<ResultSet> ptr);
44 EXPORT ~ResultSetNapi();
45
46 private:
47 EXPORT static void ResultSetNapiDestructor(napi_env env, void *nativeObject, void *finalize_hint);
48 EXPORT static napi_value ResultSetNapiConstructor(napi_env env, napi_callback_info info);
49 // NAPI Property declarations
50 EXPORT static napi_value JSGetColumnCount(napi_env env, napi_callback_info info);
51 EXPORT static napi_value JSGetRowCount(napi_env env, napi_callback_info info);
52 EXPORT static napi_value JSGetRowIndex(napi_env env, napi_callback_info info);
53 EXPORT static napi_value JSIsAtLastRow(napi_env env, napi_callback_info info);
54 // NAPI Method declarations
55 EXPORT static napi_value JSGoToRow(napi_env env, napi_callback_info info);
56 EXPORT static napi_value JSGoToFirstRow(napi_env env, napi_callback_info info);
57 EXPORT static napi_value JSGoToNextRow(napi_env env, napi_callback_info info);
58 EXPORT static napi_value JSGetBlob(napi_env env, napi_callback_info info);
59 EXPORT static napi_value JSGetString(napi_env env, napi_callback_info info);
60 EXPORT static napi_value JSGetLong(napi_env env, napi_callback_info info);
61 EXPORT static napi_value JSGetDouble(napi_env env, napi_callback_info info);
62 EXPORT static napi_value JSGetBool(napi_env env, napi_callback_info info);
63 EXPORT static napi_value JSGetValue(napi_env env, napi_callback_info info);
64 EXPORT static napi_value JSGetRow(napi_env env, napi_callback_info info);
65 EXPORT static napi_value JSClose(napi_env env, napi_callback_info info);
66
67 napi_env env_;
68 static thread_local napi_ref sResultSetConstructor_;
69 };
70 struct ResultSetAsyncContext : public NapiError {
71 std::string networkId;
72 std::string uri;
73 string errorMsg;
74 size_t argc;
75 napi_value argv[NAPI_ARGC_MAX];
76 napi_async_work work;
77 napi_deferred deferred;
78 napi_ref callbackRef;
79 napi_value napiArrayBuffer;
80 std::shared_ptr<NativeRdb::ResultSet> queryRet = nullptr;
81 ResultSetNapi *objectInfo;
82 };
83 // c++变量转换js
84 template <typename T>
Convert2JSValue(napi_env env,const T & value)85 napi_value Convert2JSValue(napi_env env, const T &value)
86 {
87 NAPI_ERR_LOG("No conversion implemented for this type, returning undefined");
88 return nullptr;
89 }
90
91 template <typename T>
Convert2JSValue(napi_env env,const std::vector<T> & value)92 napi_value Convert2JSValue(napi_env env, const std::vector<T> &value)
93 {
94 napi_value jsValue = nullptr;
95 napi_status status = napi_create_array_with_length(env, value.size(), &jsValue);
96 if (status != napi_ok) {
97 NAPI_ERR_LOG("Napi_create_array_with_length failed");
98 return jsValue;
99 }
100 for (size_t i = 0; i < value.size(); ++i) {
101 napi_set_element(env, jsValue, i, Convert2JSValue(env, value[i]));
102 }
103 return jsValue;
104 }
105
106 template <typename... Types>
Convert2JSValue(napi_env env,const std::variant<Types...> & value)107 napi_value Convert2JSValue(napi_env env, const std::variant<Types...> &value)
108 {
109 return std::visit(
110 [&](const auto &v) -> napi_value {
111 return Convert2JSValue(env, v); // 这里会自动选择最匹配的Convert2JSValue重载!
112 },
113 value);
114 }
115 }
116 }
117 #endif // INTERFACES_KITS_JS_RDB_INCLUDE_RESULT_SET_NAPI_H_