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 
16 #ifndef SECURITY_GUARD_DATABASE_HELPER_H
17 #define SECURITY_GUARD_DATABASE_HELPER_H
18 
19 #include "database.h"
20 #include "config_define.h"
21 
22 namespace OHOS::Security::SecurityGuard {
23 class DatabaseHelper : public Database {
24 public:
25     explicit DatabaseHelper(std::string dbTable);
26     ~DatabaseHelper() = default;
27     virtual int Init();
28     virtual void Release();
29     virtual int InsertEvent(const SecEvent& event);
30     virtual int QueryAllEvent(std::vector<SecEvent> &events);
31     virtual int QueryRecentEventByEventId(int64_t eventId, SecEvent &event);
32     virtual int QueryRecentEventByEventId(const std::vector<int64_t> &eventId, std::vector<SecEvent> &event);
33     virtual int QueryEventByEventId(int64_t eventId, std::vector<SecEvent> &events);
34     virtual int QueryEventByEventId(std::vector<int64_t> &eventIds, std::vector<SecEvent> &events);
35     virtual int QueryEventByEventIdAndDate(std::vector<int64_t> &eventIds, std::vector<SecEvent> &events,
36         std::string beginTime, std::string endTime);
37     virtual int QueryEventByEventType(int32_t eventType, std::vector<SecEvent> &events);
38     virtual int QueryEventByLevel(int32_t level, std::vector<SecEvent> &events);
39     virtual int QueryEventByOwner(std::string owner, std::vector<SecEvent> &events);
40     virtual int64_t CountAllEvent();
41     virtual int64_t CountEventByEventId(int64_t eventId);
42     virtual int DeleteOldEventByEventId(int64_t eventId, int64_t count);
43     virtual int DeleteAllEventByEventId(int64_t eventId);
44     virtual int FlushAllEvent();
45 
46 protected:
47     int QueryEventBase(const NativeRdb::RdbPredicates &predicates, std::vector<SecEvent> &events);
48     std::string CreateTable();
49     int32_t GetResultSetTableInfo(const std::shared_ptr<NativeRdb::ResultSet> &resultSet,
50         SecEventTableInfo &table);
51     void SetValuesBucket(const SecEvent &event, NativeRdb::ValuesBucket &values);
52     std::string dbPath_{};
53     std::string dbTable_{};
54 };
55 } // namespace OHOS::Security::SecurityGuard {
56 #endif // SECURITY_GUARD_DATABASE_HELPER_H
57