• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
40 class RdbStoreDataCallBackFormInfoStorage : public NativeRdb::RdbOpenCallback {
41 public:
42     RdbStoreDataCallBackFormInfoStorage(const FormRdbConfig &formRdbConfig);
43 
44     virtual ~RdbStoreDataCallBackFormInfoStorage();
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     FormRdbConfig formRdbConfig_;
57     bool hasTableInit_ = false;
58 };
59 
60 /**
61  * @class FormRdbDataMgr
62  * Form Rdb Data Manager.
63  */
64 class FormRdbDataMgr {
65 public:
66 
67     explicit FormRdbDataMgr(const FormRdbConfig &formRdbConfig);
68 
69     ErrCode Init();
70 
71     /**
72      * @brief Insert the form data in DB.
73      * @param key The data Key.
74      * @return Returns ERR_OK on success, others on failure.
75      */
76     ErrCode InsertData(const std::string &key, const std::string &value);
77 
78     /**
79      * @brief Delete the form data in DB.
80      * @param key The data Key.
81      * @return Returns ERR_OK on success, others on failure.
82      */
83     ErrCode DeleteData(const std::string &key);
84 
85     /**
86      * @brief Query the form data in DB.
87      * @return Returns ERR_OK on success, others on failure.
88      */
89     ErrCode QueryData(const std::string &key, std::unordered_map<std::string, std::string> &values);
90 
91     /**
92      * @brief Query all the form data in DB.
93      * @return Returns ERR_OK on success, others on failure.
94      */
95     ErrCode QueryAllData(std::unordered_map<std::string, std::string> &values);
96 
97 private:
98     FormRdbConfig formRdbConfig_;
99     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
100 };
101 } // namespace AppExecFwk
102 } // namespace OHOS
103 
104 #endif // OHOS_FORM_FWK_FORM_RDB_DATA_MGR_H
105