• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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     std::string createTableSql;
41 };
42 class RdbStoreDataCallBackFormInfoStorage : public NativeRdb::RdbOpenCallback {
43 public:
44     RdbStoreDataCallBackFormInfoStorage(const FormRdbConfig &formRdbConfig);
45 
46     virtual ~RdbStoreDataCallBackFormInfoStorage();
47 
48     int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override;
49 
50     int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override;
51 
52     int32_t OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override;
53 
54     int32_t OnOpen(NativeRdb::RdbStore &rdbStore) override;
55 
56     int32_t onCorruption(std::string databaseFile) override;
57 private:
58     FormRdbConfig formRdbConfig_;
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);
78 
79     /**
80      * @brief Insert the form data in DB.
81      * @param key The data Key.
82      * @return Returns ERR_OK on success, others on failure.
83      */
84     ErrCode InsertData(const std::string &key, const std::string &value);
85 
86     /**
87      * @brief Delete the form data in DB.
88      * @param key The data Key.
89      * @return Returns ERR_OK on success, others on failure.
90      */
91     ErrCode DeleteData(const std::string &key);
92 
93     /**
94      * @brief Query the form data in DB.
95      * @return Returns ERR_OK on success, others on failure.
96      */
97     ErrCode QueryData(const std::string &key, std::unordered_map<std::string, std::string> &values);
98 
99     /**
100      * @brief Query all the form data in DB.
101      * @return Returns ERR_OK on success, others on failure.
102      */
103     ErrCode QueryAllData(std::unordered_map<std::string, std::string> &values);
104 
105     /**
106      * @brief Query all key in DB.
107      * @return Returns ERR_OK on success, others on failure.
108      */
109     ErrCode QueryAllKeys(std::set<std::string> &datas);
110 
111     ErrCode ExecuteSql(const std::string &sql);
112 
113     ErrCode QueryData(const std::string &key, std::string &value);
114 
115     std::shared_ptr<NativeRdb::AbsSharedResultSet> QueryData(
116         const NativeRdb::AbsRdbPredicates &absRdbPredicates);
117 
118     std::shared_ptr<NativeRdb::AbsSharedResultSet> QuerySql(const std::string &sql);
119 
120     bool InsertData(
121         const std::string &tableName, const NativeRdb::ValuesBucket &valuesBucket, int64_t &rowId);
122 
123     bool DeleteData(const NativeRdb::AbsRdbPredicates &absRdbPredicates);
124 
125 private:
126     ErrCode CreateTable();
127 
128     FormRdbConfig formRdbConfig_;
129     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
130 };
131 } // namespace AppExecFwk
132 } // namespace OHOS
133 
134 #endif // OHOS_FORM_FWK_FORM_RDB_DATA_MGR_H
135