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 #include "user_property_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 "UserPropertyDao"
29
30 namespace OHOS {
31 namespace HiviewDFX {
32 using namespace AppEventCacheCommon;
33 using namespace AppEventCacheCommon::UserProperties;
34
UserPropertyDao(std::shared_ptr<NativeRdb::RdbStore> dbStore)35 UserPropertyDao::UserPropertyDao(std::shared_ptr<NativeRdb::RdbStore> dbStore) : dbStore_(dbStore)
36 {
37 if (Create() != DB_SUCC) {
38 HILOG_ERROR(LOG_CORE, "failed to create table=%{public}s", TABLE.c_str());
39 }
40 }
41
Create()42 int UserPropertyDao::Create()
43 {
44 if (dbStore_ == nullptr) {
45 return DB_FAILED;
46 }
47
48 /**
49 * table: user_properties
50 *
51 * |-------|--------|-------|
52 * | seq | name | value |
53 * |-------|--------|-------|
54 * | INT64 | TEXT | TEXT |
55 * |-------|--------|-------|
56 */
57 const std::vector<std::pair<std::string, std::string>> fields = {
58 {FIELD_NAME, SqlUtil::SQL_TEXT_TYPE},
59 {FIELD_VALUE, SqlUtil::SQL_TEXT_TYPE},
60 };
61 std::string sql = SqlUtil::CreateTable(TABLE, fields);
62 if (dbStore_->ExecuteSql(sql) != NativeRdb::E_OK) {
63 return DB_FAILED;
64 }
65 return DB_SUCC;
66 }
67
Insert(const std::string & name,const std::string & value)68 int64_t UserPropertyDao::Insert(const std::string& name, const std::string& value)
69 {
70 NativeRdb::ValuesBucket bucket;
71 bucket.PutString(FIELD_NAME, name);
72 bucket.PutString(FIELD_VALUE, value);
73 int64_t seq = 0;
74 if (dbStore_->Insert(seq, TABLE, bucket) != NativeRdb::E_OK) {
75 return DB_FAILED;
76 }
77 HILOG_INFO(LOG_CORE, "insert user property, name=%{public}s", name.c_str());
78 return seq;
79 }
80
Update(const std::string & name,const std::string & value)81 int64_t UserPropertyDao::Update(const std::string& name, const std::string& value)
82 {
83 if (dbStore_ == nullptr) {
84 return DB_FAILED;
85 }
86
87 NativeRdb::ValuesBucket bucket;
88 bucket.PutString(FIELD_NAME, name);
89 bucket.PutString(FIELD_VALUE, value);
90
91 int changedRows = 0;
92 NativeRdb::AbsRdbPredicates predicates(TABLE);
93 predicates.EqualTo(FIELD_NAME, name);
94 if (dbStore_->Update(changedRows, bucket, predicates) != NativeRdb::E_OK) {
95 return DB_FAILED;
96 }
97 HILOG_INFO(LOG_CORE, "update %{public}d user property, name=%{public}s", changedRows, name.c_str());
98 return changedRows;
99 }
100
Delete(const std::string & name)101 int UserPropertyDao::Delete(const std::string& name)
102 {
103 if (dbStore_ == nullptr) {
104 return DB_FAILED;
105 }
106
107 int deleteRows = 0;
108 NativeRdb::AbsRdbPredicates predicates(TABLE);
109 if (!name.empty()) {
110 predicates.EqualTo(FIELD_NAME, name);
111 }
112 if (dbStore_->Delete(deleteRows, predicates) != NativeRdb::E_OK) {
113 return DB_FAILED;
114 }
115 HILOG_INFO(LOG_CORE, "delete %{public}d user property, name=%{public}s", deleteRows, name.c_str());
116 return deleteRows;
117 }
118
Query(const std::string & name,std::string & out)119 int UserPropertyDao::Query(const std::string& name, std::string& out)
120 {
121 if (dbStore_ == nullptr) {
122 return DB_FAILED;
123 }
124
125 NativeRdb::AbsRdbPredicates predicates(TABLE);
126 predicates.EqualTo(FIELD_NAME, name);
127 auto resultSet = dbStore_->Query(predicates, {FIELD_VALUE});
128 if (resultSet == nullptr) {
129 HILOG_ERROR(LOG_CORE, "failed to query table, name=%{public}s", name.c_str());
130 return DB_FAILED;
131 }
132 if (resultSet->GoToNextRow() == NativeRdb::E_OK) {
133 if (resultSet->GetString(0, out) != NativeRdb::E_OK) {
134 HILOG_ERROR(LOG_CORE, "failed to get value from resultSet, name=%{public}s", name.c_str());
135 resultSet->Close();
136 return DB_FAILED;
137 }
138 }
139 resultSet->Close();
140 return DB_SUCC;
141 }
142
QueryAll(std::unordered_map<std::string,std::string> & out)143 int UserPropertyDao::QueryAll(std::unordered_map<std::string, std::string>& out)
144 {
145 if (dbStore_ == nullptr) {
146 return DB_FAILED;
147 }
148
149 NativeRdb::AbsRdbPredicates predicates(TABLE);
150 auto resultSet = dbStore_->Query(predicates, {FIELD_NAME, FIELD_VALUE});
151 if (resultSet == nullptr) {
152 HILOG_ERROR(LOG_CORE, "failed to query table");
153 return DB_FAILED;
154 }
155
156 out.clear();
157 while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
158 std::string name;
159 std::string value;
160 if (resultSet->GetString(0, name) != NativeRdb::E_OK || resultSet->GetString(1, value) != NativeRdb::E_OK) {
161 HILOG_ERROR(LOG_CORE, "failed to get data from resultSet");
162 continue;
163 }
164 out[name] = value;
165 }
166
167 resultSet->Close();
168 return DB_SUCC;
169 }
170 } // namespace HiviewDFX
171 } // namespace OHOS
172