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