DatabaseDAOPGSQL.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * This class is used to test database is well-constructed.
  4. */
  5. class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite {
  6. //PostgreSQL error codes
  7. const UNDEFINED_COLUMN = '42703';
  8. const UNDEFINED_TABLE = '42P01';
  9. public function tablesAreCorrect() {
  10. $db = FreshRSS_Context::$system_conf->db;
  11. $dbowner = $db['user'];
  12. $sql = 'SELECT * FROM pg_catalog.pg_tables where tableowner=?';
  13. $stm = $this->pdo->prepare($sql);
  14. $values = array($dbowner);
  15. $stm->execute($values);
  16. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  17. $tables = array(
  18. $this->pdo->prefix() . 'category' => false,
  19. $this->pdo->prefix() . 'feed' => false,
  20. $this->pdo->prefix() . 'entry' => false,
  21. $this->pdo->prefix() . 'entrytmp' => false,
  22. $this->pdo->prefix() . 'tag' => false,
  23. $this->pdo->prefix() . 'entrytag' => false,
  24. );
  25. foreach ($res as $value) {
  26. $tables[array_pop($value)] = true;
  27. }
  28. return count(array_keys($tables, true, true)) == count($tables);
  29. }
  30. public function getSchema($table) {
  31. $sql = 'select column_name as field, data_type as type, column_default as default, is_nullable as null from INFORMATION_SCHEMA.COLUMNS where table_name = ?';
  32. $stm = $this->pdo->prepare($sql);
  33. $stm->execute(array($this->pdo->prefix() . $table));
  34. return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
  35. }
  36. public function daoToSchema($dao) {
  37. return array(
  38. 'name' => $dao['field'],
  39. 'type' => strtolower($dao['type']),
  40. 'notnull' => (bool)$dao['null'],
  41. 'default' => $dao['default'],
  42. );
  43. }
  44. public function size($all = false) {
  45. if ($all) {
  46. $db = FreshRSS_Context::$system_conf->db;
  47. $sql = 'SELECT pg_database_size(:base)';
  48. $stm = $this->pdo->prepare($sql);
  49. $stm->bindParam(':base', $db['base']);
  50. $stm->execute();
  51. } else {
  52. $sql = "SELECT "
  53. . "pg_total_relation_size('{$this->pdo->prefix()}category') + "
  54. . "pg_total_relation_size('{$this->pdo->prefix()}feed') + "
  55. . "pg_total_relation_size('{$this->pdo->prefix()}entry') + "
  56. . "pg_total_relation_size('{$this->pdo->prefix()}entrytmp') + "
  57. . "pg_total_relation_size('{$this->pdo->prefix()}tag') + "
  58. . "pg_total_relation_size('{$this->pdo->prefix()}entrytag')";
  59. $stm = $this->pdo->query($sql);
  60. }
  61. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  62. return $res[0];
  63. }
  64. public function optimize() {
  65. $ok = true;
  66. $tables = array('category', 'feed', 'entry', 'entrytmp', 'tag', 'entrytag');
  67. foreach ($tables as $table) {
  68. $sql = 'VACUUM `_' . $table . '`';
  69. $ok &= ($this->pdo->exec($sql) !== false);
  70. }
  71. return $ok;
  72. }
  73. }