1 /*
2  * Copyright (c) 2022 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 NET_STATS_DATABASE_HELPER_H
17 #define NET_STATS_DATABASE_HELPER_H
18 
19 #include <climits>
20 #include <functional>
21 #include <string>
22 
23 #ifndef USE_SQLITE_SYMBOLS
24 #include "sqlite3.h"
25 #else
26 #include "sqlite3sym.h"
27 #endif
28 
29 #include "net_stats_sqlite_statement.h"
30 #include "net_stats_info.h"
31 
32 namespace OHOS {
33 namespace NetManagerStandard {
34 class NetStatsDatabaseHelper {
35 public:
36     using SqlCallback = sqlite3_callback;
37     explicit NetStatsDatabaseHelper(const std::string &path);
38     NetStatsDatabaseHelper() = delete;
39     ~NetStatsDatabaseHelper();
40 
41     int32_t CreateTable(const std::string &tableName, const std::string &tableInfo);
42     int32_t InsertData(const std::string &tableName, const std::string &paramList,
43                        const NetStatsInfo &info);
44     int32_t SelectData(std::vector<NetStatsInfo> &infos, const std::string &tableName, uint64_t start, uint64_t end);
45     int32_t SelectData(const uint32_t uid, uint64_t start, uint64_t end, std::vector<NetStatsInfo> &infos);
46     int32_t SelectData(const std::string &iface, uint64_t start, uint64_t end, std::vector<NetStatsInfo> &infos);
47     int32_t SelectData(const std::string &iface, const uint32_t uid, uint64_t start, uint64_t end,
48                        std::vector<NetStatsInfo> &infos);
49     int32_t QueryData(const std::string &tableName, const std::string &ident, uint64_t start, uint64_t end,
50                       std::vector<NetStatsInfo> &infos);
51     int32_t QueryData(const std::string &tableName, const uint32_t uid, const std::string &ident, uint64_t start,
52                       uint64_t end, std::vector<NetStatsInfo> &infos);
53     int32_t DeleteData(const std::string &tableName, uint64_t start, uint64_t end);
54     int32_t DeleteData(const std::string &tableName, uint64_t uid);
55     int32_t ClearData(const std::string &tableName);
56     int32_t Step(std::vector<NetStatsInfo> &infos);
57     int32_t ExecSql(const std::string &sql, void *recv, SqlCallback callback);
58     int32_t Upgrade();
59     int32_t UpdateStatsFlag(const std::string &tableName, uint32_t uid, uint32_t flag);
60 
61 private:
62     enum TableVersion : int32_t {
63         Version_0 = 0,
64         Version_1,
65         Version_2,
66         Version_3,
67     };
68 
69 private:
70     int32_t Open(const std::string &path);
71     int32_t Close();
72     int32_t BindInt64(int32_t idx, uint64_t start, uint64_t end);
73     int32_t GetTableVersion(TableVersion &version, const std::string &tableName);
74     int32_t UpdateTableVersion(TableVersion version, const std::string &tableName);
75     int32_t ExecTableUpgrade(const std::string &tableName, TableVersion newVersion);
76     sqlite3 *sqlite_ = nullptr;
77     NetStatsSqliteStatement statement_;
78 };
79 } // namespace NetManagerStandard
80 } // namespace OHOS
81 
82 #endif // NET_STATS_DATABASE_HELPER_H
83