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.

TaskMapper.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\TextToImage\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\DB\QueryBuilder\IQueryBuilder;
  31. use OCP\IDBConnection;
  32. /**
  33. * @extends QBMapper<Task>
  34. */
  35. class TaskMapper extends QBMapper {
  36. public function __construct(
  37. IDBConnection $db,
  38. private ITimeFactory $timeFactory,
  39. ) {
  40. parent::__construct($db, 'text2image_tasks', Task::class);
  41. }
  42. /**
  43. * @param int $id
  44. * @return Task
  45. * @throws Exception
  46. * @throws DoesNotExistException
  47. * @throws MultipleObjectsReturnedException
  48. */
  49. public function find(int $id): Task {
  50. $qb = $this->db->getQueryBuilder();
  51. $qb->select(Task::$columns)
  52. ->from($this->tableName)
  53. ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id)));
  54. return $this->findEntity($qb);
  55. }
  56. /**
  57. * @param int $id
  58. * @param string|null $userId
  59. * @return Task
  60. * @throws DoesNotExistException
  61. * @throws Exception
  62. * @throws MultipleObjectsReturnedException
  63. */
  64. public function findByIdAndUser(int $id, ?string $userId): Task {
  65. $qb = $this->db->getQueryBuilder();
  66. $qb->select(Task::$columns)
  67. ->from($this->tableName)
  68. ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id)));
  69. if ($userId === null) {
  70. $qb->andWhere($qb->expr()->isNull('user_id'));
  71. } else {
  72. $qb->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
  73. }
  74. return $this->findEntity($qb);
  75. }
  76. /**
  77. * @param string $userId
  78. * @param string $appId
  79. * @param string|null $identifier
  80. * @return array
  81. * @throws Exception
  82. */
  83. public function findUserTasksByApp(?string $userId, string $appId, ?string $identifier = null): array {
  84. $qb = $this->db->getQueryBuilder();
  85. $qb->select(Task::$columns)
  86. ->from($this->tableName)
  87. ->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)))
  88. ->andWhere($qb->expr()->eq('app_id', $qb->createPositionalParameter($appId)));
  89. if ($identifier !== null) {
  90. $qb->andWhere($qb->expr()->eq('identifier', $qb->createPositionalParameter($identifier)));
  91. }
  92. return $this->findEntities($qb);
  93. }
  94. /**
  95. * @param int $timeout
  96. * @return Task[] the deleted tasks
  97. * @throws Exception
  98. */
  99. public function deleteOlderThan(int $timeout): array {
  100. $datetime = $this->timeFactory->getDateTime();
  101. $datetime->sub(new \DateInterval('PT'.$timeout.'S'));
  102. $qb = $this->db->getQueryBuilder();
  103. $qb->select('*')
  104. ->from($this->tableName)
  105. ->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter($datetime, IQueryBuilder::PARAM_DATE)));
  106. $deletedTasks = $this->findEntities($qb);
  107. $qb = $this->db->getQueryBuilder();
  108. $qb->delete($this->tableName)
  109. ->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter($datetime, IQueryBuilder::PARAM_DATE)));
  110. $qb->executeStatement();
  111. return $deletedTasks;
  112. }
  113. public function update(Entity $entity): Entity {
  114. $entity->setLastUpdated($this->timeFactory->getDateTime());
  115. return parent::update($entity);
  116. }
  117. }