• 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     int32_t errCode = NativeRdb::E_OK;
112     RdbStoreDataCallBackFormInfoStorage rdbDataCallBack_(formRdbConfig_);
113     rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, formRdbConfig_.version, rdbDataCallBack_, errCode);
114     if (rdbStore_ == nullptr) {
115         HILOG_ERROR("FormInfoRdbStore init fail");
116         return ERR_APPEXECFWK_FORM_COMMON_CODE;
117     }
118     return ERR_OK;
119 }
120 
InsertData(const std::string & key,const std::string & value)121 ErrCode FormRdbDataMgr::InsertData(const std::string &key, const std::string &value)
122 {
123     HILOG_DEBUG("InsertData start");
124     if (rdbStore_ == nullptr) {
125         HILOG_ERROR("FormInfoRdbStore is null");
126         return ERR_APPEXECFWK_FORM_COMMON_CODE;
127     }
128 
129     int64_t rowId = -1;
130     NativeRdb::ValuesBucket valuesBucket;
131     valuesBucket.PutString(FORM_KEY, key);
132     valuesBucket.PutString(FORM_VALUE, value);
133     auto ret = rdbStore_->InsertWithConflictResolution(
134         rowId, formRdbConfig_.tableName, valuesBucket, NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE);
135     if (ret != NativeRdb::E_OK) {
136         HILOG_ERROR("Insert operation failed, result: %{public}d, key=%{public}s.", ret, key.c_str());
137         return ERR_APPEXECFWK_FORM_COMMON_CODE;
138     }
139     return ERR_OK;
140 }
141 
DeleteData(const std::string & key)142 ErrCode FormRdbDataMgr::DeleteData(const std::string &key)
143 {
144     HILOG_DEBUG("DeleteData start");
145     if (rdbStore_ == nullptr) {
146         HILOG_ERROR("FormInfoRdbStore is null");
147         return ERR_APPEXECFWK_FORM_COMMON_CODE;
148     }
149 
150     int32_t rowId = -1;
151     NativeRdb::AbsRdbPredicates absRdbPredicates(formRdbConfig_.tableName);
152     absRdbPredicates.EqualTo(FORM_KEY, key);
153     auto ret = rdbStore_->Delete(rowId, absRdbPredicates);
154     if (ret != NativeRdb::E_OK) {
155         HILOG_ERROR("Delete operation failed, result: %{public}d, key=%{public}s.",
156             ret, key.c_str());
157         return ERR_APPEXECFWK_FORM_COMMON_CODE;
158     }
159     return ERR_OK;
160 }
161 
QueryData(const std::string & key,std::unordered_map<std::string,std::string> & values)162 ErrCode FormRdbDataMgr::QueryData(const std::string &key, std::unordered_map<std::string, std::string> &values)
163 {
164     HILOG_DEBUG("QueryData start");
165     if (rdbStore_ == nullptr) {
166         HILOG_ERROR("FormInfoRdbStore is null");
167         return ERR_APPEXECFWK_FORM_COMMON_CODE;
168     }
169 
170     NativeRdb::AbsRdbPredicates absRdbPredicates(formRdbConfig_.tableName);
171     absRdbPredicates.BeginsWith(FORM_KEY, key);
172     auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector<std::string>());
173     if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) {
174         HILOG_ERROR("absSharedResultSet failed");
175         return ERR_APPEXECFWK_FORM_COMMON_CODE;
176     }
177 
178     if (absSharedResultSet->GoToFirstRow() != NativeRdb::E_OK) {
179         HILOG_ERROR("GoToFirstRow failed");
180         return ERR_APPEXECFWK_FORM_COMMON_CODE;
181     }
182 
183     do {
184         std::string resultKey;
185         if (absSharedResultSet->GetString(FORM_KEY_INDEX, resultKey) != NativeRdb::E_OK) {
186             HILOG_ERROR("GetString key failed");
187             return ERR_APPEXECFWK_FORM_COMMON_CODE;
188         }
189 
190         std::string resultValue;
191         if (absSharedResultSet->GetString(FORM_VALUE_INDEX, resultValue) != NativeRdb::E_OK) {
192             HILOG_ERROR("GetString value failed");
193             return ERR_APPEXECFWK_FORM_COMMON_CODE;
194         }
195 
196         values.emplace(resultKey, resultValue);
197     } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
198     absSharedResultSet->Close();
199     return ERR_OK;
200 }
201 
QueryAllData(std::unordered_map<std::string,std::string> & datas)202 ErrCode FormRdbDataMgr::QueryAllData(std::unordered_map<std::string, std::string> &datas)
203 {
204     HILOG_DEBUG("QueryAllData start");
205     if (rdbStore_ == nullptr) {
206         HILOG_ERROR("FormInfoRdbStore is null");
207         return ERR_APPEXECFWK_FORM_COMMON_CODE;
208     }
209 
210     NativeRdb::AbsRdbPredicates absRdbPredicates(formRdbConfig_.tableName);
211     auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector<std::string>());
212     if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) {
213         HILOG_ERROR("absSharedResultSet failed");
214         return ERR_APPEXECFWK_FORM_COMMON_CODE;
215     }
216 
217     if (absSharedResultSet->GoToFirstRow() != NativeRdb::E_OK) {
218         HILOG_ERROR("GoToFirstRow failed");
219         return ERR_APPEXECFWK_FORM_COMMON_CODE;
220     }
221 
222     do {
223         std::string resultKey;
224         if (absSharedResultSet->GetString(FORM_KEY_INDEX, resultKey) != NativeRdb::E_OK) {
225             HILOG_ERROR("GetString key failed");
226             return ERR_APPEXECFWK_FORM_COMMON_CODE;
227         }
228 
229         std::string resultValue;
230         if (absSharedResultSet->GetString(FORM_VALUE_INDEX, resultValue) != NativeRdb::E_OK) {
231             HILOG_ERROR("GetString value failed");
232             return ERR_APPEXECFWK_FORM_COMMON_CODE;
233         }
234 
235         datas.emplace(resultKey, resultValue);
236     } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
237     absSharedResultSet->Close();
238     return ERR_OK;
239 }
240 } // namespace AppExecFwk
241 } // namespace OHOS