1 /* 2 * Copyright (c) 2022-2023 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 OHOS_FORM_FWK_FORM_RDB_DATA_MGR_H 17 #define OHOS_FORM_FWK_FORM_RDB_DATA_MGR_H 18 19 #include <vector> 20 #include <unordered_map> 21 #include <shared_mutex> 22 #include <singleton.h> 23 #include <string> 24 #include "form_constants.h" 25 #include "rdb_errno.h" 26 #include "rdb_helper.h" 27 #include "rdb_open_callback.h" 28 #include "rdb_store_config.h" 29 #include "form_mgr_errors.h" 30 31 namespace OHOS { 32 namespace AppExecFwk { 33 struct FormRdbTableConfig { 34 std::string tableName { Constants::FORM_RDB_TABLE_NAME }; 35 std::string createTableSql; 36 }; 37 class RdbStoreDataCallBackFormInfoStorage : public NativeRdb::RdbOpenCallback { 38 public: 39 RdbStoreDataCallBackFormInfoStorage(const std::string &rdbPath); 40 41 virtual ~RdbStoreDataCallBackFormInfoStorage(); 42 43 int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override; 44 45 int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; 46 47 int32_t OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override; 48 49 int32_t OnOpen(NativeRdb::RdbStore &rdbStore) override; 50 51 int32_t onCorruption(std::string databaseFile) override; 52 53 private: 54 std::string rdbPath_; 55 }; 56 57 /** 58 * @class FormRdbDataMgr 59 * Form Rdb Data Manager. 60 */ 61 class FormRdbDataMgr final : public DelayedRefSingleton<FormRdbDataMgr> { 62 DECLARE_DELAYED_REF_SINGLETON(FormRdbDataMgr) 63 public: 64 DISALLOW_COPY_AND_MOVE(FormRdbDataMgr); 65 66 ErrCode InitFormRdbTable(const FormRdbTableConfig &formRdbTableConfig); 67 68 /** 69 * @brief Insert the form data in DB. 70 * @param tableName The name of table to be inserted 71 * @param key The data's Key. 72 * @return Returns ERR_OK on success, others on failure. 73 */ 74 ErrCode InsertData(const std::string &tableName, const std::string &key); 75 76 /** 77 * @brief Insert the form data in DB. 78 * @param tableName The name of table to be inserted 79 * @param key The data's key. 80 * @param value the data's value 81 * @return Returns ERR_OK on success, others on failure. 82 */ 83 ErrCode InsertData(const std::string &tableName, const std::string &key, const std::string &value); 84 85 /** 86 * @brief Delete the form data in DB. 87 * @param tableName The name of table to be excute deleted action. 88 * @param key The data's Key. 89 * @return Returns ERR_OK on success, others on failure. 90 */ 91 ErrCode DeleteData(const std::string &tableName, const std::string &key); 92 93 /** 94 * @brief Query the form data in DB. 95 * @param tableName The name of table to be query. 96 * @param key The data's Key. 97 * @param values The map of data's values 98 * @return Returns ERR_OK on success, others on failure. 99 */ 100 ErrCode QueryData(const std::string &tableName, const std::string &key, 101 std::unordered_map<std::string, std::string> &values); 102 103 /** 104 * @brief Query all the form data in DB. 105 * @param tableName The name of table to be query. 106 * @param datas The map of data's values 107 * @return Returns ERR_OK on success, others on failure. 108 */ 109 ErrCode QueryAllData(const std::string &tableName, std::unordered_map<std::string, std::string> &datas); 110 111 /** 112 * @brief Query all key in DB. 113 * @param tableName The name of table to be query. 114 * @param datas All keys of data 115 * @return Returns ERR_OK on success, others on failure. 116 */ 117 ErrCode QueryAllKeys(const std::string &tableName, std::set<std::string> &datas); 118 119 /** 120 * @brief Excute sql in DB. 121 * @param sql The sql to be excute. 122 * @return Returns ERR_OK on success, others on failure. 123 */ 124 ErrCode ExecuteSql(const std::string &sql); 125 126 /** 127 * @brief Query data in DB. 128 * @param tableName The name of table to be query. 129 * @param key The data's Key. 130 * @param value The data's value. 131 * @return Returns ERR_OK on success, others on failure. 132 */ 133 ErrCode QueryData(const std::string &tableName, const std::string &key, std::string &value); 134 135 /** 136 * @brief Query data in DB. 137 * @param absRdbPredicates The rdb's predicates to be query. 138 * @return Returns query result. 139 */ 140 std::shared_ptr<NativeRdb::AbsSharedResultSet> QueryData( 141 const NativeRdb::AbsRdbPredicates &absRdbPredicates); 142 143 /** 144 * @brief Query data in DB. 145 * @param sql The sql to be query. 146 * @return Returns query result. 147 */ 148 std::shared_ptr<NativeRdb::AbsSharedResultSet> QuerySql(const std::string &sql); 149 150 /** 151 * @brief Insert data in DB. 152 * @param tableName The name of table to be insert. 153 * @param valuesBucket The bucket of values. 154 * @param rowId Row's id. 155 * @return Returns true for success, false for failure. 156 */ 157 bool InsertData( 158 const std::string &tableName, const NativeRdb::ValuesBucket &valuesBucket, int64_t &rowId); 159 160 /** 161 * @brief Delete data in DB. 162 * @param absRdbPredicates The rdb's predicates to be delete. 163 * @return Returns true for success, false for failure. 164 */ 165 bool DeleteData(const NativeRdb::AbsRdbPredicates &absRdbPredicates); 166 167 private: 168 bool IsFormRdbLoaded(); 169 170 ErrCode CheckAndRebuildRdbStore(int32_t rdbOperateRet); 171 172 ErrCode LoadRdbStore(); 173 174 std::map<std::string, FormRdbTableConfig> formRdbTableCfgMap_; 175 std::shared_ptr<NativeRdb::RdbStore> rdbStore_; 176 std::shared_mutex rdbStoreMutex_; 177 int64_t lastRdbBuildTime_ = 0; 178 }; 179 } // namespace AppExecFwk 180 } // namespace OHOS 181 182 #endif // OHOS_FORM_FWK_FORM_RDB_DATA_MGR_H 183