FeedDAOPGSQL.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. // Faster than the MySQL version
  14. if (empty($feedIds)) {
  15. $whereFeedIds = 'true';
  16. $whereEntryIdFeeds = 'true';
  17. } else {
  18. $whereFeedIds = 'id IN (' . str_repeat('?,', count($feedIds) - 1) . '?)';
  19. $whereEntryIdFeeds = 'id_feed IN (' . str_repeat('?,', count($feedIds) - 1) . '?)';
  20. }
  21. $sql = <<<SQL
  22. WITH entry_counts AS (
  23. SELECT
  24. id_feed,
  25. COUNT(*) AS total_entries,
  26. SUM(CASE WHEN is_read = 0 THEN 1 ELSE 0 END) AS unread_entries
  27. FROM `_entry`
  28. WHERE $whereEntryIdFeeds
  29. GROUP BY id_feed
  30. )
  31. UPDATE `_feed`
  32. SET `cache_nbEntries` = COALESCE(c.total_entries, 0),
  33. `cache_nbUnreads` = COALESCE(c.unread_entries, 0)
  34. FROM entry_counts c
  35. WHERE id = c.id_feed AND $whereFeedIds
  36. SQL;
  37. $stm = $this->pdo->prepare($sql);
  38. if ($stm !== false && $stm->execute(array_merge($feedIds, $feedIds))) {
  39. return $stm->rowCount();
  40. } else {
  41. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  42. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  43. return false;
  44. }
  45. }
  46. }