4
0

EntryDAOPGSQL.php 3.6 KB

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