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 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 <singleton.h> 22 #include <string> 23 #include "form_constants.h" 24 #include "rdb_errno.h" 25 #include "rdb_helper.h" 26 #include "rdb_open_callback.h" 27 #include "rdb_store_config.h" 28 #include "form_mgr_errors.h" 29 30 namespace OHOS { 31 namespace AppExecFwk { 32 struct FormRdbConfig { 33 std::string dbPath { Constants::FORM_MANAGER_SERVICE_PATH }; 34 std::string dbName { Constants::FORM_RDB_NAME }; 35 std::string tableName { Constants::FORM_RDB_TABLE_NAME }; 36 std::string journalMode { Constants::FORM_JOURNAL_MODE }; 37 std::string syncMode { Constants::FORM_SYNC_MODE }; 38 int32_t version { Constants::FORM_RDB_VERSION }; 39 std::string databaseFileType { Constants::FORM_RDB_FILE_TYPE }; 40 }; 41 class RdbStoreDataCallBackFormInfoStorage : public NativeRdb::RdbOpenCallback { 42 public: 43 RdbStoreDataCallBackFormInfoStorage(const FormRdbConfig &formRdbConfig); 44 45 virtual ~RdbStoreDataCallBackFormInfoStorage(); 46 47 int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override; 48 49 int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; 50 51 int32_t OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override; 52 53 int32_t OnOpen(NativeRdb::RdbStore &rdbStore) override; 54 55 int32_t onCorruption(std::string databaseFile) override; 56 private: 57 FormRdbConfig formRdbConfig_; 58 bool hasTableInit_ = false; 59 }; 60 61 /** 62 * @class FormRdbDataMgr 63 * Form Rdb Data Manager. 64 */ 65 class FormRdbDataMgr { 66 public: 67 68 explicit FormRdbDataMgr(const FormRdbConfig &formRdbConfig); 69 70 ErrCode Init(); 71 72 /** 73 * @brief Insert the form data in DB. 74 * @param key The data Key. 75 * @return Returns ERR_OK on success, others on failure. 76 */ 77 ErrCode InsertData(const std::string &key, const std::string &value); 78 79 /** 80 * @brief Delete the form data in DB. 81 * @param key The data Key. 82 * @return Returns ERR_OK on success, others on failure. 83 */ 84 ErrCode DeleteData(const std::string &key); 85 86 /** 87 * @brief Query the form data in DB. 88 * @return Returns ERR_OK on success, others on failure. 89 */ 90 ErrCode QueryData(const std::string &key, std::unordered_map<std::string, std::string> &values); 91 92 /** 93 * @brief Query all the form data in DB. 94 * @return Returns ERR_OK on success, others on failure. 95 */ 96 ErrCode QueryAllData(std::unordered_map<std::string, std::string> &values); 97 98 private: 99 FormRdbConfig formRdbConfig_; 100 std::shared_ptr<NativeRdb::RdbStore> rdbStore_; 101 }; 102 } // namespace AppExecFwk 103 } // namespace OHOS 104 105 #endif // OHOS_FORM_FWK_FORM_RDB_DATA_MGR_H 106