• 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 QueryData(const std::string &tableName, const std::string &ident, const int32_t userId,
55                       uint64_t start, uint64_t end, std::vector<NetStatsInfo> &infos);
56     int32_t DeleteData(const std::string &tableName, uint64_t start, uint64_t end);
57     int32_t DeleteData(const std::string &tableName, uint64_t uid);
58     int32_t ClearData(const std::string &tableName);
59     int32_t Step(std::vector<NetStatsInfo> &infos);
60     int32_t ExecSql(const std::string &sql, void *recv, SqlCallback callback);
61     int32_t Upgrade();
62     int32_t UpdateStatsFlag(const std::string &tableName, uint32_t uid, uint32_t flag);
63     int32_t UpdateDataFlag(const std::string &tableName, uint32_t oldFlag, uint32_t newFlag);
64     bool BackupNetStatsDataDB(const std::string &sourceDb, const std::string &backupDb);
65     int32_t DeleteAndBackup(int32_t errCode);
66     int32_t UpdateStatsFlagByUserId(const std::string &tableName, int32_t userId, uint32_t flag);
67     int32_t UpdateStatsUserIdByUserId(const std::string &tableName, int32_t oldUserId, int32_t newUserId);
68 
69 private:
70     enum TableVersion : int32_t {
71         Version_0 = 0,
72         Version_1,
73         Version_2,
74         Version_3,
75         Version_4,
76         Version_5,
77         Version_6, // private space
78     };
79 
80 private:
81     int32_t Open(const std::string &path);
82     int32_t Close();
83     int32_t BindInt64(int32_t idx, uint64_t start, uint64_t end);
84     int32_t GetTableVersion(TableVersion &version, const std::string &tableName);
85     int32_t UpdateTableVersion(TableVersion version, const std::string &tableName);
86     int32_t ExecTableUpgrade(const std::string &tableName, TableVersion newVersion);
87     void ExecUpgradeSql(const std::string &tableName, TableVersion &oldVersion, TableVersion newVersion);
88     bool BackupNetStatsData(const std::string &sourceDb, const std::string &backupDb);
89     void ExecUpgradeSqlNext(const std::string &tableName, TableVersion &oldVersion, TableVersion newVersion);
90     sqlite3 *sqlite_ = nullptr;
91     NetStatsSqliteStatement statement_;
92     static ffrt::mutex sqliteMutex_;
93     std::mutex mutex_;
94     std::atomic<bool> isNeedUpdate_ = false;
95     std::string path_ = "";
96     bool isDisplayTrafficAncoList_ = false;
97 };
98 } // namespace NetManagerStandard
99 } // namespace OHOS
100 
101 #endif // NET_STATS_DATABASE_HELPER_H
102