dbConnection->getQueryBuilder(); $qb->insert(self::TABLE_METADATA) ->setValue('file_id', $qb->createNamedParameter($filesMetadata->getFileId(), IQueryBuilder::PARAM_INT)) ->setValue('json', $qb->createNamedParameter(json_encode($filesMetadata->jsonSerialize()))) ->setValue('sync_token', $qb->createNamedParameter($this->generateSyncToken())) ->setValue('last_update', (string)$qb->createFunction('NOW()')); $qb->executeStatement(); } /** * returns metadata for a file id * * @param int $fileId file id * * @return IFilesMetadata * @throws FilesMetadataNotFoundException if no metadata are found in database */ public function getMetadataFromFileId(int $fileId): IFilesMetadata { try { $qb = $this->dbConnection->getQueryBuilder(); $qb->select('json', 'sync_token')->from(self::TABLE_METADATA); $qb->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); $result = $qb->executeQuery(); $data = $result->fetch(); $result->closeCursor(); } catch (Exception $e) { $this->logger->warning('exception while getMetadataFromDatabase()', ['exception' => $e, 'fileId' => $fileId]); throw new FilesMetadataNotFoundException(); } if ($data === false) { throw new FilesMetadataNotFoundException(); } $metadata = new FilesMetadata($fileId); $metadata->importFromDatabase($data); return $metadata; } /** * returns metadata for multiple file ids * * @param array $fileIds file ids * * @return array File ID is the array key, files without metadata are not returned in the array * @psalm-return array */ public function getMetadataFromFileIds(array $fileIds): array { $qb = $this->dbConnection->getQueryBuilder(); $qb->select('file_id', 'json', 'sync_token')->from(self::TABLE_METADATA); $qb->where($qb->expr()->in('file_id', $qb->createNamedParameter($fileIds, IQueryBuilder::PARAM_INT_ARRAY))); $list = []; $result = $qb->executeQuery(); while ($data = $result->fetch()) { $fileId = (int)$data['file_id']; $metadata = new FilesMetadata($fileId); try { $metadata->importFromDatabase($data); } catch (FilesMetadataNotFoundException) { continue; } $list[$fileId] = $metadata; } $result->closeCursor(); return $list; } /** * drop metadata related to a file id * * @param int $fileId file id * * @return void * @throws Exception */ public function dropMetadata(int $fileId): void { $qb = $this->dbConnection->getQueryBuilder(); $qb->delete(self::TABLE_METADATA) ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); $qb->executeStatement(); } /** * update metadata in the database * * @param IFilesMetadata $filesMetadata metadata * * @return int number of affected rows * @throws Exception */ public function updateMetadata(IFilesMetadata $filesMetadata): int { $qb = $this->dbConnection->getQueryBuilder(); $expr = $qb->expr(); $qb->update(self::TABLE_METADATA) ->set('json', $qb->createNamedParameter(json_encode($filesMetadata->jsonSerialize()))) ->set('sync_token', $qb->createNamedParameter($this->generateSyncToken())) ->set('last_update', $qb->createFunction('NOW()')) ->where( $expr->andX( $expr->eq('file_id', $qb->createNamedParameter($filesMetadata->getFileId(), IQueryBuilder::PARAM_INT)), $expr->eq('sync_token', $qb->createNamedParameter($filesMetadata->getSyncToken())) ) ); return $qb->executeStatement(); } /** * generate a random token * @return string */ private function generateSyncToken(): string { $chars = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890'; $str = ''; $max = strlen($chars); for ($i = 0; $i < 7; $i++) { try { $str .= $chars[random_int(0, $max - 2)]; } catch (\Exception $e) { $this->logger->warning('exception during generateSyncToken', ['exception' => $e]); } } return $str; } } tonge/feat/add_retry_support_to_query_builder Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/services/DropServiceUtils.ts
blob: f10a09cfe27921713372adbdfd9d0de50aa5b045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178