1 /* 2 * Copyright (c) 2024 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 RELATIONAL_STORE_UTILS_H 17 #define RELATIONAL_STORE_UTILS_H 18 19 #include "rdb_store.h" 20 #include "rdb_types.h" 21 #include "securec.h" 22 #include "value_object.h" 23 24 namespace OHOS { 25 namespace Relational { 26 #define ERROR_VALUE (-1) 27 28 char *MallocCString(const std::string &origin); 29 30 struct CArrUI8 { 31 uint8_t *head; 32 int64_t size; 33 }; 34 35 struct CArrFloat { 36 float *head; 37 int64_t size; 38 }; 39 40 struct CArrStr { 41 char **head; 42 int64_t size; 43 }; 44 45 struct CryptoParam { 46 CArrUI8 encryptKey; 47 int32_t iterNum; 48 int32_t encryptAlgo; 49 int32_t hmacAlgo; 50 int32_t kdfAlgo; 51 uint32_t cryptoPageSize; 52 }; 53 54 OHOS::NativeRdb::RdbStoreConfig::CryptoParam ToCCryptoParam(CryptoParam param); 55 56 struct StoreConfig { 57 char *name; 58 int32_t securityLevel; 59 bool encrypt; 60 char *dataGroupId; 61 char *customDir; 62 bool isSearchable; 63 bool autoCleanDirtyData; 64 }; 65 66 struct StoreConfigEx { 67 char *name; 68 int32_t securityLevel; 69 bool encrypt; 70 char *dataGroupId; 71 char *customDir; 72 char *rootDir; 73 bool isSearchable; 74 bool autoCleanDirtyData; 75 bool allowRebuild; 76 bool isReadOnly; 77 CArrStr pluginLibs; 78 CryptoParam cryptoParam; 79 bool vector; 80 int32_t tokenizer; 81 bool persist; 82 }; 83 84 struct Asset { 85 const char *name; 86 const char *uri; 87 const char *path; 88 const char *createTime; 89 const char *modifyTime; 90 const char *size; 91 int32_t status; 92 }; 93 94 struct Assets { 95 Asset *head; 96 int64_t size; 97 }; 98 99 CArrStr VectorToCArrStr(const std::vector<std::string> &devices); 100 101 std::vector<std::string> CArrStrToVector(CArrStr carr); 102 103 struct ValueType { 104 int64_t integer; 105 double dou; 106 char *string; 107 bool boolean; 108 CArrUI8 Uint8Array; 109 Asset asset; 110 Assets assets; 111 uint8_t tag; 112 }; 113 114 struct BigInt { 115 CArrUI8 value; 116 bool sign; 117 }; 118 119 struct ValueTypeEx { 120 int64_t integer; 121 double dou; 122 char *string; 123 bool boolean; 124 CArrUI8 uint8Array; 125 Asset asset; 126 Assets assets; 127 CArrFloat floatArray; 128 BigInt bigInt; 129 uint8_t tag; 130 }; 131 132 std::vector<uint8_t> CArrUI8ToVector(CArrUI8 carr); 133 134 enum TagType { 135 TYPE_NULL, 136 TYPE_INT, 137 TYPE_DOU, 138 TYPE_STR, 139 TYPE_BOOL, 140 TYPE_BLOB, 141 TYPE_ASSET, 142 TYPE_ASSETS, 143 TYPE_FLOATARR, 144 TYPE_BIGINT 145 }; 146 147 struct ValuesBucket { 148 char **key; 149 ValueType *value; 150 int64_t size; 151 }; 152 153 struct ValuesBucketEx { 154 char **key; 155 ValueTypeEx *value; 156 int64_t size; 157 }; 158 159 NativeRdb::ValueObject ValueTypeToValueObject(const ValueType &value); 160 161 NativeRdb::ValueObject ValueTypeExToValueObject(const ValueTypeEx &value); 162 163 struct CArrInt32 { 164 int32_t *head; 165 int64_t size; 166 }; 167 168 struct CArrSyncResult { 169 char **str; 170 int32_t *num; 171 int64_t size; 172 }; 173 174 ValueType ValueObjectToValueType(const NativeRdb::ValueObject &object); 175 176 ValueTypeEx ValueObjectToValueTypeEx(const NativeRdb::ValueObject &object); 177 178 struct RetPRIKeyType { 179 int64_t integer; 180 double dou; 181 char *string; 182 uint8_t tag; 183 }; 184 185 std::variant<std::monostate, std::string, int64_t, double> RetPRIKeyTypeToVariant(RetPRIKeyType &value); 186 187 RetPRIKeyType VariantToRetPRIKeyType(const std::variant<std::monostate, std::string, int64_t, double> &value); 188 189 struct CArrPRIKeyType { 190 RetPRIKeyType *head; 191 int64_t size; 192 }; 193 194 std::vector<NativeRdb::RdbStore::PRIKey> CArrPRIKeyTypeToPRIKeyArray(CArrPRIKeyType &cPrimaryKeys); 195 196 struct ModifyTime { 197 RetPRIKeyType *key; 198 uint64_t *value; 199 int64_t size; 200 }; 201 202 ModifyTime MapToModifyTime(std::map<NativeRdb::RdbStore::PRIKey, NativeRdb::RdbStore::Date> &map, int32_t &errCode); 203 204 struct RetChangeInfo { 205 char *table; 206 int32_t type; 207 CArrPRIKeyType inserted; 208 CArrPRIKeyType updated; 209 CArrPRIKeyType deleted; 210 }; 211 212 struct CArrRetChangeInfo { 213 RetChangeInfo *head; 214 int64_t size; 215 }; 216 217 CArrPRIKeyType VectorToCArrPRIKeyType(std::vector<DistributedRdb::RdbStoreObserver::PrimaryKey> arr); 218 219 RetChangeInfo ToRetChangeInfo( 220 const DistributedRdb::Origin &origin, DistributedRdb::RdbStoreObserver::ChangeInfo::iterator info); 221 222 CArrRetChangeInfo ToCArrRetChangeInfo(const DistributedRdb::Origin &origin, 223 const DistributedRdb::RdbStoreObserver::PrimaryFields &fields, 224 DistributedRdb::RdbStoreObserver::ChangeInfo &&changeInfo); 225 226 struct CStatistic { 227 uint32_t total; 228 uint32_t successful; 229 uint32_t failed; 230 uint32_t remained; 231 }; 232 233 CStatistic ToStatistic(DistributedRdb::Statistic statistic); 234 235 struct CTableDetails { 236 CStatistic upload; 237 CStatistic download; 238 }; 239 240 CTableDetails ToCTableDetails(DistributedRdb::TableDetail detail); 241 242 struct CDetails { 243 char **key; 244 CTableDetails *value; 245 int64_t size; 246 }; 247 248 CDetails ToCDetails(DistributedRdb::TableDetails details); 249 250 struct CProgressDetails { 251 int32_t schedule; 252 int32_t code; 253 CDetails details; 254 }; 255 256 CProgressDetails ToCProgressDetails(const DistributedRdb::Details &details); 257 258 struct RetDistributedConfig { 259 bool autoSync; 260 }; 261 } // namespace Relational 262 } // namespace OHOS 263 #endif