• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "ffrt_inner.h"
32 
33 namespace OHOS {
34 namespace NetManagerStandard {
35 class NetStatsDatabaseHelper {
36 public:
37     using SqlCallback = sqlite3_callback;
38     explicit NetStatsDatabaseHelper(const std::string &path);
39     NetStatsDatabaseHelper() = delete;
40     ~NetStatsDatabaseHelper();
41 
42     int32_t CreateTable(const std::string &tableName, const std::string &tableInfo);
43     int32_t InsertData(const std::string &tableName, const std::string &paramList,
44                        const NetStatsInfo &info);
45     int32_t SelectData(std::vector<NetStatsInfo> &infos, const std::string &tableName, uint64_t start, uint64_t end);
46     int32_t SelectData(const uint32_t uid, uint64_t start, uint64_t end, std::vector<NetStatsInfo> &infos);
47     int32_t SelectData(const std::string &iface, uint64_t start, uint64_t end, std::vector<NetStatsInfo> &infos);
48     int32_t SelectData(const std::string &iface, const uint32_t uid, uint64_t start, uint64_t end,
49                        std::vector<NetStatsInfo> &infos);
50     int32_t QueryData(const std::string &tableName, const std::string &ident, uint64_t start, uint64_t end,
51                       std::vector<NetStatsInfo> &infos);
52     int32_t QueryData(const std::string &tableName, const uint32_t uid, const std::string &ident, uint64_t start,
53                       uint64_t end, std::vector<NetStatsInfo> &infos);
54     int32_t DeleteData(const std::string &tableName, uint64_t start, uint64_t end);
55     int32_t DeleteData(const std::string &tableName, uint64_t uid);
56     int32_t ClearData(const std::string &tableName);
57     int32_t Step(std::vector<NetStatsInfo> &infos);
58     int32_t ExecSql(const std::string &sql, void *recv, SqlCallback callback);
59     int32_t Upgrade();
60     int32_t UpdateStatsFlag(const std::string &tableName, uint32_t uid, uint32_t flag);
61     int32_t UpdateDataFlag(const std::string &tableName, uint32_t oldFlag, uint32_t newFlag);
62 
63 private:
64     enum TableVersion : int32_t {
65         Version_0 = 0,
66         Version_1,
67         Version_2,
68         Version_3,
69         Version_4,
70     };
71 
72 private:
73     int32_t Open(const std::string &path);
74     int32_t Close();
75     int32_t BindInt64(int32_t idx, uint64_t start, uint64_t end);
76     int32_t GetTableVersion(TableVersion &version, const std::string &tableName);
77     int32_t UpdateTableVersion(TableVersion version, const std::string &tableName);
78     int32_t ExecTableUpgrade(const std::string &tableName, TableVersion newVersion);
79     void ExecUpgradeSql(const std::string &tableName, TableVersion &oldVersion, TableVersion newVersion);
80     sqlite3 *sqlite_ = nullptr;
81     NetStatsSqliteStatement statement_;
82     ffrt::mutex sqliteMutex_;
83 };
84 } // namespace NetManagerStandard
85 } // namespace OHOS
86 
87 #endif // NET_STATS_DATABASE_HELPER_H
88