DatabaseDAOPGSQL.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 array<array<string,string|int|bool|null>> */
  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' => (string)($dao['field']),
  49. 'type' => strtolower((string)($dao['type'])),
  50. 'notnull' => (bool)$dao['null'],
  51. 'default' => $dao['default'],
  52. ];
  53. }
  54. #[\Override]
  55. public function size(bool $all = false): int {
  56. if ($all) {
  57. $db = FreshRSS_Context::systemConf()->db;
  58. $res = $this->fetchColumn('SELECT pg_database_size(:base)', 0, [':base' => $db['base']]);
  59. } else {
  60. $sql = <<<SQL
  61. SELECT
  62. pg_total_relation_size('`{$this->pdo->prefix()}category`') +
  63. pg_total_relation_size('`{$this->pdo->prefix()}feed`') +
  64. pg_total_relation_size('`{$this->pdo->prefix()}entry`') +
  65. pg_total_relation_size('`{$this->pdo->prefix()}entrytmp`') +
  66. pg_total_relation_size('`{$this->pdo->prefix()}tag`') +
  67. pg_total_relation_size('`{$this->pdo->prefix()}entrytag`')
  68. SQL;
  69. $res = $this->fetchColumn($sql, 0);
  70. }
  71. return (int)($res[0] ?? -1);
  72. }
  73. #[\Override]
  74. public function optimize(): bool {
  75. $ok = true;
  76. $tables = ['category', 'feed', 'entry', 'entrytmp', 'tag', 'entrytag'];
  77. foreach ($tables as $table) {
  78. $sql = 'VACUUM `_' . $table . '`';
  79. if ($this->pdo->exec($sql) === false) {
  80. $ok = false;
  81. $info = $this->pdo->errorInfo();
  82. Minz_Log::warning(__METHOD__ . ' error: ' . $sql . ' : ' . json_encode($info));
  83. }
  84. }
  85. return $ok;
  86. }
  87. }