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 BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_RDB_HELPER_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_RDB_HELPER_H 18 19 #include <mutex> 20 #include "rdb_errno.h" 21 #include "rdb_helper.h" 22 #include "rdb_open_callback.h" 23 #include "rdb_store_config.h" 24 #include "notification_constant.h" 25 #include "ffrt.h" 26 27 namespace OHOS { 28 namespace Notification { 29 30 struct DistributedRdbConfig { 31 std::string dbPath { NotificationConstant::NOTIFICATION_RDB_PATH }; 32 std::string dbName { NotificationConstant::NOTIFICATION_RDB_NAME }; 33 std::string tableName { NotificationConstant::NOTIFICATION_RDB_TABLE_NAME }; 34 std::string journalMode { NotificationConstant::NOTIFICATION_JOURNAL_MODE }; 35 std::string syncMode { NotificationConstant::NOTIFICATION_SYNC_MODE }; 36 int32_t version { NotificationConstant::NOTIFICATION_RDB_VERSION }; 37 }; 38 39 class RdbCallBack : public NativeRdb::RdbOpenCallback { 40 public: 41 42 RdbCallBack(const DistributedRdbConfig ¬ificationRdbConfig); 43 44 virtual ~RdbCallBack(); 45 46 int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override; 47 48 int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; 49 50 int32_t OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override; 51 52 int32_t OnOpen(NativeRdb::RdbStore &rdbStore) override; 53 54 int32_t onCorruption(std::string databaseFile) override; 55 private: 56 DistributedRdbConfig notificationRdbConfig_; 57 bool hasTableInit_ = false; 58 }; 59 60 class DistributedRdbHelper { 61 public: 62 63 DistributedRdbHelper(const DistributedRdbConfig ¬ificationRdbConfig); 64 65 int32_t Init(); 66 67 int32_t Destroy(); 68 69 /** 70 * @brief Insert data in DB. 71 * @param key The data Key. 72 * @param userId Optional, Indicate which table to insert data. 73 * @return Returns ERR_OK on success, others on failure. 74 */ 75 int32_t InsertData(const std::string &key, const std::string &value); 76 77 /** 78 * @brief Insert batch data in DB. 79 * @param key The data Key. 80 * @param userId Optional, Indicate which table to insert data. 81 * @return Returns ERR_OK on success, others on failure. 82 */ 83 int32_t InsertBatchData(const std::unordered_map<std::string, std::string> &values); 84 85 /** 86 * @brief Delete data in DB. 87 * @param key The data Key. 88 * @param userId Optional, Indicate which table to delete data. 89 * @return Returns ERR_OK on success, others on failure. 90 */ 91 int32_t DeleteData(const std::string &key); 92 93 /** 94 * @brief Query data from DB. 95 * @param userId Optional, Indicate which table to query data. 96 * @return Returns ERR_OK on success, others on failure. 97 */ 98 int32_t QueryData(const std::string &key, std::string &value); 99 100 /** 101 * @brief Query data begin whith key in DB. 102 * @param userId Optional, Indicate which table to query data. 103 * @return Returns ERR_OK on success, others on failure. 104 */ 105 int32_t QueryDataBeginWithKey(const std::string &key, std::unordered_map<std::string, std::string> &values); 106 107 private: 108 std::vector<std::string> GenerateOperatedTables(const int32_t &userId); 109 int32_t DeleteData(const std::string tableName, const std::string key, int32_t &rowId); 110 int32_t QueryData(const std::string tableName, const std::string key, std::string &value); 111 int32_t QueryData(const std::string tableName, const std::string key, std::vector<uint8_t> &value); 112 int32_t QueryDataBeginWithKey(const std::string tableName, const std::string key, 113 std::unordered_map<std::string, std::string> &values); 114 int32_t QueryAllData(const std::string tableName, std::unordered_map<std::string, std::string> &datas); 115 int32_t RestoreForMasterSlaver(); 116 117 private: 118 DistributedRdbConfig notificationRdbConfig_; 119 std::shared_ptr<NativeRdb::RdbStore> rdbStore_; 120 mutable ffrt::mutex rdbStorePtrMutex_; 121 }; 122 123 } 124 } 125 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_RDB_HELPER_H 126