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 int32_t ret = NativeRdb::E_OK;
93 if (rdbStore_ != nullptr) {
94 ANS_LOGD("notification rdb has existed");
95 return NativeRdb::E_OK;
96 }
97
98 NativeRdb::RdbStoreConfig rdbStoreConfig(
99 notificationRdbConfig_.dbPath + notificationRdbConfig_.dbName,
100 NativeRdb::StorageMode::MODE_DISK,
101 false,
102 std::vector<uint8_t>(),
103 notificationRdbConfig_.journalMode,
104 notificationRdbConfig_.syncMode);
105 RdbStoreDataCallBackNotificationStorage rdbDataCallBack_(notificationRdbConfig_);
106 rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(
107 rdbStoreConfig, notificationRdbConfig_.version, rdbDataCallBack_, ret);
108 if (rdbStore_ == nullptr) {
109 ANS_LOGE("notification rdb init fail");
110 return NativeRdb::E_ERROR;
111 }
112 return NativeRdb::E_OK;
113 }
114
Destroy()115 int32_t NotificationDataMgr::Destroy()
116 {
117 ANS_LOGD("Destory rdbStore");
118
119 if (rdbStore_ == nullptr) {
120 ANS_LOGE("notification rdb is null");
121 return NativeRdb::E_ERROR;
122 }
123
124 rdbStore_ = nullptr;
125 int32_t ret = NativeRdb::RdbHelper::DeleteRdbStore(notificationRdbConfig_.dbPath + notificationRdbConfig_.dbName);
126 if (ret != NativeRdb::E_OK) {
127 ANS_LOGE("failed to destroy db store");
128 return NativeRdb::E_ERROR;
129 }
130 ANS_LOGD("destroy db store successfully");
131 return NativeRdb::E_OK;
132 }
133
InsertData(const std::string & key,const std::string & value)134 int32_t NotificationDataMgr::InsertData(const std::string &key, const std::string &value)
135 {
136 ANS_LOGD("InsertData start");
137 if (rdbStore_ == nullptr) {
138 ANS_LOGE("notification rdb is null");
139 return NativeRdb::E_ERROR;
140 }
141 {
142 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
143 int64_t rowId = -1;
144 NativeRdb::ValuesBucket valuesBucket;
145 valuesBucket.PutString(NOTIFICATION_KEY, key);
146 valuesBucket.PutString(NOTIFICATION_VALUE, value);
147 int32_t ret = rdbStore_->InsertWithConflictResolution(
148 rowId, notificationRdbConfig_.tableName, valuesBucket,
149 NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE);
150 if (ret != NativeRdb::E_OK) {
151 ANS_LOGE("Insert operation failed, result: %{public}d, key=%{public}s.", ret, key.c_str());
152 return NativeRdb::E_ERROR;
153 }
154 }
155 return NativeRdb::E_OK;
156 }
157
InsertBatchData(const std::unordered_map<std::string,std::string> & values)158 int32_t NotificationDataMgr::InsertBatchData(const std::unordered_map<std::string, std::string> &values)
159 {
160 ANS_LOGD("InsertBatchData start");
161 if (rdbStore_ == nullptr) {
162 ANS_LOGE("notification rdb is null");
163 return NativeRdb::E_ERROR;
164 }
165 {
166 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
167 int64_t rowId = -1;
168 for (auto &value : values) {
169 NativeRdb::ValuesBucket valuesBucket;
170 valuesBucket.PutString(NOTIFICATION_KEY, value.first);
171 valuesBucket.PutString(NOTIFICATION_VALUE, value.second);
172 int32_t ret = rdbStore_->InsertWithConflictResolution(
173 rowId, notificationRdbConfig_.tableName, valuesBucket,
174 NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE);
175 if (ret != NativeRdb::E_OK) {
176 ANS_LOGE("Insert batch operation failed, result: %{public}d.", ret);
177 return NativeRdb::E_ERROR;
178 }
179 }
180 }
181 return NativeRdb::E_OK;
182 }
183
DeleteData(const std::string & key)184 int32_t NotificationDataMgr::DeleteData(const std::string &key)
185 {
186 ANS_LOGD("DeleteData start");
187 if (rdbStore_ == nullptr) {
188 ANS_LOGE("notification rdb is null");
189 return NativeRdb::E_ERROR;
190 }
191 {
192 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
193 int32_t rowId = -1;
194 NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName);
195 absRdbPredicates.EqualTo(NOTIFICATION_KEY, key);
196 int32_t ret = rdbStore_->Delete(rowId, absRdbPredicates);
197 if (ret != NativeRdb::E_OK) {
198 ANS_LOGE("Delete operation failed, result: %{public}d, key=%{public}s.",
199 ret, key.c_str());
200 return NativeRdb::E_ERROR;
201 }
202 }
203 return NativeRdb::E_OK;
204 }
205
DeleteBathchData(const std::vector<std::string> & keys)206 int32_t NotificationDataMgr::DeleteBathchData(const std::vector<std::string> &keys)
207 {
208 ANS_LOGD("Delete Bathch Data start");
209 if (rdbStore_ == nullptr) {
210 ANS_LOGE("notification rdb is null");
211 return NativeRdb::E_ERROR;
212 }
213 {
214 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
215 for (auto key : keys) {
216 NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName);
217 int32_t rowId = -1;
218 absRdbPredicates.EqualTo(NOTIFICATION_KEY, key);
219 int32_t ret = rdbStore_->Delete(rowId, absRdbPredicates);
220 if (ret != NativeRdb::E_OK) {
221 ANS_LOGE("Delete Batch operation failed, result: %{public}d, key=%{public}s.",
222 ret, key.c_str());
223 return NativeRdb::E_ERROR;
224 }
225 }
226 }
227 return NativeRdb::E_OK;
228 }
229
QueryData(const std::string & key,std::string & value)230 int32_t NotificationDataMgr::QueryData(const std::string &key, std::string &value)
231 {
232 ANS_LOGD("QueryData start");
233 if (rdbStore_ == nullptr) {
234 ANS_LOGE("notification rdb is null");
235 return NativeRdb::E_ERROR;
236 }
237 {
238 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
239 NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName);
240 absRdbPredicates.EqualTo(NOTIFICATION_KEY, key);
241 auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector<std::string>());
242 if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) {
243 ANS_LOGD("absSharedResultSet failed");
244 return NativeRdb::E_ERROR;
245 }
246
247 int32_t ret = absSharedResultSet->GoToFirstRow();
248 if (ret != NativeRdb::E_OK) {
249 ANS_LOGE("GoToFirstRow failed.It is empty!, key=%{public}s", key.c_str());
250 return NativeRdb::E_EMPTY_VALUES_BUCKET;
251 }
252 ret = absSharedResultSet->GetString(NOTIFICATION_VALUE_INDEX, value);
253 if (ret != NativeRdb::E_OK) {
254 ANS_LOGE("GetString value failed");
255 return NativeRdb::E_ERROR;
256 }
257 absSharedResultSet->Close();
258 }
259 return NativeRdb::E_OK;
260 }
261
QueryDataBeginWithKey(const std::string & key,std::unordered_map<std::string,std::string> & values)262 int32_t NotificationDataMgr::QueryDataBeginWithKey(
263 const std::string &key, std::unordered_map<std::string, std::string> &values)
264 {
265 ANS_LOGD("QueryData BeginWithKey start");
266 if (rdbStore_ == nullptr) {
267 ANS_LOGE("notification rdb is null");
268 return NativeRdb::E_ERROR;
269 }
270 {
271 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
272 NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName);
273 absRdbPredicates.BeginsWith(NOTIFICATION_KEY, key);
274 auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector<std::string>());
275 if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) {
276 ANS_LOGE("absSharedResultSet failed");
277 return NativeRdb::E_ERROR;
278 }
279
280 int32_t ret = absSharedResultSet->GoToFirstRow();
281 if (ret != NativeRdb::E_OK) {
282 ANS_LOGE("GoToFirstRow failed.It is empty!, key=%{public}s", key.c_str());
283 return NativeRdb::E_EMPTY_VALUES_BUCKET;
284 }
285
286 do {
287 std::string resultKey;
288 ret = absSharedResultSet->GetString(NOTIFICATION_KEY_INDEX, resultKey);
289 if (ret != NativeRdb::E_OK) {
290 ANS_LOGE("GetString key failed");
291 return NativeRdb::E_ERROR;
292 }
293
294 std::string resultValue;
295 ret = absSharedResultSet->GetString(NOTIFICATION_VALUE_INDEX, resultValue);
296 if (ret != NativeRdb::E_OK) {
297 ANS_LOGE("GetString value failed");
298 return NativeRdb::E_ERROR;
299 }
300
301 values.emplace(resultKey, resultValue);
302 } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
303 absSharedResultSet->Close();
304 }
305
306 return NativeRdb::E_OK;
307 }
308
QueryAllData(std::unordered_map<std::string,std::string> & datas)309 int32_t NotificationDataMgr::QueryAllData(std::unordered_map<std::string, std::string> &datas)
310 {
311 ANS_LOGD("QueryAllData start");
312 if (rdbStore_ == nullptr) {
313 ANS_LOGE("notification rdb is null");
314 return NativeRdb::E_ERROR;
315 }
316 {
317 std::lock_guard<std::mutex> lock(rdbStorePtrMutex_);
318 NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName);
319 auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector<std::string>());
320 if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) {
321 ANS_LOGE("absSharedResultSet failed");
322 return NativeRdb::E_ERROR;
323 }
324
325 int32_t ret = absSharedResultSet->GoToFirstRow();
326 if (ret != NativeRdb::E_OK) {
327 ANS_LOGE("GoToFirstRow failed. It is empty!");
328 return NativeRdb::E_EMPTY_VALUES_BUCKET;
329 }
330
331 do {
332 std::string resultKey;
333 ret = absSharedResultSet->GetString(NOTIFICATION_KEY_INDEX, resultKey);
334 if (ret != NativeRdb::E_OK) {
335 ANS_LOGE("GetString key failed");
336 return NativeRdb::E_ERROR;
337 }
338
339 std::string resultValue;
340 ret = absSharedResultSet->GetString(NOTIFICATION_VALUE_INDEX, resultValue);
341 if (ret != NativeRdb::E_OK) {
342 ANS_LOGE("GetString value failed");
343 return NativeRdb::E_ERROR;
344 }
345
346 datas.emplace(resultKey, resultValue);
347 } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
348 absSharedResultSet->Close();
349 }
350 return NativeRdb::E_OK;
351 }
352 }
353 }