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