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