FeedDAOPGSQL.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. // Compatible PostgreSQL, SQLite, MySQL 8.0+, but not MariaDB as of version 12.2.
  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((
  33. SELECT c.total_entries
  34. FROM entry_counts AS c
  35. WHERE c.id_feed = `_feed`.id
  36. ), 0),
  37. `cache_nbUnreads` = COALESCE((
  38. SELECT c.unread_entries
  39. FROM entry_counts AS c
  40. WHERE c.id_feed = `_feed`.id
  41. ), 0)
  42. WHERE $whereFeedIds
  43. SQL;
  44. $stm = $this->pdo->prepare($sql);
  45. if ($stm !== false && $stm->execute(array_merge($feedIds, $feedIds))) {
  46. return $stm->rowCount();
  47. } else {
  48. $info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
  49. Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
  50. return false;
  51. }
  52. }
  53. }