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_mapping_dao.h"
16
17 #include <algorithm>
18 #include <cinttypes>
19
20 #include "app_event_cache_common.h"
21 #include "app_event_store.h"
22 #include "hilog/log.h"
23 #include "rdb_helper.h"
24 #include "sql_util.h"
25
26 namespace OHOS {
27 namespace HiviewDFX {
28 using namespace AppEventCacheCommon;
29 using namespace AppEventCacheCommon::AppEventMapping;
30 namespace {
31 const HiLogLabel LABEL = { LOG_CORE, HIAPPEVENT_DOMAIN, "HiAppEvent_AppEvenMappingDao" };
32 }
AppEventMappingDao(std::shared_ptr<NativeRdb::RdbStore> dbStore)33 AppEventMappingDao::AppEventMappingDao(std::shared_ptr<NativeRdb::RdbStore> dbStore) : dbStore_(dbStore)
34 {
35 if (Create() != DB_SUCC) {
36 HiLog::Error(LABEL, "failed to create table=%{public}s", TABLE.c_str());
37 }
38 }
39
Create()40 int AppEventMappingDao::Create()
41 {
42 /**
43 * table: event_observer_mapping
44 *
45 * |-------|-----------|--------------|
46 * | seq | event_seq | observer_seq |
47 * |-------|-----------|--------------|
48 * | INT64 | INT64 | INT64 |
49 * |-------|-----------|--------------|
50 */
51 const std::vector<std::pair<std::string, std::string>> fields = {
52 {FIELD_EVENT_SEQ, SqlUtil::SQL_INT_TYPE},
53 {FIELD_OBSERVER_SEQ, SqlUtil::SQL_INT_TYPE},
54 };
55 std::string sql = SqlUtil::CreateTable(TABLE, fields);
56 if (dbStore_->ExecuteSql(sql) != NativeRdb::E_OK) {
57 return DB_FAILED;
58 }
59 return DB_SUCC;
60 }
61
Insert(int64_t eventSeq,int64_t observerSeq)62 int64_t AppEventMappingDao::Insert(int64_t eventSeq, int64_t observerSeq)
63 {
64 NativeRdb::ValuesBucket bucket;
65 bucket.PutLong(FIELD_EVENT_SEQ, eventSeq);
66 bucket.PutLong(FIELD_OBSERVER_SEQ, observerSeq);
67 int64_t seq = 0;
68 if (dbStore_->Insert(seq, TABLE, bucket) != NativeRdb::E_OK) {
69 return DB_FAILED;
70 }
71 return seq;
72 }
73
Delete(int64_t observerSeq,const std::vector<int64_t> & eventSeqs)74 int AppEventMappingDao::Delete(int64_t observerSeq, const std::vector<int64_t>& eventSeqs)
75 {
76 NativeRdb::AbsRdbPredicates predicates(TABLE);
77 if (observerSeq > 0) {
78 predicates.EqualTo(FIELD_OBSERVER_SEQ, observerSeq);
79 }
80 if (!eventSeqs.empty()) {
81 std::vector<std::string> eventSeqStrs(eventSeqs.size());
82 std::transform(eventSeqs.begin(), eventSeqs.end(), eventSeqStrs.begin(), [](int64_t eventSeq) {
83 return std::to_string(eventSeq);
84 });
85 predicates.In(FIELD_EVENT_SEQ, eventSeqStrs);
86 }
87
88 int deleteRows = 0;
89 if (dbStore_->Delete(deleteRows, predicates) != NativeRdb::E_OK) {
90 return DB_FAILED;
91 }
92 HiLog::Info(LABEL, "delete %{public}d records, observerSeq=%{public}" PRId64, deleteRows, observerSeq);
93 return deleteRows;
94 }
95 } // namespace HiviewDFX
96 } // namespace OHOS
97