4
0

EntryDAOPGSQL.php 3.1 KB

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