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