4
0

EntryDAOPGSQL.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. $replacements = [ // Convert some of the PCRE regex syntax to PostgreSQL
  38. '\\b' => '\\y', // matches only at the beginning or end of a word (was: backspace)
  39. '\\B' => '\\Y', // matches only at a point that is not the beginning or end of a word (was: backslash)
  40. ];
  41. $matches['pattern'] = str_replace(array_keys($replacements), array_values($replacements), $matches['pattern']);
  42. $matchType = $matches['matchType'] ?? '';
  43. if (str_contains($matchType, 'm')) {
  44. // newline-sensitive matching
  45. $matches['pattern'] = '(?m)' . $matches['pattern'];
  46. }
  47. $values[] = $matches['pattern'];
  48. if (str_contains($matchType, 'i')) {
  49. // case-insensitive matching
  50. return "{$expression} ~* ?";
  51. } else {
  52. // case-sensitive matching
  53. return "{$expression} ~ ?";
  54. }
  55. }
  56. return '';
  57. }
  58. #[\Override]
  59. protected function registerSqlFunctions(string $sql): void {
  60. // Nothing to do for PostgreSQL
  61. }
  62. /** @param array{0:string,1:int,2:string} $errorInfo */
  63. #[\Override]
  64. protected function autoUpdateDb(array $errorInfo): bool {
  65. if (isset($errorInfo[0])) {
  66. if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_FIELD_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_COLUMN) {
  67. $errorLines = explode("\n", $errorInfo[2], 2); // The relevant column name is on the first line, other lines are noise
  68. foreach (['attributes', 'lastUserModified'] as $column) {
  69. if (str_contains($errorLines[0], $column)) {
  70. return $this->addColumn($column);
  71. }
  72. }
  73. }
  74. }
  75. return false;
  76. }
  77. #[\Override]
  78. public function commitNewEntries(): bool {
  79. //TODO: Update to PostgreSQL 9.5+ syntax with ON CONFLICT DO NOTHING
  80. $sql = <<<'SQL'
  81. DO $$
  82. DECLARE
  83. maxrank bigint := (SELECT MAX(id) FROM `_entrytmp`);
  84. rank bigint := (SELECT maxrank - COUNT(*) FROM `_entrytmp`);
  85. BEGIN
  86. INSERT INTO `_entry`
  87. (id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes)
  88. (SELECT rank + row_number() OVER(ORDER BY etmp.date, etmp.id) AS id, guid, title, author, content,
  89. link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes
  90. FROM `_entrytmp` AS etmp
  91. WHERE NOT EXISTS (
  92. SELECT 1 FROM `_entry` AS ereal
  93. WHERE (etmp.id = ereal.id) OR (etmp.id_feed = ereal.id_feed AND etmp.guid = ereal.guid))
  94. ORDER BY etmp.date, etmp.id);
  95. DELETE FROM `_entrytmp` WHERE id <= maxrank;
  96. END $$;
  97. SQL;
  98. $hadTransaction = $this->pdo->inTransaction();
  99. if (!$hadTransaction) {
  100. $this->pdo->beginTransaction();
  101. }
  102. $result = $this->pdo->exec($sql) !== false;
  103. if (!$hadTransaction) {
  104. $this->pdo->commit();
  105. }
  106. return $result;
  107. }
  108. }