1 /*
2  * Copyright (c) 2023 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 RDB_JSKIT_NAPI_RDB_JS_UTILS_H
17 #define RDB_JSKIT_NAPI_RDB_JS_UTILS_H
18 
19 #include <stdint.h>
20 
21 #include "asset_value.h"
22 #include "js_sendable_utils.h"
23 #include "js_utils.h"
24 #include "napi_rdb_error.h"
25 #include "napi_rdb_store_observer.h"
26 #include "rdb_store_config.h"
27 #include "result_set.h"
28 #include "value_object.h"
29 #include "values_bucket.h"
30 #include "values_buckets.h"
31 
32 namespace OHOS::AppDataMgrJsKit {
33 namespace JSUtils {
34 using Asset = OHOS::NativeRdb::AssetValue;
35 using RowEntity = OHOS::NativeRdb::RowEntity;
36 using ValueObject = OHOS::NativeRdb::ValueObject;
37 using Date = OHOS::DistributedRdb::Date;
38 using JSChangeInfo = OHOS::RelationalStoreJsKit::NapiRdbStoreObserver::JSChangeInfo;
39 using PRIKey = OHOS::DistributedRdb::RdbStoreObserver::PrimaryKey;
40 using Error = RelationalStoreJsKit::Error;
41 using SecurityLevel = NativeRdb::SecurityLevel;
42 using RdbStoreConfig = NativeRdb::RdbStoreConfig;
43 using BigInt = OHOS::NativeRdb::BigInteger;
44 using SqlExecInfo = DistributedRdb::SqlObserver::SqlExecutionInfo;
45 using ValuesBucket = OHOS::NativeRdb::ValuesBucket;
46 using ValuesBuckets = OHOS::NativeRdb::ValuesBuckets;
47 using HAMode = NativeRdb::HAMode;
48 using HmacAlgo = NativeRdb::HmacAlgo;
49 using KdfAlgo = NativeRdb::KdfAlgo;
50 using EncryptAlgo = NativeRdb::EncryptAlgo;
51 using CryptoParam = NativeRdb::RdbStoreConfig::CryptoParam;
52 struct RdbConfig {
53     bool isEncrypt = false;
54     bool isSearchable = false;
55     bool isAutoClean = true;
56     bool vector = false;
57     bool allowRebuild = false;
58     bool isReadOnly = false;
59     SecurityLevel securityLevel = SecurityLevel::LAST;
60     std::string dataGroupId;
61     std::string name;
62     std::string customDir;
63     std::string path;
64     std::vector<std::string> pluginLibs = {};
65     int32_t haMode = HAMode::SINGLE;
66     CryptoParam cryptoParam;
67 };
68 
69 struct ContextParam {
70     std::string bundleName;
71     std::string moduleName;
72     std::string baseDir;
73     int32_t area;
74     bool isSystemApp = false;
75     bool isStageMode = true;
76 };
77 
78 struct TransactionOptions {
79     int32_t transactionType = 0;
80 };
81 
82 template<>
83 int32_t Convert2Value(napi_env env, napi_value input, Asset &output);
84 
85 template<>
86 int32_t Convert2Value(napi_env env, napi_value jsValue, DistributedRdb::Reference &output);
87 
88 template<>
89 int32_t Convert2Value(napi_env env, napi_value jsValue, DistributedRdb::DistributedConfig &output);
90 
91 template<>
92 int32_t Convert2Value(napi_env env, napi_value jsValue, ValueObject &valueObject);
93 
94 template<>
95 int32_t Convert2Value(napi_env env, napi_value jsValue, CryptoParam &cryptoParam);
96 
97 template<>
98 int32_t Convert2Value(napi_env env, napi_value jsValue, RdbConfig &rdbConfig);
99 
100 template<>
101 int32_t Convert2Value(napi_env env, napi_value jsValue, TransactionOptions &transactionOptions);
102 
103 template<>
104 int32_t Convert2Value(napi_env env, napi_value jsValue, ContextParam &context);
105 
106 template<>
107 napi_value Convert2JSValue(napi_env env, const Asset &value);
108 
109 template<>
110 napi_value Convert2JSValue(napi_env env, const RowEntity &value);
111 
112 template<>
113 napi_value Convert2JSValue(napi_env env, const ValueObject &value);
114 
115 template<>
116 napi_value Convert2JSValue(napi_env env, const DistributedRdb::Statistic &value);
117 template<>
118 napi_value Convert2JSValue(napi_env env, const DistributedRdb::TableDetail &value);
119 
120 template<>
121 napi_value Convert2JSValue(napi_env env, const DistributedRdb::ProgressDetail &value);
122 template<>
123 napi_value Convert2JSValue(napi_env env, const DistributedRdb::Details &details);
124 template<>
125 napi_value Convert2JSValue(napi_env env, const JSChangeInfo &value);
126 template<>
127 napi_value Convert2JSValue(napi_env env, const Date &date);
128 template<>
129 napi_value Convert2JSValue(napi_env env, const BigInt &value);
130 template<>
131 int32_t Convert2Value(napi_env env, napi_value jsValue, BigInt &value);
132 template<>
133 std::string ToString(const PRIKey &key);
134 template<>
135 napi_value Convert2JSValue(napi_env env, const SqlExecInfo &value);
136 bool IsNapiString(napi_env env, napi_value value);
137 
138 std::tuple<int32_t, std::shared_ptr<Error>> GetRealPath(
139     napi_env env, napi_value jsValue, RdbConfig &rdbConfig, ContextParam &param);
140 RdbStoreConfig GetRdbStoreConfig(const RdbConfig &rdbConfig, const ContextParam &param);
141 
142 bool HasDuplicateAssets(const ValueObject &value);
143 bool HasDuplicateAssets(const std::vector<ValueObject> &values);
144 bool HasDuplicateAssets(const ValuesBucket &value);
145 bool HasDuplicateAssets(const std::vector<ValuesBucket> &values);
146 bool HasDuplicateAssets(const ValuesBuckets &values);
147 }; // namespace JSUtils
148 } // namespace OHOS::AppDataMgrJsKit
149 #endif // RDB_JSKIT_NAPI_RDB_JS_UTILS_H