EntryDAOPGSQL.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. class FreshRSS_EntryDAOPGSQL extends FreshRSS_EntryDAOSQLite {
  3. public function hasNativeHex() {
  4. return true;
  5. }
  6. public function sqlHexDecode($x) {
  7. return 'decode(' . $x . ", 'hex')";
  8. }
  9. public function sqlHexEncode($x) {
  10. return 'encode(' . $x . ", 'hex')";
  11. }
  12. protected function autoUpdateDb($errorInfo) {
  13. if (isset($errorInfo[0])) {
  14. if ($errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_TABLE) {
  15. if (stripos($errorInfo[2], 'tag') !== false) {
  16. $tagDAO = FreshRSS_Factory::createTagDao();
  17. return $tagDAO->createTagTable(); //v1.12.0
  18. } elseif (stripos($errorInfo[2], 'entrytmp') !== false) {
  19. return $this->createEntryTempTable(); //v1.7.0
  20. }
  21. }
  22. }
  23. return false;
  24. }
  25. protected function addColumn($name) {
  26. return false;
  27. }
  28. public function commitNewEntries() {
  29. $sql = 'DO $$
  30. DECLARE
  31. maxrank bigint := (SELECT MAX(id) FROM `' . $this->prefix . 'entrytmp`);
  32. rank bigint := (SELECT maxrank - COUNT(*) FROM `' . $this->prefix . 'entrytmp`);
  33. BEGIN
  34. INSERT INTO `' . $this->prefix . 'entry` (id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags)
  35. (SELECT rank + row_number() OVER(ORDER BY date) AS id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
  36. FROM `' . $this->prefix . 'entrytmp` AS etmp
  37. WHERE NOT EXISTS (
  38. SELECT 1 FROM `' . $this->prefix . 'entry` AS ereal
  39. WHERE (etmp.id = ereal.id) OR (etmp.id_feed = ereal.id_feed AND etmp.guid = ereal.guid))
  40. ORDER BY date);
  41. DELETE FROM `' . $this->prefix . 'entrytmp` WHERE id <= maxrank;
  42. END $$;';
  43. $hadTransaction = $this->bd->inTransaction();
  44. if (!$hadTransaction) {
  45. $this->bd->beginTransaction();
  46. }
  47. $result = $this->bd->exec($sql) !== false;
  48. if (!$hadTransaction) {
  49. $this->bd->commit();
  50. }
  51. return $result;
  52. }
  53. }