TaskMapper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
  5. *
  6. * @author Marcel Klehr <mklehr@gmx.net>
  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. namespace OC\TextProcessing\Db;
  24. use OCP\AppFramework\Db\DoesNotExistException;
  25. use OCP\AppFramework\Db\Entity;
  26. use OCP\AppFramework\Db\MultipleObjectsReturnedException;
  27. use OCP\AppFramework\Db\QBMapper;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\DB\Exception;
  30. use OCP\IDBConnection;
  31. /**
  32. * @extends QBMapper<Task>
  33. */
  34. class TaskMapper extends QBMapper {
  35. public function __construct(
  36. IDBConnection $db,
  37. private ITimeFactory $timeFactory,
  38. ) {
  39. parent::__construct($db, 'textprocessing_tasks', Task::class);
  40. }
  41. /**
  42. * @param int $id
  43. * @return Task
  44. * @throws Exception
  45. * @throws DoesNotExistException
  46. * @throws MultipleObjectsReturnedException
  47. */
  48. public function find(int $id): Task {
  49. $qb = $this->db->getQueryBuilder();
  50. $qb->select(Task::$columns)
  51. ->from($this->tableName)
  52. ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id)));
  53. return $this->findEntity($qb);
  54. }
  55. /**
  56. * @param int $id
  57. * @param string|null $userId
  58. * @return Task
  59. * @throws DoesNotExistException
  60. * @throws Exception
  61. * @throws MultipleObjectsReturnedException
  62. */
  63. public function findByIdAndUser(int $id, ?string $userId): Task {
  64. $qb = $this->db->getQueryBuilder();
  65. $qb->select(Task::$columns)
  66. ->from($this->tableName)
  67. ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id)));
  68. if ($userId === null) {
  69. $qb->andWhere($qb->expr()->isNull('user_id'));
  70. } else {
  71. $qb->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
  72. }
  73. return $this->findEntity($qb);
  74. }
  75. /**
  76. * @param string $userId
  77. * @param string $appId
  78. * @param string|null $identifier
  79. * @return array
  80. * @throws Exception
  81. */
  82. public function findUserTasksByApp(string $userId, string $appId, ?string $identifier = null): array {
  83. $qb = $this->db->getQueryBuilder();
  84. $qb->select(Task::$columns)
  85. ->from($this->tableName)
  86. ->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)))
  87. ->andWhere($qb->expr()->eq('app_id', $qb->createPositionalParameter($appId)));
  88. if ($identifier !== null) {
  89. $qb->andWhere($qb->expr()->eq('identifier', $qb->createPositionalParameter($identifier)));
  90. }
  91. return $this->findEntities($qb);
  92. }
  93. /**
  94. * @param int $timeout
  95. * @return int the number of deleted tasks
  96. * @throws Exception
  97. */
  98. public function deleteOlderThan(int $timeout): int {
  99. $qb = $this->db->getQueryBuilder();
  100. $qb->delete($this->tableName)
  101. ->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter(time() - $timeout)));
  102. return $qb->executeStatement();
  103. }
  104. public function update(Entity $entity): Entity {
  105. $entity->setLastUpdated($this->timeFactory->now()->getTimestamp());
  106. return parent::update($entity);
  107. }
  108. }