EntryDAOPGSQL.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. class FreshRSS_EntryDAOPGSQL extends FreshRSS_EntryDAOSQLite {
  3. public static function hasNativeHex(): bool {
  4. return true;
  5. }
  6. public static function sqlHexDecode(string $x): string {
  7. return 'decode(' . $x . ", 'hex')";
  8. }
  9. public static function sqlHexEncode(string $x): string {
  10. return 'encode(' . $x . ", 'hex')";
  11. }
  12. public static function sqlIgnoreConflict(string $sql): string {
  13. return rtrim($sql, ' ;') . ' ON CONFLICT DO NOTHING';
  14. }
  15. /** @param array<string> $errorInfo */
  16. protected function autoUpdateDb(array $errorInfo): bool {
  17. if (isset($errorInfo[0])) {
  18. if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_FIELD_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_COLUMN) {
  19. $errorLines = explode("\n", $errorInfo[2], 2); // The relevant column name is on the first line, other lines are noise
  20. foreach (['attributes'] as $column) {
  21. if (stripos($errorLines[0], $column) !== false) {
  22. return $this->addColumn($column);
  23. }
  24. }
  25. }
  26. }
  27. return false;
  28. }
  29. public function commitNewEntries(): bool {
  30. //TODO: Update to PostgreSQL 9.5+ syntax with ON CONFLICT DO NOTHING
  31. $sql = 'DO $$
  32. DECLARE
  33. maxrank bigint := (SELECT MAX(id) FROM `_entrytmp`);
  34. rank bigint := (SELECT maxrank - COUNT(*) FROM `_entrytmp`);
  35. BEGIN
  36. INSERT INTO `_entry`
  37. (id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes)
  38. (SELECT rank + row_number() OVER(ORDER BY date, id) AS id, guid, title, author, content,
  39. link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes
  40. FROM `_entrytmp` AS etmp
  41. WHERE NOT EXISTS (
  42. SELECT 1 FROM `_entry` AS ereal
  43. WHERE (etmp.id = ereal.id) OR (etmp.id_feed = ereal.id_feed AND etmp.guid = ereal.guid))
  44. ORDER BY date, id);
  45. DELETE FROM `_entrytmp` WHERE id <= maxrank;
  46. END $$;';
  47. $hadTransaction = $this->pdo->inTransaction();
  48. if (!$hadTransaction) {
  49. $this->pdo->beginTransaction();
  50. }
  51. $result = $this->pdo->exec($sql) !== false;
  52. if (!$hadTransaction) {
  53. $this->pdo->commit();
  54. }
  55. return $result;
  56. }
  57. }