You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.0 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Files\Cache;
  25. use OC\DB\QueryBuilder\QueryBuilder;
  26. use OC\SystemConfig;
  27. use OCP\DB\QueryBuilder\IQueryBuilder;
  28. use OCP\IDBConnection;
  29. use OCP\ILogger;
  30. /**
  31. * Query builder with commonly used helpers for filecache queries
  32. */
  33. class CacheQueryBuilder extends QueryBuilder {
  34. private $alias = null;
  35. public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger) {
  36. parent::__construct($connection, $systemConfig, $logger);
  37. }
  38. public function selectFileCache(string $alias = null) {
  39. $name = $alias ? $alias : 'filecache';
  40. $this->select("$name.fileid", 'storage', 'path', 'path_hash', "$name.parent", 'name', 'mimetype', 'mimepart', 'size', 'mtime',
  41. 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum', 'metadata_etag', 'creation_time', 'upload_time')
  42. ->from('filecache', $name)
  43. ->leftJoin($name, 'filecache_extended', 'fe', $this->expr()->eq("$name.fileid", 'fe.fileid'));
  44. $this->alias = $name;
  45. return $this;
  46. }
  47. public function whereStorageId(int $storageId) {
  48. $this->andWhere($this->expr()->eq('storage', $this->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  49. return $this;
  50. }
  51. public function whereFileId(int $fileId) {
  52. $alias = $this->alias;
  53. if ($alias) {
  54. $alias .= '.';
  55. } else {
  56. $alias = '';
  57. }
  58. $this->andWhere($this->expr()->eq("{$alias}fileid", $this->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  59. return $this;
  60. }
  61. public function wherePath(string $path) {
  62. $this->andWhere($this->expr()->eq('path_hash', $this->createNamedParameter(md5($path))));
  63. return $this;
  64. }
  65. public function whereParent(int $parent) {
  66. $alias = $this->alias;
  67. if ($alias) {
  68. $alias .= '.';
  69. } else {
  70. $alias = '';
  71. }
  72. $this->andWhere($this->expr()->eq("{$alias}parent", $this->createNamedParameter($parent, IQueryBuilder::PARAM_INT)));
  73. return $this;
  74. }
  75. public function whereParentInParameter(string $parameter) {
  76. $alias = $this->alias;
  77. if ($alias) {
  78. $alias .= '.';
  79. } else {
  80. $alias = '';
  81. }
  82. $this->andWhere($this->expr()->in("{$alias}parent", $this->createParameter($parameter)));
  83. return $this;
  84. }
  85. }