@@ -24,14 +24,14 @@ |
||
24 | 24 | namespace OC\Hooks; |
25 | 25 | |
26 | 26 | class PublicEmitter extends BasicEmitter { |
27 | - /** |
|
28 | - * @param string $scope |
|
29 | - * @param string $method |
|
30 | - * @param array $arguments optional |
|
31 | - * |
|
32 | - * @suppress PhanAccessMethodProtected |
|
33 | - */ |
|
34 | - public function emit($scope, $method, array $arguments = array()) { |
|
35 | - parent::emit($scope, $method, $arguments); |
|
36 | - } |
|
27 | + /** |
|
28 | + * @param string $scope |
|
29 | + * @param string $method |
|
30 | + * @param array $arguments optional |
|
31 | + * |
|
32 | + * @suppress PhanAccessMethodProtected |
|
33 | + */ |
|
34 | + public function emit($scope, $method, array $arguments = array()) { |
|
35 | + parent::emit($scope, $method, $arguments); |
|
36 | + } |
|
37 | 37 | } |
@@ -271,7 +271,7 @@ |
||
271 | 271 | * Return the explicit version for the aliases; current, next, prev, latest |
272 | 272 | * |
273 | 273 | * @param string $alias |
274 | - * @return mixed|null|string |
|
274 | + * @return string |
|
275 | 275 | */ |
276 | 276 | public function getMigration($alias) { |
277 | 277 | switch($alias) { |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | } |
71 | 71 | |
72 | 72 | if ($appName === 'core') { |
73 | - $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations'; |
|
73 | + $this->migrationsPath = \OC::$SERVERROOT.'/core/Migrations'; |
|
74 | 74 | $this->migrationsNamespace = 'OC\\Core\\Migrations'; |
75 | 75 | } else { |
76 | 76 | if (null === $appLocator) { |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | $appPath = $appLocator->getAppPath($appName); |
80 | 80 | $namespace = App::buildAppNamespace($appName); |
81 | 81 | $this->migrationsPath = "$appPath/lib/Migration"; |
82 | - $this->migrationsNamespace = $namespace . '\\Migration'; |
|
82 | + $this->migrationsNamespace = $namespace.'\\Migration'; |
|
83 | 83 | } |
84 | 84 | } |
85 | 85 | |
@@ -201,7 +201,7 @@ discard block |
||
201 | 201 | \RegexIterator::GET_MATCH); |
202 | 202 | |
203 | 203 | $files = array_keys(iterator_to_array($iterator)); |
204 | - uasort($files, function ($a, $b) { |
|
204 | + uasort($files, function($a, $b) { |
|
205 | 205 | preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); |
206 | 206 | preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); |
207 | 207 | if (!empty($matchA) && !empty($matchB)) { |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | * @return string |
280 | 280 | */ |
281 | 281 | public function getMigrationsTableName() { |
282 | - return $this->connection->getPrefix() . 'migrations'; |
|
282 | + return $this->connection->getPrefix().'migrations'; |
|
283 | 283 | } |
284 | 284 | |
285 | 285 | /** |
@@ -307,7 +307,7 @@ discard block |
||
307 | 307 | * @return mixed|null|string |
308 | 308 | */ |
309 | 309 | public function getMigration($alias) { |
310 | - switch($alias) { |
|
310 | + switch ($alias) { |
|
311 | 311 | case 'current': |
312 | 312 | return $this->getCurrentVersion(); |
313 | 313 | case 'next': |
@@ -476,24 +476,24 @@ discard block |
||
476 | 476 | |
477 | 477 | foreach ($schema->getTables() as $table) { |
478 | 478 | if (\strlen($table->getName()) - $prefixLength > 27) { |
479 | - throw new \InvalidArgumentException('Table name "' . $table->getName() . '" is too long.'); |
|
479 | + throw new \InvalidArgumentException('Table name "'.$table->getName().'" is too long.'); |
|
480 | 480 | } |
481 | 481 | |
482 | 482 | foreach ($table->getColumns() as $thing) { |
483 | 483 | if (\strlen($thing->getName()) - $prefixLength > 27) { |
484 | - throw new \InvalidArgumentException('Column name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
484 | + throw new \InvalidArgumentException('Column name "'.$table->getName().'"."'.$thing->getName().'" is too long.'); |
|
485 | 485 | } |
486 | 486 | } |
487 | 487 | |
488 | 488 | foreach ($table->getIndexes() as $thing) { |
489 | 489 | if (\strlen($thing->getName()) - $prefixLength > 27) { |
490 | - throw new \InvalidArgumentException('Index name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
490 | + throw new \InvalidArgumentException('Index name "'.$table->getName().'"."'.$thing->getName().'" is too long.'); |
|
491 | 491 | } |
492 | 492 | } |
493 | 493 | |
494 | 494 | foreach ($table->getForeignKeys() as $thing) { |
495 | 495 | if (\strlen($thing->getName()) - $prefixLength > 27) { |
496 | - throw new \InvalidArgumentException('Foreign key name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
496 | + throw new \InvalidArgumentException('Foreign key name "'.$table->getName().'"."'.$thing->getName().'" is too long.'); |
|
497 | 497 | } |
498 | 498 | } |
499 | 499 | |
@@ -503,32 +503,32 @@ discard block |
||
503 | 503 | $isUsingDefaultName = $indexName === 'primary'; |
504 | 504 | |
505 | 505 | if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
506 | - $defaultName = $table->getName() . '_pkey'; |
|
506 | + $defaultName = $table->getName().'_pkey'; |
|
507 | 507 | $isUsingDefaultName = strtolower($defaultName) === $indexName; |
508 | 508 | |
509 | 509 | if ($isUsingDefaultName) { |
510 | - $sequenceName = $table->getName() . '_' . implode('_', $primaryKey->getColumns()) . '_seq'; |
|
510 | + $sequenceName = $table->getName().'_'.implode('_', $primaryKey->getColumns()).'_seq'; |
|
511 | 511 | $sequences = array_filter($sequences, function(Sequence $sequence) use ($sequenceName) { |
512 | 512 | return $sequence->getName() !== $sequenceName; |
513 | 513 | }); |
514 | 514 | } |
515 | 515 | } else if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) { |
516 | - $defaultName = $table->getName() . '_seq'; |
|
516 | + $defaultName = $table->getName().'_seq'; |
|
517 | 517 | $isUsingDefaultName = strtolower($defaultName) === $indexName; |
518 | 518 | } |
519 | 519 | |
520 | 520 | if (!$isUsingDefaultName && \strlen($indexName) - $prefixLength > 27) { |
521 | - throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); |
|
521 | + throw new \InvalidArgumentException('Primary index name on "'.$table->getName().'" is too long.'); |
|
522 | 522 | } |
523 | 523 | if ($isUsingDefaultName && \strlen($table->getName()) - $prefixLength > 23) { |
524 | - throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); |
|
524 | + throw new \InvalidArgumentException('Primary index name on "'.$table->getName().'" is too long.'); |
|
525 | 525 | } |
526 | 526 | } |
527 | 527 | } |
528 | 528 | |
529 | 529 | foreach ($sequences as $sequence) { |
530 | 530 | if (\strlen($sequence->getName()) - $prefixLength > 27) { |
531 | - throw new \InvalidArgumentException('Sequence name "' . $sequence->getName() . '" is too long.'); |
|
531 | + throw new \InvalidArgumentException('Sequence name "'.$sequence->getName().'" is too long.'); |
|
532 | 532 | } |
533 | 533 | } |
534 | 534 | } |
@@ -41,502 +41,502 @@ |
||
41 | 41 | |
42 | 42 | class MigrationService { |
43 | 43 | |
44 | - /** @var boolean */ |
|
45 | - private $migrationTableCreated; |
|
46 | - /** @var array */ |
|
47 | - private $migrations; |
|
48 | - /** @var IOutput */ |
|
49 | - private $output; |
|
50 | - /** @var Connection */ |
|
51 | - private $connection; |
|
52 | - /** @var string */ |
|
53 | - private $appName; |
|
54 | - |
|
55 | - /** |
|
56 | - * MigrationService constructor. |
|
57 | - * |
|
58 | - * @param $appName |
|
59 | - * @param IDBConnection $connection |
|
60 | - * @param AppLocator $appLocator |
|
61 | - * @param IOutput|null $output |
|
62 | - * @throws \Exception |
|
63 | - */ |
|
64 | - public function __construct($appName, IDBConnection $connection, IOutput $output = null, AppLocator $appLocator = null) { |
|
65 | - $this->appName = $appName; |
|
66 | - $this->connection = $connection; |
|
67 | - $this->output = $output; |
|
68 | - if (null === $this->output) { |
|
69 | - $this->output = new SimpleOutput(\OC::$server->getLogger(), $appName); |
|
70 | - } |
|
71 | - |
|
72 | - if ($appName === 'core') { |
|
73 | - $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations'; |
|
74 | - $this->migrationsNamespace = 'OC\\Core\\Migrations'; |
|
75 | - } else { |
|
76 | - if (null === $appLocator) { |
|
77 | - $appLocator = new AppLocator(); |
|
78 | - } |
|
79 | - $appPath = $appLocator->getAppPath($appName); |
|
80 | - $namespace = App::buildAppNamespace($appName); |
|
81 | - $this->migrationsPath = "$appPath/lib/Migration"; |
|
82 | - $this->migrationsNamespace = $namespace . '\\Migration'; |
|
83 | - } |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Returns the name of the app for which this migration is executed |
|
88 | - * |
|
89 | - * @return string |
|
90 | - */ |
|
91 | - public function getApp() { |
|
92 | - return $this->appName; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @return bool |
|
97 | - * @codeCoverageIgnore - this will implicitly tested on installation |
|
98 | - */ |
|
99 | - private function createMigrationTable() { |
|
100 | - if ($this->migrationTableCreated) { |
|
101 | - return false; |
|
102 | - } |
|
103 | - |
|
104 | - $schema = new SchemaWrapper($this->connection); |
|
105 | - |
|
106 | - /** |
|
107 | - * We drop the table when it has different columns or the definition does not |
|
108 | - * match. E.g. ownCloud uses a length of 177 for app and 14 for version. |
|
109 | - */ |
|
110 | - try { |
|
111 | - $table = $schema->getTable('migrations'); |
|
112 | - $columns = $table->getColumns(); |
|
113 | - |
|
114 | - if (count($columns) === 2) { |
|
115 | - try { |
|
116 | - $column = $table->getColumn('app'); |
|
117 | - $schemaMismatch = $column->getLength() !== 255; |
|
118 | - |
|
119 | - if (!$schemaMismatch) { |
|
120 | - $column = $table->getColumn('version'); |
|
121 | - $schemaMismatch = $column->getLength() !== 255; |
|
122 | - } |
|
123 | - } catch (SchemaException $e) { |
|
124 | - // One of the columns is missing |
|
125 | - $schemaMismatch = true; |
|
126 | - } |
|
127 | - |
|
128 | - if (!$schemaMismatch) { |
|
129 | - // Table exists and schema matches: return back! |
|
130 | - $this->migrationTableCreated = true; |
|
131 | - return false; |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - // Drop the table, when it didn't match our expectations. |
|
136 | - $this->connection->dropTable('migrations'); |
|
137 | - |
|
138 | - // Recreate the schema after the table was dropped. |
|
139 | - $schema = new SchemaWrapper($this->connection); |
|
140 | - |
|
141 | - } catch (SchemaException $e) { |
|
142 | - // Table not found, no need to panic, we will create it. |
|
143 | - } |
|
144 | - |
|
145 | - $table = $schema->createTable('migrations'); |
|
146 | - $table->addColumn('app', Type::STRING, ['length' => 255]); |
|
147 | - $table->addColumn('version', Type::STRING, ['length' => 255]); |
|
148 | - $table->setPrimaryKey(['app', 'version']); |
|
149 | - |
|
150 | - $this->connection->migrateToSchema($schema->getWrappedSchema()); |
|
151 | - |
|
152 | - $this->migrationTableCreated = true; |
|
153 | - |
|
154 | - return true; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Returns all versions which have already been applied |
|
159 | - * |
|
160 | - * @return string[] |
|
161 | - * @codeCoverageIgnore - no need to test this |
|
162 | - */ |
|
163 | - public function getMigratedVersions() { |
|
164 | - $this->createMigrationTable(); |
|
165 | - $qb = $this->connection->getQueryBuilder(); |
|
166 | - |
|
167 | - $qb->select('version') |
|
168 | - ->from('migrations') |
|
169 | - ->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp()))) |
|
170 | - ->orderBy('version'); |
|
171 | - |
|
172 | - $result = $qb->execute(); |
|
173 | - $rows = $result->fetchAll(\PDO::FETCH_COLUMN); |
|
174 | - $result->closeCursor(); |
|
175 | - |
|
176 | - return $rows; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Returns all versions which are available in the migration folder |
|
181 | - * |
|
182 | - * @return array |
|
183 | - */ |
|
184 | - public function getAvailableVersions() { |
|
185 | - $this->ensureMigrationsAreLoaded(); |
|
186 | - return array_map('strval', array_keys($this->migrations)); |
|
187 | - } |
|
188 | - |
|
189 | - protected function findMigrations() { |
|
190 | - $directory = realpath($this->migrationsPath); |
|
191 | - if ($directory === false || !file_exists($directory) || !is_dir($directory)) { |
|
192 | - return []; |
|
193 | - } |
|
194 | - |
|
195 | - $iterator = new \RegexIterator( |
|
196 | - new \RecursiveIteratorIterator( |
|
197 | - new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS), |
|
198 | - \RecursiveIteratorIterator::LEAVES_ONLY |
|
199 | - ), |
|
200 | - '#^.+\\/Version[^\\/]{1,255}\\.php$#i', |
|
201 | - \RegexIterator::GET_MATCH); |
|
202 | - |
|
203 | - $files = array_keys(iterator_to_array($iterator)); |
|
204 | - uasort($files, function ($a, $b) { |
|
205 | - preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); |
|
206 | - preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); |
|
207 | - if (!empty($matchA) && !empty($matchB)) { |
|
208 | - if ($matchA[1] !== $matchB[1]) { |
|
209 | - return ($matchA[1] < $matchB[1]) ? -1 : 1; |
|
210 | - } |
|
211 | - return ($matchA[2] < $matchB[2]) ? -1 : 1; |
|
212 | - } |
|
213 | - return (basename($a) < basename($b)) ? -1 : 1; |
|
214 | - }); |
|
215 | - |
|
216 | - $migrations = []; |
|
217 | - |
|
218 | - foreach ($files as $file) { |
|
219 | - $className = basename($file, '.php'); |
|
220 | - $version = (string) substr($className, 7); |
|
221 | - if ($version === '0') { |
|
222 | - throw new \InvalidArgumentException( |
|
223 | - "Cannot load a migrations with the name '$version' because it is a reserved number" |
|
224 | - ); |
|
225 | - } |
|
226 | - $migrations[$version] = sprintf('%s\\%s', $this->migrationsNamespace, $className); |
|
227 | - } |
|
228 | - |
|
229 | - return $migrations; |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * @param string $to |
|
234 | - * @return string[] |
|
235 | - */ |
|
236 | - private function getMigrationsToExecute($to) { |
|
237 | - $knownMigrations = $this->getMigratedVersions(); |
|
238 | - $availableMigrations = $this->getAvailableVersions(); |
|
239 | - |
|
240 | - $toBeExecuted = []; |
|
241 | - foreach ($availableMigrations as $v) { |
|
242 | - if ($to !== 'latest' && $v > $to) { |
|
243 | - continue; |
|
244 | - } |
|
245 | - if ($this->shallBeExecuted($v, $knownMigrations)) { |
|
246 | - $toBeExecuted[] = $v; |
|
247 | - } |
|
248 | - } |
|
249 | - |
|
250 | - return $toBeExecuted; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * @param string $m |
|
255 | - * @param string[] $knownMigrations |
|
256 | - * @return bool |
|
257 | - */ |
|
258 | - private function shallBeExecuted($m, $knownMigrations) { |
|
259 | - if (in_array($m, $knownMigrations)) { |
|
260 | - return false; |
|
261 | - } |
|
262 | - |
|
263 | - return true; |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * @param string $version |
|
268 | - */ |
|
269 | - private function markAsExecuted($version) { |
|
270 | - $this->connection->insertIfNotExist('*PREFIX*migrations', [ |
|
271 | - 'app' => $this->appName, |
|
272 | - 'version' => $version |
|
273 | - ]); |
|
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * Returns the name of the table which holds the already applied versions |
|
278 | - * |
|
279 | - * @return string |
|
280 | - */ |
|
281 | - public function getMigrationsTableName() { |
|
282 | - return $this->connection->getPrefix() . 'migrations'; |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * Returns the namespace of the version classes |
|
287 | - * |
|
288 | - * @return string |
|
289 | - */ |
|
290 | - public function getMigrationsNamespace() { |
|
291 | - return $this->migrationsNamespace; |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Returns the directory which holds the versions |
|
296 | - * |
|
297 | - * @return string |
|
298 | - */ |
|
299 | - public function getMigrationsDirectory() { |
|
300 | - return $this->migrationsPath; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Return the explicit version for the aliases; current, next, prev, latest |
|
305 | - * |
|
306 | - * @param string $alias |
|
307 | - * @return mixed|null|string |
|
308 | - */ |
|
309 | - public function getMigration($alias) { |
|
310 | - switch($alias) { |
|
311 | - case 'current': |
|
312 | - return $this->getCurrentVersion(); |
|
313 | - case 'next': |
|
314 | - return $this->getRelativeVersion($this->getCurrentVersion(), 1); |
|
315 | - case 'prev': |
|
316 | - return $this->getRelativeVersion($this->getCurrentVersion(), -1); |
|
317 | - case 'latest': |
|
318 | - $this->ensureMigrationsAreLoaded(); |
|
319 | - |
|
320 | - $migrations = $this->getAvailableVersions(); |
|
321 | - return @end($migrations); |
|
322 | - } |
|
323 | - return '0'; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * @param string $version |
|
328 | - * @param int $delta |
|
329 | - * @return null|string |
|
330 | - */ |
|
331 | - private function getRelativeVersion($version, $delta) { |
|
332 | - $this->ensureMigrationsAreLoaded(); |
|
333 | - |
|
334 | - $versions = $this->getAvailableVersions(); |
|
335 | - array_unshift($versions, 0); |
|
336 | - $offset = array_search($version, $versions, true); |
|
337 | - if ($offset === false || !isset($versions[$offset + $delta])) { |
|
338 | - // Unknown version or delta out of bounds. |
|
339 | - return null; |
|
340 | - } |
|
341 | - |
|
342 | - return (string) $versions[$offset + $delta]; |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * @return string |
|
347 | - */ |
|
348 | - private function getCurrentVersion() { |
|
349 | - $m = $this->getMigratedVersions(); |
|
350 | - if (count($m) === 0) { |
|
351 | - return '0'; |
|
352 | - } |
|
353 | - $migrations = array_values($m); |
|
354 | - return @end($migrations); |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * @param string $version |
|
359 | - * @return string |
|
360 | - * @throws \InvalidArgumentException |
|
361 | - */ |
|
362 | - private function getClass($version) { |
|
363 | - $this->ensureMigrationsAreLoaded(); |
|
364 | - |
|
365 | - if (isset($this->migrations[$version])) { |
|
366 | - return $this->migrations[$version]; |
|
367 | - } |
|
368 | - |
|
369 | - throw new \InvalidArgumentException("Version $version is unknown."); |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * Allows to set an IOutput implementation which is used for logging progress and messages |
|
374 | - * |
|
375 | - * @param IOutput $output |
|
376 | - */ |
|
377 | - public function setOutput(IOutput $output) { |
|
378 | - $this->output = $output; |
|
379 | - } |
|
380 | - |
|
381 | - /** |
|
382 | - * Applies all not yet applied versions up to $to |
|
383 | - * |
|
384 | - * @param string $to |
|
385 | - * @param bool $schemaOnly |
|
386 | - * @throws \InvalidArgumentException |
|
387 | - */ |
|
388 | - public function migrate($to = 'latest', $schemaOnly = false) { |
|
389 | - // read known migrations |
|
390 | - $toBeExecuted = $this->getMigrationsToExecute($to); |
|
391 | - foreach ($toBeExecuted as $version) { |
|
392 | - $this->executeStep($version, $schemaOnly); |
|
393 | - } |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Get the human readable descriptions for the migration steps to run |
|
398 | - * |
|
399 | - * @param string $to |
|
400 | - * @return string[] [$name => $description] |
|
401 | - */ |
|
402 | - public function describeMigrationStep($to = 'latest') { |
|
403 | - $toBeExecuted = $this->getMigrationsToExecute($to); |
|
404 | - $description = []; |
|
405 | - foreach ($toBeExecuted as $version) { |
|
406 | - $migration = $this->createInstance($version); |
|
407 | - if ($migration->name()) { |
|
408 | - $description[$migration->name()] = $migration->description(); |
|
409 | - } |
|
410 | - } |
|
411 | - return $description; |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * @param string $version |
|
416 | - * @return IMigrationStep |
|
417 | - * @throws \InvalidArgumentException |
|
418 | - */ |
|
419 | - protected function createInstance($version) { |
|
420 | - $class = $this->getClass($version); |
|
421 | - try { |
|
422 | - $s = \OC::$server->query($class); |
|
423 | - |
|
424 | - if (!$s instanceof IMigrationStep) { |
|
425 | - throw new \InvalidArgumentException('Not a valid migration'); |
|
426 | - } |
|
427 | - } catch (QueryException $e) { |
|
428 | - if (class_exists($class)) { |
|
429 | - $s = new $class(); |
|
430 | - } else { |
|
431 | - throw new \InvalidArgumentException("Migration step '$class' is unknown"); |
|
432 | - } |
|
433 | - } |
|
434 | - |
|
435 | - return $s; |
|
436 | - } |
|
437 | - |
|
438 | - /** |
|
439 | - * Executes one explicit version |
|
440 | - * |
|
441 | - * @param string $version |
|
442 | - * @param bool $schemaOnly |
|
443 | - * @throws \InvalidArgumentException |
|
444 | - */ |
|
445 | - public function executeStep($version, $schemaOnly = false) { |
|
446 | - $instance = $this->createInstance($version); |
|
447 | - |
|
448 | - if (!$schemaOnly) { |
|
449 | - $instance->preSchemaChange($this->output, function() { |
|
450 | - return new SchemaWrapper($this->connection); |
|
451 | - }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
452 | - } |
|
453 | - |
|
454 | - $toSchema = $instance->changeSchema($this->output, function() { |
|
455 | - return new SchemaWrapper($this->connection); |
|
456 | - }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
457 | - |
|
458 | - if ($toSchema instanceof SchemaWrapper) { |
|
459 | - $targetSchema = $toSchema->getWrappedSchema(); |
|
460 | - // TODO re-enable once stable14 is branched of: https://github.com/nextcloud/server/issues/10518 |
|
461 | - // $this->ensureOracleIdentifierLengthLimit($targetSchema, strlen($this->connection->getPrefix())); |
|
462 | - $this->connection->migrateToSchema($targetSchema); |
|
463 | - $toSchema->performDropTableCalls(); |
|
464 | - } |
|
465 | - |
|
466 | - if (!$schemaOnly) { |
|
467 | - $instance->postSchemaChange($this->output, function() { |
|
468 | - return new SchemaWrapper($this->connection); |
|
469 | - }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
470 | - } |
|
471 | - |
|
472 | - $this->markAsExecuted($version); |
|
473 | - } |
|
474 | - |
|
475 | - public function ensureOracleIdentifierLengthLimit(Schema $schema, int $prefixLength) { |
|
476 | - $sequences = $schema->getSequences(); |
|
477 | - |
|
478 | - foreach ($schema->getTables() as $table) { |
|
479 | - if (\strlen($table->getName()) - $prefixLength > 27) { |
|
480 | - throw new \InvalidArgumentException('Table name "' . $table->getName() . '" is too long.'); |
|
481 | - } |
|
482 | - |
|
483 | - foreach ($table->getColumns() as $thing) { |
|
484 | - if (\strlen($thing->getName()) - $prefixLength > 27) { |
|
485 | - throw new \InvalidArgumentException('Column name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
486 | - } |
|
487 | - } |
|
488 | - |
|
489 | - foreach ($table->getIndexes() as $thing) { |
|
490 | - if (\strlen($thing->getName()) - $prefixLength > 27) { |
|
491 | - throw new \InvalidArgumentException('Index name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
492 | - } |
|
493 | - } |
|
494 | - |
|
495 | - foreach ($table->getForeignKeys() as $thing) { |
|
496 | - if (\strlen($thing->getName()) - $prefixLength > 27) { |
|
497 | - throw new \InvalidArgumentException('Foreign key name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
498 | - } |
|
499 | - } |
|
500 | - |
|
501 | - $primaryKey = $table->getPrimaryKey(); |
|
502 | - if ($primaryKey instanceof Index) { |
|
503 | - $indexName = strtolower($primaryKey->getName()); |
|
504 | - $isUsingDefaultName = $indexName === 'primary'; |
|
505 | - |
|
506 | - if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
507 | - $defaultName = $table->getName() . '_pkey'; |
|
508 | - $isUsingDefaultName = strtolower($defaultName) === $indexName; |
|
509 | - |
|
510 | - if ($isUsingDefaultName) { |
|
511 | - $sequenceName = $table->getName() . '_' . implode('_', $primaryKey->getColumns()) . '_seq'; |
|
512 | - $sequences = array_filter($sequences, function(Sequence $sequence) use ($sequenceName) { |
|
513 | - return $sequence->getName() !== $sequenceName; |
|
514 | - }); |
|
515 | - } |
|
516 | - } else if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) { |
|
517 | - $defaultName = $table->getName() . '_seq'; |
|
518 | - $isUsingDefaultName = strtolower($defaultName) === $indexName; |
|
519 | - } |
|
520 | - |
|
521 | - if (!$isUsingDefaultName && \strlen($indexName) - $prefixLength > 27) { |
|
522 | - throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); |
|
523 | - } |
|
524 | - if ($isUsingDefaultName && \strlen($table->getName()) - $prefixLength > 23) { |
|
525 | - throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); |
|
526 | - } |
|
527 | - } |
|
528 | - } |
|
529 | - |
|
530 | - foreach ($sequences as $sequence) { |
|
531 | - if (\strlen($sequence->getName()) - $prefixLength > 27) { |
|
532 | - throw new \InvalidArgumentException('Sequence name "' . $sequence->getName() . '" is too long.'); |
|
533 | - } |
|
534 | - } |
|
535 | - } |
|
536 | - |
|
537 | - private function ensureMigrationsAreLoaded() { |
|
538 | - if (empty($this->migrations)) { |
|
539 | - $this->migrations = $this->findMigrations(); |
|
540 | - } |
|
541 | - } |
|
44 | + /** @var boolean */ |
|
45 | + private $migrationTableCreated; |
|
46 | + /** @var array */ |
|
47 | + private $migrations; |
|
48 | + /** @var IOutput */ |
|
49 | + private $output; |
|
50 | + /** @var Connection */ |
|
51 | + private $connection; |
|
52 | + /** @var string */ |
|
53 | + private $appName; |
|
54 | + |
|
55 | + /** |
|
56 | + * MigrationService constructor. |
|
57 | + * |
|
58 | + * @param $appName |
|
59 | + * @param IDBConnection $connection |
|
60 | + * @param AppLocator $appLocator |
|
61 | + * @param IOutput|null $output |
|
62 | + * @throws \Exception |
|
63 | + */ |
|
64 | + public function __construct($appName, IDBConnection $connection, IOutput $output = null, AppLocator $appLocator = null) { |
|
65 | + $this->appName = $appName; |
|
66 | + $this->connection = $connection; |
|
67 | + $this->output = $output; |
|
68 | + if (null === $this->output) { |
|
69 | + $this->output = new SimpleOutput(\OC::$server->getLogger(), $appName); |
|
70 | + } |
|
71 | + |
|
72 | + if ($appName === 'core') { |
|
73 | + $this->migrationsPath = \OC::$SERVERROOT . '/core/Migrations'; |
|
74 | + $this->migrationsNamespace = 'OC\\Core\\Migrations'; |
|
75 | + } else { |
|
76 | + if (null === $appLocator) { |
|
77 | + $appLocator = new AppLocator(); |
|
78 | + } |
|
79 | + $appPath = $appLocator->getAppPath($appName); |
|
80 | + $namespace = App::buildAppNamespace($appName); |
|
81 | + $this->migrationsPath = "$appPath/lib/Migration"; |
|
82 | + $this->migrationsNamespace = $namespace . '\\Migration'; |
|
83 | + } |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Returns the name of the app for which this migration is executed |
|
88 | + * |
|
89 | + * @return string |
|
90 | + */ |
|
91 | + public function getApp() { |
|
92 | + return $this->appName; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @return bool |
|
97 | + * @codeCoverageIgnore - this will implicitly tested on installation |
|
98 | + */ |
|
99 | + private function createMigrationTable() { |
|
100 | + if ($this->migrationTableCreated) { |
|
101 | + return false; |
|
102 | + } |
|
103 | + |
|
104 | + $schema = new SchemaWrapper($this->connection); |
|
105 | + |
|
106 | + /** |
|
107 | + * We drop the table when it has different columns or the definition does not |
|
108 | + * match. E.g. ownCloud uses a length of 177 for app and 14 for version. |
|
109 | + */ |
|
110 | + try { |
|
111 | + $table = $schema->getTable('migrations'); |
|
112 | + $columns = $table->getColumns(); |
|
113 | + |
|
114 | + if (count($columns) === 2) { |
|
115 | + try { |
|
116 | + $column = $table->getColumn('app'); |
|
117 | + $schemaMismatch = $column->getLength() !== 255; |
|
118 | + |
|
119 | + if (!$schemaMismatch) { |
|
120 | + $column = $table->getColumn('version'); |
|
121 | + $schemaMismatch = $column->getLength() !== 255; |
|
122 | + } |
|
123 | + } catch (SchemaException $e) { |
|
124 | + // One of the columns is missing |
|
125 | + $schemaMismatch = true; |
|
126 | + } |
|
127 | + |
|
128 | + if (!$schemaMismatch) { |
|
129 | + // Table exists and schema matches: return back! |
|
130 | + $this->migrationTableCreated = true; |
|
131 | + return false; |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + // Drop the table, when it didn't match our expectations. |
|
136 | + $this->connection->dropTable('migrations'); |
|
137 | + |
|
138 | + // Recreate the schema after the table was dropped. |
|
139 | + $schema = new SchemaWrapper($this->connection); |
|
140 | + |
|
141 | + } catch (SchemaException $e) { |
|
142 | + // Table not found, no need to panic, we will create it. |
|
143 | + } |
|
144 | + |
|
145 | + $table = $schema->createTable('migrations'); |
|
146 | + $table->addColumn('app', Type::STRING, ['length' => 255]); |
|
147 | + $table->addColumn('version', Type::STRING, ['length' => 255]); |
|
148 | + $table->setPrimaryKey(['app', 'version']); |
|
149 | + |
|
150 | + $this->connection->migrateToSchema($schema->getWrappedSchema()); |
|
151 | + |
|
152 | + $this->migrationTableCreated = true; |
|
153 | + |
|
154 | + return true; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Returns all versions which have already been applied |
|
159 | + * |
|
160 | + * @return string[] |
|
161 | + * @codeCoverageIgnore - no need to test this |
|
162 | + */ |
|
163 | + public function getMigratedVersions() { |
|
164 | + $this->createMigrationTable(); |
|
165 | + $qb = $this->connection->getQueryBuilder(); |
|
166 | + |
|
167 | + $qb->select('version') |
|
168 | + ->from('migrations') |
|
169 | + ->where($qb->expr()->eq('app', $qb->createNamedParameter($this->getApp()))) |
|
170 | + ->orderBy('version'); |
|
171 | + |
|
172 | + $result = $qb->execute(); |
|
173 | + $rows = $result->fetchAll(\PDO::FETCH_COLUMN); |
|
174 | + $result->closeCursor(); |
|
175 | + |
|
176 | + return $rows; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Returns all versions which are available in the migration folder |
|
181 | + * |
|
182 | + * @return array |
|
183 | + */ |
|
184 | + public function getAvailableVersions() { |
|
185 | + $this->ensureMigrationsAreLoaded(); |
|
186 | + return array_map('strval', array_keys($this->migrations)); |
|
187 | + } |
|
188 | + |
|
189 | + protected function findMigrations() { |
|
190 | + $directory = realpath($this->migrationsPath); |
|
191 | + if ($directory === false || !file_exists($directory) || !is_dir($directory)) { |
|
192 | + return []; |
|
193 | + } |
|
194 | + |
|
195 | + $iterator = new \RegexIterator( |
|
196 | + new \RecursiveIteratorIterator( |
|
197 | + new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS), |
|
198 | + \RecursiveIteratorIterator::LEAVES_ONLY |
|
199 | + ), |
|
200 | + '#^.+\\/Version[^\\/]{1,255}\\.php$#i', |
|
201 | + \RegexIterator::GET_MATCH); |
|
202 | + |
|
203 | + $files = array_keys(iterator_to_array($iterator)); |
|
204 | + uasort($files, function ($a, $b) { |
|
205 | + preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($a), $matchA); |
|
206 | + preg_match('/^Version(\d+)Date(\d+)\\.php$/', basename($b), $matchB); |
|
207 | + if (!empty($matchA) && !empty($matchB)) { |
|
208 | + if ($matchA[1] !== $matchB[1]) { |
|
209 | + return ($matchA[1] < $matchB[1]) ? -1 : 1; |
|
210 | + } |
|
211 | + return ($matchA[2] < $matchB[2]) ? -1 : 1; |
|
212 | + } |
|
213 | + return (basename($a) < basename($b)) ? -1 : 1; |
|
214 | + }); |
|
215 | + |
|
216 | + $migrations = []; |
|
217 | + |
|
218 | + foreach ($files as $file) { |
|
219 | + $className = basename($file, '.php'); |
|
220 | + $version = (string) substr($className, 7); |
|
221 | + if ($version === '0') { |
|
222 | + throw new \InvalidArgumentException( |
|
223 | + "Cannot load a migrations with the name '$version' because it is a reserved number" |
|
224 | + ); |
|
225 | + } |
|
226 | + $migrations[$version] = sprintf('%s\\%s', $this->migrationsNamespace, $className); |
|
227 | + } |
|
228 | + |
|
229 | + return $migrations; |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * @param string $to |
|
234 | + * @return string[] |
|
235 | + */ |
|
236 | + private function getMigrationsToExecute($to) { |
|
237 | + $knownMigrations = $this->getMigratedVersions(); |
|
238 | + $availableMigrations = $this->getAvailableVersions(); |
|
239 | + |
|
240 | + $toBeExecuted = []; |
|
241 | + foreach ($availableMigrations as $v) { |
|
242 | + if ($to !== 'latest' && $v > $to) { |
|
243 | + continue; |
|
244 | + } |
|
245 | + if ($this->shallBeExecuted($v, $knownMigrations)) { |
|
246 | + $toBeExecuted[] = $v; |
|
247 | + } |
|
248 | + } |
|
249 | + |
|
250 | + return $toBeExecuted; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * @param string $m |
|
255 | + * @param string[] $knownMigrations |
|
256 | + * @return bool |
|
257 | + */ |
|
258 | + private function shallBeExecuted($m, $knownMigrations) { |
|
259 | + if (in_array($m, $knownMigrations)) { |
|
260 | + return false; |
|
261 | + } |
|
262 | + |
|
263 | + return true; |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * @param string $version |
|
268 | + */ |
|
269 | + private function markAsExecuted($version) { |
|
270 | + $this->connection->insertIfNotExist('*PREFIX*migrations', [ |
|
271 | + 'app' => $this->appName, |
|
272 | + 'version' => $version |
|
273 | + ]); |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * Returns the name of the table which holds the already applied versions |
|
278 | + * |
|
279 | + * @return string |
|
280 | + */ |
|
281 | + public function getMigrationsTableName() { |
|
282 | + return $this->connection->getPrefix() . 'migrations'; |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * Returns the namespace of the version classes |
|
287 | + * |
|
288 | + * @return string |
|
289 | + */ |
|
290 | + public function getMigrationsNamespace() { |
|
291 | + return $this->migrationsNamespace; |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Returns the directory which holds the versions |
|
296 | + * |
|
297 | + * @return string |
|
298 | + */ |
|
299 | + public function getMigrationsDirectory() { |
|
300 | + return $this->migrationsPath; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Return the explicit version for the aliases; current, next, prev, latest |
|
305 | + * |
|
306 | + * @param string $alias |
|
307 | + * @return mixed|null|string |
|
308 | + */ |
|
309 | + public function getMigration($alias) { |
|
310 | + switch($alias) { |
|
311 | + case 'current': |
|
312 | + return $this->getCurrentVersion(); |
|
313 | + case 'next': |
|
314 | + return $this->getRelativeVersion($this->getCurrentVersion(), 1); |
|
315 | + case 'prev': |
|
316 | + return $this->getRelativeVersion($this->getCurrentVersion(), -1); |
|
317 | + case 'latest': |
|
318 | + $this->ensureMigrationsAreLoaded(); |
|
319 | + |
|
320 | + $migrations = $this->getAvailableVersions(); |
|
321 | + return @end($migrations); |
|
322 | + } |
|
323 | + return '0'; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * @param string $version |
|
328 | + * @param int $delta |
|
329 | + * @return null|string |
|
330 | + */ |
|
331 | + private function getRelativeVersion($version, $delta) { |
|
332 | + $this->ensureMigrationsAreLoaded(); |
|
333 | + |
|
334 | + $versions = $this->getAvailableVersions(); |
|
335 | + array_unshift($versions, 0); |
|
336 | + $offset = array_search($version, $versions, true); |
|
337 | + if ($offset === false || !isset($versions[$offset + $delta])) { |
|
338 | + // Unknown version or delta out of bounds. |
|
339 | + return null; |
|
340 | + } |
|
341 | + |
|
342 | + return (string) $versions[$offset + $delta]; |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * @return string |
|
347 | + */ |
|
348 | + private function getCurrentVersion() { |
|
349 | + $m = $this->getMigratedVersions(); |
|
350 | + if (count($m) === 0) { |
|
351 | + return '0'; |
|
352 | + } |
|
353 | + $migrations = array_values($m); |
|
354 | + return @end($migrations); |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * @param string $version |
|
359 | + * @return string |
|
360 | + * @throws \InvalidArgumentException |
|
361 | + */ |
|
362 | + private function getClass($version) { |
|
363 | + $this->ensureMigrationsAreLoaded(); |
|
364 | + |
|
365 | + if (isset($this->migrations[$version])) { |
|
366 | + return $this->migrations[$version]; |
|
367 | + } |
|
368 | + |
|
369 | + throw new \InvalidArgumentException("Version $version is unknown."); |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * Allows to set an IOutput implementation which is used for logging progress and messages |
|
374 | + * |
|
375 | + * @param IOutput $output |
|
376 | + */ |
|
377 | + public function setOutput(IOutput $output) { |
|
378 | + $this->output = $output; |
|
379 | + } |
|
380 | + |
|
381 | + /** |
|
382 | + * Applies all not yet applied versions up to $to |
|
383 | + * |
|
384 | + * @param string $to |
|
385 | + * @param bool $schemaOnly |
|
386 | + * @throws \InvalidArgumentException |
|
387 | + */ |
|
388 | + public function migrate($to = 'latest', $schemaOnly = false) { |
|
389 | + // read known migrations |
|
390 | + $toBeExecuted = $this->getMigrationsToExecute($to); |
|
391 | + foreach ($toBeExecuted as $version) { |
|
392 | + $this->executeStep($version, $schemaOnly); |
|
393 | + } |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Get the human readable descriptions for the migration steps to run |
|
398 | + * |
|
399 | + * @param string $to |
|
400 | + * @return string[] [$name => $description] |
|
401 | + */ |
|
402 | + public function describeMigrationStep($to = 'latest') { |
|
403 | + $toBeExecuted = $this->getMigrationsToExecute($to); |
|
404 | + $description = []; |
|
405 | + foreach ($toBeExecuted as $version) { |
|
406 | + $migration = $this->createInstance($version); |
|
407 | + if ($migration->name()) { |
|
408 | + $description[$migration->name()] = $migration->description(); |
|
409 | + } |
|
410 | + } |
|
411 | + return $description; |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * @param string $version |
|
416 | + * @return IMigrationStep |
|
417 | + * @throws \InvalidArgumentException |
|
418 | + */ |
|
419 | + protected function createInstance($version) { |
|
420 | + $class = $this->getClass($version); |
|
421 | + try { |
|
422 | + $s = \OC::$server->query($class); |
|
423 | + |
|
424 | + if (!$s instanceof IMigrationStep) { |
|
425 | + throw new \InvalidArgumentException('Not a valid migration'); |
|
426 | + } |
|
427 | + } catch (QueryException $e) { |
|
428 | + if (class_exists($class)) { |
|
429 | + $s = new $class(); |
|
430 | + } else { |
|
431 | + throw new \InvalidArgumentException("Migration step '$class' is unknown"); |
|
432 | + } |
|
433 | + } |
|
434 | + |
|
435 | + return $s; |
|
436 | + } |
|
437 | + |
|
438 | + /** |
|
439 | + * Executes one explicit version |
|
440 | + * |
|
441 | + * @param string $version |
|
442 | + * @param bool $schemaOnly |
|
443 | + * @throws \InvalidArgumentException |
|
444 | + */ |
|
445 | + public function executeStep($version, $schemaOnly = false) { |
|
446 | + $instance = $this->createInstance($version); |
|
447 | + |
|
448 | + if (!$schemaOnly) { |
|
449 | + $instance->preSchemaChange($this->output, function() { |
|
450 | + return new SchemaWrapper($this->connection); |
|
451 | + }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
452 | + } |
|
453 | + |
|
454 | + $toSchema = $instance->changeSchema($this->output, function() { |
|
455 | + return new SchemaWrapper($this->connection); |
|
456 | + }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
457 | + |
|
458 | + if ($toSchema instanceof SchemaWrapper) { |
|
459 | + $targetSchema = $toSchema->getWrappedSchema(); |
|
460 | + // TODO re-enable once stable14 is branched of: https://github.com/nextcloud/server/issues/10518 |
|
461 | + // $this->ensureOracleIdentifierLengthLimit($targetSchema, strlen($this->connection->getPrefix())); |
|
462 | + $this->connection->migrateToSchema($targetSchema); |
|
463 | + $toSchema->performDropTableCalls(); |
|
464 | + } |
|
465 | + |
|
466 | + if (!$schemaOnly) { |
|
467 | + $instance->postSchemaChange($this->output, function() { |
|
468 | + return new SchemaWrapper($this->connection); |
|
469 | + }, ['tablePrefix' => $this->connection->getPrefix()]); |
|
470 | + } |
|
471 | + |
|
472 | + $this->markAsExecuted($version); |
|
473 | + } |
|
474 | + |
|
475 | + public function ensureOracleIdentifierLengthLimit(Schema $schema, int $prefixLength) { |
|
476 | + $sequences = $schema->getSequences(); |
|
477 | + |
|
478 | + foreach ($schema->getTables() as $table) { |
|
479 | + if (\strlen($table->getName()) - $prefixLength > 27) { |
|
480 | + throw new \InvalidArgumentException('Table name "' . $table->getName() . '" is too long.'); |
|
481 | + } |
|
482 | + |
|
483 | + foreach ($table->getColumns() as $thing) { |
|
484 | + if (\strlen($thing->getName()) - $prefixLength > 27) { |
|
485 | + throw new \InvalidArgumentException('Column name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
486 | + } |
|
487 | + } |
|
488 | + |
|
489 | + foreach ($table->getIndexes() as $thing) { |
|
490 | + if (\strlen($thing->getName()) - $prefixLength > 27) { |
|
491 | + throw new \InvalidArgumentException('Index name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
492 | + } |
|
493 | + } |
|
494 | + |
|
495 | + foreach ($table->getForeignKeys() as $thing) { |
|
496 | + if (\strlen($thing->getName()) - $prefixLength > 27) { |
|
497 | + throw new \InvalidArgumentException('Foreign key name "' . $table->getName() . '"."' . $thing->getName() . '" is too long.'); |
|
498 | + } |
|
499 | + } |
|
500 | + |
|
501 | + $primaryKey = $table->getPrimaryKey(); |
|
502 | + if ($primaryKey instanceof Index) { |
|
503 | + $indexName = strtolower($primaryKey->getName()); |
|
504 | + $isUsingDefaultName = $indexName === 'primary'; |
|
505 | + |
|
506 | + if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
507 | + $defaultName = $table->getName() . '_pkey'; |
|
508 | + $isUsingDefaultName = strtolower($defaultName) === $indexName; |
|
509 | + |
|
510 | + if ($isUsingDefaultName) { |
|
511 | + $sequenceName = $table->getName() . '_' . implode('_', $primaryKey->getColumns()) . '_seq'; |
|
512 | + $sequences = array_filter($sequences, function(Sequence $sequence) use ($sequenceName) { |
|
513 | + return $sequence->getName() !== $sequenceName; |
|
514 | + }); |
|
515 | + } |
|
516 | + } else if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) { |
|
517 | + $defaultName = $table->getName() . '_seq'; |
|
518 | + $isUsingDefaultName = strtolower($defaultName) === $indexName; |
|
519 | + } |
|
520 | + |
|
521 | + if (!$isUsingDefaultName && \strlen($indexName) - $prefixLength > 27) { |
|
522 | + throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); |
|
523 | + } |
|
524 | + if ($isUsingDefaultName && \strlen($table->getName()) - $prefixLength > 23) { |
|
525 | + throw new \InvalidArgumentException('Primary index name on "' . $table->getName() . '" is too long.'); |
|
526 | + } |
|
527 | + } |
|
528 | + } |
|
529 | + |
|
530 | + foreach ($sequences as $sequence) { |
|
531 | + if (\strlen($sequence->getName()) - $prefixLength > 27) { |
|
532 | + throw new \InvalidArgumentException('Sequence name "' . $sequence->getName() . '" is too long.'); |
|
533 | + } |
|
534 | + } |
|
535 | + } |
|
536 | + |
|
537 | + private function ensureMigrationsAreLoaded() { |
|
538 | + if (empty($this->migrations)) { |
|
539 | + $this->migrations = $this->findMigrations(); |
|
540 | + } |
|
541 | + } |
|
542 | 542 | } |
@@ -60,14 +60,14 @@ discard block |
||
60 | 60 | $appName = $input->getArgument('app'); |
61 | 61 | $version = $input->getArgument('version'); |
62 | 62 | |
63 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
63 | + if (!preg_match('/^\d{1,16}$/', $version)) { |
|
64 | 64 | $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
65 | 65 | return 1; |
66 | 66 | } |
67 | 67 | |
68 | - $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; |
|
68 | + $schemaFile = $this->appManager->getAppPath($appName).'/appinfo/database.xml'; |
|
69 | 69 | if (!file_exists($schemaFile)) { |
70 | - $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); |
|
70 | + $output->writeln('<error>App '.$appName.' does not have a database.xml file</error>'); |
|
71 | 71 | return 2; |
72 | 72 | } |
73 | 73 | |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
81 | 81 | |
82 | 82 | $date = date('YmdHis'); |
83 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); |
|
83 | + $path = $this->generateMigration($ms, 'Version'.$version.'Date'.$date, $schemaBody); |
|
84 | 84 | |
85 | 85 | $output->writeln("New migration class has been generated to <info>$path</info>"); |
86 | 86 | return 0; |
@@ -36,166 +36,166 @@ |
||
36 | 36 | |
37 | 37 | class GenerateFromSchemaFileCommand extends GenerateCommand { |
38 | 38 | |
39 | - /** @var IConfig */ |
|
40 | - protected $config; |
|
39 | + /** @var IConfig */ |
|
40 | + protected $config; |
|
41 | 41 | |
42 | - public function __construct(IConfig $config, IAppManager $appManager, IDBConnection $connection) { |
|
43 | - parent::__construct($connection, $appManager); |
|
44 | - $this->config = $config; |
|
45 | - } |
|
42 | + public function __construct(IConfig $config, IAppManager $appManager, IDBConnection $connection) { |
|
43 | + parent::__construct($connection, $appManager); |
|
44 | + $this->config = $config; |
|
45 | + } |
|
46 | 46 | |
47 | 47 | |
48 | - protected function configure() { |
|
49 | - parent::configure(); |
|
48 | + protected function configure() { |
|
49 | + parent::configure(); |
|
50 | 50 | |
51 | - $this->setName('migrations:generate-from-schema'); |
|
52 | - } |
|
51 | + $this->setName('migrations:generate-from-schema'); |
|
52 | + } |
|
53 | 53 | |
54 | - public function execute(InputInterface $input, OutputInterface $output) { |
|
55 | - $appName = $input->getArgument('app'); |
|
56 | - $version = $input->getArgument('version'); |
|
54 | + public function execute(InputInterface $input, OutputInterface $output) { |
|
55 | + $appName = $input->getArgument('app'); |
|
56 | + $version = $input->getArgument('version'); |
|
57 | 57 | |
58 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
59 | - $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
60 | - return 1; |
|
61 | - } |
|
58 | + if (!preg_match('/^\d{1,16}$/',$version)) { |
|
59 | + $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
60 | + return 1; |
|
61 | + } |
|
62 | 62 | |
63 | - $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; |
|
64 | - if (!file_exists($schemaFile)) { |
|
65 | - $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); |
|
66 | - return 2; |
|
67 | - } |
|
63 | + $schemaFile = $this->appManager->getAppPath($appName) . '/appinfo/database.xml'; |
|
64 | + if (!file_exists($schemaFile)) { |
|
65 | + $output->writeln('<error>App ' . $appName . ' does not have a database.xml file</error>'); |
|
66 | + return 2; |
|
67 | + } |
|
68 | 68 | |
69 | - $reader = new MDB2SchemaReader($this->config, $this->connection->getDatabasePlatform()); |
|
70 | - $schema = new Schema(); |
|
71 | - $reader->loadSchemaFromFile($schemaFile, $schema); |
|
69 | + $reader = new MDB2SchemaReader($this->config, $this->connection->getDatabasePlatform()); |
|
70 | + $schema = new Schema(); |
|
71 | + $reader->loadSchemaFromFile($schemaFile, $schema); |
|
72 | 72 | |
73 | - $schemaBody = $this->schemaToMigration($schema); |
|
73 | + $schemaBody = $this->schemaToMigration($schema); |
|
74 | 74 | |
75 | - $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
75 | + $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
76 | 76 | |
77 | - $date = date('YmdHis'); |
|
78 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); |
|
77 | + $date = date('YmdHis'); |
|
78 | + $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date, $schemaBody); |
|
79 | 79 | |
80 | - $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
81 | - return 0; |
|
82 | - } |
|
80 | + $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
81 | + return 0; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * @param Schema $schema |
|
86 | - * @return string |
|
87 | - */ |
|
88 | - protected function schemaToMigration(Schema $schema) { |
|
89 | - $content = <<<'EOT' |
|
84 | + /** |
|
85 | + * @param Schema $schema |
|
86 | + * @return string |
|
87 | + */ |
|
88 | + protected function schemaToMigration(Schema $schema) { |
|
89 | + $content = <<<'EOT' |
|
90 | 90 | /** @var ISchemaWrapper $schema */ |
91 | 91 | $schema = $schemaClosure(); |
92 | 92 | |
93 | 93 | EOT; |
94 | 94 | |
95 | - foreach ($schema->getTables() as $table) { |
|
96 | - $content .= str_replace('{{table-name}}', substr($table->getName(), 3), <<<'EOT' |
|
95 | + foreach ($schema->getTables() as $table) { |
|
96 | + $content .= str_replace('{{table-name}}', substr($table->getName(), 3), <<<'EOT' |
|
97 | 97 | |
98 | 98 | if (!$schema->hasTable('{{table-name}}')) { |
99 | 99 | $table = $schema->createTable('{{table-name}}'); |
100 | 100 | |
101 | 101 | EOT |
102 | - ); |
|
102 | + ); |
|
103 | 103 | |
104 | - foreach ($table->getColumns() as $column) { |
|
105 | - $content .= str_replace(['{{name}}', '{{type}}'], [$column->getName(), $column->getType()->getName()], <<<'EOT' |
|
104 | + foreach ($table->getColumns() as $column) { |
|
105 | + $content .= str_replace(['{{name}}', '{{type}}'], [$column->getName(), $column->getType()->getName()], <<<'EOT' |
|
106 | 106 | $table->addColumn('{{name}}', '{{type}}', [ |
107 | 107 | |
108 | 108 | EOT |
109 | - ); |
|
110 | - if ($column->getAutoincrement()) { |
|
111 | - $content .= <<<'EOT' |
|
109 | + ); |
|
110 | + if ($column->getAutoincrement()) { |
|
111 | + $content .= <<<'EOT' |
|
112 | 112 | 'autoincrement' => true, |
113 | 113 | |
114 | 114 | EOT; |
115 | - } |
|
116 | - $content .= str_replace('{{notnull}}', $column->getNotnull() ? 'true' : 'false', <<<'EOT' |
|
115 | + } |
|
116 | + $content .= str_replace('{{notnull}}', $column->getNotnull() ? 'true' : 'false', <<<'EOT' |
|
117 | 117 | 'notnull' => {{notnull}}, |
118 | 118 | |
119 | 119 | EOT |
120 | - ); |
|
121 | - if ($column->getLength() !== null) { |
|
122 | - $content .= str_replace('{{length}}', $column->getLength(), <<<'EOT' |
|
120 | + ); |
|
121 | + if ($column->getLength() !== null) { |
|
122 | + $content .= str_replace('{{length}}', $column->getLength(), <<<'EOT' |
|
123 | 123 | 'length' => {{length}}, |
124 | 124 | |
125 | 125 | EOT |
126 | - ); |
|
127 | - } |
|
128 | - $default = $column->getDefault(); |
|
129 | - if ($default !== null) { |
|
130 | - $default = is_numeric($default) ? $default : "'$default'"; |
|
131 | - $content .= str_replace('{{default}}', $default, <<<'EOT' |
|
126 | + ); |
|
127 | + } |
|
128 | + $default = $column->getDefault(); |
|
129 | + if ($default !== null) { |
|
130 | + $default = is_numeric($default) ? $default : "'$default'"; |
|
131 | + $content .= str_replace('{{default}}', $default, <<<'EOT' |
|
132 | 132 | 'default' => {{default}}, |
133 | 133 | |
134 | 134 | EOT |
135 | - ); |
|
136 | - } |
|
137 | - if ($column->getUnsigned()) { |
|
138 | - $content .= <<<'EOT' |
|
135 | + ); |
|
136 | + } |
|
137 | + if ($column->getUnsigned()) { |
|
138 | + $content .= <<<'EOT' |
|
139 | 139 | 'unsigned' => true, |
140 | 140 | |
141 | 141 | EOT; |
142 | - } |
|
142 | + } |
|
143 | 143 | |
144 | - $content .= <<<'EOT' |
|
144 | + $content .= <<<'EOT' |
|
145 | 145 | ]); |
146 | 146 | |
147 | 147 | EOT; |
148 | - } |
|
148 | + } |
|
149 | 149 | |
150 | - $content .= <<<'EOT' |
|
150 | + $content .= <<<'EOT' |
|
151 | 151 | |
152 | 152 | EOT; |
153 | 153 | |
154 | - $primaryKey = $table->getPrimaryKey(); |
|
155 | - if ($primaryKey !== null) { |
|
156 | - $content .= str_replace('{{columns}}', implode('\', \'', $primaryKey->getUnquotedColumns()), <<<'EOT' |
|
154 | + $primaryKey = $table->getPrimaryKey(); |
|
155 | + if ($primaryKey !== null) { |
|
156 | + $content .= str_replace('{{columns}}', implode('\', \'', $primaryKey->getUnquotedColumns()), <<<'EOT' |
|
157 | 157 | $table->setPrimaryKey(['{{columns}}']); |
158 | 158 | |
159 | 159 | EOT |
160 | - ); |
|
161 | - } |
|
162 | - |
|
163 | - foreach ($table->getIndexes() as $index) { |
|
164 | - if ($index->isPrimary()) { |
|
165 | - continue; |
|
166 | - } |
|
167 | - |
|
168 | - if ($index->isUnique()) { |
|
169 | - $content .= str_replace( |
|
170 | - ['{{columns}}', '{{name}}'], |
|
171 | - [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
172 | - <<<'EOT' |
|
160 | + ); |
|
161 | + } |
|
162 | + |
|
163 | + foreach ($table->getIndexes() as $index) { |
|
164 | + if ($index->isPrimary()) { |
|
165 | + continue; |
|
166 | + } |
|
167 | + |
|
168 | + if ($index->isUnique()) { |
|
169 | + $content .= str_replace( |
|
170 | + ['{{columns}}', '{{name}}'], |
|
171 | + [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
172 | + <<<'EOT' |
|
173 | 173 | $table->addUniqueIndex(['{{columns}}'], '{{name}}'); |
174 | 174 | |
175 | 175 | EOT |
176 | - ); |
|
177 | - } else { |
|
178 | - $content .= str_replace( |
|
179 | - ['{{columns}}', '{{name}}'], |
|
180 | - [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
181 | - <<<'EOT' |
|
176 | + ); |
|
177 | + } else { |
|
178 | + $content .= str_replace( |
|
179 | + ['{{columns}}', '{{name}}'], |
|
180 | + [implode('\', \'', $index->getUnquotedColumns()), $index->getName()], |
|
181 | + <<<'EOT' |
|
182 | 182 | $table->addIndex(['{{columns}}'], '{{name}}'); |
183 | 183 | |
184 | 184 | EOT |
185 | - ); |
|
186 | - } |
|
187 | - } |
|
185 | + ); |
|
186 | + } |
|
187 | + } |
|
188 | 188 | |
189 | - $content .= <<<'EOT' |
|
189 | + $content .= <<<'EOT' |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | EOT; |
193 | - } |
|
193 | + } |
|
194 | 194 | |
195 | - $content .= <<<'EOT' |
|
195 | + $content .= <<<'EOT' |
|
196 | 196 | return $schema; |
197 | 197 | EOT; |
198 | 198 | |
199 | - return $content; |
|
200 | - } |
|
199 | + return $content; |
|
200 | + } |
|
201 | 201 | } |
@@ -27,72 +27,72 @@ |
||
27 | 27 | |
28 | 28 | class FileDeleted implements ISetting { |
29 | 29 | |
30 | - /** @var IL10N */ |
|
31 | - protected $l; |
|
30 | + /** @var IL10N */ |
|
31 | + protected $l; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @param IL10N $l |
|
35 | - */ |
|
36 | - public function __construct(IL10N $l) { |
|
37 | - $this->l = $l; |
|
38 | - } |
|
33 | + /** |
|
34 | + * @param IL10N $l |
|
35 | + */ |
|
36 | + public function __construct(IL10N $l) { |
|
37 | + $this->l = $l; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return string Lowercase a-z and underscore only identifier |
|
42 | - * @since 11.0.0 |
|
43 | - */ |
|
44 | - public function getIdentifier() { |
|
45 | - return 'file_deleted'; |
|
46 | - } |
|
40 | + /** |
|
41 | + * @return string Lowercase a-z and underscore only identifier |
|
42 | + * @since 11.0.0 |
|
43 | + */ |
|
44 | + public function getIdentifier() { |
|
45 | + return 'file_deleted'; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @return string A translated string |
|
50 | - * @since 11.0.0 |
|
51 | - */ |
|
52 | - public function getName() { |
|
53 | - return $this->l->t('A file or folder has been <strong>deleted</strong>'); |
|
54 | - } |
|
48 | + /** |
|
49 | + * @return string A translated string |
|
50 | + * @since 11.0.0 |
|
51 | + */ |
|
52 | + public function getName() { |
|
53 | + return $this->l->t('A file or folder has been <strong>deleted</strong>'); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return int whether the filter should be rather on the top or bottom of |
|
58 | - * the admin section. The filters are arranged in ascending order of the |
|
59 | - * priority values. It is required to return a value between 0 and 100. |
|
60 | - * @since 11.0.0 |
|
61 | - */ |
|
62 | - public function getPriority() { |
|
63 | - return 3; |
|
64 | - } |
|
56 | + /** |
|
57 | + * @return int whether the filter should be rather on the top or bottom of |
|
58 | + * the admin section. The filters are arranged in ascending order of the |
|
59 | + * priority values. It is required to return a value between 0 and 100. |
|
60 | + * @since 11.0.0 |
|
61 | + */ |
|
62 | + public function getPriority() { |
|
63 | + return 3; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return bool True when the option can be changed for the stream |
|
68 | - * @since 11.0.0 |
|
69 | - */ |
|
70 | - public function canChangeStream() { |
|
71 | - return true; |
|
72 | - } |
|
66 | + /** |
|
67 | + * @return bool True when the option can be changed for the stream |
|
68 | + * @since 11.0.0 |
|
69 | + */ |
|
70 | + public function canChangeStream() { |
|
71 | + return true; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @return bool True when the option can be changed for the stream |
|
76 | - * @since 11.0.0 |
|
77 | - */ |
|
78 | - public function isDefaultEnabledStream() { |
|
79 | - return true; |
|
80 | - } |
|
74 | + /** |
|
75 | + * @return bool True when the option can be changed for the stream |
|
76 | + * @since 11.0.0 |
|
77 | + */ |
|
78 | + public function isDefaultEnabledStream() { |
|
79 | + return true; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return bool True when the option can be changed for the mail |
|
84 | - * @since 11.0.0 |
|
85 | - */ |
|
86 | - public function canChangeMail() { |
|
87 | - return true; |
|
88 | - } |
|
82 | + /** |
|
83 | + * @return bool True when the option can be changed for the mail |
|
84 | + * @since 11.0.0 |
|
85 | + */ |
|
86 | + public function canChangeMail() { |
|
87 | + return true; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @return bool True when the option can be changed for the stream |
|
92 | - * @since 11.0.0 |
|
93 | - */ |
|
94 | - public function isDefaultEnabledMail() { |
|
95 | - return false; |
|
96 | - } |
|
90 | + /** |
|
91 | + * @return bool True when the option can be changed for the stream |
|
92 | + * @since 11.0.0 |
|
93 | + */ |
|
94 | + public function isDefaultEnabledMail() { |
|
95 | + return false; |
|
96 | + } |
|
97 | 97 | } |
98 | 98 |
@@ -27,72 +27,72 @@ |
||
27 | 27 | |
28 | 28 | class FileRestored implements ISetting { |
29 | 29 | |
30 | - /** @var IL10N */ |
|
31 | - protected $l; |
|
30 | + /** @var IL10N */ |
|
31 | + protected $l; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @param IL10N $l |
|
35 | - */ |
|
36 | - public function __construct(IL10N $l) { |
|
37 | - $this->l = $l; |
|
38 | - } |
|
33 | + /** |
|
34 | + * @param IL10N $l |
|
35 | + */ |
|
36 | + public function __construct(IL10N $l) { |
|
37 | + $this->l = $l; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return string Lowercase a-z and underscore only identifier |
|
42 | - * @since 11.0.0 |
|
43 | - */ |
|
44 | - public function getIdentifier() { |
|
45 | - return 'file_restored'; |
|
46 | - } |
|
40 | + /** |
|
41 | + * @return string Lowercase a-z and underscore only identifier |
|
42 | + * @since 11.0.0 |
|
43 | + */ |
|
44 | + public function getIdentifier() { |
|
45 | + return 'file_restored'; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * @return string A translated string |
|
50 | - * @since 11.0.0 |
|
51 | - */ |
|
52 | - public function getName() { |
|
53 | - return $this->l->t('A file or folder has been <strong>restored</strong>'); |
|
54 | - } |
|
48 | + /** |
|
49 | + * @return string A translated string |
|
50 | + * @since 11.0.0 |
|
51 | + */ |
|
52 | + public function getName() { |
|
53 | + return $this->l->t('A file or folder has been <strong>restored</strong>'); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @return int whether the filter should be rather on the top or bottom of |
|
58 | - * the admin section. The filters are arranged in ascending order of the |
|
59 | - * priority values. It is required to return a value between 0 and 100. |
|
60 | - * @since 11.0.0 |
|
61 | - */ |
|
62 | - public function getPriority() { |
|
63 | - return 4; |
|
64 | - } |
|
56 | + /** |
|
57 | + * @return int whether the filter should be rather on the top or bottom of |
|
58 | + * the admin section. The filters are arranged in ascending order of the |
|
59 | + * priority values. It is required to return a value between 0 and 100. |
|
60 | + * @since 11.0.0 |
|
61 | + */ |
|
62 | + public function getPriority() { |
|
63 | + return 4; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return bool True when the option can be changed for the stream |
|
68 | - * @since 11.0.0 |
|
69 | - */ |
|
70 | - public function canChangeStream() { |
|
71 | - return true; |
|
72 | - } |
|
66 | + /** |
|
67 | + * @return bool True when the option can be changed for the stream |
|
68 | + * @since 11.0.0 |
|
69 | + */ |
|
70 | + public function canChangeStream() { |
|
71 | + return true; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @return bool True when the option can be changed for the stream |
|
76 | - * @since 11.0.0 |
|
77 | - */ |
|
78 | - public function isDefaultEnabledStream() { |
|
79 | - return true; |
|
80 | - } |
|
74 | + /** |
|
75 | + * @return bool True when the option can be changed for the stream |
|
76 | + * @since 11.0.0 |
|
77 | + */ |
|
78 | + public function isDefaultEnabledStream() { |
|
79 | + return true; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * @return bool True when the option can be changed for the mail |
|
84 | - * @since 11.0.0 |
|
85 | - */ |
|
86 | - public function canChangeMail() { |
|
87 | - return true; |
|
88 | - } |
|
82 | + /** |
|
83 | + * @return bool True when the option can be changed for the mail |
|
84 | + * @since 11.0.0 |
|
85 | + */ |
|
86 | + public function canChangeMail() { |
|
87 | + return true; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * @return bool True when the option can be changed for the stream |
|
92 | - * @since 11.0.0 |
|
93 | - */ |
|
94 | - public function isDefaultEnabledMail() { |
|
95 | - return false; |
|
96 | - } |
|
90 | + /** |
|
91 | + * @return bool True when the option can be changed for the stream |
|
92 | + * @since 11.0.0 |
|
93 | + */ |
|
94 | + public function isDefaultEnabledMail() { |
|
95 | + return false; |
|
96 | + } |
|
97 | 97 | } |
98 | 98 |
@@ -44,116 +44,116 @@ |
||
44 | 44 | */ |
45 | 45 | abstract class Controller { |
46 | 46 | |
47 | - /** |
|
48 | - * app name |
|
49 | - * @var string |
|
50 | - * @since 7.0.0 |
|
51 | - */ |
|
52 | - protected $appName; |
|
53 | - |
|
54 | - /** |
|
55 | - * current request |
|
56 | - * @var \OCP\IRequest |
|
57 | - * @since 6.0.0 |
|
58 | - */ |
|
59 | - protected $request; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var array |
|
63 | - * @since 7.0.0 |
|
64 | - */ |
|
65 | - private $responders; |
|
66 | - |
|
67 | - /** |
|
68 | - * constructor of the controller |
|
69 | - * @param string $appName the name of the app |
|
70 | - * @param IRequest $request an instance of the request |
|
71 | - * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0 |
|
72 | - */ |
|
73 | - public function __construct($appName, |
|
74 | - IRequest $request) { |
|
75 | - $this->appName = $appName; |
|
76 | - $this->request = $request; |
|
77 | - |
|
78 | - // default responders |
|
79 | - $this->responders = array( |
|
80 | - 'json' => function ($data) { |
|
81 | - if ($data instanceof DataResponse) { |
|
82 | - $response = new JSONResponse( |
|
83 | - $data->getData(), |
|
84 | - $data->getStatus() |
|
85 | - ); |
|
86 | - $dataHeaders = $data->getHeaders(); |
|
87 | - $headers = $response->getHeaders(); |
|
88 | - // do not overwrite Content-Type if it already exists |
|
89 | - if (isset($dataHeaders['Content-Type'])) { |
|
90 | - unset($headers['Content-Type']); |
|
91 | - } |
|
92 | - $response->setHeaders(array_merge($dataHeaders, $headers)); |
|
93 | - return $response; |
|
94 | - } |
|
95 | - return new JSONResponse($data); |
|
96 | - } |
|
97 | - ); |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * Parses an HTTP accept header and returns the supported responder type |
|
103 | - * @param string $acceptHeader |
|
104 | - * @param string $default |
|
105 | - * @return string the responder type |
|
106 | - * @since 7.0.0 |
|
107 | - * @since 9.1.0 Added default parameter |
|
108 | - */ |
|
109 | - public function getResponderByHTTPHeader($acceptHeader, $default='json') { |
|
110 | - $headers = explode(',', $acceptHeader); |
|
111 | - |
|
112 | - // return the first matching responder |
|
113 | - foreach ($headers as $header) { |
|
114 | - $header = strtolower(trim($header)); |
|
115 | - |
|
116 | - $responder = str_replace('application/', '', $header); |
|
117 | - |
|
118 | - if (array_key_exists($responder, $this->responders)) { |
|
119 | - return $responder; |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - // no matching header return default |
|
124 | - return $default; |
|
125 | - } |
|
126 | - |
|
127 | - |
|
128 | - /** |
|
129 | - * Registers a formatter for a type |
|
130 | - * @param string $format |
|
131 | - * @param \Closure $responder |
|
132 | - * @since 7.0.0 |
|
133 | - */ |
|
134 | - protected function registerResponder($format, \Closure $responder) { |
|
135 | - $this->responders[$format] = $responder; |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * Serializes and formats a response |
|
141 | - * @param mixed $response the value that was returned from a controller and |
|
142 | - * is not a Response instance |
|
143 | - * @param string $format the format for which a formatter has been registered |
|
144 | - * @throws \DomainException if format does not match a registered formatter |
|
145 | - * @return Response |
|
146 | - * @since 7.0.0 |
|
147 | - */ |
|
148 | - public function buildResponse($response, $format='json') { |
|
149 | - if(array_key_exists($format, $this->responders)) { |
|
150 | - |
|
151 | - $responder = $this->responders[$format]; |
|
152 | - |
|
153 | - return $responder($response); |
|
154 | - |
|
155 | - } |
|
156 | - throw new \DomainException('No responder registered for format '. |
|
157 | - $format . '!'); |
|
158 | - } |
|
47 | + /** |
|
48 | + * app name |
|
49 | + * @var string |
|
50 | + * @since 7.0.0 |
|
51 | + */ |
|
52 | + protected $appName; |
|
53 | + |
|
54 | + /** |
|
55 | + * current request |
|
56 | + * @var \OCP\IRequest |
|
57 | + * @since 6.0.0 |
|
58 | + */ |
|
59 | + protected $request; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var array |
|
63 | + * @since 7.0.0 |
|
64 | + */ |
|
65 | + private $responders; |
|
66 | + |
|
67 | + /** |
|
68 | + * constructor of the controller |
|
69 | + * @param string $appName the name of the app |
|
70 | + * @param IRequest $request an instance of the request |
|
71 | + * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0 |
|
72 | + */ |
|
73 | + public function __construct($appName, |
|
74 | + IRequest $request) { |
|
75 | + $this->appName = $appName; |
|
76 | + $this->request = $request; |
|
77 | + |
|
78 | + // default responders |
|
79 | + $this->responders = array( |
|
80 | + 'json' => function ($data) { |
|
81 | + if ($data instanceof DataResponse) { |
|
82 | + $response = new JSONResponse( |
|
83 | + $data->getData(), |
|
84 | + $data->getStatus() |
|
85 | + ); |
|
86 | + $dataHeaders = $data->getHeaders(); |
|
87 | + $headers = $response->getHeaders(); |
|
88 | + // do not overwrite Content-Type if it already exists |
|
89 | + if (isset($dataHeaders['Content-Type'])) { |
|
90 | + unset($headers['Content-Type']); |
|
91 | + } |
|
92 | + $response->setHeaders(array_merge($dataHeaders, $headers)); |
|
93 | + return $response; |
|
94 | + } |
|
95 | + return new JSONResponse($data); |
|
96 | + } |
|
97 | + ); |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * Parses an HTTP accept header and returns the supported responder type |
|
103 | + * @param string $acceptHeader |
|
104 | + * @param string $default |
|
105 | + * @return string the responder type |
|
106 | + * @since 7.0.0 |
|
107 | + * @since 9.1.0 Added default parameter |
|
108 | + */ |
|
109 | + public function getResponderByHTTPHeader($acceptHeader, $default='json') { |
|
110 | + $headers = explode(',', $acceptHeader); |
|
111 | + |
|
112 | + // return the first matching responder |
|
113 | + foreach ($headers as $header) { |
|
114 | + $header = strtolower(trim($header)); |
|
115 | + |
|
116 | + $responder = str_replace('application/', '', $header); |
|
117 | + |
|
118 | + if (array_key_exists($responder, $this->responders)) { |
|
119 | + return $responder; |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + // no matching header return default |
|
124 | + return $default; |
|
125 | + } |
|
126 | + |
|
127 | + |
|
128 | + /** |
|
129 | + * Registers a formatter for a type |
|
130 | + * @param string $format |
|
131 | + * @param \Closure $responder |
|
132 | + * @since 7.0.0 |
|
133 | + */ |
|
134 | + protected function registerResponder($format, \Closure $responder) { |
|
135 | + $this->responders[$format] = $responder; |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * Serializes and formats a response |
|
141 | + * @param mixed $response the value that was returned from a controller and |
|
142 | + * is not a Response instance |
|
143 | + * @param string $format the format for which a formatter has been registered |
|
144 | + * @throws \DomainException if format does not match a registered formatter |
|
145 | + * @return Response |
|
146 | + * @since 7.0.0 |
|
147 | + */ |
|
148 | + public function buildResponse($response, $format='json') { |
|
149 | + if(array_key_exists($format, $this->responders)) { |
|
150 | + |
|
151 | + $responder = $this->responders[$format]; |
|
152 | + |
|
153 | + return $responder($response); |
|
154 | + |
|
155 | + } |
|
156 | + throw new \DomainException('No responder registered for format '. |
|
157 | + $format . '!'); |
|
158 | + } |
|
159 | 159 | } |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | |
78 | 78 | // default responders |
79 | 79 | $this->responders = array( |
80 | - 'json' => function ($data) { |
|
80 | + 'json' => function($data) { |
|
81 | 81 | if ($data instanceof DataResponse) { |
82 | 82 | $response = new JSONResponse( |
83 | 83 | $data->getData(), |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | * @since 7.0.0 |
107 | 107 | * @since 9.1.0 Added default parameter |
108 | 108 | */ |
109 | - public function getResponderByHTTPHeader($acceptHeader, $default='json') { |
|
109 | + public function getResponderByHTTPHeader($acceptHeader, $default = 'json') { |
|
110 | 110 | $headers = explode(',', $acceptHeader); |
111 | 111 | |
112 | 112 | // return the first matching responder |
@@ -145,8 +145,8 @@ discard block |
||
145 | 145 | * @return Response |
146 | 146 | * @since 7.0.0 |
147 | 147 | */ |
148 | - public function buildResponse($response, $format='json') { |
|
149 | - if(array_key_exists($format, $this->responders)) { |
|
148 | + public function buildResponse($response, $format = 'json') { |
|
149 | + if (array_key_exists($format, $this->responders)) { |
|
150 | 150 | |
151 | 151 | $responder = $this->responders[$format]; |
152 | 152 | |
@@ -154,6 +154,6 @@ discard block |
||
154 | 154 | |
155 | 155 | } |
156 | 156 | throw new \DomainException('No responder registered for format '. |
157 | - $format . '!'); |
|
157 | + $format.'!'); |
|
158 | 158 | } |
159 | 159 | } |
@@ -38,21 +38,21 @@ discard block |
||
38 | 38 | |
39 | 39 | // Backends |
40 | 40 | $authBackend = new OCA\DAV\Connector\PublicAuth( |
41 | - \OC::$server->getRequest(), |
|
42 | - \OC::$server->getShareManager(), |
|
43 | - \OC::$server->getSession() |
|
41 | + \OC::$server->getRequest(), |
|
42 | + \OC::$server->getShareManager(), |
|
43 | + \OC::$server->getSession() |
|
44 | 44 | ); |
45 | 45 | $authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend); |
46 | 46 | |
47 | 47 | $serverFactory = new OCA\DAV\Connector\Sabre\ServerFactory( |
48 | - \OC::$server->getConfig(), |
|
49 | - \OC::$server->getLogger(), |
|
50 | - \OC::$server->getDatabaseConnection(), |
|
51 | - \OC::$server->getUserSession(), |
|
52 | - \OC::$server->getMountManager(), |
|
53 | - \OC::$server->getTagManager(), |
|
54 | - \OC::$server->getRequest(), |
|
55 | - \OC::$server->getPreviewManager() |
|
48 | + \OC::$server->getConfig(), |
|
49 | + \OC::$server->getLogger(), |
|
50 | + \OC::$server->getDatabaseConnection(), |
|
51 | + \OC::$server->getUserSession(), |
|
52 | + \OC::$server->getMountManager(), |
|
53 | + \OC::$server->getTagManager(), |
|
54 | + \OC::$server->getRequest(), |
|
55 | + \OC::$server->getPreviewManager() |
|
56 | 56 | ); |
57 | 57 | |
58 | 58 | $requestUri = \OC::$server->getRequest()->getRequestUri(); |
@@ -61,43 +61,43 @@ discard block |
||
61 | 61 | $filesDropPlugin = new \OCA\DAV\Files\Sharing\FilesDropPlugin(); |
62 | 62 | |
63 | 63 | $server = $serverFactory->createServer($baseuri, $requestUri, $authPlugin, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) { |
64 | - $isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'); |
|
65 | - $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
66 | - $federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); |
|
67 | - if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false && !$isAjax) { |
|
68 | - // this is what is thrown when trying to access a non-existing share |
|
69 | - throw new \Sabre\DAV\Exception\NotAuthenticated(); |
|
70 | - } |
|
71 | - |
|
72 | - $share = $authBackend->getShare(); |
|
73 | - $owner = $share->getShareOwner(); |
|
74 | - $isReadable = $share->getPermissions() & \OCP\Constants::PERMISSION_READ; |
|
75 | - $fileId = $share->getNodeId(); |
|
76 | - |
|
77 | - // FIXME: should not add storage wrappers outside of preSetup, need to find a better way |
|
78 | - $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); |
|
79 | - \OC\Files\Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) { |
|
80 | - return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE)); |
|
81 | - }); |
|
82 | - |
|
83 | - \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog); |
|
84 | - |
|
85 | - OC_Util::tearDownFS(); |
|
86 | - OC_Util::setupFS($owner); |
|
87 | - $ownerView = new \OC\Files\View('/'. $owner . '/files'); |
|
88 | - $path = $ownerView->getPath($fileId); |
|
89 | - $fileInfo = $ownerView->getFileInfo($path); |
|
90 | - $linkCheckPlugin->setFileInfo($fileInfo); |
|
91 | - |
|
92 | - // If not readble (files_drop) enable the filesdrop plugin |
|
93 | - if (!$isReadable) { |
|
94 | - $filesDropPlugin->enable(); |
|
95 | - } |
|
96 | - |
|
97 | - $view = new \OC\Files\View($ownerView->getAbsolutePath($path)); |
|
98 | - $filesDropPlugin->setView($view); |
|
99 | - |
|
100 | - return $view; |
|
64 | + $isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'); |
|
65 | + $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
66 | + $federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); |
|
67 | + if ($federatedShareProvider->isOutgoingServer2serverShareEnabled() === false && !$isAjax) { |
|
68 | + // this is what is thrown when trying to access a non-existing share |
|
69 | + throw new \Sabre\DAV\Exception\NotAuthenticated(); |
|
70 | + } |
|
71 | + |
|
72 | + $share = $authBackend->getShare(); |
|
73 | + $owner = $share->getShareOwner(); |
|
74 | + $isReadable = $share->getPermissions() & \OCP\Constants::PERMISSION_READ; |
|
75 | + $fileId = $share->getNodeId(); |
|
76 | + |
|
77 | + // FIXME: should not add storage wrappers outside of preSetup, need to find a better way |
|
78 | + $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); |
|
79 | + \OC\Files\Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) { |
|
80 | + return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE)); |
|
81 | + }); |
|
82 | + |
|
83 | + \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($previousLog); |
|
84 | + |
|
85 | + OC_Util::tearDownFS(); |
|
86 | + OC_Util::setupFS($owner); |
|
87 | + $ownerView = new \OC\Files\View('/'. $owner . '/files'); |
|
88 | + $path = $ownerView->getPath($fileId); |
|
89 | + $fileInfo = $ownerView->getFileInfo($path); |
|
90 | + $linkCheckPlugin->setFileInfo($fileInfo); |
|
91 | + |
|
92 | + // If not readble (files_drop) enable the filesdrop plugin |
|
93 | + if (!$isReadable) { |
|
94 | + $filesDropPlugin->enable(); |
|
95 | + } |
|
96 | + |
|
97 | + $view = new \OC\Files\View($ownerView->getAbsolutePath($path)); |
|
98 | + $filesDropPlugin->setView($view); |
|
99 | + |
|
100 | + return $view; |
|
101 | 101 | }); |
102 | 102 | |
103 | 103 | $server->addPlugin($linkCheckPlugin); |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | $linkCheckPlugin = new \OCA\DAV\Files\Sharing\PublicLinkCheckPlugin(); |
61 | 61 | $filesDropPlugin = new \OCA\DAV\Files\Sharing\FilesDropPlugin(); |
62 | 62 | |
63 | -$server = $serverFactory->createServer($baseuri, $requestUri, $authPlugin, function (\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) { |
|
63 | +$server = $serverFactory->createServer($baseuri, $requestUri, $authPlugin, function(\Sabre\DAV\Server $server) use ($authBackend, $linkCheckPlugin, $filesDropPlugin) { |
|
64 | 64 | $isAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'); |
65 | 65 | $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); |
66 | 66 | $federatedShareProvider = $federatedSharingApp->getFederatedShareProvider(); |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | |
77 | 77 | // FIXME: should not add storage wrappers outside of preSetup, need to find a better way |
78 | 78 | $previousLog = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); |
79 | - \OC\Files\Filesystem::addStorageWrapper('sharePermissions', function ($mountPoint, $storage) use ($share) { |
|
79 | + \OC\Files\Filesystem::addStorageWrapper('sharePermissions', function($mountPoint, $storage) use ($share) { |
|
80 | 80 | return new \OC\Files\Storage\Wrapper\PermissionsMask(array('storage' => $storage, 'mask' => $share->getPermissions() | \OCP\Constants::PERMISSION_SHARE)); |
81 | 81 | }); |
82 | 82 | |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | |
85 | 85 | OC_Util::tearDownFS(); |
86 | 86 | OC_Util::setupFS($owner); |
87 | - $ownerView = new \OC\Files\View('/'. $owner . '/files'); |
|
87 | + $ownerView = new \OC\Files\View('/'.$owner.'/files'); |
|
88 | 88 | $path = $ownerView->getPath($fileId); |
89 | 89 | $fileInfo = $ownerView->getFileInfo($path); |
90 | 90 | $linkCheckPlugin->setFileInfo($fileInfo); |
@@ -191,9 +191,9 @@ discard block |
||
191 | 191 | $params = []; |
192 | 192 | foreach ($this->getParameters() as $placeholder => $value) { |
193 | 193 | if (is_array($value)) { |
194 | - $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')'; |
|
194 | + $params[] = $placeholder.' => (\''.implode('\', \'', $value).'\')'; |
|
195 | 195 | } else { |
196 | - $params[] = $placeholder . ' => \'' . $value . '\''; |
|
196 | + $params[] = $placeholder.' => \''.$value.'\''; |
|
197 | 197 | } |
198 | 198 | } |
199 | 199 | if (empty($params)) { |
@@ -410,7 +410,7 @@ discard block |
||
410 | 410 | public function selectAlias($select, $alias) { |
411 | 411 | |
412 | 412 | $this->queryBuilder->addSelect( |
413 | - $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias) |
|
413 | + $this->helper->quoteColumnName($select).' AS '.$this->helper->quoteColumnName($alias) |
|
414 | 414 | ); |
415 | 415 | |
416 | 416 | return $this; |
@@ -432,7 +432,7 @@ discard block |
||
432 | 432 | public function selectDistinct($select) { |
433 | 433 | |
434 | 434 | $this->queryBuilder->addSelect( |
435 | - 'DISTINCT ' . $this->helper->quoteColumnName($select) |
|
435 | + 'DISTINCT '.$this->helper->quoteColumnName($select) |
|
436 | 436 | ); |
437 | 437 | |
438 | 438 | return $this; |
@@ -1109,7 +1109,7 @@ discard block |
||
1109 | 1109 | * @return IParameter |
1110 | 1110 | */ |
1111 | 1111 | public function createParameter($name) { |
1112 | - return new Parameter(':' . $name); |
|
1112 | + return new Parameter(':'.$name); |
|
1113 | 1113 | } |
1114 | 1114 | |
1115 | 1115 | /** |
@@ -1176,7 +1176,7 @@ discard block |
||
1176 | 1176 | return $table; |
1177 | 1177 | } |
1178 | 1178 | |
1179 | - return '*PREFIX*' . $table; |
|
1179 | + return '*PREFIX*'.$table; |
|
1180 | 1180 | } |
1181 | 1181 | |
1182 | 1182 | /** |
@@ -1191,7 +1191,7 @@ discard block |
||
1191 | 1191 | $tableAlias .= '.'; |
1192 | 1192 | } |
1193 | 1193 | |
1194 | - return $this->helper->quoteColumnName($tableAlias . $column); |
|
1194 | + return $this->helper->quoteColumnName($tableAlias.$column); |
|
1195 | 1195 | } |
1196 | 1196 | |
1197 | 1197 | /** |
@@ -47,1173 +47,1173 @@ |
||
47 | 47 | |
48 | 48 | class QueryBuilder implements IQueryBuilder { |
49 | 49 | |
50 | - /** @var \OCP\IDBConnection */ |
|
51 | - private $connection; |
|
52 | - |
|
53 | - /** @var SystemConfig */ |
|
54 | - private $systemConfig; |
|
55 | - |
|
56 | - /** @var ILogger */ |
|
57 | - private $logger; |
|
58 | - |
|
59 | - /** @var \Doctrine\DBAL\Query\QueryBuilder */ |
|
60 | - private $queryBuilder; |
|
61 | - |
|
62 | - /** @var QuoteHelper */ |
|
63 | - private $helper; |
|
64 | - |
|
65 | - /** @var bool */ |
|
66 | - private $automaticTablePrefix = true; |
|
67 | - |
|
68 | - /** @var string */ |
|
69 | - protected $lastInsertedTable; |
|
70 | - |
|
71 | - /** |
|
72 | - * Initializes a new QueryBuilder. |
|
73 | - * |
|
74 | - * @param IDBConnection $connection |
|
75 | - * @param SystemConfig $systemConfig |
|
76 | - * @param ILogger $logger |
|
77 | - */ |
|
78 | - public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger) { |
|
79 | - $this->connection = $connection; |
|
80 | - $this->systemConfig = $systemConfig; |
|
81 | - $this->logger = $logger; |
|
82 | - $this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection); |
|
83 | - $this->helper = new QuoteHelper(); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Enable/disable automatic prefixing of table names with the oc_ prefix |
|
88 | - * |
|
89 | - * @param bool $enabled If set to true table names will be prefixed with the |
|
90 | - * owncloud database prefix automatically. |
|
91 | - * @since 8.2.0 |
|
92 | - */ |
|
93 | - public function automaticTablePrefix($enabled) { |
|
94 | - $this->automaticTablePrefix = (bool) $enabled; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
|
99 | - * This producer method is intended for convenient inline usage. Example: |
|
100 | - * |
|
101 | - * <code> |
|
102 | - * $qb = $conn->getQueryBuilder() |
|
103 | - * ->select('u') |
|
104 | - * ->from('users', 'u') |
|
105 | - * ->where($qb->expr()->eq('u.id', 1)); |
|
106 | - * </code> |
|
107 | - * |
|
108 | - * For more complex expression construction, consider storing the expression |
|
109 | - * builder object in a local variable. |
|
110 | - * |
|
111 | - * @return \OCP\DB\QueryBuilder\IExpressionBuilder |
|
112 | - */ |
|
113 | - public function expr() { |
|
114 | - if ($this->connection instanceof OracleConnection) { |
|
115 | - return new OCIExpressionBuilder($this->connection, $this); |
|
116 | - } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
117 | - return new PgSqlExpressionBuilder($this->connection, $this); |
|
118 | - } else if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
119 | - return new MySqlExpressionBuilder($this->connection, $this); |
|
120 | - } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
121 | - return new SqliteExpressionBuilder($this->connection, $this); |
|
122 | - } else { |
|
123 | - return new ExpressionBuilder($this->connection, $this); |
|
124 | - } |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Gets an FunctionBuilder used for object-oriented construction of query functions. |
|
129 | - * This producer method is intended for convenient inline usage. Example: |
|
130 | - * |
|
131 | - * <code> |
|
132 | - * $qb = $conn->getQueryBuilder() |
|
133 | - * ->select('u') |
|
134 | - * ->from('users', 'u') |
|
135 | - * ->where($qb->fun()->md5('u.id')); |
|
136 | - * </code> |
|
137 | - * |
|
138 | - * For more complex function construction, consider storing the function |
|
139 | - * builder object in a local variable. |
|
140 | - * |
|
141 | - * @return \OCP\DB\QueryBuilder\IFunctionBuilder |
|
142 | - */ |
|
143 | - public function func() { |
|
144 | - if ($this->connection instanceof OracleConnection) { |
|
145 | - return new OCIFunctionBuilder($this->helper); |
|
146 | - } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
147 | - return new SqliteFunctionBuilder($this->helper); |
|
148 | - } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
149 | - return new PgSqlFunctionBuilder($this->helper); |
|
150 | - } else { |
|
151 | - return new FunctionBuilder($this->helper); |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Gets the type of the currently built query. |
|
157 | - * |
|
158 | - * @return integer |
|
159 | - */ |
|
160 | - public function getType() { |
|
161 | - return $this->queryBuilder->getType(); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Gets the associated DBAL Connection for this query builder. |
|
166 | - * |
|
167 | - * @return \OCP\IDBConnection |
|
168 | - */ |
|
169 | - public function getConnection() { |
|
170 | - return $this->connection; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Gets the state of this query builder instance. |
|
175 | - * |
|
176 | - * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. |
|
177 | - */ |
|
178 | - public function getState() { |
|
179 | - return $this->queryBuilder->getState(); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Executes this query using the bound parameters and their types. |
|
184 | - * |
|
185 | - * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} |
|
186 | - * for insert, update and delete statements. |
|
187 | - * |
|
188 | - * @return \Doctrine\DBAL\Driver\Statement|int |
|
189 | - */ |
|
190 | - public function execute() { |
|
191 | - if ($this->systemConfig->getValue('log_query', false)) { |
|
192 | - $params = []; |
|
193 | - foreach ($this->getParameters() as $placeholder => $value) { |
|
194 | - if (is_array($value)) { |
|
195 | - $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')'; |
|
196 | - } else { |
|
197 | - $params[] = $placeholder . ' => \'' . $value . '\''; |
|
198 | - } |
|
199 | - } |
|
200 | - if (empty($params)) { |
|
201 | - $this->logger->debug('DB QueryBuilder: \'{query}\'', [ |
|
202 | - 'query' => $this->getSQL(), |
|
203 | - 'app' => 'core', |
|
204 | - ]); |
|
205 | - } else { |
|
206 | - $this->logger->debug('DB QueryBuilder: \'{query}\' with parameters: {params}', [ |
|
207 | - 'query' => $this->getSQL(), |
|
208 | - 'params' => implode(', ', $params), |
|
209 | - 'app' => 'core', |
|
210 | - ]); |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - return $this->queryBuilder->execute(); |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Gets the complete SQL string formed by the current specifications of this QueryBuilder. |
|
219 | - * |
|
220 | - * <code> |
|
221 | - * $qb = $conn->getQueryBuilder() |
|
222 | - * ->select('u') |
|
223 | - * ->from('User', 'u') |
|
224 | - * echo $qb->getSQL(); // SELECT u FROM User u |
|
225 | - * </code> |
|
226 | - * |
|
227 | - * @return string The SQL query string. |
|
228 | - */ |
|
229 | - public function getSQL() { |
|
230 | - return $this->queryBuilder->getSQL(); |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Sets a query parameter for the query being constructed. |
|
235 | - * |
|
236 | - * <code> |
|
237 | - * $qb = $conn->getQueryBuilder() |
|
238 | - * ->select('u') |
|
239 | - * ->from('users', 'u') |
|
240 | - * ->where('u.id = :user_id') |
|
241 | - * ->setParameter(':user_id', 1); |
|
242 | - * </code> |
|
243 | - * |
|
244 | - * @param string|integer $key The parameter position or name. |
|
245 | - * @param mixed $value The parameter value. |
|
246 | - * @param string|null|int $type One of the IQueryBuilder::PARAM_* constants. |
|
247 | - * |
|
248 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
249 | - */ |
|
250 | - public function setParameter($key, $value, $type = null) { |
|
251 | - $this->queryBuilder->setParameter($key, $value, $type); |
|
252 | - |
|
253 | - return $this; |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Sets a collection of query parameters for the query being constructed. |
|
258 | - * |
|
259 | - * <code> |
|
260 | - * $qb = $conn->getQueryBuilder() |
|
261 | - * ->select('u') |
|
262 | - * ->from('users', 'u') |
|
263 | - * ->where('u.id = :user_id1 OR u.id = :user_id2') |
|
264 | - * ->setParameters(array( |
|
265 | - * ':user_id1' => 1, |
|
266 | - * ':user_id2' => 2 |
|
267 | - * )); |
|
268 | - * </code> |
|
269 | - * |
|
270 | - * @param array $params The query parameters to set. |
|
271 | - * @param array $types The query parameters types to set. |
|
272 | - * |
|
273 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
274 | - */ |
|
275 | - public function setParameters(array $params, array $types = array()) { |
|
276 | - $this->queryBuilder->setParameters($params, $types); |
|
277 | - |
|
278 | - return $this; |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * Gets all defined query parameters for the query being constructed indexed by parameter index or name. |
|
283 | - * |
|
284 | - * @return array The currently defined query parameters indexed by parameter index or name. |
|
285 | - */ |
|
286 | - public function getParameters() { |
|
287 | - return $this->queryBuilder->getParameters(); |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Gets a (previously set) query parameter of the query being constructed. |
|
292 | - * |
|
293 | - * @param mixed $key The key (index or name) of the bound parameter. |
|
294 | - * |
|
295 | - * @return mixed The value of the bound parameter. |
|
296 | - */ |
|
297 | - public function getParameter($key) { |
|
298 | - return $this->queryBuilder->getParameter($key); |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. |
|
303 | - * |
|
304 | - * @return array The currently defined query parameter types indexed by parameter index or name. |
|
305 | - */ |
|
306 | - public function getParameterTypes() { |
|
307 | - return $this->queryBuilder->getParameterTypes(); |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Gets a (previously set) query parameter type of the query being constructed. |
|
312 | - * |
|
313 | - * @param mixed $key The key (index or name) of the bound parameter type. |
|
314 | - * |
|
315 | - * @return mixed The value of the bound parameter type. |
|
316 | - */ |
|
317 | - public function getParameterType($key) { |
|
318 | - return $this->queryBuilder->getParameterType($key); |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * Sets the position of the first result to retrieve (the "offset"). |
|
323 | - * |
|
324 | - * @param integer $firstResult The first result to return. |
|
325 | - * |
|
326 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
327 | - */ |
|
328 | - public function setFirstResult($firstResult) { |
|
329 | - $this->queryBuilder->setFirstResult($firstResult); |
|
330 | - |
|
331 | - return $this; |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Gets the position of the first result the query object was set to retrieve (the "offset"). |
|
336 | - * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder. |
|
337 | - * |
|
338 | - * @return integer The position of the first result. |
|
339 | - */ |
|
340 | - public function getFirstResult() { |
|
341 | - return $this->queryBuilder->getFirstResult(); |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Sets the maximum number of results to retrieve (the "limit"). |
|
346 | - * |
|
347 | - * NOTE: Setting max results to "0" will cause mixed behaviour. While most |
|
348 | - * of the databases will just return an empty result set, Oracle will return |
|
349 | - * all entries. |
|
350 | - * |
|
351 | - * @param integer $maxResults The maximum number of results to retrieve. |
|
352 | - * |
|
353 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
354 | - */ |
|
355 | - public function setMaxResults($maxResults) { |
|
356 | - $this->queryBuilder->setMaxResults($maxResults); |
|
357 | - |
|
358 | - return $this; |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
|
363 | - * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
|
364 | - * |
|
365 | - * @return integer The maximum number of results. |
|
366 | - */ |
|
367 | - public function getMaxResults() { |
|
368 | - return $this->queryBuilder->getMaxResults(); |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * Specifies an item that is to be returned in the query result. |
|
373 | - * Replaces any previously specified selections, if any. |
|
374 | - * |
|
375 | - * <code> |
|
376 | - * $qb = $conn->getQueryBuilder() |
|
377 | - * ->select('u.id', 'p.id') |
|
378 | - * ->from('users', 'u') |
|
379 | - * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
380 | - * </code> |
|
381 | - * |
|
382 | - * @param mixed ...$selects The selection expressions. |
|
383 | - * |
|
384 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
385 | - */ |
|
386 | - public function select(...$selects) { |
|
387 | - if (count($selects) === 1 && is_array($selects[0])) { |
|
388 | - $selects = $selects[0]; |
|
389 | - } |
|
390 | - |
|
391 | - $this->queryBuilder->select( |
|
392 | - $this->helper->quoteColumnNames($selects) |
|
393 | - ); |
|
394 | - |
|
395 | - return $this; |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * Specifies an item that is to be returned with a different name in the query result. |
|
400 | - * |
|
401 | - * <code> |
|
402 | - * $qb = $conn->getQueryBuilder() |
|
403 | - * ->selectAlias('u.id', 'user_id') |
|
404 | - * ->from('users', 'u') |
|
405 | - * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
406 | - * </code> |
|
407 | - * |
|
408 | - * @param mixed $select The selection expressions. |
|
409 | - * @param string $alias The column alias used in the constructed query. |
|
410 | - * |
|
411 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
412 | - */ |
|
413 | - public function selectAlias($select, $alias) { |
|
414 | - |
|
415 | - $this->queryBuilder->addSelect( |
|
416 | - $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias) |
|
417 | - ); |
|
418 | - |
|
419 | - return $this; |
|
420 | - } |
|
421 | - |
|
422 | - /** |
|
423 | - * Specifies an item that is to be returned uniquely in the query result. |
|
424 | - * |
|
425 | - * <code> |
|
426 | - * $qb = $conn->getQueryBuilder() |
|
427 | - * ->selectDistinct('type') |
|
428 | - * ->from('users'); |
|
429 | - * </code> |
|
430 | - * |
|
431 | - * @param mixed $select The selection expressions. |
|
432 | - * |
|
433 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
434 | - */ |
|
435 | - public function selectDistinct($select) { |
|
436 | - |
|
437 | - $this->queryBuilder->addSelect( |
|
438 | - 'DISTINCT ' . $this->helper->quoteColumnName($select) |
|
439 | - ); |
|
440 | - |
|
441 | - return $this; |
|
442 | - } |
|
443 | - |
|
444 | - /** |
|
445 | - * Adds an item that is to be returned in the query result. |
|
446 | - * |
|
447 | - * <code> |
|
448 | - * $qb = $conn->getQueryBuilder() |
|
449 | - * ->select('u.id') |
|
450 | - * ->addSelect('p.id') |
|
451 | - * ->from('users', 'u') |
|
452 | - * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id'); |
|
453 | - * </code> |
|
454 | - * |
|
455 | - * @param mixed ...$selects The selection expression. |
|
456 | - * |
|
457 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
458 | - */ |
|
459 | - public function addSelect(...$selects) { |
|
460 | - if (count($selects) === 1 && is_array($selects[0])) { |
|
461 | - $selects = $selects[0]; |
|
462 | - } |
|
463 | - |
|
464 | - $this->queryBuilder->addSelect( |
|
465 | - $this->helper->quoteColumnNames($selects) |
|
466 | - ); |
|
467 | - |
|
468 | - return $this; |
|
469 | - } |
|
470 | - |
|
471 | - /** |
|
472 | - * Turns the query being built into a bulk delete query that ranges over |
|
473 | - * a certain table. |
|
474 | - * |
|
475 | - * <code> |
|
476 | - * $qb = $conn->getQueryBuilder() |
|
477 | - * ->delete('users', 'u') |
|
478 | - * ->where('u.id = :user_id'); |
|
479 | - * ->setParameter(':user_id', 1); |
|
480 | - * </code> |
|
481 | - * |
|
482 | - * @param string $delete The table whose rows are subject to the deletion. |
|
483 | - * @param string $alias The table alias used in the constructed query. |
|
484 | - * |
|
485 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
486 | - */ |
|
487 | - public function delete($delete = null, $alias = null) { |
|
488 | - $this->queryBuilder->delete( |
|
489 | - $this->getTableName($delete), |
|
490 | - $alias |
|
491 | - ); |
|
492 | - |
|
493 | - return $this; |
|
494 | - } |
|
495 | - |
|
496 | - /** |
|
497 | - * Turns the query being built into a bulk update query that ranges over |
|
498 | - * a certain table |
|
499 | - * |
|
500 | - * <code> |
|
501 | - * $qb = $conn->getQueryBuilder() |
|
502 | - * ->update('users', 'u') |
|
503 | - * ->set('u.password', md5('password')) |
|
504 | - * ->where('u.id = ?'); |
|
505 | - * </code> |
|
506 | - * |
|
507 | - * @param string $update The table whose rows are subject to the update. |
|
508 | - * @param string $alias The table alias used in the constructed query. |
|
509 | - * |
|
510 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
511 | - */ |
|
512 | - public function update($update = null, $alias = null) { |
|
513 | - $this->queryBuilder->update( |
|
514 | - $this->getTableName($update), |
|
515 | - $alias |
|
516 | - ); |
|
517 | - |
|
518 | - return $this; |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Turns the query being built into an insert query that inserts into |
|
523 | - * a certain table |
|
524 | - * |
|
525 | - * <code> |
|
526 | - * $qb = $conn->getQueryBuilder() |
|
527 | - * ->insert('users') |
|
528 | - * ->values( |
|
529 | - * array( |
|
530 | - * 'name' => '?', |
|
531 | - * 'password' => '?' |
|
532 | - * ) |
|
533 | - * ); |
|
534 | - * </code> |
|
535 | - * |
|
536 | - * @param string $insert The table into which the rows should be inserted. |
|
537 | - * |
|
538 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
539 | - */ |
|
540 | - public function insert($insert = null) { |
|
541 | - $this->queryBuilder->insert( |
|
542 | - $this->getTableName($insert) |
|
543 | - ); |
|
544 | - |
|
545 | - $this->lastInsertedTable = $insert; |
|
546 | - |
|
547 | - return $this; |
|
548 | - } |
|
549 | - |
|
550 | - /** |
|
551 | - * Creates and adds a query root corresponding to the table identified by the |
|
552 | - * given alias, forming a cartesian product with any existing query roots. |
|
553 | - * |
|
554 | - * <code> |
|
555 | - * $qb = $conn->getQueryBuilder() |
|
556 | - * ->select('u.id') |
|
557 | - * ->from('users', 'u') |
|
558 | - * </code> |
|
559 | - * |
|
560 | - * @param string $from The table. |
|
561 | - * @param string|null $alias The alias of the table. |
|
562 | - * |
|
563 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
564 | - */ |
|
565 | - public function from($from, $alias = null) { |
|
566 | - $this->queryBuilder->from( |
|
567 | - $this->getTableName($from), |
|
568 | - $this->quoteAlias($alias) |
|
569 | - ); |
|
570 | - |
|
571 | - return $this; |
|
572 | - } |
|
573 | - |
|
574 | - /** |
|
575 | - * Creates and adds a join to the query. |
|
576 | - * |
|
577 | - * <code> |
|
578 | - * $qb = $conn->getQueryBuilder() |
|
579 | - * ->select('u.name') |
|
580 | - * ->from('users', 'u') |
|
581 | - * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
582 | - * </code> |
|
583 | - * |
|
584 | - * @param string $fromAlias The alias that points to a from clause. |
|
585 | - * @param string $join The table name to join. |
|
586 | - * @param string $alias The alias of the join table. |
|
587 | - * @param string $condition The condition for the join. |
|
588 | - * |
|
589 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
590 | - */ |
|
591 | - public function join($fromAlias, $join, $alias, $condition = null) { |
|
592 | - $this->queryBuilder->join( |
|
593 | - $this->quoteAlias($fromAlias), |
|
594 | - $this->getTableName($join), |
|
595 | - $this->quoteAlias($alias), |
|
596 | - $condition |
|
597 | - ); |
|
598 | - |
|
599 | - return $this; |
|
600 | - } |
|
601 | - |
|
602 | - /** |
|
603 | - * Creates and adds a join to the query. |
|
604 | - * |
|
605 | - * <code> |
|
606 | - * $qb = $conn->getQueryBuilder() |
|
607 | - * ->select('u.name') |
|
608 | - * ->from('users', 'u') |
|
609 | - * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
610 | - * </code> |
|
611 | - * |
|
612 | - * @param string $fromAlias The alias that points to a from clause. |
|
613 | - * @param string $join The table name to join. |
|
614 | - * @param string $alias The alias of the join table. |
|
615 | - * @param string $condition The condition for the join. |
|
616 | - * |
|
617 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
618 | - */ |
|
619 | - public function innerJoin($fromAlias, $join, $alias, $condition = null) { |
|
620 | - $this->queryBuilder->innerJoin( |
|
621 | - $this->quoteAlias($fromAlias), |
|
622 | - $this->getTableName($join), |
|
623 | - $this->quoteAlias($alias), |
|
624 | - $condition |
|
625 | - ); |
|
626 | - |
|
627 | - return $this; |
|
628 | - } |
|
629 | - |
|
630 | - /** |
|
631 | - * Creates and adds a left join to the query. |
|
632 | - * |
|
633 | - * <code> |
|
634 | - * $qb = $conn->getQueryBuilder() |
|
635 | - * ->select('u.name') |
|
636 | - * ->from('users', 'u') |
|
637 | - * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
638 | - * </code> |
|
639 | - * |
|
640 | - * @param string $fromAlias The alias that points to a from clause. |
|
641 | - * @param string $join The table name to join. |
|
642 | - * @param string $alias The alias of the join table. |
|
643 | - * @param string $condition The condition for the join. |
|
644 | - * |
|
645 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
646 | - */ |
|
647 | - public function leftJoin($fromAlias, $join, $alias, $condition = null) { |
|
648 | - $this->queryBuilder->leftJoin( |
|
649 | - $this->quoteAlias($fromAlias), |
|
650 | - $this->getTableName($join), |
|
651 | - $this->quoteAlias($alias), |
|
652 | - $condition |
|
653 | - ); |
|
654 | - |
|
655 | - return $this; |
|
656 | - } |
|
657 | - |
|
658 | - /** |
|
659 | - * Creates and adds a right join to the query. |
|
660 | - * |
|
661 | - * <code> |
|
662 | - * $qb = $conn->getQueryBuilder() |
|
663 | - * ->select('u.name') |
|
664 | - * ->from('users', 'u') |
|
665 | - * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
666 | - * </code> |
|
667 | - * |
|
668 | - * @param string $fromAlias The alias that points to a from clause. |
|
669 | - * @param string $join The table name to join. |
|
670 | - * @param string $alias The alias of the join table. |
|
671 | - * @param string $condition The condition for the join. |
|
672 | - * |
|
673 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
674 | - */ |
|
675 | - public function rightJoin($fromAlias, $join, $alias, $condition = null) { |
|
676 | - $this->queryBuilder->rightJoin( |
|
677 | - $this->quoteAlias($fromAlias), |
|
678 | - $this->getTableName($join), |
|
679 | - $this->quoteAlias($alias), |
|
680 | - $condition |
|
681 | - ); |
|
682 | - |
|
683 | - return $this; |
|
684 | - } |
|
685 | - |
|
686 | - /** |
|
687 | - * Sets a new value for a column in a bulk update query. |
|
688 | - * |
|
689 | - * <code> |
|
690 | - * $qb = $conn->getQueryBuilder() |
|
691 | - * ->update('users', 'u') |
|
692 | - * ->set('u.password', md5('password')) |
|
693 | - * ->where('u.id = ?'); |
|
694 | - * </code> |
|
695 | - * |
|
696 | - * @param string $key The column to set. |
|
697 | - * @param string $value The value, expression, placeholder, etc. |
|
698 | - * |
|
699 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
700 | - */ |
|
701 | - public function set($key, $value) { |
|
702 | - $this->queryBuilder->set( |
|
703 | - $this->helper->quoteColumnName($key), |
|
704 | - $this->helper->quoteColumnName($value) |
|
705 | - ); |
|
706 | - |
|
707 | - return $this; |
|
708 | - } |
|
709 | - |
|
710 | - /** |
|
711 | - * Specifies one or more restrictions to the query result. |
|
712 | - * Replaces any previously specified restrictions, if any. |
|
713 | - * |
|
714 | - * <code> |
|
715 | - * $qb = $conn->getQueryBuilder() |
|
716 | - * ->select('u.name') |
|
717 | - * ->from('users', 'u') |
|
718 | - * ->where('u.id = ?'); |
|
719 | - * |
|
720 | - * // You can optionally programatically build and/or expressions |
|
721 | - * $qb = $conn->getQueryBuilder(); |
|
722 | - * |
|
723 | - * $or = $qb->expr()->orx(); |
|
724 | - * $or->add($qb->expr()->eq('u.id', 1)); |
|
725 | - * $or->add($qb->expr()->eq('u.id', 2)); |
|
726 | - * |
|
727 | - * $qb->update('users', 'u') |
|
728 | - * ->set('u.password', md5('password')) |
|
729 | - * ->where($or); |
|
730 | - * </code> |
|
731 | - * |
|
732 | - * @param mixed ...$predicates The restriction predicates. |
|
733 | - * |
|
734 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
735 | - */ |
|
736 | - public function where(...$predicates) { |
|
737 | - call_user_func_array( |
|
738 | - [$this->queryBuilder, 'where'], |
|
739 | - $predicates |
|
740 | - ); |
|
741 | - |
|
742 | - return $this; |
|
743 | - } |
|
744 | - |
|
745 | - /** |
|
746 | - * Adds one or more restrictions to the query results, forming a logical |
|
747 | - * conjunction with any previously specified restrictions. |
|
748 | - * |
|
749 | - * <code> |
|
750 | - * $qb = $conn->getQueryBuilder() |
|
751 | - * ->select('u') |
|
752 | - * ->from('users', 'u') |
|
753 | - * ->where('u.username LIKE ?') |
|
754 | - * ->andWhere('u.is_active = 1'); |
|
755 | - * </code> |
|
756 | - * |
|
757 | - * @param mixed ...$where The query restrictions. |
|
758 | - * |
|
759 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
760 | - * |
|
761 | - * @see where() |
|
762 | - */ |
|
763 | - public function andWhere(...$where) { |
|
764 | - call_user_func_array( |
|
765 | - [$this->queryBuilder, 'andWhere'], |
|
766 | - $where |
|
767 | - ); |
|
768 | - |
|
769 | - return $this; |
|
770 | - } |
|
771 | - |
|
772 | - /** |
|
773 | - * Adds one or more restrictions to the query results, forming a logical |
|
774 | - * disjunction with any previously specified restrictions. |
|
775 | - * |
|
776 | - * <code> |
|
777 | - * $qb = $conn->getQueryBuilder() |
|
778 | - * ->select('u.name') |
|
779 | - * ->from('users', 'u') |
|
780 | - * ->where('u.id = 1') |
|
781 | - * ->orWhere('u.id = 2'); |
|
782 | - * </code> |
|
783 | - * |
|
784 | - * @param mixed ...$where The WHERE statement. |
|
785 | - * |
|
786 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
787 | - * |
|
788 | - * @see where() |
|
789 | - */ |
|
790 | - public function orWhere(...$where) { |
|
791 | - call_user_func_array( |
|
792 | - [$this->queryBuilder, 'orWhere'], |
|
793 | - $where |
|
794 | - ); |
|
795 | - |
|
796 | - return $this; |
|
797 | - } |
|
798 | - |
|
799 | - /** |
|
800 | - * Specifies a grouping over the results of the query. |
|
801 | - * Replaces any previously specified groupings, if any. |
|
802 | - * |
|
803 | - * <code> |
|
804 | - * $qb = $conn->getQueryBuilder() |
|
805 | - * ->select('u.name') |
|
806 | - * ->from('users', 'u') |
|
807 | - * ->groupBy('u.id'); |
|
808 | - * </code> |
|
809 | - * |
|
810 | - * @param mixed ...$groupBys The grouping expression. |
|
811 | - * |
|
812 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
813 | - */ |
|
814 | - public function groupBy(...$groupBys) { |
|
815 | - if (count($groupBys) === 1 && is_array($groupBys[0])) { |
|
816 | - $groupBys = $groupBys[0]; |
|
817 | - } |
|
818 | - |
|
819 | - call_user_func_array( |
|
820 | - [$this->queryBuilder, 'groupBy'], |
|
821 | - $this->helper->quoteColumnNames($groupBys) |
|
822 | - ); |
|
823 | - |
|
824 | - return $this; |
|
825 | - } |
|
826 | - |
|
827 | - /** |
|
828 | - * Adds a grouping expression to the query. |
|
829 | - * |
|
830 | - * <code> |
|
831 | - * $qb = $conn->getQueryBuilder() |
|
832 | - * ->select('u.name') |
|
833 | - * ->from('users', 'u') |
|
834 | - * ->groupBy('u.lastLogin'); |
|
835 | - * ->addGroupBy('u.createdAt') |
|
836 | - * </code> |
|
837 | - * |
|
838 | - * @param mixed ...$groupBy The grouping expression. |
|
839 | - * |
|
840 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
841 | - */ |
|
842 | - public function addGroupBy(...$groupBys) { |
|
843 | - if (count($groupBys) === 1 && is_array($groupBys[0])) { |
|
844 | - $$groupBys = $groupBys[0]; |
|
845 | - } |
|
846 | - |
|
847 | - call_user_func_array( |
|
848 | - [$this->queryBuilder, 'addGroupBy'], |
|
849 | - $this->helper->quoteColumnNames($groupBys) |
|
850 | - ); |
|
851 | - |
|
852 | - return $this; |
|
853 | - } |
|
854 | - |
|
855 | - /** |
|
856 | - * Sets a value for a column in an insert query. |
|
857 | - * |
|
858 | - * <code> |
|
859 | - * $qb = $conn->getQueryBuilder() |
|
860 | - * ->insert('users') |
|
861 | - * ->values( |
|
862 | - * array( |
|
863 | - * 'name' => '?' |
|
864 | - * ) |
|
865 | - * ) |
|
866 | - * ->setValue('password', '?'); |
|
867 | - * </code> |
|
868 | - * |
|
869 | - * @param string $column The column into which the value should be inserted. |
|
870 | - * @param string $value The value that should be inserted into the column. |
|
871 | - * |
|
872 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
873 | - */ |
|
874 | - public function setValue($column, $value) { |
|
875 | - $this->queryBuilder->setValue( |
|
876 | - $this->helper->quoteColumnName($column), |
|
877 | - $value |
|
878 | - ); |
|
879 | - |
|
880 | - return $this; |
|
881 | - } |
|
882 | - |
|
883 | - /** |
|
884 | - * Specifies values for an insert query indexed by column names. |
|
885 | - * Replaces any previous values, if any. |
|
886 | - * |
|
887 | - * <code> |
|
888 | - * $qb = $conn->getQueryBuilder() |
|
889 | - * ->insert('users') |
|
890 | - * ->values( |
|
891 | - * array( |
|
892 | - * 'name' => '?', |
|
893 | - * 'password' => '?' |
|
894 | - * ) |
|
895 | - * ); |
|
896 | - * </code> |
|
897 | - * |
|
898 | - * @param array $values The values to specify for the insert query indexed by column names. |
|
899 | - * |
|
900 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
901 | - */ |
|
902 | - public function values(array $values) { |
|
903 | - $quotedValues = []; |
|
904 | - foreach ($values as $key => $value) { |
|
905 | - $quotedValues[$this->helper->quoteColumnName($key)] = $value; |
|
906 | - } |
|
907 | - |
|
908 | - $this->queryBuilder->values($quotedValues); |
|
909 | - |
|
910 | - return $this; |
|
911 | - } |
|
912 | - |
|
913 | - /** |
|
914 | - * Specifies a restriction over the groups of the query. |
|
915 | - * Replaces any previous having restrictions, if any. |
|
916 | - * |
|
917 | - * @param mixed ...$having The restriction over the groups. |
|
918 | - * |
|
919 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
920 | - */ |
|
921 | - public function having(...$having) { |
|
922 | - call_user_func_array( |
|
923 | - [$this->queryBuilder, 'having'], |
|
924 | - $having |
|
925 | - ); |
|
926 | - |
|
927 | - return $this; |
|
928 | - } |
|
929 | - |
|
930 | - /** |
|
931 | - * Adds a restriction over the groups of the query, forming a logical |
|
932 | - * conjunction with any existing having restrictions. |
|
933 | - * |
|
934 | - * @param mixed ...$having The restriction to append. |
|
935 | - * |
|
936 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
937 | - */ |
|
938 | - public function andHaving(...$having) { |
|
939 | - call_user_func_array( |
|
940 | - [$this->queryBuilder, 'andHaving'], |
|
941 | - $having |
|
942 | - ); |
|
943 | - |
|
944 | - return $this; |
|
945 | - } |
|
946 | - |
|
947 | - /** |
|
948 | - * Adds a restriction over the groups of the query, forming a logical |
|
949 | - * disjunction with any existing having restrictions. |
|
950 | - * |
|
951 | - * @param mixed ...$having The restriction to add. |
|
952 | - * |
|
953 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
954 | - */ |
|
955 | - public function orHaving(...$having) { |
|
956 | - call_user_func_array( |
|
957 | - [$this->queryBuilder, 'orHaving'], |
|
958 | - $having |
|
959 | - ); |
|
960 | - |
|
961 | - return $this; |
|
962 | - } |
|
963 | - |
|
964 | - /** |
|
965 | - * Specifies an ordering for the query results. |
|
966 | - * Replaces any previously specified orderings, if any. |
|
967 | - * |
|
968 | - * @param string $sort The ordering expression. |
|
969 | - * @param string $order The ordering direction. |
|
970 | - * |
|
971 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
972 | - */ |
|
973 | - public function orderBy($sort, $order = null) { |
|
974 | - $this->queryBuilder->orderBy( |
|
975 | - $this->helper->quoteColumnName($sort), |
|
976 | - $order |
|
977 | - ); |
|
978 | - |
|
979 | - return $this; |
|
980 | - } |
|
981 | - |
|
982 | - /** |
|
983 | - * Adds an ordering to the query results. |
|
984 | - * |
|
985 | - * @param string $sort The ordering expression. |
|
986 | - * @param string $order The ordering direction. |
|
987 | - * |
|
988 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
989 | - */ |
|
990 | - public function addOrderBy($sort, $order = null) { |
|
991 | - $this->queryBuilder->addOrderBy( |
|
992 | - $this->helper->quoteColumnName($sort), |
|
993 | - $order |
|
994 | - ); |
|
995 | - |
|
996 | - return $this; |
|
997 | - } |
|
998 | - |
|
999 | - /** |
|
1000 | - * Gets a query part by its name. |
|
1001 | - * |
|
1002 | - * @param string $queryPartName |
|
1003 | - * |
|
1004 | - * @return mixed |
|
1005 | - */ |
|
1006 | - public function getQueryPart($queryPartName) { |
|
1007 | - return $this->queryBuilder->getQueryPart($queryPartName); |
|
1008 | - } |
|
1009 | - |
|
1010 | - /** |
|
1011 | - * Gets all query parts. |
|
1012 | - * |
|
1013 | - * @return array |
|
1014 | - */ |
|
1015 | - public function getQueryParts() { |
|
1016 | - return $this->queryBuilder->getQueryParts(); |
|
1017 | - } |
|
1018 | - |
|
1019 | - /** |
|
1020 | - * Resets SQL parts. |
|
1021 | - * |
|
1022 | - * @param array|null $queryPartNames |
|
1023 | - * |
|
1024 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
1025 | - */ |
|
1026 | - public function resetQueryParts($queryPartNames = null) { |
|
1027 | - $this->queryBuilder->resetQueryParts($queryPartNames); |
|
1028 | - |
|
1029 | - return $this; |
|
1030 | - } |
|
1031 | - |
|
1032 | - /** |
|
1033 | - * Resets a single SQL part. |
|
1034 | - * |
|
1035 | - * @param string $queryPartName |
|
1036 | - * |
|
1037 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
1038 | - */ |
|
1039 | - public function resetQueryPart($queryPartName) { |
|
1040 | - $this->queryBuilder->resetQueryPart($queryPartName); |
|
1041 | - |
|
1042 | - return $this; |
|
1043 | - } |
|
1044 | - |
|
1045 | - /** |
|
1046 | - * Creates a new named parameter and bind the value $value to it. |
|
1047 | - * |
|
1048 | - * This method provides a shortcut for PDOStatement::bindValue |
|
1049 | - * when using prepared statements. |
|
1050 | - * |
|
1051 | - * The parameter $value specifies the value that you want to bind. If |
|
1052 | - * $placeholder is not provided bindValue() will automatically create a |
|
1053 | - * placeholder for you. An automatic placeholder will be of the name |
|
1054 | - * ':dcValue1', ':dcValue2' etc. |
|
1055 | - * |
|
1056 | - * For more information see {@link http://php.net/pdostatement-bindparam} |
|
1057 | - * |
|
1058 | - * Example: |
|
1059 | - * <code> |
|
1060 | - * $value = 2; |
|
1061 | - * $q->eq( 'id', $q->bindValue( $value ) ); |
|
1062 | - * $stmt = $q->executeQuery(); // executed with 'id = 2' |
|
1063 | - * </code> |
|
1064 | - * |
|
1065 | - * @license New BSD License |
|
1066 | - * @link http://www.zetacomponents.org |
|
1067 | - * |
|
1068 | - * @param mixed $value |
|
1069 | - * @param mixed $type |
|
1070 | - * @param string $placeHolder The name to bind with. The string must start with a colon ':'. |
|
1071 | - * |
|
1072 | - * @return IParameter the placeholder name used. |
|
1073 | - */ |
|
1074 | - public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) { |
|
1075 | - return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder)); |
|
1076 | - } |
|
1077 | - |
|
1078 | - /** |
|
1079 | - * Creates a new positional parameter and bind the given value to it. |
|
1080 | - * |
|
1081 | - * Attention: If you are using positional parameters with the query builder you have |
|
1082 | - * to be very careful to bind all parameters in the order they appear in the SQL |
|
1083 | - * statement , otherwise they get bound in the wrong order which can lead to serious |
|
1084 | - * bugs in your code. |
|
1085 | - * |
|
1086 | - * Example: |
|
1087 | - * <code> |
|
1088 | - * $qb = $conn->getQueryBuilder(); |
|
1089 | - * $qb->select('u.*') |
|
1090 | - * ->from('users', 'u') |
|
1091 | - * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR)) |
|
1092 | - * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR)) |
|
1093 | - * </code> |
|
1094 | - * |
|
1095 | - * @param mixed $value |
|
1096 | - * @param integer $type |
|
1097 | - * |
|
1098 | - * @return IParameter |
|
1099 | - */ |
|
1100 | - public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) { |
|
1101 | - return new Parameter($this->queryBuilder->createPositionalParameter($value, $type)); |
|
1102 | - } |
|
1103 | - |
|
1104 | - /** |
|
1105 | - * Creates a new parameter |
|
1106 | - * |
|
1107 | - * Example: |
|
1108 | - * <code> |
|
1109 | - * $qb = $conn->getQueryBuilder(); |
|
1110 | - * $qb->select('u.*') |
|
1111 | - * ->from('users', 'u') |
|
1112 | - * ->where('u.username = ' . $qb->createParameter('name')) |
|
1113 | - * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR)) |
|
1114 | - * </code> |
|
1115 | - * |
|
1116 | - * @param string $name |
|
1117 | - * |
|
1118 | - * @return IParameter |
|
1119 | - */ |
|
1120 | - public function createParameter($name) { |
|
1121 | - return new Parameter(':' . $name); |
|
1122 | - } |
|
1123 | - |
|
1124 | - /** |
|
1125 | - * Creates a new function |
|
1126 | - * |
|
1127 | - * Attention: Column names inside the call have to be quoted before hand |
|
1128 | - * |
|
1129 | - * Example: |
|
1130 | - * <code> |
|
1131 | - * $qb = $conn->getQueryBuilder(); |
|
1132 | - * $qb->select($qb->createFunction('COUNT(*)')) |
|
1133 | - * ->from('users', 'u') |
|
1134 | - * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u |
|
1135 | - * </code> |
|
1136 | - * <code> |
|
1137 | - * $qb = $conn->getQueryBuilder(); |
|
1138 | - * $qb->select($qb->createFunction('COUNT(`column`)')) |
|
1139 | - * ->from('users', 'u') |
|
1140 | - * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u |
|
1141 | - * </code> |
|
1142 | - * |
|
1143 | - * @param string $call |
|
1144 | - * |
|
1145 | - * @return IQueryFunction |
|
1146 | - */ |
|
1147 | - public function createFunction($call) { |
|
1148 | - return new QueryFunction($call); |
|
1149 | - } |
|
1150 | - |
|
1151 | - /** |
|
1152 | - * Used to get the id of the last inserted element |
|
1153 | - * @return int |
|
1154 | - * @throws \BadMethodCallException When being called before an insert query has been run. |
|
1155 | - */ |
|
1156 | - public function getLastInsertId() { |
|
1157 | - if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) { |
|
1158 | - // lastInsertId() needs the prefix but no quotes |
|
1159 | - $table = $this->prefixTableName($this->lastInsertedTable); |
|
1160 | - return (int) $this->connection->lastInsertId($table); |
|
1161 | - } |
|
1162 | - |
|
1163 | - throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.'); |
|
1164 | - } |
|
1165 | - |
|
1166 | - /** |
|
1167 | - * Returns the table name quoted and with database prefix as needed by the implementation |
|
1168 | - * |
|
1169 | - * @param string $table |
|
1170 | - * @return string |
|
1171 | - */ |
|
1172 | - public function getTableName($table) { |
|
1173 | - $table = $this->prefixTableName($table); |
|
1174 | - return $this->helper->quoteColumnName($table); |
|
1175 | - } |
|
1176 | - |
|
1177 | - /** |
|
1178 | - * Returns the table name with database prefix as needed by the implementation |
|
1179 | - * |
|
1180 | - * @param string $table |
|
1181 | - * @return string |
|
1182 | - */ |
|
1183 | - protected function prefixTableName($table) { |
|
1184 | - if ($this->automaticTablePrefix === false || strpos($table, '*PREFIX*') === 0) { |
|
1185 | - return $table; |
|
1186 | - } |
|
1187 | - |
|
1188 | - return '*PREFIX*' . $table; |
|
1189 | - } |
|
1190 | - |
|
1191 | - /** |
|
1192 | - * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
1193 | - * |
|
1194 | - * @param string $column |
|
1195 | - * @param string $tableAlias |
|
1196 | - * @return string |
|
1197 | - */ |
|
1198 | - public function getColumnName($column, $tableAlias = '') { |
|
1199 | - if ($tableAlias !== '') { |
|
1200 | - $tableAlias .= '.'; |
|
1201 | - } |
|
1202 | - |
|
1203 | - return $this->helper->quoteColumnName($tableAlias . $column); |
|
1204 | - } |
|
1205 | - |
|
1206 | - /** |
|
1207 | - * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
1208 | - * |
|
1209 | - * @param string $alias |
|
1210 | - * @return string |
|
1211 | - */ |
|
1212 | - public function quoteAlias($alias) { |
|
1213 | - if ($alias === '' || $alias === null) { |
|
1214 | - return $alias; |
|
1215 | - } |
|
1216 | - |
|
1217 | - return $this->helper->quoteColumnName($alias); |
|
1218 | - } |
|
50 | + /** @var \OCP\IDBConnection */ |
|
51 | + private $connection; |
|
52 | + |
|
53 | + /** @var SystemConfig */ |
|
54 | + private $systemConfig; |
|
55 | + |
|
56 | + /** @var ILogger */ |
|
57 | + private $logger; |
|
58 | + |
|
59 | + /** @var \Doctrine\DBAL\Query\QueryBuilder */ |
|
60 | + private $queryBuilder; |
|
61 | + |
|
62 | + /** @var QuoteHelper */ |
|
63 | + private $helper; |
|
64 | + |
|
65 | + /** @var bool */ |
|
66 | + private $automaticTablePrefix = true; |
|
67 | + |
|
68 | + /** @var string */ |
|
69 | + protected $lastInsertedTable; |
|
70 | + |
|
71 | + /** |
|
72 | + * Initializes a new QueryBuilder. |
|
73 | + * |
|
74 | + * @param IDBConnection $connection |
|
75 | + * @param SystemConfig $systemConfig |
|
76 | + * @param ILogger $logger |
|
77 | + */ |
|
78 | + public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger) { |
|
79 | + $this->connection = $connection; |
|
80 | + $this->systemConfig = $systemConfig; |
|
81 | + $this->logger = $logger; |
|
82 | + $this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection); |
|
83 | + $this->helper = new QuoteHelper(); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Enable/disable automatic prefixing of table names with the oc_ prefix |
|
88 | + * |
|
89 | + * @param bool $enabled If set to true table names will be prefixed with the |
|
90 | + * owncloud database prefix automatically. |
|
91 | + * @since 8.2.0 |
|
92 | + */ |
|
93 | + public function automaticTablePrefix($enabled) { |
|
94 | + $this->automaticTablePrefix = (bool) $enabled; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
|
99 | + * This producer method is intended for convenient inline usage. Example: |
|
100 | + * |
|
101 | + * <code> |
|
102 | + * $qb = $conn->getQueryBuilder() |
|
103 | + * ->select('u') |
|
104 | + * ->from('users', 'u') |
|
105 | + * ->where($qb->expr()->eq('u.id', 1)); |
|
106 | + * </code> |
|
107 | + * |
|
108 | + * For more complex expression construction, consider storing the expression |
|
109 | + * builder object in a local variable. |
|
110 | + * |
|
111 | + * @return \OCP\DB\QueryBuilder\IExpressionBuilder |
|
112 | + */ |
|
113 | + public function expr() { |
|
114 | + if ($this->connection instanceof OracleConnection) { |
|
115 | + return new OCIExpressionBuilder($this->connection, $this); |
|
116 | + } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
117 | + return new PgSqlExpressionBuilder($this->connection, $this); |
|
118 | + } else if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
119 | + return new MySqlExpressionBuilder($this->connection, $this); |
|
120 | + } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
121 | + return new SqliteExpressionBuilder($this->connection, $this); |
|
122 | + } else { |
|
123 | + return new ExpressionBuilder($this->connection, $this); |
|
124 | + } |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Gets an FunctionBuilder used for object-oriented construction of query functions. |
|
129 | + * This producer method is intended for convenient inline usage. Example: |
|
130 | + * |
|
131 | + * <code> |
|
132 | + * $qb = $conn->getQueryBuilder() |
|
133 | + * ->select('u') |
|
134 | + * ->from('users', 'u') |
|
135 | + * ->where($qb->fun()->md5('u.id')); |
|
136 | + * </code> |
|
137 | + * |
|
138 | + * For more complex function construction, consider storing the function |
|
139 | + * builder object in a local variable. |
|
140 | + * |
|
141 | + * @return \OCP\DB\QueryBuilder\IFunctionBuilder |
|
142 | + */ |
|
143 | + public function func() { |
|
144 | + if ($this->connection instanceof OracleConnection) { |
|
145 | + return new OCIFunctionBuilder($this->helper); |
|
146 | + } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
147 | + return new SqliteFunctionBuilder($this->helper); |
|
148 | + } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
149 | + return new PgSqlFunctionBuilder($this->helper); |
|
150 | + } else { |
|
151 | + return new FunctionBuilder($this->helper); |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Gets the type of the currently built query. |
|
157 | + * |
|
158 | + * @return integer |
|
159 | + */ |
|
160 | + public function getType() { |
|
161 | + return $this->queryBuilder->getType(); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Gets the associated DBAL Connection for this query builder. |
|
166 | + * |
|
167 | + * @return \OCP\IDBConnection |
|
168 | + */ |
|
169 | + public function getConnection() { |
|
170 | + return $this->connection; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Gets the state of this query builder instance. |
|
175 | + * |
|
176 | + * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. |
|
177 | + */ |
|
178 | + public function getState() { |
|
179 | + return $this->queryBuilder->getState(); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Executes this query using the bound parameters and their types. |
|
184 | + * |
|
185 | + * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} |
|
186 | + * for insert, update and delete statements. |
|
187 | + * |
|
188 | + * @return \Doctrine\DBAL\Driver\Statement|int |
|
189 | + */ |
|
190 | + public function execute() { |
|
191 | + if ($this->systemConfig->getValue('log_query', false)) { |
|
192 | + $params = []; |
|
193 | + foreach ($this->getParameters() as $placeholder => $value) { |
|
194 | + if (is_array($value)) { |
|
195 | + $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')'; |
|
196 | + } else { |
|
197 | + $params[] = $placeholder . ' => \'' . $value . '\''; |
|
198 | + } |
|
199 | + } |
|
200 | + if (empty($params)) { |
|
201 | + $this->logger->debug('DB QueryBuilder: \'{query}\'', [ |
|
202 | + 'query' => $this->getSQL(), |
|
203 | + 'app' => 'core', |
|
204 | + ]); |
|
205 | + } else { |
|
206 | + $this->logger->debug('DB QueryBuilder: \'{query}\' with parameters: {params}', [ |
|
207 | + 'query' => $this->getSQL(), |
|
208 | + 'params' => implode(', ', $params), |
|
209 | + 'app' => 'core', |
|
210 | + ]); |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + return $this->queryBuilder->execute(); |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Gets the complete SQL string formed by the current specifications of this QueryBuilder. |
|
219 | + * |
|
220 | + * <code> |
|
221 | + * $qb = $conn->getQueryBuilder() |
|
222 | + * ->select('u') |
|
223 | + * ->from('User', 'u') |
|
224 | + * echo $qb->getSQL(); // SELECT u FROM User u |
|
225 | + * </code> |
|
226 | + * |
|
227 | + * @return string The SQL query string. |
|
228 | + */ |
|
229 | + public function getSQL() { |
|
230 | + return $this->queryBuilder->getSQL(); |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Sets a query parameter for the query being constructed. |
|
235 | + * |
|
236 | + * <code> |
|
237 | + * $qb = $conn->getQueryBuilder() |
|
238 | + * ->select('u') |
|
239 | + * ->from('users', 'u') |
|
240 | + * ->where('u.id = :user_id') |
|
241 | + * ->setParameter(':user_id', 1); |
|
242 | + * </code> |
|
243 | + * |
|
244 | + * @param string|integer $key The parameter position or name. |
|
245 | + * @param mixed $value The parameter value. |
|
246 | + * @param string|null|int $type One of the IQueryBuilder::PARAM_* constants. |
|
247 | + * |
|
248 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
249 | + */ |
|
250 | + public function setParameter($key, $value, $type = null) { |
|
251 | + $this->queryBuilder->setParameter($key, $value, $type); |
|
252 | + |
|
253 | + return $this; |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Sets a collection of query parameters for the query being constructed. |
|
258 | + * |
|
259 | + * <code> |
|
260 | + * $qb = $conn->getQueryBuilder() |
|
261 | + * ->select('u') |
|
262 | + * ->from('users', 'u') |
|
263 | + * ->where('u.id = :user_id1 OR u.id = :user_id2') |
|
264 | + * ->setParameters(array( |
|
265 | + * ':user_id1' => 1, |
|
266 | + * ':user_id2' => 2 |
|
267 | + * )); |
|
268 | + * </code> |
|
269 | + * |
|
270 | + * @param array $params The query parameters to set. |
|
271 | + * @param array $types The query parameters types to set. |
|
272 | + * |
|
273 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
274 | + */ |
|
275 | + public function setParameters(array $params, array $types = array()) { |
|
276 | + $this->queryBuilder->setParameters($params, $types); |
|
277 | + |
|
278 | + return $this; |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * Gets all defined query parameters for the query being constructed indexed by parameter index or name. |
|
283 | + * |
|
284 | + * @return array The currently defined query parameters indexed by parameter index or name. |
|
285 | + */ |
|
286 | + public function getParameters() { |
|
287 | + return $this->queryBuilder->getParameters(); |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Gets a (previously set) query parameter of the query being constructed. |
|
292 | + * |
|
293 | + * @param mixed $key The key (index or name) of the bound parameter. |
|
294 | + * |
|
295 | + * @return mixed The value of the bound parameter. |
|
296 | + */ |
|
297 | + public function getParameter($key) { |
|
298 | + return $this->queryBuilder->getParameter($key); |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. |
|
303 | + * |
|
304 | + * @return array The currently defined query parameter types indexed by parameter index or name. |
|
305 | + */ |
|
306 | + public function getParameterTypes() { |
|
307 | + return $this->queryBuilder->getParameterTypes(); |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Gets a (previously set) query parameter type of the query being constructed. |
|
312 | + * |
|
313 | + * @param mixed $key The key (index or name) of the bound parameter type. |
|
314 | + * |
|
315 | + * @return mixed The value of the bound parameter type. |
|
316 | + */ |
|
317 | + public function getParameterType($key) { |
|
318 | + return $this->queryBuilder->getParameterType($key); |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * Sets the position of the first result to retrieve (the "offset"). |
|
323 | + * |
|
324 | + * @param integer $firstResult The first result to return. |
|
325 | + * |
|
326 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
327 | + */ |
|
328 | + public function setFirstResult($firstResult) { |
|
329 | + $this->queryBuilder->setFirstResult($firstResult); |
|
330 | + |
|
331 | + return $this; |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Gets the position of the first result the query object was set to retrieve (the "offset"). |
|
336 | + * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder. |
|
337 | + * |
|
338 | + * @return integer The position of the first result. |
|
339 | + */ |
|
340 | + public function getFirstResult() { |
|
341 | + return $this->queryBuilder->getFirstResult(); |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Sets the maximum number of results to retrieve (the "limit"). |
|
346 | + * |
|
347 | + * NOTE: Setting max results to "0" will cause mixed behaviour. While most |
|
348 | + * of the databases will just return an empty result set, Oracle will return |
|
349 | + * all entries. |
|
350 | + * |
|
351 | + * @param integer $maxResults The maximum number of results to retrieve. |
|
352 | + * |
|
353 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
354 | + */ |
|
355 | + public function setMaxResults($maxResults) { |
|
356 | + $this->queryBuilder->setMaxResults($maxResults); |
|
357 | + |
|
358 | + return $this; |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
|
363 | + * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
|
364 | + * |
|
365 | + * @return integer The maximum number of results. |
|
366 | + */ |
|
367 | + public function getMaxResults() { |
|
368 | + return $this->queryBuilder->getMaxResults(); |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * Specifies an item that is to be returned in the query result. |
|
373 | + * Replaces any previously specified selections, if any. |
|
374 | + * |
|
375 | + * <code> |
|
376 | + * $qb = $conn->getQueryBuilder() |
|
377 | + * ->select('u.id', 'p.id') |
|
378 | + * ->from('users', 'u') |
|
379 | + * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
380 | + * </code> |
|
381 | + * |
|
382 | + * @param mixed ...$selects The selection expressions. |
|
383 | + * |
|
384 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
385 | + */ |
|
386 | + public function select(...$selects) { |
|
387 | + if (count($selects) === 1 && is_array($selects[0])) { |
|
388 | + $selects = $selects[0]; |
|
389 | + } |
|
390 | + |
|
391 | + $this->queryBuilder->select( |
|
392 | + $this->helper->quoteColumnNames($selects) |
|
393 | + ); |
|
394 | + |
|
395 | + return $this; |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * Specifies an item that is to be returned with a different name in the query result. |
|
400 | + * |
|
401 | + * <code> |
|
402 | + * $qb = $conn->getQueryBuilder() |
|
403 | + * ->selectAlias('u.id', 'user_id') |
|
404 | + * ->from('users', 'u') |
|
405 | + * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
406 | + * </code> |
|
407 | + * |
|
408 | + * @param mixed $select The selection expressions. |
|
409 | + * @param string $alias The column alias used in the constructed query. |
|
410 | + * |
|
411 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
412 | + */ |
|
413 | + public function selectAlias($select, $alias) { |
|
414 | + |
|
415 | + $this->queryBuilder->addSelect( |
|
416 | + $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias) |
|
417 | + ); |
|
418 | + |
|
419 | + return $this; |
|
420 | + } |
|
421 | + |
|
422 | + /** |
|
423 | + * Specifies an item that is to be returned uniquely in the query result. |
|
424 | + * |
|
425 | + * <code> |
|
426 | + * $qb = $conn->getQueryBuilder() |
|
427 | + * ->selectDistinct('type') |
|
428 | + * ->from('users'); |
|
429 | + * </code> |
|
430 | + * |
|
431 | + * @param mixed $select The selection expressions. |
|
432 | + * |
|
433 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
434 | + */ |
|
435 | + public function selectDistinct($select) { |
|
436 | + |
|
437 | + $this->queryBuilder->addSelect( |
|
438 | + 'DISTINCT ' . $this->helper->quoteColumnName($select) |
|
439 | + ); |
|
440 | + |
|
441 | + return $this; |
|
442 | + } |
|
443 | + |
|
444 | + /** |
|
445 | + * Adds an item that is to be returned in the query result. |
|
446 | + * |
|
447 | + * <code> |
|
448 | + * $qb = $conn->getQueryBuilder() |
|
449 | + * ->select('u.id') |
|
450 | + * ->addSelect('p.id') |
|
451 | + * ->from('users', 'u') |
|
452 | + * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id'); |
|
453 | + * </code> |
|
454 | + * |
|
455 | + * @param mixed ...$selects The selection expression. |
|
456 | + * |
|
457 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
458 | + */ |
|
459 | + public function addSelect(...$selects) { |
|
460 | + if (count($selects) === 1 && is_array($selects[0])) { |
|
461 | + $selects = $selects[0]; |
|
462 | + } |
|
463 | + |
|
464 | + $this->queryBuilder->addSelect( |
|
465 | + $this->helper->quoteColumnNames($selects) |
|
466 | + ); |
|
467 | + |
|
468 | + return $this; |
|
469 | + } |
|
470 | + |
|
471 | + /** |
|
472 | + * Turns the query being built into a bulk delete query that ranges over |
|
473 | + * a certain table. |
|
474 | + * |
|
475 | + * <code> |
|
476 | + * $qb = $conn->getQueryBuilder() |
|
477 | + * ->delete('users', 'u') |
|
478 | + * ->where('u.id = :user_id'); |
|
479 | + * ->setParameter(':user_id', 1); |
|
480 | + * </code> |
|
481 | + * |
|
482 | + * @param string $delete The table whose rows are subject to the deletion. |
|
483 | + * @param string $alias The table alias used in the constructed query. |
|
484 | + * |
|
485 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
486 | + */ |
|
487 | + public function delete($delete = null, $alias = null) { |
|
488 | + $this->queryBuilder->delete( |
|
489 | + $this->getTableName($delete), |
|
490 | + $alias |
|
491 | + ); |
|
492 | + |
|
493 | + return $this; |
|
494 | + } |
|
495 | + |
|
496 | + /** |
|
497 | + * Turns the query being built into a bulk update query that ranges over |
|
498 | + * a certain table |
|
499 | + * |
|
500 | + * <code> |
|
501 | + * $qb = $conn->getQueryBuilder() |
|
502 | + * ->update('users', 'u') |
|
503 | + * ->set('u.password', md5('password')) |
|
504 | + * ->where('u.id = ?'); |
|
505 | + * </code> |
|
506 | + * |
|
507 | + * @param string $update The table whose rows are subject to the update. |
|
508 | + * @param string $alias The table alias used in the constructed query. |
|
509 | + * |
|
510 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
511 | + */ |
|
512 | + public function update($update = null, $alias = null) { |
|
513 | + $this->queryBuilder->update( |
|
514 | + $this->getTableName($update), |
|
515 | + $alias |
|
516 | + ); |
|
517 | + |
|
518 | + return $this; |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Turns the query being built into an insert query that inserts into |
|
523 | + * a certain table |
|
524 | + * |
|
525 | + * <code> |
|
526 | + * $qb = $conn->getQueryBuilder() |
|
527 | + * ->insert('users') |
|
528 | + * ->values( |
|
529 | + * array( |
|
530 | + * 'name' => '?', |
|
531 | + * 'password' => '?' |
|
532 | + * ) |
|
533 | + * ); |
|
534 | + * </code> |
|
535 | + * |
|
536 | + * @param string $insert The table into which the rows should be inserted. |
|
537 | + * |
|
538 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
539 | + */ |
|
540 | + public function insert($insert = null) { |
|
541 | + $this->queryBuilder->insert( |
|
542 | + $this->getTableName($insert) |
|
543 | + ); |
|
544 | + |
|
545 | + $this->lastInsertedTable = $insert; |
|
546 | + |
|
547 | + return $this; |
|
548 | + } |
|
549 | + |
|
550 | + /** |
|
551 | + * Creates and adds a query root corresponding to the table identified by the |
|
552 | + * given alias, forming a cartesian product with any existing query roots. |
|
553 | + * |
|
554 | + * <code> |
|
555 | + * $qb = $conn->getQueryBuilder() |
|
556 | + * ->select('u.id') |
|
557 | + * ->from('users', 'u') |
|
558 | + * </code> |
|
559 | + * |
|
560 | + * @param string $from The table. |
|
561 | + * @param string|null $alias The alias of the table. |
|
562 | + * |
|
563 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
564 | + */ |
|
565 | + public function from($from, $alias = null) { |
|
566 | + $this->queryBuilder->from( |
|
567 | + $this->getTableName($from), |
|
568 | + $this->quoteAlias($alias) |
|
569 | + ); |
|
570 | + |
|
571 | + return $this; |
|
572 | + } |
|
573 | + |
|
574 | + /** |
|
575 | + * Creates and adds a join to the query. |
|
576 | + * |
|
577 | + * <code> |
|
578 | + * $qb = $conn->getQueryBuilder() |
|
579 | + * ->select('u.name') |
|
580 | + * ->from('users', 'u') |
|
581 | + * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
582 | + * </code> |
|
583 | + * |
|
584 | + * @param string $fromAlias The alias that points to a from clause. |
|
585 | + * @param string $join The table name to join. |
|
586 | + * @param string $alias The alias of the join table. |
|
587 | + * @param string $condition The condition for the join. |
|
588 | + * |
|
589 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
590 | + */ |
|
591 | + public function join($fromAlias, $join, $alias, $condition = null) { |
|
592 | + $this->queryBuilder->join( |
|
593 | + $this->quoteAlias($fromAlias), |
|
594 | + $this->getTableName($join), |
|
595 | + $this->quoteAlias($alias), |
|
596 | + $condition |
|
597 | + ); |
|
598 | + |
|
599 | + return $this; |
|
600 | + } |
|
601 | + |
|
602 | + /** |
|
603 | + * Creates and adds a join to the query. |
|
604 | + * |
|
605 | + * <code> |
|
606 | + * $qb = $conn->getQueryBuilder() |
|
607 | + * ->select('u.name') |
|
608 | + * ->from('users', 'u') |
|
609 | + * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
610 | + * </code> |
|
611 | + * |
|
612 | + * @param string $fromAlias The alias that points to a from clause. |
|
613 | + * @param string $join The table name to join. |
|
614 | + * @param string $alias The alias of the join table. |
|
615 | + * @param string $condition The condition for the join. |
|
616 | + * |
|
617 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
618 | + */ |
|
619 | + public function innerJoin($fromAlias, $join, $alias, $condition = null) { |
|
620 | + $this->queryBuilder->innerJoin( |
|
621 | + $this->quoteAlias($fromAlias), |
|
622 | + $this->getTableName($join), |
|
623 | + $this->quoteAlias($alias), |
|
624 | + $condition |
|
625 | + ); |
|
626 | + |
|
627 | + return $this; |
|
628 | + } |
|
629 | + |
|
630 | + /** |
|
631 | + * Creates and adds a left join to the query. |
|
632 | + * |
|
633 | + * <code> |
|
634 | + * $qb = $conn->getQueryBuilder() |
|
635 | + * ->select('u.name') |
|
636 | + * ->from('users', 'u') |
|
637 | + * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
638 | + * </code> |
|
639 | + * |
|
640 | + * @param string $fromAlias The alias that points to a from clause. |
|
641 | + * @param string $join The table name to join. |
|
642 | + * @param string $alias The alias of the join table. |
|
643 | + * @param string $condition The condition for the join. |
|
644 | + * |
|
645 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
646 | + */ |
|
647 | + public function leftJoin($fromAlias, $join, $alias, $condition = null) { |
|
648 | + $this->queryBuilder->leftJoin( |
|
649 | + $this->quoteAlias($fromAlias), |
|
650 | + $this->getTableName($join), |
|
651 | + $this->quoteAlias($alias), |
|
652 | + $condition |
|
653 | + ); |
|
654 | + |
|
655 | + return $this; |
|
656 | + } |
|
657 | + |
|
658 | + /** |
|
659 | + * Creates and adds a right join to the query. |
|
660 | + * |
|
661 | + * <code> |
|
662 | + * $qb = $conn->getQueryBuilder() |
|
663 | + * ->select('u.name') |
|
664 | + * ->from('users', 'u') |
|
665 | + * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
666 | + * </code> |
|
667 | + * |
|
668 | + * @param string $fromAlias The alias that points to a from clause. |
|
669 | + * @param string $join The table name to join. |
|
670 | + * @param string $alias The alias of the join table. |
|
671 | + * @param string $condition The condition for the join. |
|
672 | + * |
|
673 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
674 | + */ |
|
675 | + public function rightJoin($fromAlias, $join, $alias, $condition = null) { |
|
676 | + $this->queryBuilder->rightJoin( |
|
677 | + $this->quoteAlias($fromAlias), |
|
678 | + $this->getTableName($join), |
|
679 | + $this->quoteAlias($alias), |
|
680 | + $condition |
|
681 | + ); |
|
682 | + |
|
683 | + return $this; |
|
684 | + } |
|
685 | + |
|
686 | + /** |
|
687 | + * Sets a new value for a column in a bulk update query. |
|
688 | + * |
|
689 | + * <code> |
|
690 | + * $qb = $conn->getQueryBuilder() |
|
691 | + * ->update('users', 'u') |
|
692 | + * ->set('u.password', md5('password')) |
|
693 | + * ->where('u.id = ?'); |
|
694 | + * </code> |
|
695 | + * |
|
696 | + * @param string $key The column to set. |
|
697 | + * @param string $value The value, expression, placeholder, etc. |
|
698 | + * |
|
699 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
700 | + */ |
|
701 | + public function set($key, $value) { |
|
702 | + $this->queryBuilder->set( |
|
703 | + $this->helper->quoteColumnName($key), |
|
704 | + $this->helper->quoteColumnName($value) |
|
705 | + ); |
|
706 | + |
|
707 | + return $this; |
|
708 | + } |
|
709 | + |
|
710 | + /** |
|
711 | + * Specifies one or more restrictions to the query result. |
|
712 | + * Replaces any previously specified restrictions, if any. |
|
713 | + * |
|
714 | + * <code> |
|
715 | + * $qb = $conn->getQueryBuilder() |
|
716 | + * ->select('u.name') |
|
717 | + * ->from('users', 'u') |
|
718 | + * ->where('u.id = ?'); |
|
719 | + * |
|
720 | + * // You can optionally programatically build and/or expressions |
|
721 | + * $qb = $conn->getQueryBuilder(); |
|
722 | + * |
|
723 | + * $or = $qb->expr()->orx(); |
|
724 | + * $or->add($qb->expr()->eq('u.id', 1)); |
|
725 | + * $or->add($qb->expr()->eq('u.id', 2)); |
|
726 | + * |
|
727 | + * $qb->update('users', 'u') |
|
728 | + * ->set('u.password', md5('password')) |
|
729 | + * ->where($or); |
|
730 | + * </code> |
|
731 | + * |
|
732 | + * @param mixed ...$predicates The restriction predicates. |
|
733 | + * |
|
734 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
735 | + */ |
|
736 | + public function where(...$predicates) { |
|
737 | + call_user_func_array( |
|
738 | + [$this->queryBuilder, 'where'], |
|
739 | + $predicates |
|
740 | + ); |
|
741 | + |
|
742 | + return $this; |
|
743 | + } |
|
744 | + |
|
745 | + /** |
|
746 | + * Adds one or more restrictions to the query results, forming a logical |
|
747 | + * conjunction with any previously specified restrictions. |
|
748 | + * |
|
749 | + * <code> |
|
750 | + * $qb = $conn->getQueryBuilder() |
|
751 | + * ->select('u') |
|
752 | + * ->from('users', 'u') |
|
753 | + * ->where('u.username LIKE ?') |
|
754 | + * ->andWhere('u.is_active = 1'); |
|
755 | + * </code> |
|
756 | + * |
|
757 | + * @param mixed ...$where The query restrictions. |
|
758 | + * |
|
759 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
760 | + * |
|
761 | + * @see where() |
|
762 | + */ |
|
763 | + public function andWhere(...$where) { |
|
764 | + call_user_func_array( |
|
765 | + [$this->queryBuilder, 'andWhere'], |
|
766 | + $where |
|
767 | + ); |
|
768 | + |
|
769 | + return $this; |
|
770 | + } |
|
771 | + |
|
772 | + /** |
|
773 | + * Adds one or more restrictions to the query results, forming a logical |
|
774 | + * disjunction with any previously specified restrictions. |
|
775 | + * |
|
776 | + * <code> |
|
777 | + * $qb = $conn->getQueryBuilder() |
|
778 | + * ->select('u.name') |
|
779 | + * ->from('users', 'u') |
|
780 | + * ->where('u.id = 1') |
|
781 | + * ->orWhere('u.id = 2'); |
|
782 | + * </code> |
|
783 | + * |
|
784 | + * @param mixed ...$where The WHERE statement. |
|
785 | + * |
|
786 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
787 | + * |
|
788 | + * @see where() |
|
789 | + */ |
|
790 | + public function orWhere(...$where) { |
|
791 | + call_user_func_array( |
|
792 | + [$this->queryBuilder, 'orWhere'], |
|
793 | + $where |
|
794 | + ); |
|
795 | + |
|
796 | + return $this; |
|
797 | + } |
|
798 | + |
|
799 | + /** |
|
800 | + * Specifies a grouping over the results of the query. |
|
801 | + * Replaces any previously specified groupings, if any. |
|
802 | + * |
|
803 | + * <code> |
|
804 | + * $qb = $conn->getQueryBuilder() |
|
805 | + * ->select('u.name') |
|
806 | + * ->from('users', 'u') |
|
807 | + * ->groupBy('u.id'); |
|
808 | + * </code> |
|
809 | + * |
|
810 | + * @param mixed ...$groupBys The grouping expression. |
|
811 | + * |
|
812 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
813 | + */ |
|
814 | + public function groupBy(...$groupBys) { |
|
815 | + if (count($groupBys) === 1 && is_array($groupBys[0])) { |
|
816 | + $groupBys = $groupBys[0]; |
|
817 | + } |
|
818 | + |
|
819 | + call_user_func_array( |
|
820 | + [$this->queryBuilder, 'groupBy'], |
|
821 | + $this->helper->quoteColumnNames($groupBys) |
|
822 | + ); |
|
823 | + |
|
824 | + return $this; |
|
825 | + } |
|
826 | + |
|
827 | + /** |
|
828 | + * Adds a grouping expression to the query. |
|
829 | + * |
|
830 | + * <code> |
|
831 | + * $qb = $conn->getQueryBuilder() |
|
832 | + * ->select('u.name') |
|
833 | + * ->from('users', 'u') |
|
834 | + * ->groupBy('u.lastLogin'); |
|
835 | + * ->addGroupBy('u.createdAt') |
|
836 | + * </code> |
|
837 | + * |
|
838 | + * @param mixed ...$groupBy The grouping expression. |
|
839 | + * |
|
840 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
841 | + */ |
|
842 | + public function addGroupBy(...$groupBys) { |
|
843 | + if (count($groupBys) === 1 && is_array($groupBys[0])) { |
|
844 | + $$groupBys = $groupBys[0]; |
|
845 | + } |
|
846 | + |
|
847 | + call_user_func_array( |
|
848 | + [$this->queryBuilder, 'addGroupBy'], |
|
849 | + $this->helper->quoteColumnNames($groupBys) |
|
850 | + ); |
|
851 | + |
|
852 | + return $this; |
|
853 | + } |
|
854 | + |
|
855 | + /** |
|
856 | + * Sets a value for a column in an insert query. |
|
857 | + * |
|
858 | + * <code> |
|
859 | + * $qb = $conn->getQueryBuilder() |
|
860 | + * ->insert('users') |
|
861 | + * ->values( |
|
862 | + * array( |
|
863 | + * 'name' => '?' |
|
864 | + * ) |
|
865 | + * ) |
|
866 | + * ->setValue('password', '?'); |
|
867 | + * </code> |
|
868 | + * |
|
869 | + * @param string $column The column into which the value should be inserted. |
|
870 | + * @param string $value The value that should be inserted into the column. |
|
871 | + * |
|
872 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
873 | + */ |
|
874 | + public function setValue($column, $value) { |
|
875 | + $this->queryBuilder->setValue( |
|
876 | + $this->helper->quoteColumnName($column), |
|
877 | + $value |
|
878 | + ); |
|
879 | + |
|
880 | + return $this; |
|
881 | + } |
|
882 | + |
|
883 | + /** |
|
884 | + * Specifies values for an insert query indexed by column names. |
|
885 | + * Replaces any previous values, if any. |
|
886 | + * |
|
887 | + * <code> |
|
888 | + * $qb = $conn->getQueryBuilder() |
|
889 | + * ->insert('users') |
|
890 | + * ->values( |
|
891 | + * array( |
|
892 | + * 'name' => '?', |
|
893 | + * 'password' => '?' |
|
894 | + * ) |
|
895 | + * ); |
|
896 | + * </code> |
|
897 | + * |
|
898 | + * @param array $values The values to specify for the insert query indexed by column names. |
|
899 | + * |
|
900 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
901 | + */ |
|
902 | + public function values(array $values) { |
|
903 | + $quotedValues = []; |
|
904 | + foreach ($values as $key => $value) { |
|
905 | + $quotedValues[$this->helper->quoteColumnName($key)] = $value; |
|
906 | + } |
|
907 | + |
|
908 | + $this->queryBuilder->values($quotedValues); |
|
909 | + |
|
910 | + return $this; |
|
911 | + } |
|
912 | + |
|
913 | + /** |
|
914 | + * Specifies a restriction over the groups of the query. |
|
915 | + * Replaces any previous having restrictions, if any. |
|
916 | + * |
|
917 | + * @param mixed ...$having The restriction over the groups. |
|
918 | + * |
|
919 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
920 | + */ |
|
921 | + public function having(...$having) { |
|
922 | + call_user_func_array( |
|
923 | + [$this->queryBuilder, 'having'], |
|
924 | + $having |
|
925 | + ); |
|
926 | + |
|
927 | + return $this; |
|
928 | + } |
|
929 | + |
|
930 | + /** |
|
931 | + * Adds a restriction over the groups of the query, forming a logical |
|
932 | + * conjunction with any existing having restrictions. |
|
933 | + * |
|
934 | + * @param mixed ...$having The restriction to append. |
|
935 | + * |
|
936 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
937 | + */ |
|
938 | + public function andHaving(...$having) { |
|
939 | + call_user_func_array( |
|
940 | + [$this->queryBuilder, 'andHaving'], |
|
941 | + $having |
|
942 | + ); |
|
943 | + |
|
944 | + return $this; |
|
945 | + } |
|
946 | + |
|
947 | + /** |
|
948 | + * Adds a restriction over the groups of the query, forming a logical |
|
949 | + * disjunction with any existing having restrictions. |
|
950 | + * |
|
951 | + * @param mixed ...$having The restriction to add. |
|
952 | + * |
|
953 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
954 | + */ |
|
955 | + public function orHaving(...$having) { |
|
956 | + call_user_func_array( |
|
957 | + [$this->queryBuilder, 'orHaving'], |
|
958 | + $having |
|
959 | + ); |
|
960 | + |
|
961 | + return $this; |
|
962 | + } |
|
963 | + |
|
964 | + /** |
|
965 | + * Specifies an ordering for the query results. |
|
966 | + * Replaces any previously specified orderings, if any. |
|
967 | + * |
|
968 | + * @param string $sort The ordering expression. |
|
969 | + * @param string $order The ordering direction. |
|
970 | + * |
|
971 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
972 | + */ |
|
973 | + public function orderBy($sort, $order = null) { |
|
974 | + $this->queryBuilder->orderBy( |
|
975 | + $this->helper->quoteColumnName($sort), |
|
976 | + $order |
|
977 | + ); |
|
978 | + |
|
979 | + return $this; |
|
980 | + } |
|
981 | + |
|
982 | + /** |
|
983 | + * Adds an ordering to the query results. |
|
984 | + * |
|
985 | + * @param string $sort The ordering expression. |
|
986 | + * @param string $order The ordering direction. |
|
987 | + * |
|
988 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
989 | + */ |
|
990 | + public function addOrderBy($sort, $order = null) { |
|
991 | + $this->queryBuilder->addOrderBy( |
|
992 | + $this->helper->quoteColumnName($sort), |
|
993 | + $order |
|
994 | + ); |
|
995 | + |
|
996 | + return $this; |
|
997 | + } |
|
998 | + |
|
999 | + /** |
|
1000 | + * Gets a query part by its name. |
|
1001 | + * |
|
1002 | + * @param string $queryPartName |
|
1003 | + * |
|
1004 | + * @return mixed |
|
1005 | + */ |
|
1006 | + public function getQueryPart($queryPartName) { |
|
1007 | + return $this->queryBuilder->getQueryPart($queryPartName); |
|
1008 | + } |
|
1009 | + |
|
1010 | + /** |
|
1011 | + * Gets all query parts. |
|
1012 | + * |
|
1013 | + * @return array |
|
1014 | + */ |
|
1015 | + public function getQueryParts() { |
|
1016 | + return $this->queryBuilder->getQueryParts(); |
|
1017 | + } |
|
1018 | + |
|
1019 | + /** |
|
1020 | + * Resets SQL parts. |
|
1021 | + * |
|
1022 | + * @param array|null $queryPartNames |
|
1023 | + * |
|
1024 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
1025 | + */ |
|
1026 | + public function resetQueryParts($queryPartNames = null) { |
|
1027 | + $this->queryBuilder->resetQueryParts($queryPartNames); |
|
1028 | + |
|
1029 | + return $this; |
|
1030 | + } |
|
1031 | + |
|
1032 | + /** |
|
1033 | + * Resets a single SQL part. |
|
1034 | + * |
|
1035 | + * @param string $queryPartName |
|
1036 | + * |
|
1037 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
1038 | + */ |
|
1039 | + public function resetQueryPart($queryPartName) { |
|
1040 | + $this->queryBuilder->resetQueryPart($queryPartName); |
|
1041 | + |
|
1042 | + return $this; |
|
1043 | + } |
|
1044 | + |
|
1045 | + /** |
|
1046 | + * Creates a new named parameter and bind the value $value to it. |
|
1047 | + * |
|
1048 | + * This method provides a shortcut for PDOStatement::bindValue |
|
1049 | + * when using prepared statements. |
|
1050 | + * |
|
1051 | + * The parameter $value specifies the value that you want to bind. If |
|
1052 | + * $placeholder is not provided bindValue() will automatically create a |
|
1053 | + * placeholder for you. An automatic placeholder will be of the name |
|
1054 | + * ':dcValue1', ':dcValue2' etc. |
|
1055 | + * |
|
1056 | + * For more information see {@link http://php.net/pdostatement-bindparam} |
|
1057 | + * |
|
1058 | + * Example: |
|
1059 | + * <code> |
|
1060 | + * $value = 2; |
|
1061 | + * $q->eq( 'id', $q->bindValue( $value ) ); |
|
1062 | + * $stmt = $q->executeQuery(); // executed with 'id = 2' |
|
1063 | + * </code> |
|
1064 | + * |
|
1065 | + * @license New BSD License |
|
1066 | + * @link http://www.zetacomponents.org |
|
1067 | + * |
|
1068 | + * @param mixed $value |
|
1069 | + * @param mixed $type |
|
1070 | + * @param string $placeHolder The name to bind with. The string must start with a colon ':'. |
|
1071 | + * |
|
1072 | + * @return IParameter the placeholder name used. |
|
1073 | + */ |
|
1074 | + public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) { |
|
1075 | + return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder)); |
|
1076 | + } |
|
1077 | + |
|
1078 | + /** |
|
1079 | + * Creates a new positional parameter and bind the given value to it. |
|
1080 | + * |
|
1081 | + * Attention: If you are using positional parameters with the query builder you have |
|
1082 | + * to be very careful to bind all parameters in the order they appear in the SQL |
|
1083 | + * statement , otherwise they get bound in the wrong order which can lead to serious |
|
1084 | + * bugs in your code. |
|
1085 | + * |
|
1086 | + * Example: |
|
1087 | + * <code> |
|
1088 | + * $qb = $conn->getQueryBuilder(); |
|
1089 | + * $qb->select('u.*') |
|
1090 | + * ->from('users', 'u') |
|
1091 | + * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR)) |
|
1092 | + * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR)) |
|
1093 | + * </code> |
|
1094 | + * |
|
1095 | + * @param mixed $value |
|
1096 | + * @param integer $type |
|
1097 | + * |
|
1098 | + * @return IParameter |
|
1099 | + */ |
|
1100 | + public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) { |
|
1101 | + return new Parameter($this->queryBuilder->createPositionalParameter($value, $type)); |
|
1102 | + } |
|
1103 | + |
|
1104 | + /** |
|
1105 | + * Creates a new parameter |
|
1106 | + * |
|
1107 | + * Example: |
|
1108 | + * <code> |
|
1109 | + * $qb = $conn->getQueryBuilder(); |
|
1110 | + * $qb->select('u.*') |
|
1111 | + * ->from('users', 'u') |
|
1112 | + * ->where('u.username = ' . $qb->createParameter('name')) |
|
1113 | + * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR)) |
|
1114 | + * </code> |
|
1115 | + * |
|
1116 | + * @param string $name |
|
1117 | + * |
|
1118 | + * @return IParameter |
|
1119 | + */ |
|
1120 | + public function createParameter($name) { |
|
1121 | + return new Parameter(':' . $name); |
|
1122 | + } |
|
1123 | + |
|
1124 | + /** |
|
1125 | + * Creates a new function |
|
1126 | + * |
|
1127 | + * Attention: Column names inside the call have to be quoted before hand |
|
1128 | + * |
|
1129 | + * Example: |
|
1130 | + * <code> |
|
1131 | + * $qb = $conn->getQueryBuilder(); |
|
1132 | + * $qb->select($qb->createFunction('COUNT(*)')) |
|
1133 | + * ->from('users', 'u') |
|
1134 | + * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u |
|
1135 | + * </code> |
|
1136 | + * <code> |
|
1137 | + * $qb = $conn->getQueryBuilder(); |
|
1138 | + * $qb->select($qb->createFunction('COUNT(`column`)')) |
|
1139 | + * ->from('users', 'u') |
|
1140 | + * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u |
|
1141 | + * </code> |
|
1142 | + * |
|
1143 | + * @param string $call |
|
1144 | + * |
|
1145 | + * @return IQueryFunction |
|
1146 | + */ |
|
1147 | + public function createFunction($call) { |
|
1148 | + return new QueryFunction($call); |
|
1149 | + } |
|
1150 | + |
|
1151 | + /** |
|
1152 | + * Used to get the id of the last inserted element |
|
1153 | + * @return int |
|
1154 | + * @throws \BadMethodCallException When being called before an insert query has been run. |
|
1155 | + */ |
|
1156 | + public function getLastInsertId() { |
|
1157 | + if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) { |
|
1158 | + // lastInsertId() needs the prefix but no quotes |
|
1159 | + $table = $this->prefixTableName($this->lastInsertedTable); |
|
1160 | + return (int) $this->connection->lastInsertId($table); |
|
1161 | + } |
|
1162 | + |
|
1163 | + throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.'); |
|
1164 | + } |
|
1165 | + |
|
1166 | + /** |
|
1167 | + * Returns the table name quoted and with database prefix as needed by the implementation |
|
1168 | + * |
|
1169 | + * @param string $table |
|
1170 | + * @return string |
|
1171 | + */ |
|
1172 | + public function getTableName($table) { |
|
1173 | + $table = $this->prefixTableName($table); |
|
1174 | + return $this->helper->quoteColumnName($table); |
|
1175 | + } |
|
1176 | + |
|
1177 | + /** |
|
1178 | + * Returns the table name with database prefix as needed by the implementation |
|
1179 | + * |
|
1180 | + * @param string $table |
|
1181 | + * @return string |
|
1182 | + */ |
|
1183 | + protected function prefixTableName($table) { |
|
1184 | + if ($this->automaticTablePrefix === false || strpos($table, '*PREFIX*') === 0) { |
|
1185 | + return $table; |
|
1186 | + } |
|
1187 | + |
|
1188 | + return '*PREFIX*' . $table; |
|
1189 | + } |
|
1190 | + |
|
1191 | + /** |
|
1192 | + * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
1193 | + * |
|
1194 | + * @param string $column |
|
1195 | + * @param string $tableAlias |
|
1196 | + * @return string |
|
1197 | + */ |
|
1198 | + public function getColumnName($column, $tableAlias = '') { |
|
1199 | + if ($tableAlias !== '') { |
|
1200 | + $tableAlias .= '.'; |
|
1201 | + } |
|
1202 | + |
|
1203 | + return $this->helper->quoteColumnName($tableAlias . $column); |
|
1204 | + } |
|
1205 | + |
|
1206 | + /** |
|
1207 | + * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
1208 | + * |
|
1209 | + * @param string $alias |
|
1210 | + * @return string |
|
1211 | + */ |
|
1212 | + public function quoteAlias($alias) { |
|
1213 | + if ($alias === '' || $alias === null) { |
|
1214 | + return $alias; |
|
1215 | + } |
|
1216 | + |
|
1217 | + return $this->helper->quoteColumnName($alias); |
|
1218 | + } |
|
1219 | 1219 | } |
@@ -27,55 +27,55 @@ |
||
27 | 27 | use OCP\DB\QueryBuilder\IQueryFunction; |
28 | 28 | |
29 | 29 | class QuoteHelper { |
30 | - /** |
|
31 | - * @param array|string|ILiteral|IParameter|IQueryFunction $strings string, Literal or Parameter |
|
32 | - * @return array|string |
|
33 | - */ |
|
34 | - public function quoteColumnNames($strings) { |
|
35 | - if (!is_array($strings)) { |
|
36 | - return $this->quoteColumnName($strings); |
|
37 | - } |
|
30 | + /** |
|
31 | + * @param array|string|ILiteral|IParameter|IQueryFunction $strings string, Literal or Parameter |
|
32 | + * @return array|string |
|
33 | + */ |
|
34 | + public function quoteColumnNames($strings) { |
|
35 | + if (!is_array($strings)) { |
|
36 | + return $this->quoteColumnName($strings); |
|
37 | + } |
|
38 | 38 | |
39 | - $return = []; |
|
40 | - foreach ($strings as $string) { |
|
41 | - $return[] = $this->quoteColumnName($string); |
|
42 | - } |
|
39 | + $return = []; |
|
40 | + foreach ($strings as $string) { |
|
41 | + $return[] = $this->quoteColumnName($string); |
|
42 | + } |
|
43 | 43 | |
44 | - return $return; |
|
45 | - } |
|
44 | + return $return; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @param string|ILiteral|IParameter|IQueryFunction $string string, Literal or Parameter |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - public function quoteColumnName($string) { |
|
52 | - if ($string instanceof IParameter || $string instanceof ILiteral || $string instanceof IQueryFunction) { |
|
53 | - return (string) $string; |
|
54 | - } |
|
47 | + /** |
|
48 | + * @param string|ILiteral|IParameter|IQueryFunction $string string, Literal or Parameter |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public function quoteColumnName($string) { |
|
52 | + if ($string instanceof IParameter || $string instanceof ILiteral || $string instanceof IQueryFunction) { |
|
53 | + return (string) $string; |
|
54 | + } |
|
55 | 55 | |
56 | - if ($string === null || $string === 'null' || $string === '*') { |
|
57 | - return $string; |
|
58 | - } |
|
56 | + if ($string === null || $string === 'null' || $string === '*') { |
|
57 | + return $string; |
|
58 | + } |
|
59 | 59 | |
60 | - if (!is_string($string)) { |
|
61 | - throw new \InvalidArgumentException('Only strings, Literals and Parameters are allowed'); |
|
62 | - } |
|
60 | + if (!is_string($string)) { |
|
61 | + throw new \InvalidArgumentException('Only strings, Literals and Parameters are allowed'); |
|
62 | + } |
|
63 | 63 | |
64 | - $string = str_replace(' AS ', ' as ', $string); |
|
65 | - if (substr_count($string, ' as ')) { |
|
66 | - return implode(' as ', array_map([$this, 'quoteColumnName'], explode(' as ', $string, 2))); |
|
67 | - } |
|
64 | + $string = str_replace(' AS ', ' as ', $string); |
|
65 | + if (substr_count($string, ' as ')) { |
|
66 | + return implode(' as ', array_map([$this, 'quoteColumnName'], explode(' as ', $string, 2))); |
|
67 | + } |
|
68 | 68 | |
69 | - if (substr_count($string, '.')) { |
|
70 | - list($alias, $columnName) = explode('.', $string, 2); |
|
69 | + if (substr_count($string, '.')) { |
|
70 | + list($alias, $columnName) = explode('.', $string, 2); |
|
71 | 71 | |
72 | - if ($columnName === '*') { |
|
73 | - return '`' . $alias . '`.*'; |
|
74 | - } |
|
72 | + if ($columnName === '*') { |
|
73 | + return '`' . $alias . '`.*'; |
|
74 | + } |
|
75 | 75 | |
76 | - return '`' . $alias . '`.`' . $columnName . '`'; |
|
77 | - } |
|
76 | + return '`' . $alias . '`.`' . $columnName . '`'; |
|
77 | + } |
|
78 | 78 | |
79 | - return '`' . $string . '`'; |
|
80 | - } |
|
79 | + return '`' . $string . '`'; |
|
80 | + } |
|
81 | 81 | } |
@@ -70,12 +70,12 @@ |
||
70 | 70 | list($alias, $columnName) = explode('.', $string, 2); |
71 | 71 | |
72 | 72 | if ($columnName === '*') { |
73 | - return '`' . $alias . '`.*'; |
|
73 | + return '`'.$alias.'`.*'; |
|
74 | 74 | } |
75 | 75 | |
76 | - return '`' . $alias . '`.`' . $columnName . '`'; |
|
76 | + return '`'.$alias.'`.`'.$columnName.'`'; |
|
77 | 77 | } |
78 | 78 | |
79 | - return '`' . $string . '`'; |
|
79 | + return '`'.$string.'`'; |
|
80 | 80 | } |
81 | 81 | } |