EntryDAOPGSQL.php 2.9 KB

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