1 /* 2 * Copyright (c) 2022 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_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_RDB_DATA_MGR_H 17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_RDB_DATA_MGR_H 18 19 #include <vector> 20 #include <string> 21 #include <unordered_map> 22 #include "notification_constant.h" 23 #include "rdb_errno.h" 24 #include "rdb_helper.h" 25 #include "rdb_open_callback.h" 26 #include "rdb_store_config.h" 27 28 namespace OHOS { 29 namespace Notification { 30 struct NotificationRdbConfig { 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 class RdbStoreDataCallBackNotificationStorage : public NativeRdb::RdbOpenCallback { 39 public: 40 41 RdbStoreDataCallBackNotificationStorage(const NotificationRdbConfig ¬ificationRdbConfig); 42 43 virtual ~RdbStoreDataCallBackNotificationStorage(); 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 NotificationRdbConfig notificationRdbConfig_; 56 bool hasTableInit_ = false; 57 }; 58 59 /** 60 * @class NotificationDataMgr 61 * Notification Data Manager. 62 */ 63 class NotificationDataMgr { 64 public: 65 66 NotificationDataMgr(const NotificationRdbConfig ¬ificationRdbConfig); 67 68 int32_t Init(); 69 70 int32_t Destroy(); 71 72 /** 73 * @brief Insert data in DB. 74 * @param key The data Key. 75 * @return Returns ERR_OK on success, others on failure. 76 */ 77 int32_t InsertData(const std::string &key, const std::string &value); 78 79 /** 80 * @brief Insert batch data in DB. 81 * @param key The data Key. 82 * @return Returns ERR_OK on success, others on failure. 83 */ 84 int32_t InsertBatchData(const std::unordered_map<std::string, std::string> &values); 85 86 /** 87 * @brief Delete data in DB. 88 * @param key The data Key. 89 * @return Returns ERR_OK on success, others on failure. 90 */ 91 int32_t DeleteData(const std::string &key); 92 93 /** 94 * @brief Delete batch data in DB. 95 * @param key The data Key. 96 * @return Returns ERR_OK on success, others on failure. 97 */ 98 int32_t DeleteBathchData(const std::vector<std::string> &keys); 99 100 /** 101 * @brief Query data in DB. 102 * @return Returns ERR_OK on success, others on failure. 103 */ 104 int32_t QueryData(const std::string &key, std::string &value); 105 106 /** 107 * @brief Query data begin whith key in DB. 108 * @return Returns ERR_OK on success, others on failure. 109 */ 110 int32_t QueryDataBeginWithKey(const std::string &key, std::unordered_map<std::string, std::string> &values); 111 112 /** 113 * @brief Query all data in DB. 114 * @return Returns ERR_OK on success, others on failure. 115 */ 116 int32_t QueryAllData(std::unordered_map<std::string, std::string> &values); 117 118 private: 119 NotificationRdbConfig notificationRdbConfig_; 120 std::shared_ptr<NativeRdb::RdbStore> rdbStore_; 121 mutable std::mutex rdbStorePtrMutex_; 122 }; 123 } // namespace Notification 124 } // namespace OHOS 125 126 #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_RDB_DATA_MGR_H 127