EntryDAOPGSQL.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_EntryDAOPGSQL extends FreshRSS_EntryDAOSQLite {
  4. #[\Override]
  5. public static function hasNativeHex(): bool {
  6. return true;
  7. }
  8. #[\Override]
  9. public static function sqlHexDecode(string $x): string {
  10. return 'decode(' . $x . ", 'hex')";
  11. }
  12. #[\Override]
  13. public static function sqlHexEncode(string $x): string {
  14. return 'encode(' . $x . ", 'hex')";
  15. }
  16. #[\Override]
  17. public static function sqlIgnoreConflict(string $sql): string {
  18. return rtrim($sql, ' ;') . ' ON CONFLICT DO NOTHING';
  19. }
  20. #[\Override]
  21. protected static function sqlLimitAll(): string {
  22. // https://www.postgresql.org/docs/current/queries-limit.html
  23. return 'ALL';
  24. }
  25. #[\Override]
  26. public static function sqlGreatest(string $a, string $b): string {
  27. return 'GREATEST(' . $a . ', ' . $b . ')';
  28. }
  29. #[\Override]
  30. public static function sqlRandom(): string {
  31. return 'RANDOM()';
  32. }
  33. #[\Override]
  34. protected static function sqlRegex(string $expression, string $regex, array &$values): string {
  35. $matches = static::regexToSql($regex);
  36. if (isset($matches['pattern'])) {
  37. $matchType = $matches['matchType'] ?? '';
  38. if (str_contains($matchType, 'm')) {
  39. // newline-sensitive matching
  40. $matches['pattern'] = '(?m)' . $matches['pattern'];
  41. }
  42. $values[] = $matches['pattern'];
  43. if (str_contains($matchType, 'i')) {
  44. // case-insensitive matching
  45. return "{$expression} ~* ?";
  46. } else {
  47. // case-sensitive matching
  48. return "{$expression} ~ ?";
  49. }
  50. }
  51. return '';
  52. }
  53. #[\Override]
  54. protected function registerSqlFunctions(string $sql): void {
  55. // Nothing to do for PostgreSQL
  56. }
  57. /** @param array{0:string,1:int,2:string} $errorInfo */
  58. #[\Override]
  59. protected function autoUpdateDb(array $errorInfo): bool {
  60. if (isset($errorInfo[0])) {
  61. if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_FIELD_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_COLUMN) {
  62. $errorLines = explode("\n", $errorInfo[2], 2); // The relevant column name is on the first line, other lines are noise
  63. foreach (['attributes', 'lastUserModified'] as $column) {
  64. if (str_contains($errorLines[0], $column)) {
  65. return $this->addColumn($column);
  66. }
  67. }
  68. }
  69. }
  70. return false;
  71. }
  72. #[\Override]
  73. public function commitNewEntries(): bool {
  74. //TODO: Update to PostgreSQL 9.5+ syntax with ON CONFLICT DO NOTHING
  75. $sql = 'DO $$
  76. DECLARE
  77. maxrank bigint := (SELECT MAX(id) FROM `_entrytmp`);
  78. rank bigint := (SELECT maxrank - COUNT(*) FROM `_entrytmp`);
  79. BEGIN
  80. INSERT INTO `_entry`
  81. (id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes)
  82. (SELECT rank + row_number() OVER(ORDER BY date, id) AS id, guid, title, author, content,
  83. link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes
  84. FROM `_entrytmp` AS etmp
  85. WHERE NOT EXISTS (
  86. SELECT 1 FROM `_entry` AS ereal
  87. WHERE (etmp.id = ereal.id) OR (etmp.id_feed = ereal.id_feed AND etmp.guid = ereal.guid))
  88. ORDER BY date, id);
  89. DELETE FROM `_entrytmp` WHERE id <= maxrank;
  90. END $$;';
  91. $hadTransaction = $this->pdo->inTransaction();
  92. if (!$hadTransaction) {
  93. $this->pdo->beginTransaction();
  94. }
  95. $result = $this->pdo->exec($sql) !== false;
  96. if (!$hadTransaction) {
  97. $this->pdo->commit();
  98. }
  99. return $result;
  100. }
  101. }