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 "notification_rdb_data_mgr.h"
16
17 #include "ans_log_wrapper.h"
18
19 namespace OHOS {
20 namespace Notification {
21 namespace {
22 const std::string NOTIFICATION_KEY = "KEY";
23 const std::string NOTIFICATION_VALUE = "VALUE";
24 const int32_t NOTIFICATION_KEY_INDEX = 0;
25 const int32_t NOTIFICATION_VALUE_INDEX = 1;
26 } // namespace
RdbStoreDataCallBackNotificationStorage(const NotificationRdbConfig & notificationRdbConfig)27 RdbStoreDataCallBackNotificationStorage::RdbStoreDataCallBackNotificationStorage(
28 const NotificationRdbConfig ¬ificationRdbConfig): notificationRdbConfig_(notificationRdbConfig)
29 {
30 ANS_LOGD("create rdb store callback instance");
31 }
32
~RdbStoreDataCallBackNotificationStorage()33 RdbStoreDataCallBackNotificationStorage::~RdbStoreDataCallBackNotificationStorage()
34 {
35 ANS_LOGD("destroy rdb store callback instance");
36 }
37
OnCreate(NativeRdb::RdbStore & rdbStore)38 int32_t RdbStoreDataCallBackNotificationStorage::OnCreate(NativeRdb::RdbStore &rdbStore)
39 {
40 ANS_LOGD("OnCreate");
41 int ret = NativeRdb::E_OK;
42 if (hasTableInit_) {
43 return ret;
44 }
45 std::string createTableSql = "CREATE TABLE IF NOT EXISTS " + notificationRdbConfig_.tableName
46 + " (KEY TEXT NOT NULL PRIMARY KEY, VALUE TEXT NOT NULL);";
47 ret = rdbStore.ExecuteSql(createTableSql);
48 if (ret == NativeRdb::E_OK) {
49 hasTableInit_ = true;
50 ANS_LOGD("createTable succeed");
51 }
52 return ret;
53 }
54
OnUpgrade(NativeRdb::RdbStore & rdbStore,int32_t oldVersion,int32_t newVersion)55 int32_t RdbStoreDataCallBackNotificationStorage::OnUpgrade(
56 NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion)
57 {
58 ANS_LOGD("OnUpgrade currentVersion: %{plubic}d, targetVersion: %{plubic}d",
59 oldVersion, newVersion);
60 return NativeRdb::E_OK;
61 }
62
OnDowngrade(NativeRdb::RdbStore & rdbStore,int currentVersion,int targetVersion)63 int32_t RdbStoreDataCallBackNotificationStorage::OnDowngrade(
64 NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion)
65 {
66 ANS_LOGD("OnDowngrade currentVersion: %{plubic}d, targetVersion: %{plubic}d",
67 currentVersion, targetVersion);
68 return NativeRdb::E_OK;
69 }
70
OnOpen(NativeRdb::RdbStore & rdbStore)71 int32_t RdbStoreDataCallBackNotificationStorage::OnOpen(NativeRdb::RdbStore &rdbStore)
72 {
73 ANS_LOGD("OnOpen");
74 return NativeRdb::E_OK;
75 }
76
onCorruption(std::string databaseFile)77 int32_t RdbStoreDataCallBackNotificationStorage::onCorruption(std::string databaseFile)
78 {
79 return NativeRdb::E_OK;
80 }
81
NotificationDataMgr(const NotificationRdbConfig & notificationRdbConfig)82 NotificationDataMgr::NotificationDataMgr(const NotificationRdbConfig ¬ificationRdbConfig)
83 : notificationRdbConfig_(notificationRdbConfig)
84 {
85 ANS_LOGD("create notification rdb data manager");
86 }
87
Init()88 int32_t NotificationDataMgr::Init()
89 {
90 ANS_LOGD("Create rdbStore");
91 {
92 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
93 if (rdbStore_ != nullptr) {
94 ANS_LOGD("notification rdb has existed");
95 return NativeRdb::E_OK;
96 }
97 }
98
99 NativeRdb::RdbStoreConfig rdbStoreConfig(
100 notificationRdbConfig_.dbPath + notificationRdbConfig_.dbName,
101 NativeRdb::StorageMode::MODE_DISK,
102 false,
103 std::vector<uint8_t>(),
104 notificationRdbConfig_.journalMode,
105 notificationRdbConfig_.syncMode);
106 rdbStoreConfig.SetSecurityLevel(NativeRdb::SecurityLevel::S1);
107 RdbStoreDataCallBackNotificationStorage rdbDataCallBack_(notificationRdbConfig_);
108
109 {
110 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
111 int32_t ret = NativeRdb::E_OK;
112 rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, notificationRdbConfig_.version,
113 rdbDataCallBack_, ret);
114 if (rdbStore_ == nullptr) {
115 ANS_LOGE("notification rdb init fail");
116 return NativeRdb::E_ERROR;
117 }
118 }
119
120 return NativeRdb::E_OK;
121 }
122
Destroy()123 int32_t NotificationDataMgr::Destroy()
124 {
125 ANS_LOGD("Destory rdbStore");
126 {
127 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
128 if (rdbStore_ == nullptr) {
129 ANS_LOGE("notification rdb is null");
130 return NativeRdb::E_ERROR;
131 }
132
133 rdbStore_ = nullptr;
134 }
135
136 int32_t ret = NativeRdb::RdbHelper::DeleteRdbStore(notificationRdbConfig_.dbPath + notificationRdbConfig_.dbName);
137 if (ret != NativeRdb::E_OK) {
138 ANS_LOGE("failed to destroy db store");
139 return NativeRdb::E_ERROR;
140 }
141 ANS_LOGD("destroy db store successfully");
142 return NativeRdb::E_OK;
143 }
144
InsertData(const std::string & key,const std::string & value)145 int32_t NotificationDataMgr::InsertData(const std::string &key, const std::string &value)
146 {
147 ANS_LOGD("InsertData start");
148 if (rdbStore_ == nullptr) {
149 ANS_LOGE("notification rdb is null");
150 return NativeRdb::E_ERROR;
151 }
152 {
153 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
154 int64_t rowId = -1;
155 NativeRdb::ValuesBucket valuesBucket;
156 valuesBucket.PutString(NOTIFICATION_KEY, key);
157 valuesBucket.PutString(NOTIFICATION_VALUE, value);
158 int32_t ret = rdbStore_->InsertWithConflictResolution(
159 rowId, notificationRdbConfig_.tableName, valuesBucket,
160 NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE);
161 if (ret != NativeRdb::E_OK) {
162 ANS_LOGE("Insert operation failed, result: %{public}d, key=%{public}s.", ret, key.c_str());
163 return NativeRdb::E_ERROR;
164 }
165 }
166 return NativeRdb::E_OK;
167 }
168
InsertBatchData(const std::unordered_map<std::string,std::string> & values)169 int32_t NotificationDataMgr::InsertBatchData(const std::unordered_map<std::string, std::string> &values)
170 {
171 ANS_LOGD("InsertBatchData start");
172 if (rdbStore_ == nullptr) {
173 ANS_LOGE("notification rdb is null");
174 return NativeRdb::E_ERROR;
175 }
176 {
177 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
178 int64_t rowId = -1;
179 for (auto &value : values) {
180 NativeRdb::ValuesBucket valuesBucket;
181 valuesBucket.PutString(NOTIFICATION_KEY, value.first);
182 valuesBucket.PutString(NOTIFICATION_VALUE, value.second);
183 int32_t ret = rdbStore_->InsertWithConflictResolution(
184 rowId, notificationRdbConfig_.tableName, valuesBucket,
185 NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE);
186 if (ret != NativeRdb::E_OK) {
187 ANS_LOGE("Insert batch operation failed, result: %{public}d.", ret);
188 return NativeRdb::E_ERROR;
189 }
190 }
191 }
192 return NativeRdb::E_OK;
193 }
194
DeleteData(const std::string & key)195 int32_t NotificationDataMgr::DeleteData(const std::string &key)
196 {
197 ANS_LOGD("DeleteData start");
198 if (rdbStore_ == nullptr) {
199 ANS_LOGE("notification rdb is null");
200 return NativeRdb::E_ERROR;
201 }
202 {
203 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
204 int32_t rowId = -1;
205 NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName);
206 absRdbPredicates.EqualTo(NOTIFICATION_KEY, key);
207 int32_t ret = rdbStore_->Delete(rowId, absRdbPredicates);
208 if (ret != NativeRdb::E_OK) {
209 ANS_LOGE("Delete operation failed, result: %{public}d, key=%{public}s.",
210 ret, key.c_str());
211 return NativeRdb::E_ERROR;
212 }
213 }
214 return NativeRdb::E_OK;
215 }
216
DeleteBathchData(const std::vector<std::string> & keys)217 int32_t NotificationDataMgr::DeleteBathchData(const std::vector<std::string> &keys)
218 {
219 ANS_LOGD("Delete Bathch Data start");
220 if (rdbStore_ == nullptr) {
221 ANS_LOGE("notification rdb is null");
222 return NativeRdb::E_ERROR;
223 }
224 {
225 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
226 for (auto key : keys) {
227 NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName);
228 int32_t rowId = -1;
229 absRdbPredicates.EqualTo(NOTIFICATION_KEY, key);
230 int32_t ret = rdbStore_->Delete(rowId, absRdbPredicates);
231 if (ret != NativeRdb::E_OK) {
232 ANS_LOGE("Delete Batch operation failed, result: %{public}d, key=%{public}s.",
233 ret, key.c_str());
234 return NativeRdb::E_ERROR;
235 }
236 }
237 }
238 return NativeRdb::E_OK;
239 }
240
QueryData(const std::string & key,std::string & value)241 int32_t NotificationDataMgr::QueryData(const std::string &key, std::string &value)
242 {
243 ANS_LOGD("QueryData start");
244 if (rdbStore_ == nullptr) {
245 ANS_LOGE("notification rdb is null");
246 return NativeRdb::E_ERROR;
247 }
248 {
249 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
250 NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName);
251 absRdbPredicates.EqualTo(NOTIFICATION_KEY, key);
252 auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector<std::string>());
253 if (absSharedResultSet == nullptr) {
254 ANS_LOGD("absSharedResultSet failed");
255 return NativeRdb::E_ERROR;
256 }
257
258 int32_t ret = absSharedResultSet->GoToFirstRow();
259 if (ret != NativeRdb::E_OK) {
260 ANS_LOGE("GoToFirstRow failed.It is empty!, key=%{public}s", key.c_str());
261 return NativeRdb::E_EMPTY_VALUES_BUCKET;
262 }
263 ret = absSharedResultSet->GetString(NOTIFICATION_VALUE_INDEX, value);
264 if (ret != NativeRdb::E_OK) {
265 ANS_LOGE("GetString value failed");
266 return NativeRdb::E_ERROR;
267 }
268 absSharedResultSet->Close();
269 }
270 return NativeRdb::E_OK;
271 }
272
QueryDataBeginWithKey(const std::string & key,std::unordered_map<std::string,std::string> & values)273 int32_t NotificationDataMgr::QueryDataBeginWithKey(
274 const std::string &key, std::unordered_map<std::string, std::string> &values)
275 {
276 ANS_LOGD("QueryData BeginWithKey start");
277 if (rdbStore_ == nullptr) {
278 ANS_LOGE("notification rdb is null");
279 return NativeRdb::E_ERROR;
280 }
281 {
282 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
283 NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName);
284 absRdbPredicates.BeginsWith(NOTIFICATION_KEY, key);
285 auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector<std::string>());
286 if (absSharedResultSet == nullptr) {
287 ANS_LOGE("absSharedResultSet failed");
288 return NativeRdb::E_ERROR;
289 }
290
291 int32_t ret = absSharedResultSet->GoToFirstRow();
292 if (ret != NativeRdb::E_OK) {
293 ANS_LOGE("GoToFirstRow failed.It is empty!, key=%{public}s", key.c_str());
294 return NativeRdb::E_EMPTY_VALUES_BUCKET;
295 }
296
297 do {
298 std::string resultKey;
299 ret = absSharedResultSet->GetString(NOTIFICATION_KEY_INDEX, resultKey);
300 if (ret != NativeRdb::E_OK) {
301 ANS_LOGE("GetString key failed");
302 return NativeRdb::E_ERROR;
303 }
304
305 std::string resultValue;
306 ret = absSharedResultSet->GetString(NOTIFICATION_VALUE_INDEX, resultValue);
307 if (ret != NativeRdb::E_OK) {
308 ANS_LOGE("GetString value failed");
309 return NativeRdb::E_ERROR;
310 }
311
312 values.emplace(resultKey, resultValue);
313 } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
314 absSharedResultSet->Close();
315 }
316
317 return NativeRdb::E_OK;
318 }
319
QueryAllData(std::unordered_map<std::string,std::string> & datas)320 int32_t NotificationDataMgr::QueryAllData(std::unordered_map<std::string, std::string> &datas)
321 {
322 ANS_LOGD("QueryAllData start");
323 if (rdbStore_ == nullptr) {
324 ANS_LOGE("notification rdb is null");
325 return NativeRdb::E_ERROR;
326 }
327 {
328 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
329 NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName);
330 auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector<std::string>());
331 if (absSharedResultSet == nullptr) {
332 ANS_LOGE("absSharedResultSet failed");
333 return NativeRdb::E_ERROR;
334 }
335
336 int32_t ret = absSharedResultSet->GoToFirstRow();
337 if (ret != NativeRdb::E_OK) {
338 ANS_LOGE("GoToFirstRow failed. It is empty!");
339 return NativeRdb::E_EMPTY_VALUES_BUCKET;
340 }
341
342 do {
343 std::string resultKey;
344 ret = absSharedResultSet->GetString(NOTIFICATION_KEY_INDEX, resultKey);
345 if (ret != NativeRdb::E_OK) {
346 ANS_LOGE("GetString key failed");
347 return NativeRdb::E_ERROR;
348 }
349
350 std::string resultValue;
351 ret = absSharedResultSet->GetString(NOTIFICATION_VALUE_INDEX, resultValue);
352 if (ret != NativeRdb::E_OK) {
353 ANS_LOGE("GetString value failed");
354 return NativeRdb::E_ERROR;
355 }
356
357 datas.emplace(resultKey, resultValue);
358 } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
359 absSharedResultSet->Close();
360 }
361 return NativeRdb::E_OK;
362 }
363 }
364 }