TaskMapper.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\TextToImage\Db;
  8. use OCP\AppFramework\Db\DoesNotExistException;
  9. use OCP\AppFramework\Db\Entity;
  10. use OCP\AppFramework\Db\MultipleObjectsReturnedException;
  11. use OCP\AppFramework\Db\QBMapper;
  12. use OCP\AppFramework\Utility\ITimeFactory;
  13. use OCP\DB\Exception;
  14. use OCP\DB\QueryBuilder\IQueryBuilder;
  15. use OCP\IDBConnection;
  16. /**
  17. * @extends QBMapper<Task>
  18. */
  19. class TaskMapper extends QBMapper {
  20. public function __construct(
  21. IDBConnection $db,
  22. private ITimeFactory $timeFactory,
  23. ) {
  24. parent::__construct($db, 'text2image_tasks', Task::class);
  25. }
  26. /**
  27. * @param int $id
  28. * @return Task
  29. * @throws Exception
  30. * @throws DoesNotExistException
  31. * @throws MultipleObjectsReturnedException
  32. */
  33. public function find(int $id): Task {
  34. $qb = $this->db->getQueryBuilder();
  35. $qb->select(Task::$columns)
  36. ->from($this->tableName)
  37. ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id)));
  38. return $this->findEntity($qb);
  39. }
  40. /**
  41. * @param int $id
  42. * @param string|null $userId
  43. * @return Task
  44. * @throws DoesNotExistException
  45. * @throws Exception
  46. * @throws MultipleObjectsReturnedException
  47. */
  48. public function findByIdAndUser(int $id, ?string $userId): 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. if ($userId === null) {
  54. $qb->andWhere($qb->expr()->isNull('user_id'));
  55. } else {
  56. $qb->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
  57. }
  58. return $this->findEntity($qb);
  59. }
  60. /**
  61. * @param string $userId
  62. * @param string $appId
  63. * @param string|null $identifier
  64. * @return array
  65. * @throws Exception
  66. */
  67. public function findUserTasksByApp(?string $userId, string $appId, ?string $identifier = null): array {
  68. $qb = $this->db->getQueryBuilder();
  69. $qb->select(Task::$columns)
  70. ->from($this->tableName)
  71. ->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)))
  72. ->andWhere($qb->expr()->eq('app_id', $qb->createPositionalParameter($appId)));
  73. if ($identifier !== null) {
  74. $qb->andWhere($qb->expr()->eq('identifier', $qb->createPositionalParameter($identifier)));
  75. }
  76. return $this->findEntities($qb);
  77. }
  78. /**
  79. * @param int $timeout
  80. * @return Task[] the deleted tasks
  81. * @throws Exception
  82. */
  83. public function deleteOlderThan(int $timeout): array {
  84. $datetime = $this->timeFactory->getDateTime();
  85. $datetime->sub(new \DateInterval('PT' . $timeout . 'S'));
  86. $qb = $this->db->getQueryBuilder();
  87. $qb->select('*')
  88. ->from($this->tableName)
  89. ->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter($datetime, IQueryBuilder::PARAM_DATE)));
  90. $deletedTasks = $this->findEntities($qb);
  91. $qb = $this->db->getQueryBuilder();
  92. $qb->delete($this->tableName)
  93. ->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter($datetime, IQueryBuilder::PARAM_DATE)));
  94. $qb->executeStatement();
  95. return $deletedTasks;
  96. }
  97. public function update(Entity $entity): Entity {
  98. $entity->setLastUpdated($this->timeFactory->getDateTime());
  99. return parent::update($entity);
  100. }
  101. }