4
0

EntryDAOPGSQL.php 3.0 KB

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