EntryDAOPGSQL.php 1.8 KB

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