• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 C_REQUEST_DATABASE_H
17 #define C_REQUEST_DATABASE_H
18 
19 #include <cstdint>
20 #include <vector>
21 
22 #include "c_enumration.h"
23 #include "c_filter.h"
24 #include "c_progress.h"
25 #include "c_task_config.h"
26 #include "c_task_info.h"
27 #include "rdb_errno.h"
28 #include "rdb_helper.h"
29 #include "rdb_open_callback.h"
30 #include "rdb_predicates.h"
31 #include "rdb_store.h"
32 #include "result_set.h"
33 #include "value_object.h"
34 
35 namespace OHOS::Request {
36 constexpr const char *DB_NAME = "/data/service/el1/public/database/request/request.db";
37 constexpr const char *REQUEST_DATABASE_VERSION = "API11_4.1-release";
38 constexpr const char *REQUEST_TASK_TABLE_NAME = "request_task";
39 constexpr int DATABASE_OPEN_VERSION = 1;
40 constexpr int DATABASE_NEW_VERSION = 2;
41 constexpr int QUERY_ERR = -1;
42 constexpr int QUERY_OK = 0;
43 
44 constexpr const char *CREATE_REQUEST_VERSION_TABLE = "CREATE TABLE IF NOT EXISTS request_version "
45                                                      "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
46                                                      "version TEXT, "
47                                                      "task_table TEXT)";
48 
49 constexpr const char *CREATE_REQUEST_TASK_TABLE = "CREATE TABLE IF NOT EXISTS request_task "
50                                                   "(task_id INTEGER PRIMARY KEY, "
51                                                   "uid INTEGER, "
52                                                   "token_id INTEGER, "
53                                                   "action INTEGER, "
54                                                   "mode INTEGER, "
55                                                   "cover INTEGER, "
56                                                   "network INTEGER, "
57                                                   "metered INTEGER, "
58                                                   "roaming INTEGER, "
59                                                   "ctime INTEGER, "
60                                                   "mtime INTEGER, "
61                                                   "reason INTEGER, "
62                                                   "gauge INTEGER, "
63                                                   "retry INTEGER, "
64                                                   "redirect INTEGER, "
65                                                   "tries INTEGER, "
66                                                   "version INTEGER, "
67                                                   "config_idx INTEGER, "
68                                                   "begins INTEGER, "
69                                                   "ends INTEGER, "
70                                                   "precise INTEGER, "
71                                                   "priority INTEGER, "
72                                                   "background INTEGER, "
73                                                   "bundle TEXT, "
74                                                   "url TEXT, "
75                                                   "data TEXT, "
76                                                   "token TEXT, "
77                                                   "title TEXT, "
78                                                   "description TEXT, "
79                                                   "method TEXT, "
80                                                   "headers TEXT, "
81                                                   "config_extras TEXT, "
82                                                   "mime_type TEXT, "
83                                                   "state INTEGER, "
84                                                   "idx INTEGER, "
85                                                   "total_processed INTEGER, "
86                                                   "sizes TEXT, "
87                                                   "processed TEXT, "
88                                                   "extras TEXT, "
89                                                   "form_items BLOB, "
90                                                   "file_specs BLOB, "
91                                                   "each_file_status BLOB, "
92                                                   "body_file_names BLOB, "
93                                                   "certs_paths BLOB)";
94 
95 class RequestDataBase {
96 public:
97     static RequestDataBase &GetInstance();
98     RequestDataBase(const RequestDataBase &) = delete;
99     RequestDataBase &operator=(const RequestDataBase &) = delete;
100     bool Insert(const std::string &table, const OHOS::NativeRdb::ValuesBucket &insertValues);
101     bool Update(const OHOS::NativeRdb::ValuesBucket values, const OHOS::NativeRdb::AbsRdbPredicates &predicates);
102     std::shared_ptr<OHOS::NativeRdb::ResultSet> Query(
103         const OHOS::NativeRdb::AbsRdbPredicates &predicates, const std::vector<std::string> &columns);
104     bool Delete(const OHOS::NativeRdb::AbsRdbPredicates &predicates);
105 
106 private:
107     RequestDataBase();
108 
109 private:
110     std::shared_ptr<OHOS::NativeRdb::RdbStore> store_;
111 };
112 
113 class RequestDBOpenCallback : public OHOS::NativeRdb::RdbOpenCallback {
114 public:
115     int OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override;
116     int OnOpen(OHOS::NativeRdb::RdbStore &rdbStore) override;
117     int OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion) override;
118     int OnDowngrade(OHOS::NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override;
119 };
120 } // namespace OHOS::Request
121 
122 #ifdef __cplusplus
123 extern "C" {
124 #endif
125 
126 struct CVectorWrapper {
127     uint32_t *ptr;
128     uint64_t len;
129 };
130 
131 // Request Database Modify.
132 bool HasRequestTaskRecord(uint32_t taskId);
133 bool RecordRequestTask(CTaskInfo *taskInfo, CTaskConfig *taskConfig);
134 bool UpdateRequestTask(uint32_t taskId, CUpdateInfo *updateInfo);
135 bool ChangeRequestTaskState(uint32_t taskId, uint64_t uid, State state);
136 CTaskInfo *Show(uint32_t taskId, uint64_t uid);
137 CTaskInfo *Touch(uint32_t taskId, uint64_t uid, CStringWrapper token);
138 CTaskInfo *Query(uint32_t taskId, Action queryAction);
139 CVectorWrapper Search(CFilter filter);
140 void DeleteCVectorWrapper(uint32_t *ptr);
141 void GetCommonTaskInfo(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet, TaskInfo &taskInfo);
142 int TouchRequestTaskInfo(const OHOS::NativeRdb::RdbPredicates &rdbPredicates, TaskInfo &taskInfo);
143 int QueryRequestTaskInfo(const OHOS::NativeRdb::RdbPredicates &rdbPredicates, TaskInfo &taskInfo);
144 CTaskInfo *BuildCTaskInfo(const TaskInfo &taskInfo);
145 CProgress BuildCProgress(const Progress &progress);
146 bool HasTaskConfigRecord(uint32_t taskId);
147 CTaskConfig **QueryAllTaskConfig();
148 int QueryTaskConfigLen();
149 int QueryRequestTaskConfig(const OHOS::NativeRdb::RdbPredicates &rdbPredicates, std::vector<TaskConfig> &taskConfigs);
150 CTaskConfig **BuildCTaskConfigs(const std::vector<TaskConfig> &taskConfigs);
151 bool CleanTaskConfigTable(uint32_t taskId, uint64_t uid);
152 void RequestDBRemoveRecordsFromTime(uint64_t time);
153 
154 #ifdef __cplusplus
155 }
156 #endif
157 #endif // C_REQUEST_DATABASE_H