DatabaseDAOPGSQL.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This class is used to test database is well-constructed.
  5. */
  6. class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite {
  7. //PostgreSQL error codes
  8. public const UNDEFINED_COLUMN = '42703';
  9. public const UNDEFINED_TABLE = '42P01';
  10. #[\Override]
  11. public function tablesAreCorrect(): bool {
  12. $db = FreshRSS_Context::systemConf()->db;
  13. $sql = 'SELECT * FROM pg_catalog.pg_tables where tableowner=:tableowner';
  14. $res = $this->fetchAssoc($sql, [':tableowner' => $db['user']]);
  15. if ($res == null) {
  16. return false;
  17. }
  18. $tables = [
  19. $this->pdo->prefix() . 'category' => false,
  20. $this->pdo->prefix() . 'feed' => false,
  21. $this->pdo->prefix() . 'entry' => false,
  22. $this->pdo->prefix() . 'entrytmp' => false,
  23. $this->pdo->prefix() . 'tag' => false,
  24. $this->pdo->prefix() . 'entrytag' => false,
  25. ];
  26. foreach ($res as $value) {
  27. $tables[array_pop($value)] = true;
  28. }
  29. return count(array_keys($tables, true, true)) === count($tables);
  30. }
  31. /** @return list<array{name:string,type:string,notnull:bool,default:mixed}> */
  32. #[\Override]
  33. public function getSchema(string $table): array {
  34. $sql = <<<'SQL'
  35. SELECT column_name AS field, data_type AS type, column_default AS default, is_nullable AS null
  36. FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table_name
  37. SQL;
  38. $res = $this->fetchAssoc($sql, [':table_name' => $this->pdo->prefix() . $table]);
  39. return $res == null ? [] : $this->listDaoToSchema($res);
  40. }
  41. /**
  42. * @param array<string,string|int|bool|null> $dao
  43. * @return array{'name':string,'type':string,'notnull':bool,'default':mixed}
  44. */
  45. #[\Override]
  46. public function daoToSchema(array $dao): array {
  47. return [
  48. 'name' => is_string($dao['field'] ?? null) ? $dao['field'] : '',
  49. 'type' => is_string($dao['type'] ?? null) ? strtolower($dao['type']) : '',
  50. 'notnull' => empty($dao['null']),
  51. 'default' => is_scalar($dao['default'] ?? null) ? $dao['default'] : null,
  52. ];
  53. }
  54. #[\Override]
  55. protected function selectVersion(): string {
  56. return $this->fetchValue('SELECT version()') ?? '';
  57. }
  58. #[\Override]
  59. public function size(bool $all = false): int {
  60. if ($all) {
  61. $db = FreshRSS_Context::systemConf()->db;
  62. $res = $this->fetchColumn('SELECT pg_database_size(:base)', 0, [':base' => $db['base']]);
  63. } else {
  64. $sql = <<<SQL
  65. SELECT
  66. pg_total_relation_size('`{$this->pdo->prefix()}category`') +
  67. pg_total_relation_size('`{$this->pdo->prefix()}feed`') +
  68. pg_total_relation_size('`{$this->pdo->prefix()}entry`') +
  69. pg_total_relation_size('`{$this->pdo->prefix()}entrytmp`') +
  70. pg_total_relation_size('`{$this->pdo->prefix()}tag`') +
  71. pg_total_relation_size('`{$this->pdo->prefix()}entrytag`')
  72. SQL;
  73. $res = $this->fetchColumn($sql, 0);
  74. }
  75. return (int)($res[0] ?? -1);
  76. }
  77. #[\Override]
  78. public function optimize(): bool {
  79. $ok = true;
  80. $tables = ['category', 'feed', 'entry', 'entrytmp', 'tag', 'entrytag'];
  81. foreach ($tables as $table) {
  82. $sql = 'VACUUM `_' . $table . '`';
  83. if ($this->pdo->exec($sql) === false) {
  84. $ok = false;
  85. $info = $this->pdo->errorInfo();
  86. Minz_Log::warning(__METHOD__ . ' error: ' . $sql . ' : ' . json_encode($info));
  87. }
  88. }
  89. return $ok;
  90. }
  91. }