1 /*
2  * Copyright (c) 2022-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 HIVIEW_PLUGINS_USAGE_EVENT_REPORT_SREVICE_EVENT_DB_HELPER_H
17 #define HIVIEW_PLUGINS_USAGE_EVENT_REPORT_SREVICE_EVENT_DB_HELPER_H
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 
23 #include "logger_event.h"
24 #include "rdb_helper.h"
25 #include "rdb_store.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 class EventDbHelper {
30 public:
31     EventDbHelper(const std::string workPath);
32     ~EventDbHelper();
33     int InsertPluginStatsEvent(std::shared_ptr<LoggerEvent> event);
34     int InsertSysUsageEvent(std::shared_ptr<LoggerEvent> event, const std::string& table);
35     int QueryPluginStatsEvent(std::vector<std::shared_ptr<LoggerEvent>>& events, const std::string& pluginName = "");
36     int QuerySysUsageEvent(std::shared_ptr<LoggerEvent>& events, const std::string& table);
37     int DeletePluginStatsEvent();
38     int DeleteSysUsageEvent(const std::string& table);
39 
40 private:
41     void InitDbStore();
42     int CreateTable(const std::string& table, const std::vector<std::pair<std::string, std::string>>& fields);
43     int CreatePluginStatsTable(const std::string& table);
44     int CreateSysUsageTable(const std::string& table);
45     int InsertPluginStatsTable(const std::string& pluginName, const std::string& eventStr);
46     int InsertSysUsageTable(const std::string& table, const std::string& eventStr);
47     int UpdatePluginStatsTable(const std::string& pluginName, const std::string& eventStr);
48     int UpdateSysUsageTable(const std::string& table, const std::string& eventStr);
49     int QueryPluginStatsTable(std::vector<std::string>& eventStrs, const std::string& pluginName);
50     int QuerySysUsageTable(std::string& eventStr, const std::string& table);
51     int QueryDb(std::vector<std::string>& eventStrs, const std::string& table,
52         const std::vector<std::pair<std::string, std::string>>& queryConds);
53     int DeleteTableData(const std::string& table);
54 
55 private:
56     std::string dbPath_;
57     std::shared_ptr<NativeRdb::RdbStore> rdbStore_;
58 };
59 
60 class EventDbStoreCallback : public NativeRdb::RdbOpenCallback {
61 public:
62     int OnCreate(NativeRdb::RdbStore &rdbStore) override;
63     int OnUpgrade(NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion) override;
64 };
65 } // namespace HiviewDFX
66 } // namespace OHOS
67 #endif // HIVIEW_PLUGINS_USAGE_EVENT_REPORT_SREVICE_EVENT_DB_HELPER_H
68