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, const std::string &syncMode = "", 175 const std::string &databaseFileType = "", SecurityLevel securityLevel = SecurityLevel::LAST, 176 bool isCreateNecessary = true, bool autoCheck = false, int journalSize = DB_JOURNAL_SIZE, 177 int pageSize = DB_PAGE_SIZE, const std::string &encryptAlgo = DB_DEFAULT_ENCRYPT_ALGO); 178 179 /** 180 * @brief Destructor. 181 */ 182 API_EXPORT ~RdbStoreConfig(); 183 184 /** 185 * @brief Obtains the database name. 186 */ 187 API_EXPORT std::string GetName() const; 188 189 /** 190 * @brief Obtains the database path. 191 */ 192 API_EXPORT std::string GetPath() const; 193 194 /** 195 * @brief Obtains the storage mode. 196 */ 197 API_EXPORT StorageMode GetStorageMode() const; 198 199 /** 200 * @brief Obtains the journal mode in this {@code StoreConfig} object. 201 */ 202 API_EXPORT std::string GetJournalMode() const; 203 204 /** 205 * @brief Obtains the synchronization mode in this {@code StoreConfig} object. 206 */ 207 API_EXPORT std::string GetSyncMode() const; 208 209 /** 210 * @brief Checks whether the database is read-only. 211 */ 212 API_EXPORT bool IsReadOnly() const; 213 214 /** 215 * @brief Checks whether the database is memory. 216 */ 217 API_EXPORT bool IsMemoryRdb() const; 218 219 /** 220 * @brief Obtains the database file type in this {@code StoreConfig} object. 221 */ 222 API_EXPORT std::string GetDatabaseFileType() const; 223 224 /** 225 * @brief Obtains the database security level in this {@code StoreConfig} object. 226 */ 227 API_EXPORT SecurityLevel GetSecurityLevel() const; 228 229 /** 230 * @brief Set encrypt status for the current database. 231 */ 232 API_EXPORT void SetEncryptStatus(const bool status); 233 234 /** 235 * @brief Checks whether the database is encrypt. 236 */ 237 API_EXPORT bool IsEncrypt() const; 238 239 /** 240 * @brief Checks whether the database is create necessary. 241 */ 242 API_EXPORT bool IsCreateNecessary() const; 243 244 /** 245 * @brief Sets the name for the object. 246 */ 247 API_EXPORT void SetName(std::string name); 248 249 /** 250 * @brief Sets the journal mode, if not set, the default mode is WAL 251 */ 252 API_EXPORT void SetJournalMode(JournalMode journalMode); 253 254 /** 255 * @brief Sets the path for the object. 256 */ 257 API_EXPORT void SetPath(std::string path); 258 259 /** 260 * @brief Sets whether the database is read-only. 261 */ 262 API_EXPORT void SetReadOnly(bool readOnly); 263 264 /** 265 * @brief Sets the storage mode for the object. 266 */ 267 API_EXPORT void SetStorageMode(StorageMode storageMode); 268 269 /** 270 * @brief Sets database file type. 271 */ 272 API_EXPORT void SetDatabaseFileType(DatabaseFileType type); 273 274 /** 275 * @brief Sets database security level. 276 */ 277 API_EXPORT void SetSecurityLevel(SecurityLevel secLevel); 278 279 /** 280 * @brief Sets whether the database is create necessary. 281 */ 282 API_EXPORT void SetCreateNecessary(bool isCreateNecessary); 283 284 /** 285 * @brief Sets the bundle name for the object. 286 */ 287 API_EXPORT int SetBundleName(const std::string &bundleName); 288 289 /** 290 * @brief Obtains the bundle name in this {@code StoreConfig} object. 291 */ 292 API_EXPORT std::string GetBundleName() const; 293 294 /** 295 * @brief Sets the distributed type for the object. 296 */ 297 API_EXPORT int SetDistributedType(DistributedType type); 298 299 /** 300 * @brief Obtains the distributed type in this {@code StoreConfig} object. 301 */ 302 API_EXPORT DistributedType GetDistributedType() const; 303 304 /** 305 * @brief Sets the module name for the object. 306 */ 307 API_EXPORT void SetModuleName(const std::string& moduleName); 308 309 /** 310 * @brief Obtains the module name in this {@code StoreConfig} object. 311 */ 312 API_EXPORT std::string GetModuleName() const; 313 314 /** 315 * @brief Sets the service name for the object. 316 */ 317 API_EXPORT void SetServiceName(const std::string& serviceName); 318 319 /** 320 * @brief Sets the area for the object. 321 */ 322 API_EXPORT void SetArea(int32_t area); 323 324 /** 325 * @brief Obtains the area in this {@code StoreConfig} object. 326 */ 327 API_EXPORT int32_t GetArea() const; 328 329 /** 330 * @brief Obtains the uri in this {@code StoreConfig} object. 331 */ 332 API_EXPORT std::string GetUri() const; 333 334 /** 335 * @brief Sets the uri for the object. 336 */ 337 API_EXPORT void SetUri(const std::string& uri); 338 339 /** 340 * @brief Obtains the read permission in this {@code StoreConfig} object. 341 */ 342 API_EXPORT std::string GetReadPermission() const; 343 344 /** 345 * @brief Sets the read permission for the object. 346 */ 347 API_EXPORT void SetReadPermission(const std::string& permission); 348 349 /** 350 * @brief Obtains the write permission in this {@code StoreConfig} object. 351 */ 352 API_EXPORT std::string GetWritePermission() const; 353 354 /** 355 * @brief Sets the write permission for the object. 356 */ 357 API_EXPORT void SetWritePermission(const std::string& permission); 358 359 /** 360 * @brief Obtains the journal mode value in this {@code StoreConfig} object. 361 */ 362 API_EXPORT static std::string GetJournalModeValue(JournalMode journalMode); 363 364 /** 365 * @brief Obtains the sync mode value in this {@code StoreConfig} object. 366 */ 367 API_EXPORT static std::string GetSyncModeValue(SyncMode syncMode); 368 369 /** 370 * @brief Obtains the database file type in this {@code StoreConfig} object. 371 */ 372 API_EXPORT static std::string GetDatabaseFileTypeValue(DatabaseFileType databaseFileType); 373 374 /** 375 * @brief Checks whether the database is auto check. 376 */ 377 API_EXPORT bool IsAutoCheck() const; 378 379 /** 380 * @brief Sets whether the database is auto check. 381 */ 382 API_EXPORT void SetAutoCheck(bool autoCheck); 383 384 /** 385 * @brief Obtains the journal size in this {@code StoreConfig} object. 386 */ 387 API_EXPORT int GetJournalSize() const; 388 389 /** 390 * @brief Sets the journal size for the object. 391 */ 392 API_EXPORT void SetJournalSize(int journalSize); 393 394 /** 395 * @brief Obtains the page size in this {@code StoreConfig} object. 396 */ 397 API_EXPORT int GetPageSize() const; 398 399 /** 400 * @brief Sets the page size for the object. 401 */ 402 API_EXPORT void SetPageSize(int pageSize); 403 404 /** 405 * @brief Obtains the encrypt algorithm in this {@code StoreConfig} object. 406 */ 407 API_EXPORT const std::string GetEncryptAlgo() const; 408 409 /** 410 * @brief Sets the encrypt algorithm for the object. 411 */ 412 API_EXPORT void SetEncryptAlgo(const std::string &encryptAlgo); 413 414 /** 415 * @brief Obtains the read connection size in this {@code StoreConfig} object. 416 */ 417 API_EXPORT int GetReadConSize() const; 418 419 /** 420 * @brief Sets the read connection size for the object. 421 */ 422 API_EXPORT void SetReadConSize(int readConSize); 423 424 /** 425 * @brief Sets the encrypt key for the object. 426 */ 427 void SetEncryptKey(const std::vector<uint8_t> &encryptKey); 428 429 /** 430 * @brief Obtains the encrypt key in this {@code StoreConfig} object. 431 */ 432 std::vector<uint8_t> GetEncryptKey() const; 433 434 /** 435 * @brief Sets the scalar function for the object. 436 */ 437 API_EXPORT void SetScalarFunction(const std::string &functionName, int argc, ScalarFunction function); 438 439 /** 440 * @brief Obtains the registered scalar functions in this {@code StoreConfig} object. 441 */ 442 API_EXPORT std::map<std::string, ScalarFunctionInfo> GetScalarFunctions() const; 443 444 /** 445 * @brief Sets the module name for the object. 446 */ 447 void SetDataGroupId(const std::string &DataGroupId); 448 449 /** 450 * @brief Obtains the module name in this {@code StoreConfig} object. 451 */ 452 std::string GetDataGroupId() const; 453 454 /** 455 * @brief Sets the autoCleanDirtyData for the object. 456 */ 457 void SetAutoClean(bool isAutoClean); 458 459 /** 460 * @brief Obtains the autoCleanDirtyData in this {@code StoreConfig} object. 461 */ 462 bool GetAutoClean() const; 463 464 /** 465 * @brief Sets the customDir directory for the object. 466 */ 467 void SetCustomDir(const std::string &customDir); 468 469 /** 470 * @brief Obtains the customDir directory in this {@code StoreConfig} object. 471 */ 472 std::string GetCustomDir() const; 473 474 /** 475 * @brief Overload the line number operator. 476 */ 477 bool operator==(const RdbStoreConfig &config) const 478 { 479 if (this->customScalarFunctions.size() != config.customScalarFunctions.size()) { 480 return false; 481 } 482 483 auto iter1 = this->customScalarFunctions.begin(); 484 auto iter2 = config.customScalarFunctions.begin(); 485 for (; iter1 != this->customScalarFunctions.end(); ++iter1, ++iter2) { 486 if (iter1->first != iter2->first) { 487 return false; 488 } 489 } 490 491 if (this->encryptKey_.size() != config.encryptKey_.size()) { 492 return false; 493 } 494 495 for (size_t i = 0; i < encryptKey_.size(); i++) { 496 if (this->encryptKey_[i] != config.encryptKey_[i]) { 497 return false; 498 } 499 } 500 501 return this->path == config.path && this->storageMode == config.storageMode 502 && this->storageMode == config.storageMode && this->journalMode == config.journalMode 503 && this->syncMode == config.syncMode && this->databaseFileType == config.databaseFileType 504 && this->isEncrypt_ == config.isEncrypt_ && this->securityLevel == config.securityLevel 505 && this->journalSize == config.journalSize && this->pageSize == config.pageSize 506 && this->readConSize_ == config.readConSize_ && this->customDir_ == config.customDir_; 507 } 508 509 /** 510 * @brief Checks whether the database isSearchable necessary. 511 */ 512 bool IsSearchable() const; 513 /** 514 * @brief Sets whether the database Searchable necessary. 515 */ 516 void SetSearchable(bool searchable); 517 518 /** 519 * @brief Sets the timeout to get write connection for the object. 520 */ 521 int GetWriteTime() const; 522 523 /** 524 * @brief Gets the timeout to get write connection for the object. 525 */ 526 void SetWriteTime(int timeout); 527 528 /** 529 * @brief Sets the timeout to get read connection for the object. 530 */ 531 int GetReadTime() const; 532 533 /** 534 * @brief Gets the timeout to get read connection for the object. 535 */ 536 void SetReadTime(int timeout); 537 538 private: 539 void ClearEncryptKey(); 540 541 std::string name; 542 std::string path; 543 StorageMode storageMode; 544 std::string journalMode; 545 std::string syncMode; 546 547 bool readOnly; 548 std::string databaseFileType; 549 550 // distributed rdb 551 DistributedType distributedType_ = DistributedRdb::RdbDistributedType::RDB_DEVICE_COLLABORATION; 552 int32_t area_ = 0; 553 std::string bundleName_; 554 std::string moduleName_; 555 556 bool isEncrypt_ = false; 557 std::vector<uint8_t> encryptKey_{}; 558 SecurityLevel securityLevel = SecurityLevel::LAST; 559 std::string uri_; 560 std::string readPermission_; 561 std::string writePermission_; 562 bool isCreateNecessary_; 563 564 bool autoCheck; 565 bool isAutoClean_ = true; 566 int journalSize; 567 int pageSize; 568 int readConSize_ = 4; 569 std::string encryptAlgo; 570 std::string dataGroupId_; 571 std::string customDir_; 572 573 std::map<std::string, ScalarFunctionInfo> customScalarFunctions; 574 bool isSearchable_ = false; 575 int writeTimeout_ = 2; // seconds 576 int readTimeout_ = 1; // seconds 577 }; 578 } // namespace OHOS::NativeRdb 579 580 #endif 581