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.

110 lines
3.1 KiB

3 years ago
  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 $cache;
  35. private $alias = null;
  36. public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger, Cache $cache) {
  37. parent::__construct($connection, $systemConfig, $logger);
  38. $this->cache = $cache;
  39. }
  40. public function selectFileCache(string $alias = null) {
  41. $name = $alias ? $alias : 'filecache';
  42. $this->select("$name.fileid", 'storage', 'path', 'path_hash', "$name.parent", 'name', 'mimetype', 'mimepart', 'size', 'mtime',
  43. 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum', 'metadata_etag', 'creation_time', 'upload_time')
  44. ->from('filecache', $name)
  45. ->leftJoin($name, 'filecache_extended', 'fe', $this->expr()->eq("$name.fileid", 'fe.fileid'));
  46. $this->alias = $name;
  47. return $this;
  48. }
  49. public function whereStorageId() {
  50. $this->andWhere($this->expr()->eq('storage', $this->createNamedParameter($this->cache->getNumericStorageId(), IQueryBuilder::PARAM_INT)));
  51. return $this;
  52. }
  53. public function whereFileId(int $fileId) {
  54. $alias = $this->alias;
  55. if ($alias) {
  56. $alias .= '.';
  57. } else {
  58. $alias = '';
  59. }
  60. $this->andWhere($this->expr()->eq("{$alias}fileid", $this->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  61. return $this;
  62. }
  63. public function wherePath(string $path) {
  64. $this->andWhere($this->expr()->eq('path_hash', $this->createNamedParameter(md5($path))));
  65. return $this;
  66. }
  67. public function whereParent(int $parent) {
  68. $alias = $this->alias;
  69. if ($alias) {
  70. $alias .= '.';
  71. } else {
  72. $alias = '';
  73. }
  74. $this->andWhere($this->expr()->eq("{$alias}parent", $this->createNamedParameter($parent, IQueryBuilder::PARAM_INT)));
  75. return $this;
  76. }
  77. public function whereParentIn(array $parents) {
  78. $alias = $this->alias;
  79. if ($alias) {
  80. $alias .= '.';
  81. } else {
  82. $alias = '';
  83. }
  84. $this->andWhere($this->expr()->in("{$alias}parent", $this->createNamedParameter($parents, IQueryBuilder::PARAM_INT_ARRAY)));
  85. return $this;
  86. }
  87. }