EntryDAOPGSQL.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 `_entrytmp`);
  32. rank bigint := (SELECT maxrank - COUNT(*) FROM `_entrytmp`);
  33. BEGIN
  34. INSERT INTO `_entry`
  35. (id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags)
  36. (SELECT rank + row_number() OVER(ORDER BY date) AS id, guid, title, author, content,
  37. link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
  38. FROM `_entrytmp` AS etmp
  39. WHERE NOT EXISTS (
  40. SELECT 1 FROM `_entry` AS ereal
  41. WHERE (etmp.id = ereal.id) OR (etmp.id_feed = ereal.id_feed AND etmp.guid = ereal.guid))
  42. ORDER BY date);
  43. DELETE FROM `_entrytmp` WHERE id <= maxrank;
  44. END $$;';
  45. $hadTransaction = $this->pdo->inTransaction();
  46. if (!$hadTransaction) {
  47. $this->pdo->beginTransaction();
  48. }
  49. $result = $this->pdo->exec($sql) !== false;
  50. if (!$hadTransaction) {
  51. $this->pdo->commit();
  52. }
  53. return $result;
  54. }
  55. }