1 /* 2 * Copyright (c) 2021 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 NATIVE_RDB_RDB_STORE_CONFIG_H 17 #define NATIVE_RDB_RDB_STORE_CONFIG_H 18 19 #include <string> 20 #include <vector> 21 #include "rdb_types.h" 22 #include "rdb_visibility.h" 23 24 namespace OHOS::NativeRdb { 25 /** 26 * @brief Indicates the type of the storage. 27 */ 28 enum class StorageMode { 29 /** Indicates the database storage is memory.*/ 30 MODE_MEMORY = 101, 31 /** Indicates the database storage is disk.*/ 32 MODE_DISK, 33 }; 34 35 /** 36 * @brief Indicates the type of the journal. 37 */ 38 enum class JournalMode { 39 /** Indicates the database journal mode is delete.*/ 40 MODE_DELETE, 41 /** Indicates the database journal mode is truncate.*/ 42 MODE_TRUNCATE, 43 /** Indicates the database journal mode is persist.*/ 44 MODE_PERSIST, 45 /** Indicates the database journal mode is memory.*/ 46 MODE_MEMORY, 47 /** Indicates the database journal mode is wal.*/ 48 MODE_WAL, 49 /** Indicates the database journal mode is off.*/ 50 MODE_OFF, 51 }; 52 53 /** 54 * @brief Indicates the database synchronization mode. 55 */ 56 enum class SyncMode { 57 /** Indicates the sync mode is off.*/ 58 MODE_OFF, 59 /** Indicates the sync mode is normal.*/ 60 MODE_NORMAL, 61 /** Indicates the sync mode is full.*/ 62 MODE_FULL, 63 /** Indicates the sync mode is extra.*/ 64 MODE_EXTRA, 65 }; 66 67 /** 68 * @brief Indicates the database file type. 69 */ 70 enum class DatabaseFileType { 71 /** Indicates the database file type is normal.*/ 72 NORMAL, 73 /** Indicates the database file type is backup.*/ 74 BACKUP, 75 /** Indicates the database file type is corrupt.*/ 76 CORRUPT, 77 }; 78 79 /** 80 * @brief Describes the {@link RdbStore} type. 81 */ 82 enum class SecurityLevel : int32_t { 83 /** 84 * @brief S1: means the db is low level security. 85 * 86 * There are some low impact, when the data is leaked. 87 */ 88 S1 = 1, 89 /** 90 * @brief S2: means the db is middle level security. 91 * 92 * There are some major impact, when the data is leaked. 93 */ 94 S2, 95 /** 96 * @brief S3: means the db is high level security 97 * 98 * There are some severity impact, when the data is leaked. 99 */ 100 S3, 101 /** 102 * @brief S3: means the db is critical level security 103 * 104 * There are some critical impact, when the data is leaked. 105 */ 106 S4, 107 /** 108 * @brief LAST: This is a boundary judgment value. 109 */ 110 LAST 111 }; 112 113 /** 114 * @brief The constant indicates the database default page size. 115 */ 116 static constexpr int DB_PAGE_SIZE = 4096; /* default page size : 4k */ 117 118 /** 119 * @brief The constant indicates the database default journal size. 120 */ 121 static constexpr int DB_JOURNAL_SIZE = 1024 * 1024; /* default file size : 1M */ 122 123 /** 124 * @brief The constant indicates the database default journal mode. 125 */ 126 static constexpr char DB_DEFAULT_JOURNAL_MODE[] = "WAL"; 127 128 /** 129 * @brief The constant indicates the database default encrypt algorithm. 130 */ 131 static constexpr char DB_DEFAULT_ENCRYPT_ALGO[] = "sha256"; 132 133 /** 134 * @brief Use DistributedType replace OHOS::DistributedRdb::RdbDistributedType. 135 */ 136 using DistributedType = OHOS::DistributedRdb::RdbDistributedType; 137 138 /** 139 * @brief Use ScalarFunction replace std::function<std::string(const std::vector<std::string>&)>. 140 */ 141 using ScalarFunction = std::function<std::string(const std::vector<std::string>&)>; 142 143 struct ScalarFunctionInfo { ScalarFunctionInfoScalarFunctionInfo144 ScalarFunctionInfo(ScalarFunction function, int argc) : function_(function), argc_(argc) {} 145 ScalarFunction function_; 146 int argc_; 147 }; 148 /** 149 * Manages relational database configurations. 150 */ 151 class API_EXPORT RdbStoreConfig { 152 public: 153 /** 154 * @brief Constructor. 155 * 156 * A parameterized constructor used to create an RdbStoreConfig instance. 157 * 158 * @param path Indicates the path of the database. 159 * @param storageMode Indicates the storage mode of the database. 160 * @param readOnly Indicates whether the database is read-only. 161 * @param encryptKey Indicates the encrypt key of the database. 162 * @param journalMode Indicates the journal mode of the database. 163 * @param syncMode Indicates the sync mode of the database. 164 * @param databaseFileType Indicates the file table of the database. 165 * @param securityLevel Indicates the security level of the database. 166 * @param isCreateNecessary Indicates whether the database is create necessary. 167 * @param autoCheck Indicates whether the database is auto check. 168 * @param journalSize Indicates the journal size of the database. 169 * @param pageSize Indicates the page size of the database. 170 * @param encryptAlgo Indicates the encrypt algorithm of the database. 171 */ 172 API_EXPORT RdbStoreConfig(const std::string &path, StorageMode storageMode = StorageMode::MODE_DISK, 173 bool readOnly = false, const std::vector<uint8_t> &encryptKey = std::vector<uint8_t>(), 174 const std::string &journalMode = DB_DEFAULT_JOURNAL_MODE, 175 const std::string &syncMode = "", const std::string &databaseFileType = "", 176 SecurityLevel securityLevel = SecurityLevel::LAST, bool isCreateNecessary = true, 177 bool autoCheck = false, int journalSize = DB_JOURNAL_SIZE, int pageSize = DB_PAGE_SIZE, 178 const std::string &encryptAlgo = DB_DEFAULT_ENCRYPT_ALGO); 179 180 /** 181 * @brief Destructor. 182 */ 183 API_EXPORT ~RdbStoreConfig(); 184 185 /** 186 * @brief Obtains the database name. 187 */ 188 API_EXPORT std::string GetName() const; 189 190 /** 191 * @brief Obtains the database path. 192 */ 193 API_EXPORT std::string GetPath() const; 194 195 /** 196 * @brief Obtains the storage mode. 197 */ 198 API_EXPORT StorageMode GetStorageMode() const; 199 200 /** 201 * @brief Obtains the journal mode in this {@code StoreConfig} object. 202 */ 203 API_EXPORT std::string GetJournalMode() const; 204 205 /** 206 * @brief Obtains the synchronization mode in this {@code StoreConfig} object. 207 */ 208 API_EXPORT std::string GetSyncMode() const; 209 210 /** 211 * @brief Checks whether the database is read-only. 212 */ 213 API_EXPORT bool IsReadOnly() const; 214 215 /** 216 * @brief Checks whether the database is memory. 217 */ 218 API_EXPORT bool IsMemoryRdb() const; 219 220 /** 221 * @brief Obtains the database file type in this {@code StoreConfig} object. 222 */ 223 API_EXPORT std::string GetDatabaseFileType() const; 224 225 /** 226 * @brief Obtains the database security level in this {@code StoreConfig} object. 227 */ 228 API_EXPORT SecurityLevel GetSecurityLevel() const; 229 230 /** 231 * @brief Set encrypt status for the current database. 232 */ 233 API_EXPORT void SetEncryptStatus(const bool status); 234 235 /** 236 * @brief Checks whether the database is encrypt. 237 */ 238 API_EXPORT bool IsEncrypt() const; 239 240 /** 241 * @brief Checks whether the database is create necessary. 242 */ 243 API_EXPORT bool IsCreateNecessary() const; 244 245 /** 246 * @brief Sets the name for the object. 247 */ 248 API_EXPORT void SetName(std::string name); 249 250 /** 251 * @brief Sets the journal mode, if not set, the default mode is WAL 252 */ 253 API_EXPORT void SetJournalMode(JournalMode journalMode); 254 255 /** 256 * @brief Sets the path for the object. 257 */ 258 API_EXPORT void SetPath(std::string path); 259 260 /** 261 * @brief Sets whether the database is read-only. 262 */ 263 API_EXPORT void SetReadOnly(bool readOnly); 264 265 /** 266 * @brief Sets the storage mode for the object. 267 */ 268 API_EXPORT void SetStorageMode(StorageMode storageMode); 269 270 /** 271 * @brief Sets database file type. 272 */ 273 API_EXPORT void SetDatabaseFileType(DatabaseFileType type); 274 275 /** 276 * @brief Sets database security level. 277 */ 278 API_EXPORT void SetSecurityLevel(SecurityLevel secLevel); 279 280 /** 281 * @brief Sets whether the database is create necessary. 282 */ 283 API_EXPORT void SetCreateNecessary(bool isCreateNecessary); 284 285 /** 286 * @brief Sets the bundle name for the object. 287 */ 288 API_EXPORT int SetBundleName(const std::string &bundleName); 289 290 /** 291 * @brief Obtains the bundle name in this {@code StoreConfig} object. 292 */ 293 API_EXPORT std::string GetBundleName() const; 294 295 /** 296 * @brief Sets the distributed type for the object. 297 */ 298 API_EXPORT int SetDistributedType(DistributedType type); 299 300 /** 301 * @brief Obtains the distributed type in this {@code StoreConfig} object. 302 */ 303 API_EXPORT DistributedType GetDistributedType() const; 304 305 /** 306 * @brief Sets the module name for the object. 307 */ 308 API_EXPORT void SetModuleName(const std::string& moduleName); 309 310 /** 311 * @brief Obtains the module name in this {@code StoreConfig} object. 312 */ 313 API_EXPORT std::string GetModuleName() const; 314 315 /** 316 * @brief Sets the service name for the object. 317 */ 318 API_EXPORT void SetServiceName(const std::string& serviceName); 319 320 /** 321 * @brief Sets the area for the object. 322 */ 323 API_EXPORT void SetArea(int32_t area); 324 325 /** 326 * @brief Obtains the area in this {@code StoreConfig} object. 327 */ 328 API_EXPORT int32_t GetArea() const; 329 330 /** 331 * @brief Obtains the uri in this {@code StoreConfig} object. 332 */ 333 API_EXPORT std::string GetUri() const; 334 335 /** 336 * @brief Sets the uri for the object. 337 */ 338 API_EXPORT void SetUri(const std::string& uri); 339 340 /** 341 * @brief Obtains the read permission in this {@code StoreConfig} object. 342 */ 343 API_EXPORT std::string GetReadPermission() const; 344 345 /** 346 * @brief Sets the read permission for the object. 347 */ 348 API_EXPORT void SetReadPermission(const std::string& permission); 349 350 /** 351 * @brief Obtains the write permission in this {@code StoreConfig} object. 352 */ 353 API_EXPORT std::string GetWritePermission() const; 354 355 /** 356 * @brief Sets the write permission for the object. 357 */ 358 API_EXPORT void SetWritePermission(const std::string& permission); 359 360 /** 361 * @brief Obtains the journal mode value in this {@code StoreConfig} object. 362 */ 363 API_EXPORT static std::string GetJournalModeValue(JournalMode journalMode); 364 365 /** 366 * @brief Obtains the sync mode value in this {@code StoreConfig} object. 367 */ 368 API_EXPORT static std::string GetSyncModeValue(SyncMode syncMode); 369 370 /** 371 * @brief Obtains the database file type in this {@code StoreConfig} object. 372 */ 373 API_EXPORT static std::string GetDatabaseFileTypeValue(DatabaseFileType databaseFileType); 374 375 /** 376 * @brief Checks whether the database is auto check. 377 */ 378 API_EXPORT bool IsAutoCheck() const; 379 380 /** 381 * @brief Sets whether the database is auto check. 382 */ 383 API_EXPORT void SetAutoCheck(bool autoCheck); 384 385 /** 386 * @brief Obtains the journal size in this {@code StoreConfig} object. 387 */ 388 API_EXPORT int GetJournalSize() const; 389 390 /** 391 * @brief Sets the journal size for the object. 392 */ 393 API_EXPORT void SetJournalSize(int journalSize); 394 395 /** 396 * @brief Obtains the page size in this {@code StoreConfig} object. 397 */ 398 API_EXPORT int GetPageSize() const; 399 400 /** 401 * @brief Sets the page size for the object. 402 */ 403 API_EXPORT void SetPageSize(int pageSize); 404 405 /** 406 * @brief Obtains the encrypt algorithm in this {@code StoreConfig} object. 407 */ 408 API_EXPORT const std::string GetEncryptAlgo() const; 409 410 /** 411 * @brief Sets the encrypt algorithm for the object. 412 */ 413 API_EXPORT void SetEncryptAlgo(const std::string &encryptAlgo); 414 415 /** 416 * @brief Obtains the read connection size in this {@code StoreConfig} object. 417 */ 418 API_EXPORT int GetReadConSize() const; 419 420 /** 421 * @brief Sets the read connection size for the object. 422 */ 423 API_EXPORT void SetReadConSize(int readConSize); 424 425 /** 426 * @brief Sets the encrypt key for the object. 427 */ 428 void SetEncryptKey(const std::vector<uint8_t> &encryptKey); 429 430 /** 431 * @brief Obtains the encrypt key in this {@code StoreConfig} object. 432 */ 433 std::vector<uint8_t> GetEncryptKey() const; 434 435 /** 436 * @brief Sets the scalar function for the object. 437 */ 438 API_EXPORT void SetScalarFunction(const std::string &functionName, int argc, ScalarFunction function); 439 440 /** 441 * @brief Obtains the registered scalar functions in this {@code StoreConfig} object. 442 */ 443 API_EXPORT std::map<std::string, ScalarFunctionInfo> GetScalarFunctions() const; 444 445 /** 446 * @brief Sets the module name for the object. 447 */ 448 void SetDataGroupId(const std::string &DataGroupId); 449 450 /** 451 * @brief Obtains the module name in this {@code StoreConfig} object. 452 */ 453 std::string GetDataGroupId() const; 454 455 /** 456 * @brief Overload the line number operator. 457 */ 458 bool operator==(const RdbStoreConfig &config) const 459 { 460 if (this->customScalarFunctions.size() != config.customScalarFunctions.size()) { 461 return false; 462 } 463 464 auto iter1 = this->customScalarFunctions.begin(); 465 auto iter2 = config.customScalarFunctions.begin(); 466 for (; iter1 != this->customScalarFunctions.end(); ++iter1, ++iter2) { 467 if (iter1->first != iter2->first) { 468 return false; 469 } 470 } 471 472 if (this->encryptKey_.size() != config.encryptKey_.size()) { 473 return false; 474 } 475 476 for (size_t i = 0; i < encryptKey_.size(); i++) { 477 if (this->encryptKey_[i] != config.encryptKey_[i]) { 478 return false; 479 } 480 } 481 482 return this->path == config.path && this->storageMode == config.storageMode 483 && this->storageMode == config.storageMode && this->journalMode == config.journalMode 484 && this->syncMode == config.syncMode && this->databaseFileType == config.databaseFileType 485 && this->isEncrypt_ == config.isEncrypt_ && this->securityLevel == config.securityLevel 486 && this->journalSize == config.journalSize && this->pageSize == config.pageSize 487 && this->readConSize_ == config.readConSize_; 488 } 489 490 private: 491 void ClearEncryptKey(); 492 493 std::string name; 494 std::string path; 495 StorageMode storageMode; 496 std::string journalMode; 497 std::string syncMode; 498 499 bool readOnly; 500 std::string databaseFileType; 501 502 // distributed rdb 503 DistributedType distributedType_ = DistributedRdb::RdbDistributedType::RDB_DEVICE_COLLABORATION; 504 int32_t area_ = 0; 505 std::string bundleName_; 506 std::string moduleName_; 507 508 bool isEncrypt_ = false; 509 std::vector<uint8_t> encryptKey_{}; 510 SecurityLevel securityLevel = SecurityLevel::LAST; 511 std::string uri_; 512 std::string readPermission_; 513 std::string writePermission_; 514 bool isCreateNecessary_; 515 516 bool autoCheck; 517 int journalSize; 518 int pageSize; 519 int readConSize_ = 4; 520 std::string encryptAlgo; 521 std::string dataGroupId_; 522 523 std::map<std::string, ScalarFunctionInfo> customScalarFunctions; 524 }; 525 } // namespace OHOS::NativeRdb 526 527 #endif 528