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