1 /*
2 * Copyright (c) 2023 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
16 #include "data_share_dao.h"
17
18 #include <map>
19 #include <string>
20 #include <vector>
21
22 #include "hiview_logger.h"
23 #include "rdb_errno.h"
24 #include "rdb_store.h"
25 #include "value_object.h"
26 #include "values_bucket.h"
27
28 #include "data_share_common.h"
29 #include "data_share_util.h"
30 #include "data_share_store.h"
31 #include "string_util.h"
32 #include "time_util.h"
33
34 using namespace OHOS::HiviewDFX::SubscribeStore;
35 namespace OHOS {
36 namespace HiviewDFX {
37 DEFINE_LOG_TAG("HiView-DataShareDao");
DataShareDao(std::shared_ptr<DataShareStore> store)38 DataShareDao::DataShareDao(std::shared_ptr<DataShareStore> store) : store_(store)
39 {
40 eventTable_ = EventTable::TABLE;
41 }
42
IsUidExists(int32_t uid)43 bool DataShareDao::IsUidExists(int32_t uid)
44 {
45 auto dbStore = store_->GetDbStore();
46 if (dbStore == nullptr) {
47 HIVIEW_LOGE("DataShareDao::IsUidExists, dbStore is null.");
48 return false;
49 }
50 std::string sql;
51 sql.append("SELECT COUNT(*) FROM ").append(eventTable_)
52 .append(" WHERE ").append(EventTable::FIELD_UID).append(" = ?");
53 int64_t count = 0;
54 std::vector<NativeRdb::ValueObject> objects = { NativeRdb::ValueObject(uid) };
55 if (int ret = dbStore->ExecuteAndGetLong(count, sql, objects); ret != NativeRdb::E_OK) {
56 HIVIEW_LOGE("failed to query uid info: %{public}d, ret=%{public}d", uid, ret);
57 return false;
58 }
59 return count != 0;
60 }
61
SaveSubscriberInfo(int32_t uid,const std::string & events)62 int DataShareDao::SaveSubscriberInfo(int32_t uid, const std::string& events)
63 {
64 auto dbStore = store_->GetDbStore();
65 if (dbStore == nullptr) {
66 HIVIEW_LOGE("DataShareDao::SaveSubscriberInfo, dbStore is null.");
67 return DB_FAILED;
68 }
69 std::string bundleName = OHOS::HiviewDFX::DataShareUtil::GetBundleNameById(uid);
70 if (this->IsUidExists(uid)) {
71 int rows = 0;
72 NativeRdb::ValuesBucket values;
73 values.PutInt(EventTable::FIELD_UID, uid);
74 values.PutString(EventTable::FIELD_BUNDLE_NAME, bundleName);
75 values.PutLong(EventTable::FIELD_SUBSCRIBETIME, TimeUtil::GetMilliseconds());
76 values.PutString(EventTable::FIELD_EVENTLIST, events);
77 std::string whereClause = "uid = ?";
78 std::vector<std::string> whereArgs = {std::to_string(uid)};
79 if (int ret = dbStore->Update(rows, eventTable_, values, whereClause, whereArgs); ret != NativeRdb::E_OK) {
80 HIVIEW_LOGE("failed to update uid %{public}d, ret=%{public}d", uid, ret);
81 return DB_FAILED;
82 }
83 return DB_SUCC;
84 }
85 NativeRdb::ValuesBucket valuesBucket;
86 valuesBucket.PutInt(EventTable::FIELD_UID, uid);
87 valuesBucket.PutString(EventTable::FIELD_BUNDLE_NAME, bundleName);
88 valuesBucket.PutLong(EventTable::FIELD_SUBSCRIBETIME, TimeUtil::GetMilliseconds());
89 valuesBucket.PutString(EventTable::FIELD_EVENTLIST, events);
90 int64_t seq = 0;
91 if (int ret = dbStore->Insert(seq, eventTable_, valuesBucket); ret != NativeRdb::E_OK) {
92 HIVIEW_LOGE("failed to add uid %{public}d, ret=%{public}d", uid, ret);
93 return DB_FAILED;
94 }
95 return DB_SUCC;
96 }
97
DeleteSubscriberInfo(int32_t uid)98 int DataShareDao::DeleteSubscriberInfo(int32_t uid)
99 {
100 auto dbStore = store_->GetDbStore();
101 if (dbStore == nullptr) {
102 HIVIEW_LOGE("failed to delete subscriberInfo from table.");
103 return DB_FAILED;
104 }
105 std::string cond;
106 cond += EventTable::FIELD_UID;
107 cond += " = ?";
108 int delRow = 0;
109 std::vector<std::string> fields = { std::to_string(uid) };
110 if (int ret = dbStore->Delete(delRow, eventTable_, cond, fields); ret != NativeRdb::E_OK) {
111 HIVIEW_LOGE("failed to delete subscriberInfo from table.");
112 return DB_FAILED;
113 }
114 return DB_SUCC;
115 }
116
GetEventListByUid(int32_t uid,std::string & events)117 int DataShareDao::GetEventListByUid(int32_t uid, std::string& events)
118 {
119 auto dbStore = store_->GetDbStore();
120 if (dbStore == nullptr) {
121 HIVIEW_LOGE("failed to query from table %{public}s, db is null", eventTable_.c_str());
122 return DB_FAILED;
123 }
124 std::string sql;
125 sql.append("SELECT ")
126 .append(EventTable::FIELD_EVENTLIST)
127 .append(" FROM ").append(eventTable_)
128 .append(" WHERE uid")
129 .append(" = ?");
130 auto resultSet = dbStore->QuerySql(sql, std::vector<std::string> {std::to_string(uid)});
131 if (resultSet == nullptr) {
132 HIVIEW_LOGE("failed to get eventList");
133 return DB_FAILED;
134 }
135 if (resultSet->GoToNextRow() == NativeRdb::E_OK) {
136 resultSet->GetString(0, events); // 0 means eventList
137 }
138 resultSet->Close();
139 return DB_SUCC;
140 }
141
GetUidByBundleName(const std::string & bundleName,int32_t & uid)142 int DataShareDao::GetUidByBundleName(const std::string& bundleName, int32_t& uid)
143 {
144 auto dbStore = store_->GetDbStore();
145 if (dbStore == nullptr) {
146 HIVIEW_LOGE("failed to query from table %{public}s, db is null", eventTable_.c_str());
147 return DB_FAILED;
148 }
149 std::string sql;
150 sql.append("SELECT ")
151 .append(EventTable::FIELD_UID)
152 .append(" FROM ").append(eventTable_)
153 .append(" WHERE bundle_name")
154 .append(" = ?");
155 auto resultSet = dbStore->QuerySql(sql, std::vector<std::string> {bundleName});
156 if (resultSet == nullptr) {
157 HIVIEW_LOGE("failed to get eventList");
158 return DB_FAILED;
159 }
160 if (resultSet->GoToNextRow() == NativeRdb::E_OK) {
161 resultSet->GetInt(0, uid); // 0 means eventList
162 }
163 resultSet->Close();
164 return DB_SUCC;
165 }
166
GetTotalSubscriberInfo(std::map<int,std::string> & map)167 int DataShareDao::GetTotalSubscriberInfo(std::map<int, std::string>& map)
168 {
169 auto dbStore = store_->GetDbStore();
170 if (dbStore == nullptr) {
171 HIVIEW_LOGE("failed to query from table %{public}s, db is null", eventTable_.c_str());
172 return DB_FAILED;
173 }
174 std::string sql;
175 sql.append("SELECT ")
176 .append(EventTable::FIELD_UID)
177 .append(", ")
178 .append(EventTable::FIELD_EVENTLIST)
179 .append(" FROM ").append(eventTable_);
180 auto resultSet = dbStore->QuerySql(sql, std::vector<std::string> {});
181 if (resultSet == nullptr) {
182 HIVIEW_LOGE("failed to get eventList");
183 return DB_FAILED;
184 }
185 while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
186 int uid = 0;
187 resultSet->GetInt(0, uid); // 0 means uid field
188 std::string events;
189 resultSet->GetString(1, events); // 1 means events field
190 map.insert(std::make_pair(uid, events));
191 }
192 resultSet->Close();
193 return DB_SUCC;
194 }
195
196 } // namespace HiviewDFX
197 } // namespace OHOS
198