• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 "custom_event_param_dao.h"
16 
17 #include "app_event_cache_common.h"
18 #include "app_event_store.h"
19 #include "hiappevent_base.h"
20 #include "hilog/log.h"
21 #include "rdb_helper.h"
22 #include "sql_util.h"
23 
24 #undef LOG_DOMAIN
25 #define LOG_DOMAIN 0xD002D07
26 
27 #undef LOG_TAG
28 #define LOG_TAG "CustomEventParamDao"
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 namespace CustomEventParamDao {
33 using namespace AppEventCacheCommon;
34 using namespace AppEventCacheCommon::CustomEventParams;
Create(NativeRdb::RdbStore & dbStore)35 int Create(NativeRdb::RdbStore& dbStore)
36 {
37     /**
38      * table: custom_event_params
39      *
40      * |-------|------------|--------|------|-----------|-------------|------------|
41      * |  seq  | running_id | domain | name | param_key | param_value | param_type |
42      * |-------|----------- |--------|------|-----------|-------------|------------|
43      * | INT64 |    TEXT    |  TEXT  | TEXT |   TEXT    |    TEXT     |    INT     |
44      * |-------|------------|--------|------|-----------|-------------|------------|
45      */
46     const std::vector<std::pair<std::string, std::string>> fields = {
47         {FIELD_RUNNING_ID, SqlUtil::SQL_TEXT_TYPE},
48         {FIELD_DOMAIN, SqlUtil::SQL_TEXT_TYPE},
49         {FIELD_NAME, SqlUtil::SQL_TEXT_TYPE},
50         {FIELD_PARAM_KEY, SqlUtil::SQL_TEXT_TYPE},
51         {FIELD_PARAM_VALUE, SqlUtil::SQL_TEXT_TYPE},
52         {FIELD_PARAM_TYPE, SqlUtil::SQL_INT_TYPE},
53     };
54     std::string sql = SqlUtil::CreateTable(TABLE, fields);
55     return dbStore.ExecuteSql(sql);
56 }
57 
BatchInsert(std::shared_ptr<NativeRdb::RdbStore> dbStore,const CustomEvent & customEvent)58 int BatchInsert(std::shared_ptr<NativeRdb::RdbStore> dbStore, const CustomEvent& customEvent)
59 {
60     std::vector<NativeRdb::ValuesBucket> buckets;
61     for (const auto& param : customEvent.params) {
62         NativeRdb::ValuesBucket bucket;
63         bucket.PutString(FIELD_RUNNING_ID, customEvent.runningId);
64         bucket.PutString(FIELD_DOMAIN, customEvent.domain);
65         bucket.PutString(FIELD_NAME, customEvent.name);
66         bucket.PutString(FIELD_PARAM_KEY, param.key);
67         bucket.PutString(FIELD_PARAM_VALUE, param.value);
68         bucket.PutInt(FIELD_PARAM_TYPE, param.type);
69         buckets.emplace_back(bucket);
70     }
71     int64_t insertRows = 0;
72     return dbStore->BatchInsert(insertRows, TABLE, buckets);
73 }
74 
Updates(std::shared_ptr<NativeRdb::RdbStore> dbStore,const CustomEvent & customEvent)75 int Updates(std::shared_ptr<NativeRdb::RdbStore> dbStore, const CustomEvent& customEvent)
76 {
77     for (const auto& param : customEvent.params) {
78         NativeRdb::ValuesBucket bucket;
79         bucket.PutString(FIELD_PARAM_VALUE, param.value);
80         bucket.PutInt(FIELD_PARAM_TYPE, param.type);
81 
82         int changedRows = 0;
83         NativeRdb::AbsRdbPredicates predicates(TABLE);
84         predicates.EqualTo(FIELD_RUNNING_ID, customEvent.runningId);
85         predicates.EqualTo(FIELD_DOMAIN, customEvent.domain);
86         predicates.EqualTo(FIELD_NAME, customEvent.name);
87         predicates.EqualTo(FIELD_PARAM_KEY, param.key);
88         if (int ret = dbStore->Update(changedRows, bucket, predicates); ret != NativeRdb::E_OK) {
89             return ret;
90         }
91     }
92     return NativeRdb::E_OK;
93 }
94 
Delete(std::shared_ptr<NativeRdb::RdbStore> dbStore)95 int Delete(std::shared_ptr<NativeRdb::RdbStore> dbStore)
96 {
97     NativeRdb::AbsRdbPredicates predicates(TABLE);
98     int deleteRows = 0;
99     int ret = dbStore->Delete(deleteRows, predicates);
100     HILOG_INFO(LOG_CORE, "delete %{public}d records, ret=%{public}d", deleteRows, ret);
101     return ret;
102 }
103 
QueryParamkeys(std::shared_ptr<NativeRdb::RdbStore> dbStore,std::unordered_set<std::string> & out,const CustomEvent & customEvent)104 int QueryParamkeys(std::shared_ptr<NativeRdb::RdbStore> dbStore, std::unordered_set<std::string>& out,
105     const CustomEvent& customEvent)
106 {
107     NativeRdb::AbsRdbPredicates predicates(TABLE);
108     predicates.EqualTo(FIELD_RUNNING_ID, customEvent.runningId);
109     predicates.EqualTo(FIELD_DOMAIN, customEvent.domain);
110     predicates.EqualTo(FIELD_NAME, customEvent.name);
111     auto resultSet = dbStore->Query(predicates, {FIELD_PARAM_KEY});
112     if (resultSet == nullptr) {
113         HILOG_ERROR(LOG_CORE, "failed to query table");
114         return NativeRdb::E_ERROR;
115     }
116     int ret = resultSet->GoToNextRow();
117     while (ret == NativeRdb::E_OK) {
118         std::string paramKey;
119         if (resultSet->GetString(0, paramKey) != NativeRdb::E_OK) {
120             HILOG_ERROR(LOG_CORE, "failed to get value, runningId=%{public}s, domain=%{public}s, name=%{public}s",
121                 customEvent.runningId.c_str(), customEvent.domain.c_str(), customEvent.name.c_str());
122             ret = resultSet->GoToNextRow();
123             continue;
124         }
125         out.insert(paramKey);
126         ret = resultSet->GoToNextRow();
127     }
128     resultSet->Close();
129     return ret == NativeRdb::E_SQLITE_CORRUPT ? ret : NativeRdb::E_OK;
130 }
131 
Query(std::shared_ptr<NativeRdb::RdbStore> dbStore,std::unordered_map<std::string,std::string> & params,const CustomEvent & customEvent)132 int Query(std::shared_ptr<NativeRdb::RdbStore> dbStore, std::unordered_map<std::string, std::string>& params,
133     const CustomEvent& customEvent)
134 {
135     NativeRdb::AbsRdbPredicates predicates(TABLE);
136     predicates.EqualTo(FIELD_RUNNING_ID, customEvent.runningId);
137     predicates.EqualTo(FIELD_DOMAIN, customEvent.domain);
138     predicates.EqualTo(FIELD_NAME, customEvent.name);
139     auto resultSet = dbStore->Query(predicates, {FIELD_PARAM_KEY, FIELD_PARAM_VALUE});
140     if (resultSet == nullptr) {
141         HILOG_ERROR(LOG_CORE, "failed to query table");
142         return NativeRdb::E_ERROR;
143     }
144     int ret = resultSet->GoToNextRow();
145     while (ret == NativeRdb::E_OK) {
146         std::string paramKey;
147         std::string paramValue;
148         if (resultSet->GetString(0, paramKey) != NativeRdb::E_OK
149             || resultSet->GetString(1, paramValue) != NativeRdb::E_OK) {
150             HILOG_ERROR(LOG_CORE, "failed to get value, runningId=%{public}s, domain=%{public}s, name=%{public}s",
151                 customEvent.runningId.c_str(), customEvent.domain.c_str(), customEvent.name.c_str());
152             ret = resultSet->GoToNextRow();
153             continue;
154         }
155         params[paramKey] = paramValue;
156         ret = resultSet->GoToNextRow();
157     }
158     resultSet->Close();
159     return ret == NativeRdb::E_SQLITE_CORRUPT ? ret : NativeRdb::E_OK;
160 }
161 } // namespace CustomEventParamDao
162 } // namespace HiviewDFX
163 } // namespace OHOS
164