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 "app_event_observer_dao.h"
16 
17 #include "app_event_cache_common.h"
18 #include "app_event_store.h"
19 #include "hilog/log.h"
20 #include "rdb_helper.h"
21 #include "sql_util.h"
22 
23 #undef LOG_DOMAIN
24 #define LOG_DOMAIN 0xD002D07
25 
26 #undef LOG_TAG
27 #define LOG_TAG "ObserverDao"
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 using namespace AppEventCacheCommon;
32 
AppEventObserverDao(std::shared_ptr<NativeRdb::RdbStore> dbStore)33 AppEventObserverDao::AppEventObserverDao(std::shared_ptr<NativeRdb::RdbStore> dbStore) : dbStore_(dbStore)
34 {
35     if (Create() != DB_SUCC) {
36         HILOG_ERROR(LOG_CORE, "failed to create table=%{public}s", Observers::TABLE.c_str());
37     }
38 }
39 
Create()40 int AppEventObserverDao::Create()
41 {
42     /**
43      * table: observers
44      *
45      * |-------|------|------|
46      * |  seq  | name | hash |
47      * |-------|------|------|
48      * | INT64 | TEXT | INT64|
49      * |-------|------|------|
50      */
51     const std::vector<std::pair<std::string, std::string>> fields = {
52         {Observers::FIELD_NAME, SqlUtil::SQL_TEXT_TYPE},
53         {Observers::FIELD_HASH, SqlUtil::SQL_INT_TYPE},
54     };
55     std::string sql = SqlUtil::CreateTable(Observers::TABLE, fields);
56     if (dbStore_->ExecuteSql(sql) != NativeRdb::E_OK) {
57         return DB_FAILED;
58     }
59     return DB_SUCC;
60 }
61 
Insert(const std::string & observer,int64_t hashCode)62 int64_t AppEventObserverDao::Insert(const std::string& observer, int64_t hashCode)
63 {
64     NativeRdb::ValuesBucket bucket;
65     bucket.PutString(Observers::FIELD_NAME, observer);
66     bucket.PutLong(Observers::FIELD_HASH, hashCode);
67     int64_t seq = 0;
68     if (dbStore_->Insert(seq, Observers::TABLE, bucket) != NativeRdb::E_OK) {
69         return DB_FAILED;
70     }
71     return seq;
72 }
73 
QuerySeq(const std::string & observer,int64_t hashCode)74 int64_t AppEventObserverDao::QuerySeq(const std::string& observer, int64_t hashCode)
75 {
76     NativeRdb::AbsRdbPredicates predicates(Observers::TABLE);
77     predicates.EqualTo(Observers::FIELD_NAME, observer);
78     predicates.EqualTo(Observers::FIELD_HASH, hashCode);
79     auto resultSet = dbStore_->Query(predicates, {Observers::FIELD_SEQ});
80     if (resultSet == nullptr) {
81         HILOG_ERROR(LOG_CORE, "failed to query table, observer name=%{public}s, hash code=%{public}" PRId64,
82             observer.c_str(), hashCode);
83         return DB_FAILED;
84     }
85 
86     // the hash code is unique, so get only the first
87     int64_t observerSeq = 0;
88     if (resultSet->GoToNextRow() == NativeRdb::E_OK && resultSet->GetLong(0, observerSeq) == NativeRdb::E_OK) {
89         HILOG_INFO(LOG_CORE, "succ to query observer seq=%{public}" PRId64 ", name=%{public}s, hash=%{public}" PRId64,
90             observerSeq, observer.c_str(), hashCode);
91         resultSet->Close();
92         return observerSeq;
93     }
94     resultSet->Close();
95     return DB_FAILED;
96 }
97 
QuerySeqs(const std::string & observer,std::vector<int64_t> & observerSeqs)98 int AppEventObserverDao::QuerySeqs(const std::string& observer, std::vector<int64_t>& observerSeqs)
99 {
100     NativeRdb::AbsRdbPredicates predicates(Observers::TABLE);
101     predicates.EqualTo(Observers::FIELD_NAME, observer);
102     auto resultSet = dbStore_->Query(predicates, {Observers::FIELD_SEQ});
103     if (resultSet == nullptr) {
104         HILOG_ERROR(LOG_CORE, "failed to query table, observer=%{public}s", observer.c_str());
105         return DB_FAILED;
106     }
107     while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
108         int64_t observerSeq = 0;
109         if (resultSet->GetLong(0, observerSeq) != NativeRdb::E_OK) {
110             HILOG_ERROR(LOG_CORE, "failed to get seq value from resultSet, observer=%{public}s", observer.c_str());
111             continue;
112         }
113         observerSeqs.emplace_back(observerSeq);
114     }
115     resultSet->Close();
116     return DB_SUCC;
117 }
118 
Delete(const std::string & observer)119 int AppEventObserverDao::Delete(const std::string& observer)
120 {
121     int deleteRows = 0;
122     NativeRdb::AbsRdbPredicates predicates(Observers::TABLE);
123     predicates.EqualTo(Observers::FIELD_NAME, observer);
124     if (dbStore_->Delete(deleteRows, predicates) != NativeRdb::E_OK) {
125         HILOG_ERROR(LOG_CORE, "failed to delete records, observer=%{public}s", observer.c_str());
126         return DB_FAILED;
127     }
128     HILOG_INFO(LOG_CORE, "delete %{public}d records, observer=%{public}s", deleteRows, observer.c_str());
129     return deleteRows;
130 }
131 
Delete(int64_t observerSeq)132 int AppEventObserverDao::Delete(int64_t observerSeq)
133 {
134     int deleteRows = 0;
135     NativeRdb::AbsRdbPredicates predicates(Observers::TABLE);
136     predicates.EqualTo(Observers::FIELD_SEQ, observerSeq);
137     if (dbStore_->Delete(deleteRows, predicates) != NativeRdb::E_OK) {
138         HILOG_ERROR(LOG_CORE, "failed to delete records, observer seq=%{public}" PRId64, observerSeq);
139         return DB_FAILED;
140     }
141     HILOG_INFO(LOG_CORE, "delete %{public}d records, observerSeq=%{public}" PRId64, deleteRows, observerSeq);
142     return deleteRows;
143 }
144 } // namespace HiviewDFX
145 } // namespace OHOS
146