@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | } |
155 | 155 | $query = $this->connection->prepare('SELECT `lock` from `*PREFIX*file_locks` WHERE `key` = ?'); |
156 | 156 | $query->execute([$path]); |
157 | - $lockValue = (int)$query->fetchColumn(); |
|
157 | + $lockValue = (int) $query->fetchColumn(); |
|
158 | 158 | if ($type === self::LOCK_SHARED) { |
159 | 159 | if ($this->isLocallyLocked($path)) { |
160 | 160 | // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db |
@@ -291,7 +291,7 @@ discard block |
||
291 | 291 | } |
292 | 292 | // since we keep shared locks we need to manually clean those |
293 | 293 | $lockedPaths = array_keys($this->sharedLocks); |
294 | - $lockedPaths = array_filter($lockedPaths, function ($path) { |
|
294 | + $lockedPaths = array_filter($lockedPaths, function($path) { |
|
295 | 295 | return $this->sharedLocks[$path]; |
296 | 296 | }); |
297 | 297 |
@@ -39,279 +39,279 @@ |
||
39 | 39 | * Locking provider that stores the locks in the database |
40 | 40 | */ |
41 | 41 | class DBLockingProvider extends AbstractLockingProvider { |
42 | - /** |
|
43 | - * @var \OCP\IDBConnection |
|
44 | - */ |
|
45 | - private $connection; |
|
42 | + /** |
|
43 | + * @var \OCP\IDBConnection |
|
44 | + */ |
|
45 | + private $connection; |
|
46 | 46 | |
47 | - /** |
|
48 | - * @var \OCP\ILogger |
|
49 | - */ |
|
50 | - private $logger; |
|
47 | + /** |
|
48 | + * @var \OCP\ILogger |
|
49 | + */ |
|
50 | + private $logger; |
|
51 | 51 | |
52 | - /** |
|
53 | - * @var \OCP\AppFramework\Utility\ITimeFactory |
|
54 | - */ |
|
55 | - private $timeFactory; |
|
52 | + /** |
|
53 | + * @var \OCP\AppFramework\Utility\ITimeFactory |
|
54 | + */ |
|
55 | + private $timeFactory; |
|
56 | 56 | |
57 | - private $sharedLocks = []; |
|
57 | + private $sharedLocks = []; |
|
58 | 58 | |
59 | - /** |
|
60 | - * @var bool |
|
61 | - */ |
|
62 | - private $cacheSharedLocks; |
|
59 | + /** |
|
60 | + * @var bool |
|
61 | + */ |
|
62 | + private $cacheSharedLocks; |
|
63 | 63 | |
64 | - /** |
|
65 | - * Check if we have an open shared lock for a path |
|
66 | - * |
|
67 | - * @param string $path |
|
68 | - * @return bool |
|
69 | - */ |
|
70 | - protected function isLocallyLocked(string $path): bool { |
|
71 | - return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path]; |
|
72 | - } |
|
64 | + /** |
|
65 | + * Check if we have an open shared lock for a path |
|
66 | + * |
|
67 | + * @param string $path |
|
68 | + * @return bool |
|
69 | + */ |
|
70 | + protected function isLocallyLocked(string $path): bool { |
|
71 | + return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path]; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Mark a locally acquired lock |
|
76 | - * |
|
77 | - * @param string $path |
|
78 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
79 | - */ |
|
80 | - protected function markAcquire(string $path, int $type) { |
|
81 | - parent::markAcquire($path, $type); |
|
82 | - if ($this->cacheSharedLocks) { |
|
83 | - if ($type === self::LOCK_SHARED) { |
|
84 | - $this->sharedLocks[$path] = true; |
|
85 | - } |
|
86 | - } |
|
87 | - } |
|
74 | + /** |
|
75 | + * Mark a locally acquired lock |
|
76 | + * |
|
77 | + * @param string $path |
|
78 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
79 | + */ |
|
80 | + protected function markAcquire(string $path, int $type) { |
|
81 | + parent::markAcquire($path, $type); |
|
82 | + if ($this->cacheSharedLocks) { |
|
83 | + if ($type === self::LOCK_SHARED) { |
|
84 | + $this->sharedLocks[$path] = true; |
|
85 | + } |
|
86 | + } |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Change the type of an existing tracked lock |
|
91 | - * |
|
92 | - * @param string $path |
|
93 | - * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
94 | - */ |
|
95 | - protected function markChange(string $path, int $targetType) { |
|
96 | - parent::markChange($path, $targetType); |
|
97 | - if ($this->cacheSharedLocks) { |
|
98 | - if ($targetType === self::LOCK_SHARED) { |
|
99 | - $this->sharedLocks[$path] = true; |
|
100 | - } else if ($targetType === self::LOCK_EXCLUSIVE) { |
|
101 | - $this->sharedLocks[$path] = false; |
|
102 | - } |
|
103 | - } |
|
104 | - } |
|
89 | + /** |
|
90 | + * Change the type of an existing tracked lock |
|
91 | + * |
|
92 | + * @param string $path |
|
93 | + * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
94 | + */ |
|
95 | + protected function markChange(string $path, int $targetType) { |
|
96 | + parent::markChange($path, $targetType); |
|
97 | + if ($this->cacheSharedLocks) { |
|
98 | + if ($targetType === self::LOCK_SHARED) { |
|
99 | + $this->sharedLocks[$path] = true; |
|
100 | + } else if ($targetType === self::LOCK_EXCLUSIVE) { |
|
101 | + $this->sharedLocks[$path] = false; |
|
102 | + } |
|
103 | + } |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * @param \OCP\IDBConnection $connection |
|
108 | - * @param \OCP\ILogger $logger |
|
109 | - * @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory |
|
110 | - * @param int $ttl |
|
111 | - * @param bool $cacheSharedLocks |
|
112 | - */ |
|
113 | - public function __construct( |
|
114 | - IDBConnection $connection, |
|
115 | - ILogger $logger, |
|
116 | - ITimeFactory $timeFactory, |
|
117 | - int $ttl = 3600, |
|
118 | - $cacheSharedLocks = true |
|
119 | - ) { |
|
120 | - $this->connection = $connection; |
|
121 | - $this->logger = $logger; |
|
122 | - $this->timeFactory = $timeFactory; |
|
123 | - $this->ttl = $ttl; |
|
124 | - $this->cacheSharedLocks = $cacheSharedLocks; |
|
125 | - } |
|
106 | + /** |
|
107 | + * @param \OCP\IDBConnection $connection |
|
108 | + * @param \OCP\ILogger $logger |
|
109 | + * @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory |
|
110 | + * @param int $ttl |
|
111 | + * @param bool $cacheSharedLocks |
|
112 | + */ |
|
113 | + public function __construct( |
|
114 | + IDBConnection $connection, |
|
115 | + ILogger $logger, |
|
116 | + ITimeFactory $timeFactory, |
|
117 | + int $ttl = 3600, |
|
118 | + $cacheSharedLocks = true |
|
119 | + ) { |
|
120 | + $this->connection = $connection; |
|
121 | + $this->logger = $logger; |
|
122 | + $this->timeFactory = $timeFactory; |
|
123 | + $this->ttl = $ttl; |
|
124 | + $this->cacheSharedLocks = $cacheSharedLocks; |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * Insert a file locking row if it does not exists. |
|
129 | - * |
|
130 | - * @param string $path |
|
131 | - * @param int $lock |
|
132 | - * @return int number of inserted rows |
|
133 | - */ |
|
134 | - protected function initLockField(string $path, int $lock = 0): int { |
|
135 | - $expire = $this->getExpireTime(); |
|
136 | - return $this->connection->insertIgnoreConflict('file_locks', [ |
|
137 | - 'key' => $path, |
|
138 | - 'lock' => $lock, |
|
139 | - 'ttl' => $expire |
|
140 | - ]); |
|
141 | - } |
|
127 | + /** |
|
128 | + * Insert a file locking row if it does not exists. |
|
129 | + * |
|
130 | + * @param string $path |
|
131 | + * @param int $lock |
|
132 | + * @return int number of inserted rows |
|
133 | + */ |
|
134 | + protected function initLockField(string $path, int $lock = 0): int { |
|
135 | + $expire = $this->getExpireTime(); |
|
136 | + return $this->connection->insertIgnoreConflict('file_locks', [ |
|
137 | + 'key' => $path, |
|
138 | + 'lock' => $lock, |
|
139 | + 'ttl' => $expire |
|
140 | + ]); |
|
141 | + } |
|
142 | 142 | |
143 | - /** |
|
144 | - * @return int |
|
145 | - */ |
|
146 | - protected function getExpireTime(): int { |
|
147 | - return $this->timeFactory->getTime() + $this->ttl; |
|
148 | - } |
|
143 | + /** |
|
144 | + * @return int |
|
145 | + */ |
|
146 | + protected function getExpireTime(): int { |
|
147 | + return $this->timeFactory->getTime() + $this->ttl; |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * @param string $path |
|
152 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
153 | - * @return bool |
|
154 | - */ |
|
155 | - public function isLocked(string $path, int $type): bool { |
|
156 | - if ($this->hasAcquiredLock($path, $type)) { |
|
157 | - return true; |
|
158 | - } |
|
159 | - $query = $this->connection->prepare('SELECT `lock` from `*PREFIX*file_locks` WHERE `key` = ?'); |
|
160 | - $query->execute([$path]); |
|
161 | - $lockValue = (int)$query->fetchColumn(); |
|
162 | - if ($type === self::LOCK_SHARED) { |
|
163 | - if ($this->isLocallyLocked($path)) { |
|
164 | - // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db |
|
165 | - return $lockValue > 1; |
|
166 | - } else { |
|
167 | - return $lockValue > 0; |
|
168 | - } |
|
169 | - } else if ($type === self::LOCK_EXCLUSIVE) { |
|
170 | - return $lockValue === -1; |
|
171 | - } else { |
|
172 | - return false; |
|
173 | - } |
|
174 | - } |
|
150 | + /** |
|
151 | + * @param string $path |
|
152 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
153 | + * @return bool |
|
154 | + */ |
|
155 | + public function isLocked(string $path, int $type): bool { |
|
156 | + if ($this->hasAcquiredLock($path, $type)) { |
|
157 | + return true; |
|
158 | + } |
|
159 | + $query = $this->connection->prepare('SELECT `lock` from `*PREFIX*file_locks` WHERE `key` = ?'); |
|
160 | + $query->execute([$path]); |
|
161 | + $lockValue = (int)$query->fetchColumn(); |
|
162 | + if ($type === self::LOCK_SHARED) { |
|
163 | + if ($this->isLocallyLocked($path)) { |
|
164 | + // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db |
|
165 | + return $lockValue > 1; |
|
166 | + } else { |
|
167 | + return $lockValue > 0; |
|
168 | + } |
|
169 | + } else if ($type === self::LOCK_EXCLUSIVE) { |
|
170 | + return $lockValue === -1; |
|
171 | + } else { |
|
172 | + return false; |
|
173 | + } |
|
174 | + } |
|
175 | 175 | |
176 | - /** |
|
177 | - * @param string $path |
|
178 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
179 | - * @throws \OCP\Lock\LockedException |
|
180 | - */ |
|
181 | - public function acquireLock(string $path, int $type) { |
|
182 | - $expire = $this->getExpireTime(); |
|
183 | - if ($type === self::LOCK_SHARED) { |
|
184 | - if (!$this->isLocallyLocked($path)) { |
|
185 | - $result = $this->initLockField($path, 1); |
|
186 | - if ($result <= 0) { |
|
187 | - $result = $this->connection->executeUpdate( |
|
188 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = `lock` + 1, `ttl` = ? WHERE `key` = ? AND `lock` >= 0', |
|
189 | - [$expire, $path] |
|
190 | - ); |
|
191 | - } |
|
192 | - } else { |
|
193 | - $result = 1; |
|
194 | - } |
|
195 | - } else { |
|
196 | - $existing = 0; |
|
197 | - if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) { |
|
198 | - $existing = 1; |
|
199 | - } |
|
200 | - $result = $this->initLockField($path, -1); |
|
201 | - if ($result <= 0) { |
|
202 | - $result = $this->connection->executeUpdate( |
|
203 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = ?', |
|
204 | - [$expire, $path, $existing] |
|
205 | - ); |
|
206 | - } |
|
207 | - } |
|
208 | - if ($result !== 1) { |
|
209 | - throw new LockedException($path); |
|
210 | - } |
|
211 | - $this->markAcquire($path, $type); |
|
212 | - } |
|
176 | + /** |
|
177 | + * @param string $path |
|
178 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
179 | + * @throws \OCP\Lock\LockedException |
|
180 | + */ |
|
181 | + public function acquireLock(string $path, int $type) { |
|
182 | + $expire = $this->getExpireTime(); |
|
183 | + if ($type === self::LOCK_SHARED) { |
|
184 | + if (!$this->isLocallyLocked($path)) { |
|
185 | + $result = $this->initLockField($path, 1); |
|
186 | + if ($result <= 0) { |
|
187 | + $result = $this->connection->executeUpdate( |
|
188 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = `lock` + 1, `ttl` = ? WHERE `key` = ? AND `lock` >= 0', |
|
189 | + [$expire, $path] |
|
190 | + ); |
|
191 | + } |
|
192 | + } else { |
|
193 | + $result = 1; |
|
194 | + } |
|
195 | + } else { |
|
196 | + $existing = 0; |
|
197 | + if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) { |
|
198 | + $existing = 1; |
|
199 | + } |
|
200 | + $result = $this->initLockField($path, -1); |
|
201 | + if ($result <= 0) { |
|
202 | + $result = $this->connection->executeUpdate( |
|
203 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = ?', |
|
204 | + [$expire, $path, $existing] |
|
205 | + ); |
|
206 | + } |
|
207 | + } |
|
208 | + if ($result !== 1) { |
|
209 | + throw new LockedException($path); |
|
210 | + } |
|
211 | + $this->markAcquire($path, $type); |
|
212 | + } |
|
213 | 213 | |
214 | - /** |
|
215 | - * @param string $path |
|
216 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
217 | - * |
|
218 | - * @suppress SqlInjectionChecker |
|
219 | - */ |
|
220 | - public function releaseLock(string $path, int $type) { |
|
221 | - $this->markRelease($path, $type); |
|
214 | + /** |
|
215 | + * @param string $path |
|
216 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
217 | + * |
|
218 | + * @suppress SqlInjectionChecker |
|
219 | + */ |
|
220 | + public function releaseLock(string $path, int $type) { |
|
221 | + $this->markRelease($path, $type); |
|
222 | 222 | |
223 | - // we keep shared locks till the end of the request so we can re-use them |
|
224 | - if ($type === self::LOCK_EXCLUSIVE) { |
|
225 | - $this->connection->executeUpdate( |
|
226 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = 0 WHERE `key` = ? AND `lock` = -1', |
|
227 | - [$path] |
|
228 | - ); |
|
229 | - } else if (!$this->cacheSharedLocks) { |
|
230 | - $query = $this->connection->getQueryBuilder(); |
|
231 | - $query->update('file_locks') |
|
232 | - ->set('lock', $query->func()->subtract('lock', $query->createNamedParameter(1))) |
|
233 | - ->where($query->expr()->eq('key', $query->createNamedParameter($path))) |
|
234 | - ->andWhere($query->expr()->gt('lock', $query->createNamedParameter(0))); |
|
235 | - $query->execute(); |
|
236 | - } |
|
237 | - } |
|
223 | + // we keep shared locks till the end of the request so we can re-use them |
|
224 | + if ($type === self::LOCK_EXCLUSIVE) { |
|
225 | + $this->connection->executeUpdate( |
|
226 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = 0 WHERE `key` = ? AND `lock` = -1', |
|
227 | + [$path] |
|
228 | + ); |
|
229 | + } else if (!$this->cacheSharedLocks) { |
|
230 | + $query = $this->connection->getQueryBuilder(); |
|
231 | + $query->update('file_locks') |
|
232 | + ->set('lock', $query->func()->subtract('lock', $query->createNamedParameter(1))) |
|
233 | + ->where($query->expr()->eq('key', $query->createNamedParameter($path))) |
|
234 | + ->andWhere($query->expr()->gt('lock', $query->createNamedParameter(0))); |
|
235 | + $query->execute(); |
|
236 | + } |
|
237 | + } |
|
238 | 238 | |
239 | - /** |
|
240 | - * Change the type of an existing lock |
|
241 | - * |
|
242 | - * @param string $path |
|
243 | - * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
244 | - * @throws \OCP\Lock\LockedException |
|
245 | - */ |
|
246 | - public function changeLock(string $path, int $targetType) { |
|
247 | - $expire = $this->getExpireTime(); |
|
248 | - if ($targetType === self::LOCK_SHARED) { |
|
249 | - $result = $this->connection->executeUpdate( |
|
250 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = 1, `ttl` = ? WHERE `key` = ? AND `lock` = -1', |
|
251 | - [$expire, $path] |
|
252 | - ); |
|
253 | - } else { |
|
254 | - // since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually |
|
255 | - if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) { |
|
256 | - throw new LockedException($path); |
|
257 | - } |
|
258 | - $result = $this->connection->executeUpdate( |
|
259 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = 1', |
|
260 | - [$expire, $path] |
|
261 | - ); |
|
262 | - } |
|
263 | - if ($result !== 1) { |
|
264 | - throw new LockedException($path); |
|
265 | - } |
|
266 | - $this->markChange($path, $targetType); |
|
267 | - } |
|
239 | + /** |
|
240 | + * Change the type of an existing lock |
|
241 | + * |
|
242 | + * @param string $path |
|
243 | + * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
244 | + * @throws \OCP\Lock\LockedException |
|
245 | + */ |
|
246 | + public function changeLock(string $path, int $targetType) { |
|
247 | + $expire = $this->getExpireTime(); |
|
248 | + if ($targetType === self::LOCK_SHARED) { |
|
249 | + $result = $this->connection->executeUpdate( |
|
250 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = 1, `ttl` = ? WHERE `key` = ? AND `lock` = -1', |
|
251 | + [$expire, $path] |
|
252 | + ); |
|
253 | + } else { |
|
254 | + // since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually |
|
255 | + if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) { |
|
256 | + throw new LockedException($path); |
|
257 | + } |
|
258 | + $result = $this->connection->executeUpdate( |
|
259 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = 1', |
|
260 | + [$expire, $path] |
|
261 | + ); |
|
262 | + } |
|
263 | + if ($result !== 1) { |
|
264 | + throw new LockedException($path); |
|
265 | + } |
|
266 | + $this->markChange($path, $targetType); |
|
267 | + } |
|
268 | 268 | |
269 | - /** |
|
270 | - * cleanup empty locks |
|
271 | - */ |
|
272 | - public function cleanExpiredLocks() { |
|
273 | - $expire = $this->timeFactory->getTime(); |
|
274 | - try { |
|
275 | - $this->connection->executeUpdate( |
|
276 | - 'DELETE FROM `*PREFIX*file_locks` WHERE `ttl` < ?', |
|
277 | - [$expire] |
|
278 | - ); |
|
279 | - } catch (\Exception $e) { |
|
280 | - // If the table is missing, the clean up was successful |
|
281 | - if ($this->connection->tableExists('file_locks')) { |
|
282 | - throw $e; |
|
283 | - } |
|
284 | - } |
|
285 | - } |
|
269 | + /** |
|
270 | + * cleanup empty locks |
|
271 | + */ |
|
272 | + public function cleanExpiredLocks() { |
|
273 | + $expire = $this->timeFactory->getTime(); |
|
274 | + try { |
|
275 | + $this->connection->executeUpdate( |
|
276 | + 'DELETE FROM `*PREFIX*file_locks` WHERE `ttl` < ?', |
|
277 | + [$expire] |
|
278 | + ); |
|
279 | + } catch (\Exception $e) { |
|
280 | + // If the table is missing, the clean up was successful |
|
281 | + if ($this->connection->tableExists('file_locks')) { |
|
282 | + throw $e; |
|
283 | + } |
|
284 | + } |
|
285 | + } |
|
286 | 286 | |
287 | - /** |
|
288 | - * release all lock acquired by this instance which were marked using the mark* methods |
|
289 | - * |
|
290 | - * @suppress SqlInjectionChecker |
|
291 | - */ |
|
292 | - public function releaseAll() { |
|
293 | - parent::releaseAll(); |
|
287 | + /** |
|
288 | + * release all lock acquired by this instance which were marked using the mark* methods |
|
289 | + * |
|
290 | + * @suppress SqlInjectionChecker |
|
291 | + */ |
|
292 | + public function releaseAll() { |
|
293 | + parent::releaseAll(); |
|
294 | 294 | |
295 | - if (!$this->cacheSharedLocks) { |
|
296 | - return; |
|
297 | - } |
|
298 | - // since we keep shared locks we need to manually clean those |
|
299 | - $lockedPaths = array_keys($this->sharedLocks); |
|
300 | - $lockedPaths = array_filter($lockedPaths, function ($path) { |
|
301 | - return $this->sharedLocks[$path]; |
|
302 | - }); |
|
295 | + if (!$this->cacheSharedLocks) { |
|
296 | + return; |
|
297 | + } |
|
298 | + // since we keep shared locks we need to manually clean those |
|
299 | + $lockedPaths = array_keys($this->sharedLocks); |
|
300 | + $lockedPaths = array_filter($lockedPaths, function ($path) { |
|
301 | + return $this->sharedLocks[$path]; |
|
302 | + }); |
|
303 | 303 | |
304 | - $chunkedPaths = array_chunk($lockedPaths, 100); |
|
304 | + $chunkedPaths = array_chunk($lockedPaths, 100); |
|
305 | 305 | |
306 | - foreach ($chunkedPaths as $chunk) { |
|
307 | - $builder = $this->connection->getQueryBuilder(); |
|
306 | + foreach ($chunkedPaths as $chunk) { |
|
307 | + $builder = $this->connection->getQueryBuilder(); |
|
308 | 308 | |
309 | - $query = $builder->update('file_locks') |
|
310 | - ->set('lock', $builder->func()->subtract('lock', $builder->expr()->literal(1))) |
|
311 | - ->where($builder->expr()->in('key', $builder->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY))) |
|
312 | - ->andWhere($builder->expr()->gt('lock', new Literal(0))); |
|
309 | + $query = $builder->update('file_locks') |
|
310 | + ->set('lock', $builder->func()->subtract('lock', $builder->expr()->literal(1))) |
|
311 | + ->where($builder->expr()->in('key', $builder->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY))) |
|
312 | + ->andWhere($builder->expr()->gt('lock', new Literal(0))); |
|
313 | 313 | |
314 | - $query->execute(); |
|
315 | - } |
|
316 | - } |
|
314 | + $query->execute(); |
|
315 | + } |
|
316 | + } |
|
317 | 317 | } |
@@ -46,256 +46,256 @@ |
||
46 | 46 | */ |
47 | 47 | interface IDBConnection { |
48 | 48 | |
49 | - const ADD_MISSING_INDEXES_EVENT = self::class . '::ADD_MISSING_INDEXES'; |
|
50 | - const CHECK_MISSING_INDEXES_EVENT = self::class . '::CHECK_MISSING_INDEXES'; |
|
51 | - |
|
52 | - /** |
|
53 | - * Gets the QueryBuilder for the connection. |
|
54 | - * |
|
55 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
56 | - * @since 8.2.0 |
|
57 | - */ |
|
58 | - public function getQueryBuilder(); |
|
59 | - |
|
60 | - /** |
|
61 | - * Used to abstract the ownCloud database access away |
|
62 | - * @param string $sql the sql query with ? placeholder for params |
|
63 | - * @param int $limit the maximum number of rows |
|
64 | - * @param int $offset from which row we want to start |
|
65 | - * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
66 | - * @since 6.0.0 |
|
67 | - */ |
|
68 | - public function prepare($sql, $limit=null, $offset=null); |
|
69 | - |
|
70 | - /** |
|
71 | - * Executes an, optionally parameterized, SQL query. |
|
72 | - * |
|
73 | - * If the query is parameterized, a prepared statement is used. |
|
74 | - * If an SQLLogger is configured, the execution is logged. |
|
75 | - * |
|
76 | - * @param string $query The SQL query to execute. |
|
77 | - * @param string[] $params The parameters to bind to the query, if any. |
|
78 | - * @param array $types The types the previous parameters are in. |
|
79 | - * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
80 | - * @since 8.0.0 |
|
81 | - */ |
|
82 | - public function executeQuery($query, array $params = array(), $types = array()); |
|
83 | - |
|
84 | - /** |
|
85 | - * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
86 | - * and returns the number of affected rows. |
|
87 | - * |
|
88 | - * This method supports PDO binding types as well as DBAL mapping types. |
|
89 | - * |
|
90 | - * @param string $query The SQL query. |
|
91 | - * @param array $params The query parameters. |
|
92 | - * @param array $types The parameter types. |
|
93 | - * @return integer The number of affected rows. |
|
94 | - * @since 8.0.0 |
|
95 | - */ |
|
96 | - public function executeUpdate($query, array $params = array(), array $types = array()); |
|
97 | - |
|
98 | - /** |
|
99 | - * Used to get the id of the just inserted element |
|
100 | - * @param string $table the name of the table where we inserted the item |
|
101 | - * @return int the id of the inserted element |
|
102 | - * @since 6.0.0 |
|
103 | - */ |
|
104 | - public function lastInsertId($table = null); |
|
105 | - |
|
106 | - /** |
|
107 | - * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
108 | - * it is needed that there is also a unique constraint on the values. Then this method will |
|
109 | - * catch the exception and return 0. |
|
110 | - * |
|
111 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
112 | - * @param array $input data that should be inserted into the table (column name => value) |
|
113 | - * @param array|null $compare List of values that should be checked for "if not exists" |
|
114 | - * If this is null or an empty array, all keys of $input will be compared |
|
115 | - * Please note: text fields (clob) must not be used in the compare array |
|
116 | - * @return int number of inserted rows |
|
117 | - * @throws \Doctrine\DBAL\DBALException |
|
118 | - * @since 6.0.0 - parameter $compare was added in 8.1.0, return type changed from boolean in 8.1.0 |
|
119 | - * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
120 | - */ |
|
121 | - public function insertIfNotExist($table, $input, array $compare = null); |
|
122 | - |
|
123 | - |
|
124 | - /** |
|
125 | - * |
|
126 | - * Insert a row if the row does not exist. Eventual conflicts during insert will be ignored. |
|
127 | - * |
|
128 | - * Implementation is not fully finished and should not be used! |
|
129 | - * |
|
130 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
131 | - * @param array $values data that should be inserted into the table (column name => value) |
|
132 | - * @return int number of inserted rows |
|
133 | - * @since 16.0.0 |
|
134 | - */ |
|
135 | - public function insertIgnoreConflict(string $table,array $values) : int; |
|
136 | - |
|
137 | - /** |
|
138 | - * Insert or update a row value |
|
139 | - * |
|
140 | - * @param string $table |
|
141 | - * @param array $keys (column name => value) |
|
142 | - * @param array $values (column name => value) |
|
143 | - * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
144 | - * @return int number of new rows |
|
145 | - * @throws \Doctrine\DBAL\DBALException |
|
146 | - * @throws PreconditionNotMetException |
|
147 | - * @since 9.0.0 |
|
148 | - */ |
|
149 | - public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []); |
|
150 | - |
|
151 | - /** |
|
152 | - * Create an exclusive read+write lock on a table |
|
153 | - * |
|
154 | - * Important Note: Due to the nature how locks work on different DBs, it is |
|
155 | - * only possible to lock one table at a time. You should also NOT start a |
|
156 | - * transaction while holding a lock. |
|
157 | - * |
|
158 | - * @param string $tableName |
|
159 | - * @since 9.1.0 |
|
160 | - */ |
|
161 | - public function lockTable($tableName); |
|
162 | - |
|
163 | - /** |
|
164 | - * Release a previous acquired lock again |
|
165 | - * |
|
166 | - * @since 9.1.0 |
|
167 | - */ |
|
168 | - public function unlockTable(); |
|
169 | - |
|
170 | - /** |
|
171 | - * Start a transaction |
|
172 | - * @since 6.0.0 |
|
173 | - */ |
|
174 | - public function beginTransaction(); |
|
175 | - |
|
176 | - /** |
|
177 | - * Check if a transaction is active |
|
178 | - * |
|
179 | - * @return bool |
|
180 | - * @since 8.2.0 |
|
181 | - */ |
|
182 | - public function inTransaction(); |
|
183 | - |
|
184 | - /** |
|
185 | - * Commit the database changes done during a transaction that is in progress |
|
186 | - * @since 6.0.0 |
|
187 | - */ |
|
188 | - public function commit(); |
|
189 | - |
|
190 | - /** |
|
191 | - * Rollback the database changes done during a transaction that is in progress |
|
192 | - * @since 6.0.0 |
|
193 | - */ |
|
194 | - public function rollBack(); |
|
195 | - |
|
196 | - /** |
|
197 | - * Gets the error code and message as a string for logging |
|
198 | - * @return string |
|
199 | - * @since 6.0.0 |
|
200 | - */ |
|
201 | - public function getError(); |
|
202 | - |
|
203 | - /** |
|
204 | - * Fetch the SQLSTATE associated with the last database operation. |
|
205 | - * |
|
206 | - * @return integer The last error code. |
|
207 | - * @since 8.0.0 |
|
208 | - */ |
|
209 | - public function errorCode(); |
|
210 | - |
|
211 | - /** |
|
212 | - * Fetch extended error information associated with the last database operation. |
|
213 | - * |
|
214 | - * @return array The last error information. |
|
215 | - * @since 8.0.0 |
|
216 | - */ |
|
217 | - public function errorInfo(); |
|
218 | - |
|
219 | - /** |
|
220 | - * Establishes the connection with the database. |
|
221 | - * |
|
222 | - * @return bool |
|
223 | - * @since 8.0.0 |
|
224 | - */ |
|
225 | - public function connect(); |
|
226 | - |
|
227 | - /** |
|
228 | - * Close the database connection |
|
229 | - * @since 8.0.0 |
|
230 | - */ |
|
231 | - public function close(); |
|
232 | - |
|
233 | - /** |
|
234 | - * Quotes a given input parameter. |
|
235 | - * |
|
236 | - * @param mixed $input Parameter to be quoted. |
|
237 | - * @param int $type Type of the parameter. |
|
238 | - * @return string The quoted parameter. |
|
239 | - * @since 8.0.0 |
|
240 | - */ |
|
241 | - public function quote($input, $type = IQueryBuilder::PARAM_STR); |
|
242 | - |
|
243 | - /** |
|
244 | - * Gets the DatabasePlatform instance that provides all the metadata about |
|
245 | - * the platform this driver connects to. |
|
246 | - * |
|
247 | - * @return \Doctrine\DBAL\Platforms\AbstractPlatform The database platform. |
|
248 | - * @since 8.0.0 |
|
249 | - */ |
|
250 | - public function getDatabasePlatform(); |
|
251 | - |
|
252 | - /** |
|
253 | - * Drop a table from the database if it exists |
|
254 | - * |
|
255 | - * @param string $table table name without the prefix |
|
256 | - * @since 8.0.0 |
|
257 | - */ |
|
258 | - public function dropTable($table); |
|
259 | - |
|
260 | - /** |
|
261 | - * Check if a table exists |
|
262 | - * |
|
263 | - * @param string $table table name without the prefix |
|
264 | - * @return bool |
|
265 | - * @since 8.0.0 |
|
266 | - */ |
|
267 | - public function tableExists($table); |
|
268 | - |
|
269 | - /** |
|
270 | - * Escape a parameter to be used in a LIKE query |
|
271 | - * |
|
272 | - * @param string $param |
|
273 | - * @return string |
|
274 | - * @since 9.0.0 |
|
275 | - */ |
|
276 | - public function escapeLikeParameter($param); |
|
277 | - |
|
278 | - /** |
|
279 | - * Check whether or not the current database support 4byte wide unicode |
|
280 | - * |
|
281 | - * @return bool |
|
282 | - * @since 11.0.0 |
|
283 | - */ |
|
284 | - public function supports4ByteText(); |
|
285 | - |
|
286 | - /** |
|
287 | - * Create the schema of the connected database |
|
288 | - * |
|
289 | - * @return Schema |
|
290 | - * @since 13.0.0 |
|
291 | - */ |
|
292 | - public function createSchema(); |
|
293 | - |
|
294 | - /** |
|
295 | - * Migrate the database to the given schema |
|
296 | - * |
|
297 | - * @param Schema $toSchema |
|
298 | - * @since 13.0.0 |
|
299 | - */ |
|
300 | - public function migrateToSchema(Schema $toSchema); |
|
49 | + const ADD_MISSING_INDEXES_EVENT = self::class . '::ADD_MISSING_INDEXES'; |
|
50 | + const CHECK_MISSING_INDEXES_EVENT = self::class . '::CHECK_MISSING_INDEXES'; |
|
51 | + |
|
52 | + /** |
|
53 | + * Gets the QueryBuilder for the connection. |
|
54 | + * |
|
55 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
56 | + * @since 8.2.0 |
|
57 | + */ |
|
58 | + public function getQueryBuilder(); |
|
59 | + |
|
60 | + /** |
|
61 | + * Used to abstract the ownCloud database access away |
|
62 | + * @param string $sql the sql query with ? placeholder for params |
|
63 | + * @param int $limit the maximum number of rows |
|
64 | + * @param int $offset from which row we want to start |
|
65 | + * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
66 | + * @since 6.0.0 |
|
67 | + */ |
|
68 | + public function prepare($sql, $limit=null, $offset=null); |
|
69 | + |
|
70 | + /** |
|
71 | + * Executes an, optionally parameterized, SQL query. |
|
72 | + * |
|
73 | + * If the query is parameterized, a prepared statement is used. |
|
74 | + * If an SQLLogger is configured, the execution is logged. |
|
75 | + * |
|
76 | + * @param string $query The SQL query to execute. |
|
77 | + * @param string[] $params The parameters to bind to the query, if any. |
|
78 | + * @param array $types The types the previous parameters are in. |
|
79 | + * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
80 | + * @since 8.0.0 |
|
81 | + */ |
|
82 | + public function executeQuery($query, array $params = array(), $types = array()); |
|
83 | + |
|
84 | + /** |
|
85 | + * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
86 | + * and returns the number of affected rows. |
|
87 | + * |
|
88 | + * This method supports PDO binding types as well as DBAL mapping types. |
|
89 | + * |
|
90 | + * @param string $query The SQL query. |
|
91 | + * @param array $params The query parameters. |
|
92 | + * @param array $types The parameter types. |
|
93 | + * @return integer The number of affected rows. |
|
94 | + * @since 8.0.0 |
|
95 | + */ |
|
96 | + public function executeUpdate($query, array $params = array(), array $types = array()); |
|
97 | + |
|
98 | + /** |
|
99 | + * Used to get the id of the just inserted element |
|
100 | + * @param string $table the name of the table where we inserted the item |
|
101 | + * @return int the id of the inserted element |
|
102 | + * @since 6.0.0 |
|
103 | + */ |
|
104 | + public function lastInsertId($table = null); |
|
105 | + |
|
106 | + /** |
|
107 | + * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
108 | + * it is needed that there is also a unique constraint on the values. Then this method will |
|
109 | + * catch the exception and return 0. |
|
110 | + * |
|
111 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
112 | + * @param array $input data that should be inserted into the table (column name => value) |
|
113 | + * @param array|null $compare List of values that should be checked for "if not exists" |
|
114 | + * If this is null or an empty array, all keys of $input will be compared |
|
115 | + * Please note: text fields (clob) must not be used in the compare array |
|
116 | + * @return int number of inserted rows |
|
117 | + * @throws \Doctrine\DBAL\DBALException |
|
118 | + * @since 6.0.0 - parameter $compare was added in 8.1.0, return type changed from boolean in 8.1.0 |
|
119 | + * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
120 | + */ |
|
121 | + public function insertIfNotExist($table, $input, array $compare = null); |
|
122 | + |
|
123 | + |
|
124 | + /** |
|
125 | + * |
|
126 | + * Insert a row if the row does not exist. Eventual conflicts during insert will be ignored. |
|
127 | + * |
|
128 | + * Implementation is not fully finished and should not be used! |
|
129 | + * |
|
130 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
131 | + * @param array $values data that should be inserted into the table (column name => value) |
|
132 | + * @return int number of inserted rows |
|
133 | + * @since 16.0.0 |
|
134 | + */ |
|
135 | + public function insertIgnoreConflict(string $table,array $values) : int; |
|
136 | + |
|
137 | + /** |
|
138 | + * Insert or update a row value |
|
139 | + * |
|
140 | + * @param string $table |
|
141 | + * @param array $keys (column name => value) |
|
142 | + * @param array $values (column name => value) |
|
143 | + * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
144 | + * @return int number of new rows |
|
145 | + * @throws \Doctrine\DBAL\DBALException |
|
146 | + * @throws PreconditionNotMetException |
|
147 | + * @since 9.0.0 |
|
148 | + */ |
|
149 | + public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []); |
|
150 | + |
|
151 | + /** |
|
152 | + * Create an exclusive read+write lock on a table |
|
153 | + * |
|
154 | + * Important Note: Due to the nature how locks work on different DBs, it is |
|
155 | + * only possible to lock one table at a time. You should also NOT start a |
|
156 | + * transaction while holding a lock. |
|
157 | + * |
|
158 | + * @param string $tableName |
|
159 | + * @since 9.1.0 |
|
160 | + */ |
|
161 | + public function lockTable($tableName); |
|
162 | + |
|
163 | + /** |
|
164 | + * Release a previous acquired lock again |
|
165 | + * |
|
166 | + * @since 9.1.0 |
|
167 | + */ |
|
168 | + public function unlockTable(); |
|
169 | + |
|
170 | + /** |
|
171 | + * Start a transaction |
|
172 | + * @since 6.0.0 |
|
173 | + */ |
|
174 | + public function beginTransaction(); |
|
175 | + |
|
176 | + /** |
|
177 | + * Check if a transaction is active |
|
178 | + * |
|
179 | + * @return bool |
|
180 | + * @since 8.2.0 |
|
181 | + */ |
|
182 | + public function inTransaction(); |
|
183 | + |
|
184 | + /** |
|
185 | + * Commit the database changes done during a transaction that is in progress |
|
186 | + * @since 6.0.0 |
|
187 | + */ |
|
188 | + public function commit(); |
|
189 | + |
|
190 | + /** |
|
191 | + * Rollback the database changes done during a transaction that is in progress |
|
192 | + * @since 6.0.0 |
|
193 | + */ |
|
194 | + public function rollBack(); |
|
195 | + |
|
196 | + /** |
|
197 | + * Gets the error code and message as a string for logging |
|
198 | + * @return string |
|
199 | + * @since 6.0.0 |
|
200 | + */ |
|
201 | + public function getError(); |
|
202 | + |
|
203 | + /** |
|
204 | + * Fetch the SQLSTATE associated with the last database operation. |
|
205 | + * |
|
206 | + * @return integer The last error code. |
|
207 | + * @since 8.0.0 |
|
208 | + */ |
|
209 | + public function errorCode(); |
|
210 | + |
|
211 | + /** |
|
212 | + * Fetch extended error information associated with the last database operation. |
|
213 | + * |
|
214 | + * @return array The last error information. |
|
215 | + * @since 8.0.0 |
|
216 | + */ |
|
217 | + public function errorInfo(); |
|
218 | + |
|
219 | + /** |
|
220 | + * Establishes the connection with the database. |
|
221 | + * |
|
222 | + * @return bool |
|
223 | + * @since 8.0.0 |
|
224 | + */ |
|
225 | + public function connect(); |
|
226 | + |
|
227 | + /** |
|
228 | + * Close the database connection |
|
229 | + * @since 8.0.0 |
|
230 | + */ |
|
231 | + public function close(); |
|
232 | + |
|
233 | + /** |
|
234 | + * Quotes a given input parameter. |
|
235 | + * |
|
236 | + * @param mixed $input Parameter to be quoted. |
|
237 | + * @param int $type Type of the parameter. |
|
238 | + * @return string The quoted parameter. |
|
239 | + * @since 8.0.0 |
|
240 | + */ |
|
241 | + public function quote($input, $type = IQueryBuilder::PARAM_STR); |
|
242 | + |
|
243 | + /** |
|
244 | + * Gets the DatabasePlatform instance that provides all the metadata about |
|
245 | + * the platform this driver connects to. |
|
246 | + * |
|
247 | + * @return \Doctrine\DBAL\Platforms\AbstractPlatform The database platform. |
|
248 | + * @since 8.0.0 |
|
249 | + */ |
|
250 | + public function getDatabasePlatform(); |
|
251 | + |
|
252 | + /** |
|
253 | + * Drop a table from the database if it exists |
|
254 | + * |
|
255 | + * @param string $table table name without the prefix |
|
256 | + * @since 8.0.0 |
|
257 | + */ |
|
258 | + public function dropTable($table); |
|
259 | + |
|
260 | + /** |
|
261 | + * Check if a table exists |
|
262 | + * |
|
263 | + * @param string $table table name without the prefix |
|
264 | + * @return bool |
|
265 | + * @since 8.0.0 |
|
266 | + */ |
|
267 | + public function tableExists($table); |
|
268 | + |
|
269 | + /** |
|
270 | + * Escape a parameter to be used in a LIKE query |
|
271 | + * |
|
272 | + * @param string $param |
|
273 | + * @return string |
|
274 | + * @since 9.0.0 |
|
275 | + */ |
|
276 | + public function escapeLikeParameter($param); |
|
277 | + |
|
278 | + /** |
|
279 | + * Check whether or not the current database support 4byte wide unicode |
|
280 | + * |
|
281 | + * @return bool |
|
282 | + * @since 11.0.0 |
|
283 | + */ |
|
284 | + public function supports4ByteText(); |
|
285 | + |
|
286 | + /** |
|
287 | + * Create the schema of the connected database |
|
288 | + * |
|
289 | + * @return Schema |
|
290 | + * @since 13.0.0 |
|
291 | + */ |
|
292 | + public function createSchema(); |
|
293 | + |
|
294 | + /** |
|
295 | + * Migrate the database to the given schema |
|
296 | + * |
|
297 | + * @param Schema $toSchema |
|
298 | + * @since 13.0.0 |
|
299 | + */ |
|
300 | + public function migrateToSchema(Schema $toSchema); |
|
301 | 301 | } |
@@ -46,8 +46,8 @@ discard block |
||
46 | 46 | */ |
47 | 47 | interface IDBConnection { |
48 | 48 | |
49 | - const ADD_MISSING_INDEXES_EVENT = self::class . '::ADD_MISSING_INDEXES'; |
|
50 | - const CHECK_MISSING_INDEXES_EVENT = self::class . '::CHECK_MISSING_INDEXES'; |
|
49 | + const ADD_MISSING_INDEXES_EVENT = self::class.'::ADD_MISSING_INDEXES'; |
|
50 | + const CHECK_MISSING_INDEXES_EVENT = self::class.'::CHECK_MISSING_INDEXES'; |
|
51 | 51 | |
52 | 52 | /** |
53 | 53 | * Gets the QueryBuilder for the connection. |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
66 | 66 | * @since 6.0.0 |
67 | 67 | */ |
68 | - public function prepare($sql, $limit=null, $offset=null); |
|
68 | + public function prepare($sql, $limit = null, $offset = null); |
|
69 | 69 | |
70 | 70 | /** |
71 | 71 | * Executes an, optionally parameterized, SQL query. |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | * @return int number of inserted rows |
133 | 133 | * @since 16.0.0 |
134 | 134 | */ |
135 | - public function insertIgnoreConflict(string $table,array $values) : int; |
|
135 | + public function insertIgnoreConflict(string $table, array $values) : int; |
|
136 | 136 | |
137 | 137 | /** |
138 | 138 | * Insert or update a row value |
@@ -44,412 +44,412 @@ |
||
44 | 44 | use OCP\PreConditionNotMetException; |
45 | 45 | |
46 | 46 | class Connection extends ReconnectWrapper implements IDBConnection { |
47 | - /** |
|
48 | - * @var string $tablePrefix |
|
49 | - */ |
|
50 | - protected $tablePrefix; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var \OC\DB\Adapter $adapter |
|
54 | - */ |
|
55 | - protected $adapter; |
|
56 | - |
|
57 | - protected $lockedTable = null; |
|
58 | - |
|
59 | - public function connect() { |
|
60 | - try { |
|
61 | - return parent::connect(); |
|
62 | - } catch (DBALException $e) { |
|
63 | - // throw a new exception to prevent leaking info from the stacktrace |
|
64 | - throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
65 | - } |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Returns a QueryBuilder for the connection. |
|
70 | - * |
|
71 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
72 | - */ |
|
73 | - public function getQueryBuilder() { |
|
74 | - return new QueryBuilder( |
|
75 | - $this, |
|
76 | - \OC::$server->getSystemConfig(), |
|
77 | - \OC::$server->getLogger() |
|
78 | - ); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Gets the QueryBuilder for the connection. |
|
83 | - * |
|
84 | - * @return \Doctrine\DBAL\Query\QueryBuilder |
|
85 | - * @deprecated please use $this->getQueryBuilder() instead |
|
86 | - */ |
|
87 | - public function createQueryBuilder() { |
|
88 | - $backtrace = $this->getCallerBacktrace(); |
|
89 | - \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
90 | - return parent::createQueryBuilder(); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Gets the ExpressionBuilder for the connection. |
|
95 | - * |
|
96 | - * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
97 | - * @deprecated please use $this->getQueryBuilder()->expr() instead |
|
98 | - */ |
|
99 | - public function getExpressionBuilder() { |
|
100 | - $backtrace = $this->getCallerBacktrace(); |
|
101 | - \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
102 | - return parent::getExpressionBuilder(); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
107 | - * |
|
108 | - * @return string |
|
109 | - */ |
|
110 | - protected function getCallerBacktrace() { |
|
111 | - $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
112 | - |
|
113 | - // 0 is the method where we use `getCallerBacktrace` |
|
114 | - // 1 is the target method which uses the method we want to log |
|
115 | - if (isset($traces[1])) { |
|
116 | - return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
117 | - } |
|
118 | - |
|
119 | - return ''; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @return string |
|
124 | - */ |
|
125 | - public function getPrefix() { |
|
126 | - return $this->tablePrefix; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Initializes a new instance of the Connection class. |
|
131 | - * |
|
132 | - * @param array $params The connection parameters. |
|
133 | - * @param \Doctrine\DBAL\Driver $driver |
|
134 | - * @param \Doctrine\DBAL\Configuration $config |
|
135 | - * @param \Doctrine\Common\EventManager $eventManager |
|
136 | - * @throws \Exception |
|
137 | - */ |
|
138 | - public function __construct(array $params, Driver $driver, Configuration $config = null, |
|
139 | - EventManager $eventManager = null) |
|
140 | - { |
|
141 | - if (!isset($params['adapter'])) { |
|
142 | - throw new \Exception('adapter not set'); |
|
143 | - } |
|
144 | - if (!isset($params['tablePrefix'])) { |
|
145 | - throw new \Exception('tablePrefix not set'); |
|
146 | - } |
|
147 | - parent::__construct($params, $driver, $config, $eventManager); |
|
148 | - $this->adapter = new $params['adapter']($this); |
|
149 | - $this->tablePrefix = $params['tablePrefix']; |
|
150 | - |
|
151 | - parent::setTransactionIsolation(parent::TRANSACTION_READ_COMMITTED); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Prepares an SQL statement. |
|
156 | - * |
|
157 | - * @param string $statement The SQL statement to prepare. |
|
158 | - * @param int $limit |
|
159 | - * @param int $offset |
|
160 | - * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
161 | - */ |
|
162 | - public function prepare( $statement, $limit=null, $offset=null ) { |
|
163 | - if ($limit === -1) { |
|
164 | - $limit = null; |
|
165 | - } |
|
166 | - if (!is_null($limit)) { |
|
167 | - $platform = $this->getDatabasePlatform(); |
|
168 | - $statement = $platform->modifyLimitQuery($statement, $limit, $offset); |
|
169 | - } |
|
170 | - $statement = $this->replaceTablePrefix($statement); |
|
171 | - $statement = $this->adapter->fixupStatement($statement); |
|
172 | - |
|
173 | - return parent::prepare($statement); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Executes an, optionally parametrized, SQL query. |
|
178 | - * |
|
179 | - * If the query is parametrized, a prepared statement is used. |
|
180 | - * If an SQLLogger is configured, the execution is logged. |
|
181 | - * |
|
182 | - * @param string $query The SQL query to execute. |
|
183 | - * @param array $params The parameters to bind to the query, if any. |
|
184 | - * @param array $types The types the previous parameters are in. |
|
185 | - * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
186 | - * |
|
187 | - * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
188 | - * |
|
189 | - * @throws \Doctrine\DBAL\DBALException |
|
190 | - */ |
|
191 | - public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) |
|
192 | - { |
|
193 | - $query = $this->replaceTablePrefix($query); |
|
194 | - $query = $this->adapter->fixupStatement($query); |
|
195 | - return parent::executeQuery($query, $params, $types, $qcp); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
200 | - * and returns the number of affected rows. |
|
201 | - * |
|
202 | - * This method supports PDO binding types as well as DBAL mapping types. |
|
203 | - * |
|
204 | - * @param string $query The SQL query. |
|
205 | - * @param array $params The query parameters. |
|
206 | - * @param array $types The parameter types. |
|
207 | - * |
|
208 | - * @return integer The number of affected rows. |
|
209 | - * |
|
210 | - * @throws \Doctrine\DBAL\DBALException |
|
211 | - */ |
|
212 | - public function executeUpdate($query, array $params = array(), array $types = array()) |
|
213 | - { |
|
214 | - $query = $this->replaceTablePrefix($query); |
|
215 | - $query = $this->adapter->fixupStatement($query); |
|
216 | - return parent::executeUpdate($query, $params, $types); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
221 | - * depending on the underlying driver. |
|
222 | - * |
|
223 | - * Note: This method may not return a meaningful or consistent result across different drivers, |
|
224 | - * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
225 | - * columns or sequences. |
|
226 | - * |
|
227 | - * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
228 | - * @return string A string representation of the last inserted ID. |
|
229 | - */ |
|
230 | - public function lastInsertId($seqName = null) { |
|
231 | - if ($seqName) { |
|
232 | - $seqName = $this->replaceTablePrefix($seqName); |
|
233 | - } |
|
234 | - return $this->adapter->lastInsertId($seqName); |
|
235 | - } |
|
236 | - |
|
237 | - // internal use |
|
238 | - public function realLastInsertId($seqName = null) { |
|
239 | - return parent::lastInsertId($seqName); |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
244 | - * it is needed that there is also a unique constraint on the values. Then this method will |
|
245 | - * catch the exception and return 0. |
|
246 | - * |
|
247 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
248 | - * @param array $input data that should be inserted into the table (column name => value) |
|
249 | - * @param array|null $compare List of values that should be checked for "if not exists" |
|
250 | - * If this is null or an empty array, all keys of $input will be compared |
|
251 | - * Please note: text fields (clob) must not be used in the compare array |
|
252 | - * @return int number of inserted rows |
|
253 | - * @throws \Doctrine\DBAL\DBALException |
|
254 | - * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
255 | - */ |
|
256 | - public function insertIfNotExist($table, $input, array $compare = null) { |
|
257 | - return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
258 | - } |
|
259 | - |
|
260 | - public function insertIgnoreConflict(string $table, array $values) : int { |
|
261 | - return $this->adapter->insertIgnoreConflict($table, $values); |
|
262 | - } |
|
263 | - |
|
264 | - private function getType($value) { |
|
265 | - if (is_bool($value)) { |
|
266 | - return IQueryBuilder::PARAM_BOOL; |
|
267 | - } else if (is_int($value)) { |
|
268 | - return IQueryBuilder::PARAM_INT; |
|
269 | - } else { |
|
270 | - return IQueryBuilder::PARAM_STR; |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * Insert or update a row value |
|
276 | - * |
|
277 | - * @param string $table |
|
278 | - * @param array $keys (column name => value) |
|
279 | - * @param array $values (column name => value) |
|
280 | - * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
281 | - * @return int number of new rows |
|
282 | - * @throws \Doctrine\DBAL\DBALException |
|
283 | - * @throws PreConditionNotMetException |
|
284 | - * @suppress SqlInjectionChecker |
|
285 | - */ |
|
286 | - public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
|
287 | - try { |
|
288 | - $insertQb = $this->getQueryBuilder(); |
|
289 | - $insertQb->insert($table) |
|
290 | - ->values( |
|
291 | - array_map(function($value) use ($insertQb) { |
|
292 | - return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
293 | - }, array_merge($keys, $values)) |
|
294 | - ); |
|
295 | - return $insertQb->execute(); |
|
296 | - } catch (ConstraintViolationException $e) { |
|
297 | - // value already exists, try update |
|
298 | - $updateQb = $this->getQueryBuilder(); |
|
299 | - $updateQb->update($table); |
|
300 | - foreach ($values as $name => $value) { |
|
301 | - $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
302 | - } |
|
303 | - $where = $updateQb->expr()->andX(); |
|
304 | - $whereValues = array_merge($keys, $updatePreconditionValues); |
|
305 | - foreach ($whereValues as $name => $value) { |
|
306 | - $where->add($updateQb->expr()->eq( |
|
307 | - $name, |
|
308 | - $updateQb->createNamedParameter($value, $this->getType($value)), |
|
309 | - $this->getType($value) |
|
310 | - )); |
|
311 | - } |
|
312 | - $updateQb->where($where); |
|
313 | - $affected = $updateQb->execute(); |
|
314 | - |
|
315 | - if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
316 | - throw new PreConditionNotMetException(); |
|
317 | - } |
|
318 | - |
|
319 | - return 0; |
|
320 | - } |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * Create an exclusive read+write lock on a table |
|
325 | - * |
|
326 | - * @param string $tableName |
|
327 | - * @throws \BadMethodCallException When trying to acquire a second lock |
|
328 | - * @since 9.1.0 |
|
329 | - */ |
|
330 | - public function lockTable($tableName) { |
|
331 | - if ($this->lockedTable !== null) { |
|
332 | - throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
333 | - } |
|
334 | - |
|
335 | - $tableName = $this->tablePrefix . $tableName; |
|
336 | - $this->lockedTable = $tableName; |
|
337 | - $this->adapter->lockTable($tableName); |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Release a previous acquired lock again |
|
342 | - * |
|
343 | - * @since 9.1.0 |
|
344 | - */ |
|
345 | - public function unlockTable() { |
|
346 | - $this->adapter->unlockTable(); |
|
347 | - $this->lockedTable = null; |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * returns the error code and message as a string for logging |
|
352 | - * works with DoctrineException |
|
353 | - * @return string |
|
354 | - */ |
|
355 | - public function getError() { |
|
356 | - $msg = $this->errorCode() . ': '; |
|
357 | - $errorInfo = $this->errorInfo(); |
|
358 | - if (is_array($errorInfo)) { |
|
359 | - $msg .= 'SQLSTATE = '.$errorInfo[0] . ', '; |
|
360 | - $msg .= 'Driver Code = '.$errorInfo[1] . ', '; |
|
361 | - $msg .= 'Driver Message = '.$errorInfo[2]; |
|
362 | - } |
|
363 | - return $msg; |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * Drop a table from the database if it exists |
|
368 | - * |
|
369 | - * @param string $table table name without the prefix |
|
370 | - */ |
|
371 | - public function dropTable($table) { |
|
372 | - $table = $this->tablePrefix . trim($table); |
|
373 | - $schema = $this->getSchemaManager(); |
|
374 | - if($schema->tablesExist(array($table))) { |
|
375 | - $schema->dropTable($table); |
|
376 | - } |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * Check if a table exists |
|
381 | - * |
|
382 | - * @param string $table table name without the prefix |
|
383 | - * @return bool |
|
384 | - */ |
|
385 | - public function tableExists($table){ |
|
386 | - $table = $this->tablePrefix . trim($table); |
|
387 | - $schema = $this->getSchemaManager(); |
|
388 | - return $schema->tablesExist(array($table)); |
|
389 | - } |
|
390 | - |
|
391 | - // internal use |
|
392 | - /** |
|
393 | - * @param string $statement |
|
394 | - * @return string |
|
395 | - */ |
|
396 | - protected function replaceTablePrefix($statement) { |
|
397 | - return str_replace( '*PREFIX*', $this->tablePrefix, $statement ); |
|
398 | - } |
|
399 | - |
|
400 | - /** |
|
401 | - * Check if a transaction is active |
|
402 | - * |
|
403 | - * @return bool |
|
404 | - * @since 8.2.0 |
|
405 | - */ |
|
406 | - public function inTransaction() { |
|
407 | - return $this->getTransactionNestingLevel() > 0; |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * Escape a parameter to be used in a LIKE query |
|
412 | - * |
|
413 | - * @param string $param |
|
414 | - * @return string |
|
415 | - */ |
|
416 | - public function escapeLikeParameter($param) { |
|
417 | - return addcslashes($param, '\\_%'); |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Check whether or not the current database support 4byte wide unicode |
|
422 | - * |
|
423 | - * @return bool |
|
424 | - * @since 11.0.0 |
|
425 | - */ |
|
426 | - public function supports4ByteText() { |
|
427 | - if (!$this->getDatabasePlatform() instanceof MySqlPlatform) { |
|
428 | - return true; |
|
429 | - } |
|
430 | - return $this->getParams()['charset'] === 'utf8mb4'; |
|
431 | - } |
|
432 | - |
|
433 | - |
|
434 | - /** |
|
435 | - * Create the schema of the connected database |
|
436 | - * |
|
437 | - * @return Schema |
|
438 | - */ |
|
439 | - public function createSchema() { |
|
440 | - $schemaManager = new MDB2SchemaManager($this); |
|
441 | - $migrator = $schemaManager->getMigrator(); |
|
442 | - return $migrator->createSchema(); |
|
443 | - } |
|
444 | - |
|
445 | - /** |
|
446 | - * Migrate the database to the given schema |
|
447 | - * |
|
448 | - * @param Schema $toSchema |
|
449 | - */ |
|
450 | - public function migrateToSchema(Schema $toSchema) { |
|
451 | - $schemaManager = new MDB2SchemaManager($this); |
|
452 | - $migrator = $schemaManager->getMigrator(); |
|
453 | - $migrator->migrate($toSchema); |
|
454 | - } |
|
47 | + /** |
|
48 | + * @var string $tablePrefix |
|
49 | + */ |
|
50 | + protected $tablePrefix; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var \OC\DB\Adapter $adapter |
|
54 | + */ |
|
55 | + protected $adapter; |
|
56 | + |
|
57 | + protected $lockedTable = null; |
|
58 | + |
|
59 | + public function connect() { |
|
60 | + try { |
|
61 | + return parent::connect(); |
|
62 | + } catch (DBALException $e) { |
|
63 | + // throw a new exception to prevent leaking info from the stacktrace |
|
64 | + throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
65 | + } |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Returns a QueryBuilder for the connection. |
|
70 | + * |
|
71 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
72 | + */ |
|
73 | + public function getQueryBuilder() { |
|
74 | + return new QueryBuilder( |
|
75 | + $this, |
|
76 | + \OC::$server->getSystemConfig(), |
|
77 | + \OC::$server->getLogger() |
|
78 | + ); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Gets the QueryBuilder for the connection. |
|
83 | + * |
|
84 | + * @return \Doctrine\DBAL\Query\QueryBuilder |
|
85 | + * @deprecated please use $this->getQueryBuilder() instead |
|
86 | + */ |
|
87 | + public function createQueryBuilder() { |
|
88 | + $backtrace = $this->getCallerBacktrace(); |
|
89 | + \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
90 | + return parent::createQueryBuilder(); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Gets the ExpressionBuilder for the connection. |
|
95 | + * |
|
96 | + * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
97 | + * @deprecated please use $this->getQueryBuilder()->expr() instead |
|
98 | + */ |
|
99 | + public function getExpressionBuilder() { |
|
100 | + $backtrace = $this->getCallerBacktrace(); |
|
101 | + \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
102 | + return parent::getExpressionBuilder(); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
107 | + * |
|
108 | + * @return string |
|
109 | + */ |
|
110 | + protected function getCallerBacktrace() { |
|
111 | + $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
112 | + |
|
113 | + // 0 is the method where we use `getCallerBacktrace` |
|
114 | + // 1 is the target method which uses the method we want to log |
|
115 | + if (isset($traces[1])) { |
|
116 | + return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
117 | + } |
|
118 | + |
|
119 | + return ''; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @return string |
|
124 | + */ |
|
125 | + public function getPrefix() { |
|
126 | + return $this->tablePrefix; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Initializes a new instance of the Connection class. |
|
131 | + * |
|
132 | + * @param array $params The connection parameters. |
|
133 | + * @param \Doctrine\DBAL\Driver $driver |
|
134 | + * @param \Doctrine\DBAL\Configuration $config |
|
135 | + * @param \Doctrine\Common\EventManager $eventManager |
|
136 | + * @throws \Exception |
|
137 | + */ |
|
138 | + public function __construct(array $params, Driver $driver, Configuration $config = null, |
|
139 | + EventManager $eventManager = null) |
|
140 | + { |
|
141 | + if (!isset($params['adapter'])) { |
|
142 | + throw new \Exception('adapter not set'); |
|
143 | + } |
|
144 | + if (!isset($params['tablePrefix'])) { |
|
145 | + throw new \Exception('tablePrefix not set'); |
|
146 | + } |
|
147 | + parent::__construct($params, $driver, $config, $eventManager); |
|
148 | + $this->adapter = new $params['adapter']($this); |
|
149 | + $this->tablePrefix = $params['tablePrefix']; |
|
150 | + |
|
151 | + parent::setTransactionIsolation(parent::TRANSACTION_READ_COMMITTED); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Prepares an SQL statement. |
|
156 | + * |
|
157 | + * @param string $statement The SQL statement to prepare. |
|
158 | + * @param int $limit |
|
159 | + * @param int $offset |
|
160 | + * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
161 | + */ |
|
162 | + public function prepare( $statement, $limit=null, $offset=null ) { |
|
163 | + if ($limit === -1) { |
|
164 | + $limit = null; |
|
165 | + } |
|
166 | + if (!is_null($limit)) { |
|
167 | + $platform = $this->getDatabasePlatform(); |
|
168 | + $statement = $platform->modifyLimitQuery($statement, $limit, $offset); |
|
169 | + } |
|
170 | + $statement = $this->replaceTablePrefix($statement); |
|
171 | + $statement = $this->adapter->fixupStatement($statement); |
|
172 | + |
|
173 | + return parent::prepare($statement); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Executes an, optionally parametrized, SQL query. |
|
178 | + * |
|
179 | + * If the query is parametrized, a prepared statement is used. |
|
180 | + * If an SQLLogger is configured, the execution is logged. |
|
181 | + * |
|
182 | + * @param string $query The SQL query to execute. |
|
183 | + * @param array $params The parameters to bind to the query, if any. |
|
184 | + * @param array $types The types the previous parameters are in. |
|
185 | + * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
186 | + * |
|
187 | + * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
188 | + * |
|
189 | + * @throws \Doctrine\DBAL\DBALException |
|
190 | + */ |
|
191 | + public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) |
|
192 | + { |
|
193 | + $query = $this->replaceTablePrefix($query); |
|
194 | + $query = $this->adapter->fixupStatement($query); |
|
195 | + return parent::executeQuery($query, $params, $types, $qcp); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
200 | + * and returns the number of affected rows. |
|
201 | + * |
|
202 | + * This method supports PDO binding types as well as DBAL mapping types. |
|
203 | + * |
|
204 | + * @param string $query The SQL query. |
|
205 | + * @param array $params The query parameters. |
|
206 | + * @param array $types The parameter types. |
|
207 | + * |
|
208 | + * @return integer The number of affected rows. |
|
209 | + * |
|
210 | + * @throws \Doctrine\DBAL\DBALException |
|
211 | + */ |
|
212 | + public function executeUpdate($query, array $params = array(), array $types = array()) |
|
213 | + { |
|
214 | + $query = $this->replaceTablePrefix($query); |
|
215 | + $query = $this->adapter->fixupStatement($query); |
|
216 | + return parent::executeUpdate($query, $params, $types); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
221 | + * depending on the underlying driver. |
|
222 | + * |
|
223 | + * Note: This method may not return a meaningful or consistent result across different drivers, |
|
224 | + * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
225 | + * columns or sequences. |
|
226 | + * |
|
227 | + * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
228 | + * @return string A string representation of the last inserted ID. |
|
229 | + */ |
|
230 | + public function lastInsertId($seqName = null) { |
|
231 | + if ($seqName) { |
|
232 | + $seqName = $this->replaceTablePrefix($seqName); |
|
233 | + } |
|
234 | + return $this->adapter->lastInsertId($seqName); |
|
235 | + } |
|
236 | + |
|
237 | + // internal use |
|
238 | + public function realLastInsertId($seqName = null) { |
|
239 | + return parent::lastInsertId($seqName); |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
244 | + * it is needed that there is also a unique constraint on the values. Then this method will |
|
245 | + * catch the exception and return 0. |
|
246 | + * |
|
247 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
248 | + * @param array $input data that should be inserted into the table (column name => value) |
|
249 | + * @param array|null $compare List of values that should be checked for "if not exists" |
|
250 | + * If this is null or an empty array, all keys of $input will be compared |
|
251 | + * Please note: text fields (clob) must not be used in the compare array |
|
252 | + * @return int number of inserted rows |
|
253 | + * @throws \Doctrine\DBAL\DBALException |
|
254 | + * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
255 | + */ |
|
256 | + public function insertIfNotExist($table, $input, array $compare = null) { |
|
257 | + return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
258 | + } |
|
259 | + |
|
260 | + public function insertIgnoreConflict(string $table, array $values) : int { |
|
261 | + return $this->adapter->insertIgnoreConflict($table, $values); |
|
262 | + } |
|
263 | + |
|
264 | + private function getType($value) { |
|
265 | + if (is_bool($value)) { |
|
266 | + return IQueryBuilder::PARAM_BOOL; |
|
267 | + } else if (is_int($value)) { |
|
268 | + return IQueryBuilder::PARAM_INT; |
|
269 | + } else { |
|
270 | + return IQueryBuilder::PARAM_STR; |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * Insert or update a row value |
|
276 | + * |
|
277 | + * @param string $table |
|
278 | + * @param array $keys (column name => value) |
|
279 | + * @param array $values (column name => value) |
|
280 | + * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
281 | + * @return int number of new rows |
|
282 | + * @throws \Doctrine\DBAL\DBALException |
|
283 | + * @throws PreConditionNotMetException |
|
284 | + * @suppress SqlInjectionChecker |
|
285 | + */ |
|
286 | + public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
|
287 | + try { |
|
288 | + $insertQb = $this->getQueryBuilder(); |
|
289 | + $insertQb->insert($table) |
|
290 | + ->values( |
|
291 | + array_map(function($value) use ($insertQb) { |
|
292 | + return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
293 | + }, array_merge($keys, $values)) |
|
294 | + ); |
|
295 | + return $insertQb->execute(); |
|
296 | + } catch (ConstraintViolationException $e) { |
|
297 | + // value already exists, try update |
|
298 | + $updateQb = $this->getQueryBuilder(); |
|
299 | + $updateQb->update($table); |
|
300 | + foreach ($values as $name => $value) { |
|
301 | + $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
302 | + } |
|
303 | + $where = $updateQb->expr()->andX(); |
|
304 | + $whereValues = array_merge($keys, $updatePreconditionValues); |
|
305 | + foreach ($whereValues as $name => $value) { |
|
306 | + $where->add($updateQb->expr()->eq( |
|
307 | + $name, |
|
308 | + $updateQb->createNamedParameter($value, $this->getType($value)), |
|
309 | + $this->getType($value) |
|
310 | + )); |
|
311 | + } |
|
312 | + $updateQb->where($where); |
|
313 | + $affected = $updateQb->execute(); |
|
314 | + |
|
315 | + if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
316 | + throw new PreConditionNotMetException(); |
|
317 | + } |
|
318 | + |
|
319 | + return 0; |
|
320 | + } |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * Create an exclusive read+write lock on a table |
|
325 | + * |
|
326 | + * @param string $tableName |
|
327 | + * @throws \BadMethodCallException When trying to acquire a second lock |
|
328 | + * @since 9.1.0 |
|
329 | + */ |
|
330 | + public function lockTable($tableName) { |
|
331 | + if ($this->lockedTable !== null) { |
|
332 | + throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
333 | + } |
|
334 | + |
|
335 | + $tableName = $this->tablePrefix . $tableName; |
|
336 | + $this->lockedTable = $tableName; |
|
337 | + $this->adapter->lockTable($tableName); |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Release a previous acquired lock again |
|
342 | + * |
|
343 | + * @since 9.1.0 |
|
344 | + */ |
|
345 | + public function unlockTable() { |
|
346 | + $this->adapter->unlockTable(); |
|
347 | + $this->lockedTable = null; |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * returns the error code and message as a string for logging |
|
352 | + * works with DoctrineException |
|
353 | + * @return string |
|
354 | + */ |
|
355 | + public function getError() { |
|
356 | + $msg = $this->errorCode() . ': '; |
|
357 | + $errorInfo = $this->errorInfo(); |
|
358 | + if (is_array($errorInfo)) { |
|
359 | + $msg .= 'SQLSTATE = '.$errorInfo[0] . ', '; |
|
360 | + $msg .= 'Driver Code = '.$errorInfo[1] . ', '; |
|
361 | + $msg .= 'Driver Message = '.$errorInfo[2]; |
|
362 | + } |
|
363 | + return $msg; |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * Drop a table from the database if it exists |
|
368 | + * |
|
369 | + * @param string $table table name without the prefix |
|
370 | + */ |
|
371 | + public function dropTable($table) { |
|
372 | + $table = $this->tablePrefix . trim($table); |
|
373 | + $schema = $this->getSchemaManager(); |
|
374 | + if($schema->tablesExist(array($table))) { |
|
375 | + $schema->dropTable($table); |
|
376 | + } |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * Check if a table exists |
|
381 | + * |
|
382 | + * @param string $table table name without the prefix |
|
383 | + * @return bool |
|
384 | + */ |
|
385 | + public function tableExists($table){ |
|
386 | + $table = $this->tablePrefix . trim($table); |
|
387 | + $schema = $this->getSchemaManager(); |
|
388 | + return $schema->tablesExist(array($table)); |
|
389 | + } |
|
390 | + |
|
391 | + // internal use |
|
392 | + /** |
|
393 | + * @param string $statement |
|
394 | + * @return string |
|
395 | + */ |
|
396 | + protected function replaceTablePrefix($statement) { |
|
397 | + return str_replace( '*PREFIX*', $this->tablePrefix, $statement ); |
|
398 | + } |
|
399 | + |
|
400 | + /** |
|
401 | + * Check if a transaction is active |
|
402 | + * |
|
403 | + * @return bool |
|
404 | + * @since 8.2.0 |
|
405 | + */ |
|
406 | + public function inTransaction() { |
|
407 | + return $this->getTransactionNestingLevel() > 0; |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * Escape a parameter to be used in a LIKE query |
|
412 | + * |
|
413 | + * @param string $param |
|
414 | + * @return string |
|
415 | + */ |
|
416 | + public function escapeLikeParameter($param) { |
|
417 | + return addcslashes($param, '\\_%'); |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Check whether or not the current database support 4byte wide unicode |
|
422 | + * |
|
423 | + * @return bool |
|
424 | + * @since 11.0.0 |
|
425 | + */ |
|
426 | + public function supports4ByteText() { |
|
427 | + if (!$this->getDatabasePlatform() instanceof MySqlPlatform) { |
|
428 | + return true; |
|
429 | + } |
|
430 | + return $this->getParams()['charset'] === 'utf8mb4'; |
|
431 | + } |
|
432 | + |
|
433 | + |
|
434 | + /** |
|
435 | + * Create the schema of the connected database |
|
436 | + * |
|
437 | + * @return Schema |
|
438 | + */ |
|
439 | + public function createSchema() { |
|
440 | + $schemaManager = new MDB2SchemaManager($this); |
|
441 | + $migrator = $schemaManager->getMigrator(); |
|
442 | + return $migrator->createSchema(); |
|
443 | + } |
|
444 | + |
|
445 | + /** |
|
446 | + * Migrate the database to the given schema |
|
447 | + * |
|
448 | + * @param Schema $toSchema |
|
449 | + */ |
|
450 | + public function migrateToSchema(Schema $toSchema) { |
|
451 | + $schemaManager = new MDB2SchemaManager($this); |
|
452 | + $migrator = $schemaManager->getMigrator(); |
|
453 | + $migrator->migrate($toSchema); |
|
454 | + } |
|
455 | 455 | } |
@@ -25,27 +25,27 @@ |
||
25 | 25 | namespace OC\DB; |
26 | 26 | |
27 | 27 | class AdapterPgSql extends Adapter { |
28 | - public function lastInsertId($table) { |
|
29 | - return $this->conn->fetchColumn('SELECT lastval()'); |
|
30 | - } |
|
28 | + public function lastInsertId($table) { |
|
29 | + return $this->conn->fetchColumn('SELECT lastval()'); |
|
30 | + } |
|
31 | 31 | |
32 | - const UNIX_TIMESTAMP_REPLACEMENT = 'cast(extract(epoch from current_timestamp) as integer)'; |
|
33 | - public function fixupStatement($statement) { |
|
34 | - $statement = str_replace( '`', '"', $statement ); |
|
35 | - $statement = str_ireplace( 'UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement ); |
|
36 | - return $statement; |
|
37 | - } |
|
32 | + const UNIX_TIMESTAMP_REPLACEMENT = 'cast(extract(epoch from current_timestamp) as integer)'; |
|
33 | + public function fixupStatement($statement) { |
|
34 | + $statement = str_replace( '`', '"', $statement ); |
|
35 | + $statement = str_ireplace( 'UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement ); |
|
36 | + return $statement; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * @suppress SqlInjectionChecker |
|
41 | - */ |
|
42 | - public function insertIgnoreConflict(string $table,array $values) : int { |
|
43 | - $builder = $this->conn->getQueryBuilder(); |
|
44 | - $builder->insert($table); |
|
45 | - foreach($values as $key => $value) { |
|
46 | - $builder->setValue($key, $builder->createNamedParameter($value)); |
|
47 | - } |
|
48 | - $queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING'; |
|
49 | - return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes()); |
|
50 | - } |
|
39 | + /** |
|
40 | + * @suppress SqlInjectionChecker |
|
41 | + */ |
|
42 | + public function insertIgnoreConflict(string $table,array $values) : int { |
|
43 | + $builder = $this->conn->getQueryBuilder(); |
|
44 | + $builder->insert($table); |
|
45 | + foreach($values as $key => $value) { |
|
46 | + $builder->setValue($key, $builder->createNamedParameter($value)); |
|
47 | + } |
|
48 | + $queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING'; |
|
49 | + return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes()); |
|
50 | + } |
|
51 | 51 | } |
@@ -31,21 +31,21 @@ |
||
31 | 31 | |
32 | 32 | const UNIX_TIMESTAMP_REPLACEMENT = 'cast(extract(epoch from current_timestamp) as integer)'; |
33 | 33 | public function fixupStatement($statement) { |
34 | - $statement = str_replace( '`', '"', $statement ); |
|
35 | - $statement = str_ireplace( 'UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement ); |
|
34 | + $statement = str_replace('`', '"', $statement); |
|
35 | + $statement = str_ireplace('UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement); |
|
36 | 36 | return $statement; |
37 | 37 | } |
38 | 38 | |
39 | 39 | /** |
40 | 40 | * @suppress SqlInjectionChecker |
41 | 41 | */ |
42 | - public function insertIgnoreConflict(string $table,array $values) : int { |
|
42 | + public function insertIgnoreConflict(string $table, array $values) : int { |
|
43 | 43 | $builder = $this->conn->getQueryBuilder(); |
44 | 44 | $builder->insert($table); |
45 | - foreach($values as $key => $value) { |
|
45 | + foreach ($values as $key => $value) { |
|
46 | 46 | $builder->setValue($key, $builder->createNamedParameter($value)); |
47 | 47 | } |
48 | - $queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING'; |
|
48 | + $queryString = $builder->getSQL().' ON CONFLICT DO NOTHING'; |
|
49 | 49 | return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes()); |
50 | 50 | } |
51 | 51 | } |
@@ -35,111 +35,111 @@ |
||
35 | 35 | */ |
36 | 36 | class Adapter { |
37 | 37 | |
38 | - /** |
|
39 | - * @var \OC\DB\Connection $conn |
|
40 | - */ |
|
41 | - protected $conn; |
|
38 | + /** |
|
39 | + * @var \OC\DB\Connection $conn |
|
40 | + */ |
|
41 | + protected $conn; |
|
42 | 42 | |
43 | - public function __construct($conn) { |
|
44 | - $this->conn = $conn; |
|
45 | - } |
|
43 | + public function __construct($conn) { |
|
44 | + $this->conn = $conn; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @param string $table name |
|
49 | - * @return int id of last insert statement |
|
50 | - */ |
|
51 | - public function lastInsertId($table) { |
|
52 | - return $this->conn->realLastInsertId($table); |
|
53 | - } |
|
47 | + /** |
|
48 | + * @param string $table name |
|
49 | + * @return int id of last insert statement |
|
50 | + */ |
|
51 | + public function lastInsertId($table) { |
|
52 | + return $this->conn->realLastInsertId($table); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @param string $statement that needs to be changed so the db can handle it |
|
57 | - * @return string changed statement |
|
58 | - */ |
|
59 | - public function fixupStatement($statement) { |
|
60 | - return $statement; |
|
61 | - } |
|
55 | + /** |
|
56 | + * @param string $statement that needs to be changed so the db can handle it |
|
57 | + * @return string changed statement |
|
58 | + */ |
|
59 | + public function fixupStatement($statement) { |
|
60 | + return $statement; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Create an exclusive read+write lock on a table |
|
65 | - * |
|
66 | - * @param string $tableName |
|
67 | - * @since 9.1.0 |
|
68 | - */ |
|
69 | - public function lockTable($tableName) { |
|
70 | - $this->conn->beginTransaction(); |
|
71 | - $this->conn->executeUpdate('LOCK TABLE `' .$tableName . '` IN EXCLUSIVE MODE'); |
|
72 | - } |
|
63 | + /** |
|
64 | + * Create an exclusive read+write lock on a table |
|
65 | + * |
|
66 | + * @param string $tableName |
|
67 | + * @since 9.1.0 |
|
68 | + */ |
|
69 | + public function lockTable($tableName) { |
|
70 | + $this->conn->beginTransaction(); |
|
71 | + $this->conn->executeUpdate('LOCK TABLE `' .$tableName . '` IN EXCLUSIVE MODE'); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Release a previous acquired lock again |
|
76 | - * |
|
77 | - * @since 9.1.0 |
|
78 | - */ |
|
79 | - public function unlockTable() { |
|
80 | - $this->conn->commit(); |
|
81 | - } |
|
74 | + /** |
|
75 | + * Release a previous acquired lock again |
|
76 | + * |
|
77 | + * @since 9.1.0 |
|
78 | + */ |
|
79 | + public function unlockTable() { |
|
80 | + $this->conn->commit(); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
85 | - * it is needed that there is also a unique constraint on the values. Then this method will |
|
86 | - * catch the exception and return 0. |
|
87 | - * |
|
88 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
89 | - * @param array $input data that should be inserted into the table (column name => value) |
|
90 | - * @param array|null $compare List of values that should be checked for "if not exists" |
|
91 | - * If this is null or an empty array, all keys of $input will be compared |
|
92 | - * Please note: text fields (clob) must not be used in the compare array |
|
93 | - * @return int number of inserted rows |
|
94 | - * @throws \Doctrine\DBAL\DBALException |
|
95 | - * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
96 | - */ |
|
97 | - public function insertIfNotExist($table, $input, array $compare = null) { |
|
98 | - if (empty($compare)) { |
|
99 | - $compare = array_keys($input); |
|
100 | - } |
|
101 | - $query = 'INSERT INTO `' .$table . '` (`' |
|
102 | - . implode('`,`', array_keys($input)) . '`) SELECT ' |
|
103 | - . str_repeat('?,', count($input)-1).'? ' // Is there a prettier alternative? |
|
104 | - . 'FROM `' . $table . '` WHERE '; |
|
83 | + /** |
|
84 | + * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
85 | + * it is needed that there is also a unique constraint on the values. Then this method will |
|
86 | + * catch the exception and return 0. |
|
87 | + * |
|
88 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
89 | + * @param array $input data that should be inserted into the table (column name => value) |
|
90 | + * @param array|null $compare List of values that should be checked for "if not exists" |
|
91 | + * If this is null or an empty array, all keys of $input will be compared |
|
92 | + * Please note: text fields (clob) must not be used in the compare array |
|
93 | + * @return int number of inserted rows |
|
94 | + * @throws \Doctrine\DBAL\DBALException |
|
95 | + * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
96 | + */ |
|
97 | + public function insertIfNotExist($table, $input, array $compare = null) { |
|
98 | + if (empty($compare)) { |
|
99 | + $compare = array_keys($input); |
|
100 | + } |
|
101 | + $query = 'INSERT INTO `' .$table . '` (`' |
|
102 | + . implode('`,`', array_keys($input)) . '`) SELECT ' |
|
103 | + . str_repeat('?,', count($input)-1).'? ' // Is there a prettier alternative? |
|
104 | + . 'FROM `' . $table . '` WHERE '; |
|
105 | 105 | |
106 | - $inserts = array_values($input); |
|
107 | - foreach($compare as $key) { |
|
108 | - $query .= '`' . $key . '`'; |
|
109 | - if (is_null($input[$key])) { |
|
110 | - $query .= ' IS NULL AND '; |
|
111 | - } else { |
|
112 | - $inserts[] = $input[$key]; |
|
113 | - $query .= ' = ? AND '; |
|
114 | - } |
|
115 | - } |
|
116 | - $query = substr($query, 0, -5); |
|
117 | - $query .= ' HAVING COUNT(*) = 0'; |
|
106 | + $inserts = array_values($input); |
|
107 | + foreach($compare as $key) { |
|
108 | + $query .= '`' . $key . '`'; |
|
109 | + if (is_null($input[$key])) { |
|
110 | + $query .= ' IS NULL AND '; |
|
111 | + } else { |
|
112 | + $inserts[] = $input[$key]; |
|
113 | + $query .= ' = ? AND '; |
|
114 | + } |
|
115 | + } |
|
116 | + $query = substr($query, 0, -5); |
|
117 | + $query .= ' HAVING COUNT(*) = 0'; |
|
118 | 118 | |
119 | - try { |
|
120 | - return $this->conn->executeUpdate($query, $inserts); |
|
121 | - } catch (UniqueConstraintViolationException $e) { |
|
122 | - // if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it |
|
123 | - // it's fine to ignore this then |
|
124 | - // |
|
125 | - // more discussions about this can be found at https://github.com/nextcloud/server/pull/12315 |
|
126 | - return 0; |
|
127 | - } |
|
128 | - } |
|
119 | + try { |
|
120 | + return $this->conn->executeUpdate($query, $inserts); |
|
121 | + } catch (UniqueConstraintViolationException $e) { |
|
122 | + // if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it |
|
123 | + // it's fine to ignore this then |
|
124 | + // |
|
125 | + // more discussions about this can be found at https://github.com/nextcloud/server/pull/12315 |
|
126 | + return 0; |
|
127 | + } |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * @suppress SqlInjectionChecker |
|
132 | - */ |
|
133 | - public function insertIgnoreConflict(string $table,array $values) : int { |
|
134 | - try { |
|
135 | - $builder = $this->conn->getQueryBuilder(); |
|
136 | - $builder->insert($table); |
|
137 | - foreach($values as $key => $value) { |
|
138 | - $builder->setValue($key, $builder->createNamedParameter($value)); |
|
139 | - } |
|
140 | - return $builder->execute(); |
|
141 | - } catch(UniqueConstraintViolationException $e) { |
|
142 | - return 0; |
|
143 | - } |
|
144 | - } |
|
130 | + /** |
|
131 | + * @suppress SqlInjectionChecker |
|
132 | + */ |
|
133 | + public function insertIgnoreConflict(string $table,array $values) : int { |
|
134 | + try { |
|
135 | + $builder = $this->conn->getQueryBuilder(); |
|
136 | + $builder->insert($table); |
|
137 | + foreach($values as $key => $value) { |
|
138 | + $builder->setValue($key, $builder->createNamedParameter($value)); |
|
139 | + } |
|
140 | + return $builder->execute(); |
|
141 | + } catch(UniqueConstraintViolationException $e) { |
|
142 | + return 0; |
|
143 | + } |
|
144 | + } |
|
145 | 145 | } |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | */ |
69 | 69 | public function lockTable($tableName) { |
70 | 70 | $this->conn->beginTransaction(); |
71 | - $this->conn->executeUpdate('LOCK TABLE `' .$tableName . '` IN EXCLUSIVE MODE'); |
|
71 | + $this->conn->executeUpdate('LOCK TABLE `'.$tableName.'` IN EXCLUSIVE MODE'); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -98,14 +98,14 @@ discard block |
||
98 | 98 | if (empty($compare)) { |
99 | 99 | $compare = array_keys($input); |
100 | 100 | } |
101 | - $query = 'INSERT INTO `' .$table . '` (`' |
|
102 | - . implode('`,`', array_keys($input)) . '`) SELECT ' |
|
103 | - . str_repeat('?,', count($input)-1).'? ' // Is there a prettier alternative? |
|
104 | - . 'FROM `' . $table . '` WHERE '; |
|
101 | + $query = 'INSERT INTO `'.$table.'` (`' |
|
102 | + . implode('`,`', array_keys($input)).'`) SELECT ' |
|
103 | + . str_repeat('?,', count($input) - 1).'? ' // Is there a prettier alternative? |
|
104 | + . 'FROM `'.$table.'` WHERE '; |
|
105 | 105 | |
106 | 106 | $inserts = array_values($input); |
107 | - foreach($compare as $key) { |
|
108 | - $query .= '`' . $key . '`'; |
|
107 | + foreach ($compare as $key) { |
|
108 | + $query .= '`'.$key.'`'; |
|
109 | 109 | if (is_null($input[$key])) { |
110 | 110 | $query .= ' IS NULL AND '; |
111 | 111 | } else { |
@@ -130,15 +130,15 @@ discard block |
||
130 | 130 | /** |
131 | 131 | * @suppress SqlInjectionChecker |
132 | 132 | */ |
133 | - public function insertIgnoreConflict(string $table,array $values) : int { |
|
133 | + public function insertIgnoreConflict(string $table, array $values) : int { |
|
134 | 134 | try { |
135 | 135 | $builder = $this->conn->getQueryBuilder(); |
136 | 136 | $builder->insert($table); |
137 | - foreach($values as $key => $value) { |
|
137 | + foreach ($values as $key => $value) { |
|
138 | 138 | $builder->setValue($key, $builder->createNamedParameter($value)); |
139 | 139 | } |
140 | 140 | return $builder->execute(); |
141 | - } catch(UniqueConstraintViolationException $e) { |
|
141 | + } catch (UniqueConstraintViolationException $e) { |
|
142 | 142 | return 0; |
143 | 143 | } |
144 | 144 | } |