FeedDAOPGSQL.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_FeedDAOPGSQL extends FreshRSS_FeedDAO {
  4. #[\Override]
  5. public function sqlResetSequence(): bool {
  6. $sql = <<<'SQL'
  7. SELECT setval('`_feed_id_seq`', COALESCE(MAX(id), 0) + 1, false) FROM `_feed`
  8. SQL;
  9. return $this->pdo->exec($sql) !== false;
  10. }
  11. #[\Override]
  12. public function updateCachedValues(int ...$feedIds): int|false {
  13. if (empty($feedIds)) {
  14. $whereFeedIds = 'true';
  15. $whereEntryIdFeeds = 'true';
  16. } else {
  17. $whereFeedIds = 'id IN (' . str_repeat('?,', count($feedIds) - 1) . '?)';
  18. $whereEntryIdFeeds = 'id_feed IN (' . str_repeat('?,', count($feedIds) - 1) . '?)';
  19. }
  20. $sql = <<<SQL
  21. WITH entry_counts AS (
  22. SELECT
  23. id_feed,
  24. COUNT(*) AS total_entries,
  25. SUM(CASE WHEN is_read = 0 THEN 1 ELSE 0 END) AS unread_entries
  26. FROM `_entry`
  27. WHERE $whereEntryIdFeeds
  28. GROUP BY id_feed
  29. )
  30. UPDATE `_feed`
  31. SET `cache_nbEntries` = COALESCE((
  32. SELECT c.total_entries
  33. FROM entry_counts AS c
  34. WHERE c.id_feed = `_feed`.id
  35. ), 0),
  36. `cache_nbUnreads` = COALESCE((
  37. SELECT c.unread_entries
  38. FROM entry_counts AS c
  39. WHERE c.id_feed = `_feed`.id
  40. ), 0)
  41. WHERE $whereFeedIds
  42. SQL;
  43. $stm = $this->pdo->prepare($sql);
  44. if ($stm !== false && $stm->execute(array_merge($feedIds, $feedIds))) {
  45. return $stm->rowCount();
  46. } else {
  47. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  48. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  49. return false;
  50. }
  51. }
  52. }