• 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 #include "form_rdb_data_mgr.h"
16 
17 #include <cinttypes>
18 #include <thread>
19 #include <filesystem>
20 #include <sstream>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include "form_constants.h"
24 #include "form_mgr_errors.h"
25 #include "hilog_wrapper.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
29 namespace {
30 const std::string FORM_KEY = "KEY";
31 const std::string FORM_VALUE = "VALUE";
32 const int32_t FORM_KEY_INDEX = 0;
33 const int32_t FORM_VALUE_INDEX = 1;
34 } // namespace
RdbStoreDataCallBackFormInfoStorage(const FormRdbConfig & formRdbConfig)35 RdbStoreDataCallBackFormInfoStorage::RdbStoreDataCallBackFormInfoStorage(const FormRdbConfig &formRdbConfig)
36     : formRdbConfig_(formRdbConfig)
37 {
38     HILOG_DEBUG("create rdb store callback instance");
39 }
40 
~RdbStoreDataCallBackFormInfoStorage()41 RdbStoreDataCallBackFormInfoStorage::~RdbStoreDataCallBackFormInfoStorage()
42 {
43     HILOG_DEBUG("destroy rdb store callback instance");
44 }
45 
OnCreate(NativeRdb::RdbStore & rdbStore)46 int32_t RdbStoreDataCallBackFormInfoStorage::OnCreate(NativeRdb::RdbStore &rdbStore)
47 {
48     HILOG_DEBUG("OnCreate");
49     int ret = NativeRdb::E_OK;
50     if (hasTableInit_) {
51         return ret;
52     }
53     std::string createTableSql = "CREATE TABLE IF NOT EXISTS " + formRdbConfig_.tableName
54         + " (KEY TEXT NOT NULL PRIMARY KEY, VALUE TEXT NOT NULL);";
55     ret = rdbStore.ExecuteSql(createTableSql);
56     if (ret == NativeRdb::E_OK) {
57         hasTableInit_ = true;
58     }
59     return ret;
60 }
61 
OnUpgrade(NativeRdb::RdbStore & rdbStore,int currentVersion,int targetVersion)62 int32_t RdbStoreDataCallBackFormInfoStorage::OnUpgrade(
63     NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion)
64 {
65     HILOG_DEBUG("OnUpgrade currentVersion: %{plubic}d, targetVersion: %{plubic}d",
66         currentVersion, targetVersion);
67     return NativeRdb::E_OK;
68 }
69 
OnDowngrade(NativeRdb::RdbStore & rdbStore,int currentVersion,int targetVersion)70 int32_t RdbStoreDataCallBackFormInfoStorage::OnDowngrade(
71     NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion)
72 {
73     HILOG_DEBUG("OnDowngrade  currentVersion: %{plubic}d, targetVersion: %{plubic}d",
74         currentVersion, targetVersion);
75     return NativeRdb::E_OK;
76 }
77 
OnOpen(NativeRdb::RdbStore & rdbStore)78 int32_t RdbStoreDataCallBackFormInfoStorage::OnOpen(NativeRdb::RdbStore &rdbStore)
79 {
80     HILOG_DEBUG("OnOpen");
81     return NativeRdb::E_OK;
82 }
83 
onCorruption(std::string databaseFile)84 int32_t RdbStoreDataCallBackFormInfoStorage::onCorruption(std::string databaseFile)
85 {
86     return NativeRdb::E_OK;
87 }
88 
FormRdbDataMgr(const FormRdbConfig & formRdbConfig)89 FormRdbDataMgr::FormRdbDataMgr(const FormRdbConfig &formRdbConfig)
90     : formRdbConfig_(formRdbConfig)
91 {
92     HILOG_DEBUG("create form rdb data manager");
93 }
94 
Init()95 ErrCode FormRdbDataMgr::Init()
96 {
97     HILOG_DEBUG("Create rdbStore");
98 
99     if (rdbStore_ != nullptr) {
100         HILOG_DEBUG("FormInfoRdbStore has existed");
101         return ERR_OK;
102     }
103 
104     NativeRdb::RdbStoreConfig rdbStoreConfig(
105         formRdbConfig_.dbPath + formRdbConfig_.dbName,
106         NativeRdb::StorageMode::MODE_DISK,
107         false,
108         std::vector<uint8_t>(),
109         formRdbConfig_.journalMode,
110         formRdbConfig_.syncMode,
111         "",
112         NativeRdb::SecurityLevel::S1);
113     int32_t errCode = NativeRdb::E_OK;
114     RdbStoreDataCallBackFormInfoStorage rdbDataCallBack_(formRdbConfig_);
115     rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, formRdbConfig_.version, rdbDataCallBack_, errCode);
116     if (rdbStore_ == nullptr) {
117         HILOG_ERROR("FormInfoRdbStore init fail");
118         return ERR_APPEXECFWK_FORM_COMMON_CODE;
119     }
120     return ERR_OK;
121 }
122 
InsertData(const std::string & key,const std::string & value)123 ErrCode FormRdbDataMgr::InsertData(const std::string &key, const std::string &value)
124 {
125     HILOG_DEBUG("InsertData start");
126     if (rdbStore_ == nullptr) {
127         HILOG_ERROR("FormInfoRdbStore is null");
128         return ERR_APPEXECFWK_FORM_COMMON_CODE;
129     }
130 
131     int64_t rowId = -1;
132     NativeRdb::ValuesBucket valuesBucket;
133     valuesBucket.PutString(FORM_KEY, key);
134     valuesBucket.PutString(FORM_VALUE, value);
135     auto ret = rdbStore_->InsertWithConflictResolution(
136         rowId, formRdbConfig_.tableName, valuesBucket, NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE);
137     if (ret != NativeRdb::E_OK) {
138         HILOG_ERROR("Insert operation failed, result: %{public}d, key=%{public}s.", ret, key.c_str());
139         return ERR_APPEXECFWK_FORM_COMMON_CODE;
140     }
141     return ERR_OK;
142 }
143 
DeleteData(const std::string & key)144 ErrCode FormRdbDataMgr::DeleteData(const std::string &key)
145 {
146     HILOG_DEBUG("DeleteData start");
147     if (rdbStore_ == nullptr) {
148         HILOG_ERROR("FormInfoRdbStore is null");
149         return ERR_APPEXECFWK_FORM_COMMON_CODE;
150     }
151 
152     int32_t rowId = -1;
153     NativeRdb::AbsRdbPredicates absRdbPredicates(formRdbConfig_.tableName);
154     absRdbPredicates.EqualTo(FORM_KEY, key);
155     auto ret = rdbStore_->Delete(rowId, absRdbPredicates);
156     if (ret != NativeRdb::E_OK) {
157         HILOG_ERROR("Delete operation failed, result: %{public}d, key=%{public}s.",
158             ret, key.c_str());
159         return ERR_APPEXECFWK_FORM_COMMON_CODE;
160     }
161     return ERR_OK;
162 }
163 
QueryData(const std::string & key,std::unordered_map<std::string,std::string> & values)164 ErrCode FormRdbDataMgr::QueryData(const std::string &key, std::unordered_map<std::string, std::string> &values)
165 {
166     HILOG_DEBUG("QueryData start");
167     if (rdbStore_ == nullptr) {
168         HILOG_ERROR("FormInfoRdbStore is null");
169         return ERR_APPEXECFWK_FORM_COMMON_CODE;
170     }
171 
172     NativeRdb::AbsRdbPredicates absRdbPredicates(formRdbConfig_.tableName);
173     absRdbPredicates.BeginsWith(FORM_KEY, key);
174     auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector<std::string>());
175     if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) {
176         HILOG_ERROR("absSharedResultSet failed");
177         return ERR_APPEXECFWK_FORM_COMMON_CODE;
178     }
179 
180     if (absSharedResultSet->GoToFirstRow() != NativeRdb::E_OK) {
181         HILOG_ERROR("GoToFirstRow failed");
182         return ERR_APPEXECFWK_FORM_COMMON_CODE;
183     }
184 
185     do {
186         std::string resultKey;
187         if (absSharedResultSet->GetString(FORM_KEY_INDEX, resultKey) != NativeRdb::E_OK) {
188             HILOG_ERROR("GetString key failed");
189             return ERR_APPEXECFWK_FORM_COMMON_CODE;
190         }
191 
192         std::string resultValue;
193         if (absSharedResultSet->GetString(FORM_VALUE_INDEX, resultValue) != NativeRdb::E_OK) {
194             HILOG_ERROR("GetString value failed");
195             return ERR_APPEXECFWK_FORM_COMMON_CODE;
196         }
197 
198         values.emplace(resultKey, resultValue);
199     } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
200     absSharedResultSet->Close();
201     return ERR_OK;
202 }
203 
QueryAllData(std::unordered_map<std::string,std::string> & datas)204 ErrCode FormRdbDataMgr::QueryAllData(std::unordered_map<std::string, std::string> &datas)
205 {
206     HILOG_DEBUG("QueryAllData start");
207     if (rdbStore_ == nullptr) {
208         HILOG_ERROR("FormInfoRdbStore is null");
209         return ERR_APPEXECFWK_FORM_COMMON_CODE;
210     }
211 
212     NativeRdb::AbsRdbPredicates absRdbPredicates(formRdbConfig_.tableName);
213     auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector<std::string>());
214     if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) {
215         HILOG_ERROR("absSharedResultSet failed");
216         return ERR_APPEXECFWK_FORM_COMMON_CODE;
217     }
218 
219     if (absSharedResultSet->GoToFirstRow() != NativeRdb::E_OK) {
220         HILOG_ERROR("GoToFirstRow failed");
221         return ERR_APPEXECFWK_FORM_COMMON_CODE;
222     }
223 
224     do {
225         std::string resultKey;
226         if (absSharedResultSet->GetString(FORM_KEY_INDEX, resultKey) != NativeRdb::E_OK) {
227             HILOG_ERROR("GetString key failed");
228             return ERR_APPEXECFWK_FORM_COMMON_CODE;
229         }
230 
231         std::string resultValue;
232         if (absSharedResultSet->GetString(FORM_VALUE_INDEX, resultValue) != NativeRdb::E_OK) {
233             HILOG_ERROR("GetString value failed");
234             return ERR_APPEXECFWK_FORM_COMMON_CODE;
235         }
236 
237         datas.emplace(resultKey, resultValue);
238     } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
239     absSharedResultSet->Close();
240     return ERR_OK;
241 }
242 } // namespace AppExecFwk
243 } // namespace OHOS