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 "app_event_blocks_dao.h"
16
17 #include "app_event_cache_common.h"
18 #include "app_event_store.h"
19 #include "hilog/log.h"
20 #include "rdb_errno.h"
21 #include "rdb_store.h"
22 #include "value_object.h"
23 #include "values_bucket.h"
24
25 using namespace OHOS::HiviewDFX::AppEventCacheCommon;
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace {
29 const HiLogLabel LABEL = { LOG_CORE, HIAPPEVENT_DOMAIN, "HiAppEvent_BlocksDao" };
30 }
AppEventBlocksDao(std::shared_ptr<AppEventStore> store)31 AppEventBlocksDao::AppEventBlocksDao(std::shared_ptr<AppEventStore> store) : store_(store)
32 {
33 table_ = Blocks::TABLE;
34 }
35
IsBlockExists(const std::string & name)36 bool AppEventBlocksDao::IsBlockExists(const std::string& name)
37 {
38 auto dbStore = store_->GetDbStore();
39 if (dbStore == nullptr) {
40 return false;
41 }
42
43 std::string sql;
44 sql.append("SELECT COUNT(*) FROM ").append(table_)
45 .append(" WHERE ").append(Blocks::FIELD_NAME).append(" = ?");
46 int64_t count = 0;
47 std::vector<NativeRdb::ValueObject> objects = { NativeRdb::ValueObject(name) };
48 if (int ret = dbStore->ExecuteAndGetLong(count, sql, objects); ret != NativeRdb::E_OK) {
49 HiLog::Error(LABEL, "failed to query block %{public}s", name.c_str());
50 return false;
51 }
52 return count != 0;
53 }
54
AddBlock(const std::string & name)55 int AppEventBlocksDao::AddBlock(const std::string& name)
56 {
57 auto dbStore = store_->GetDbStore();
58 if (dbStore == nullptr) {
59 return DB_FAILED;
60 }
61
62 HiLog::Debug(LABEL, "start to add block %{public}s", name.c_str());
63 NativeRdb::ValuesBucket values;
64 values.PutString(Blocks::FIELD_NAME, name);
65 int64_t seq = 0;
66 if (int ret = dbStore->Insert(seq, table_, values); ret != NativeRdb::E_OK) {
67 HiLog::Error(LABEL, "failed to add block %{public}s, ret=%{public}d", name.c_str(), ret);
68 return DB_FAILED;
69 }
70 return DB_SUCC;
71 }
72
RemoveBlock(const std::string & name)73 int AppEventBlocksDao::RemoveBlock(const std::string& name)
74 {
75 auto dbStore = store_->GetDbStore();
76 if (dbStore == nullptr) {
77 return DB_FAILED;
78 }
79
80 HiLog::Debug(LABEL, "start to remove block %{public}s", name.c_str());
81 std::string cond;
82 cond += Blocks::FIELD_NAME;
83 cond += " = ?";
84 int delRow = 0;
85 std::vector<std::string> fields = { name };
86 if (int ret = dbStore->Delete(delRow, table_, cond, fields); ret != NativeRdb::E_OK) {
87 HiLog::Error(LABEL, "failed to remove block %{public}s, ret=%{public}d", name.c_str(), ret);
88 return DB_FAILED;
89 }
90 return DB_SUCC;
91 }
92
GetBlocks(std::vector<std::string> & names)93 int AppEventBlocksDao::GetBlocks(std::vector<std::string>& names)
94 {
95 auto dbStore = store_->GetDbStore();
96 if (dbStore == nullptr) {
97 return DB_FAILED;
98 }
99
100 std::string sql;
101 sql.append("SELECT ").append(Blocks::FIELD_NAME).append(" FROM ").append(table_);
102 auto resultSet = dbStore->QuerySql(sql, std::vector<std::string> {});
103 if (resultSet == nullptr) {
104 HiLog::Error(LABEL, "failed to get blocks");
105 return DB_FAILED;
106 }
107 while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
108 std::string name;
109 resultSet->GetString(0, name); // 0 means blockName field
110 names.emplace_back(name);
111 }
112 resultSet->Close();
113 return DB_SUCC;
114 }
115 } // namespace HiviewDFX
116 } // namespace OHOS
117