1 | <?php |
||
2 | /** |
||
3 | * Analytics |
||
4 | * |
||
5 | * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello |
||
6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
||
7 | */ |
||
8 | |||
9 | namespace OCA\Analytics\Db; |
||
10 | |||
11 | use OCP\DB\Exception; |
||
0 ignored issues
–
show
|
|||
12 | use OCP\IDBConnection; |
||
0 ignored issues
–
show
The type
OCP\IDBConnection was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
13 | use Psr\Log\LoggerInterface; |
||
0 ignored issues
–
show
The type
Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
14 | |||
15 | class DataloadMapper |
||
16 | { |
||
17 | private $userId; |
||
18 | private $db; |
||
19 | private $logger; |
||
20 | const TABLE_NAME = 'analytics_dataload'; |
||
21 | |||
22 | public function __construct( |
||
23 | $userId, |
||
24 | IDBConnection $db, |
||
25 | LoggerInterface $logger |
||
26 | ) |
||
27 | { |
||
28 | $this->userId = $userId; |
||
29 | $this->db = $db; |
||
30 | $this->logger = $logger; |
||
31 | self::TABLE_NAME; |
||
32 | } |
||
33 | |||
34 | public function beginTransaction() |
||
35 | { |
||
36 | $this->db->beginTransaction(); |
||
37 | } |
||
38 | |||
39 | public function commit() |
||
40 | { |
||
41 | $this->db->commit(); |
||
42 | } |
||
43 | |||
44 | public function rollBack() |
||
45 | { |
||
46 | $this->db->rollBack(); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * create a new dataload |
||
51 | * |
||
52 | * @NoAdminRequired |
||
53 | * @param int $datasetId |
||
54 | * @param int $datasourceId |
||
55 | * @return integer |
||
56 | * @throws \OCP\DB\Exception |
||
57 | */ |
||
58 | public function create(int $datasetId, int $datasourceId) |
||
59 | { |
||
60 | $sql = $this->db->getQueryBuilder(); |
||
61 | $sql->insert(self::TABLE_NAME) |
||
62 | ->values([ |
||
63 | 'user_id' => $sql->createNamedParameter($this->userId), |
||
64 | 'name' => $sql->createNamedParameter('New'), |
||
65 | 'dataset' => $sql->createNamedParameter($datasetId), |
||
66 | 'datasource' => $sql->createNamedParameter($datasourceId), |
||
67 | 'option' => $sql->createNamedParameter('{}'), |
||
68 | ]); |
||
69 | $sql->executeStatement(); |
||
70 | return (int)$sql->getLastInsertId(); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * get all data loads for a dataset |
||
75 | * |
||
76 | * @NoAdminRequired |
||
77 | * @param int $datasetId |
||
78 | * @return array |
||
79 | */ |
||
80 | public function read(int $datasetId) |
||
81 | { |
||
82 | $sql = $this->db->getQueryBuilder(); |
||
83 | $sql->from(self::TABLE_NAME) |
||
84 | ->select('*') |
||
85 | ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId))) |
||
86 | ->andWhere($sql->expr()->eq('dataset', $sql->createNamedParameter($datasetId))); |
||
87 | $statement = $sql->executeQuery(); |
||
88 | $result = $statement->fetchAll(); |
||
89 | $statement->closeCursor(); |
||
90 | return $result; |
||
91 | } |
||
92 | |||
93 | public function getAllDataloadMetadata() |
||
94 | { |
||
95 | $sql = $this->db->getQueryBuilder(); |
||
96 | $sql->from(self::TABLE_NAME) |
||
97 | ->select('dataset') |
||
98 | ->selectAlias($sql->func()->count('id'), 'dataloads') |
||
99 | ->selectAlias($sql->func()->max('schedule'), 'schedules') |
||
100 | ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId))) |
||
101 | ->addgroupBy('dataset'); |
||
102 | $statement = $sql->executeQuery(); |
||
103 | $result = $statement->fetchAll(); |
||
104 | $statement->closeCursor(); |
||
105 | return $result; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * get all data load & schedule metadata |
||
110 | * |
||
111 | * @NoAdminRequired |
||
112 | * @param $schedule |
||
113 | * @return array |
||
114 | */ |
||
115 | public function getDataloadBySchedule($schedule) |
||
116 | { |
||
117 | $sql = $this->db->getQueryBuilder(); |
||
118 | $sql->from(self::TABLE_NAME) |
||
119 | ->select('*') |
||
120 | ->where($sql->expr()->eq('schedule', $sql->createNamedParameter($schedule))); |
||
121 | $statement = $sql->executeQuery(); |
||
122 | $result = $statement->fetchAll(); |
||
123 | $statement->closeCursor(); |
||
124 | return $result; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * update dataload |
||
129 | * |
||
130 | * @NoAdminRequired |
||
131 | * @param int $dataloadId |
||
132 | * @param $name |
||
133 | * @param $option |
||
134 | * @param $schedule |
||
135 | * @return bool |
||
136 | */ |
||
137 | public function update(int $dataloadId, $name, $option, $schedule) |
||
138 | { |
||
139 | $name = $this->truncate($name, 64); |
||
140 | $sql = $this->db->getQueryBuilder(); |
||
141 | $sql->update(self::TABLE_NAME) |
||
142 | ->set('name', $sql->createNamedParameter($name)) |
||
143 | ->set('option', $sql->createNamedParameter($option)) |
||
144 | ->set('schedule', $sql->createNamedParameter($schedule)) |
||
145 | ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId))) |
||
146 | ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($dataloadId))); |
||
147 | $sql->executeStatement(); |
||
148 | return true; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * copy a data load |
||
153 | * |
||
154 | * @NoAdminRequired |
||
155 | * @param int $dataloadId |
||
156 | * @return bool |
||
157 | * @throws Exception |
||
158 | */ |
||
159 | public function copy(int $dataloadId) |
||
160 | { |
||
161 | $sql = $this->db->getQueryBuilder(); |
||
162 | $selectSql = $sql->select('*') |
||
163 | ->from(self::TABLE_NAME) |
||
164 | ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId))) |
||
165 | ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($dataloadId))); |
||
166 | $statement = $sql->executeQuery($selectSql); |
||
167 | $record = $statement->fetch(); |
||
168 | |||
169 | if ($record) { |
||
170 | $insertSql = $this->db->getQueryBuilder(); |
||
171 | $insertSql->insert(self::TABLE_NAME) |
||
172 | ->values([ |
||
173 | 'user_id' => $insertSql->createNamedParameter($this->userId), |
||
174 | 'name' => $insertSql->createNamedParameter($record['name'] . ' (copy)'), |
||
175 | 'dataset' => $insertSql->createNamedParameter($record['dataset']), |
||
176 | 'datasource' => $insertSql->createNamedParameter($record['datasource']), |
||
177 | 'option' => $insertSql->createNamedParameter($record['option']), |
||
178 | ]); |
||
179 | $insertSql->executeStatement(); |
||
180 | return true; |
||
181 | } else { |
||
182 | return false; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * delete a dataload |
||
188 | * |
||
189 | * @NoAdminRequired |
||
190 | * @param int $dataloadId |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function delete(int $dataloadId) |
||
194 | { |
||
195 | $sql = $this->db->getQueryBuilder(); |
||
196 | $sql->delete(self::TABLE_NAME) |
||
197 | ->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId))) |
||
198 | ->andWhere($sql->expr()->eq('id', $sql->createNamedParameter($dataloadId))); |
||
199 | $sql->executeStatement(); |
||
200 | return true; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * delete a dataload |
||
205 | * |
||
206 | * @NoAdminRequired |
||
207 | * @param int $datasetId |
||
208 | * @return bool |
||
209 | */ |
||
210 | public function deleteByDataset(int $datasetId) |
||
211 | { |
||
212 | $sql = $this->db->getQueryBuilder(); |
||
213 | $sql->delete(self::TABLE_NAME) |
||
214 | ->where($sql->expr()->eq('dataset', $sql->createNamedParameter($datasetId))); |
||
215 | $sql->executeStatement(); |
||
216 | return true; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * get data load by id |
||
221 | * @param int $dataloadId |
||
222 | * @return array |
||
223 | */ |
||
224 | public function getDataloadById(int $dataloadId) |
||
225 | { |
||
226 | $sql = $this->db->getQueryBuilder(); |
||
227 | $sql->from(self::TABLE_NAME) |
||
228 | ->select('*') |
||
229 | ->where($sql->expr()->eq('id', $sql->createNamedParameter($dataloadId))); |
||
230 | $statement = $sql->executeQuery(); |
||
231 | $result = $statement->fetch(); |
||
232 | $statement->closeCursor(); |
||
233 | return $result; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * truncates fiels do DB-field size |
||
238 | * |
||
239 | * @param $string |
||
240 | * @param $length |
||
241 | * @param $dots |
||
242 | * @return string |
||
243 | */ |
||
244 | private function truncate($string, $length, $dots = "...") |
||
245 | { |
||
246 | return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string; |
||
247 | } |
||
248 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths