DatabaseDAOPGSQL.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * This class is used to test database is well-constructed.
  4. */
  5. class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAO {
  6. public function tablesAreCorrect() {
  7. $db = FreshRSS_Context::$system_conf->db;
  8. $dbowner = $db['user'];
  9. $sql = 'SELECT * FROM pg_catalog.pg_tables where tableowner=?';
  10. $stm = $this->bd->prepare($sql);
  11. $values = array($dbowner);
  12. $stm->execute($values);
  13. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  14. $tables = array(
  15. $this->prefix . 'category' => false,
  16. $this->prefix . 'feed' => false,
  17. $this->prefix . 'entry' => false,
  18. );
  19. foreach ($res as $value) {
  20. $tables[array_pop($value)] = true;
  21. }
  22. return count(array_keys($tables, true, true)) == count($tables);
  23. }
  24. public function getSchema($table) {
  25. $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 = ?';
  26. $stm = $this->bd->prepare($sql);
  27. $stm->execute(array($this->prefix . $table));
  28. return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
  29. }
  30. public function daoToSchema($dao) {
  31. return array(
  32. 'name' => $dao['field'],
  33. 'type' => strtolower($dao['type']),
  34. 'notnull' => (bool)$dao['null'],
  35. 'default' => $dao['default'],
  36. );
  37. }
  38. public function size($all = true) {
  39. $db = FreshRSS_Context::$system_conf->db;
  40. $sql = 'SELECT pg_size_pretty(pg_database_size(?))';
  41. $values = array($db['base']);
  42. $stm = $this->bd->prepare($sql);
  43. $stm->execute($values);
  44. $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
  45. return $res[0];
  46. }
  47. public function optimize() {
  48. $ok = true;
  49. $sql = 'VACUUM `' . $this->prefix . 'entry`';
  50. $stm = $this->bd->prepare($sql);
  51. $ok &= $stm != false;
  52. if ($stm) {
  53. $ok &= $stm->execute();
  54. }
  55. $sql = 'VACUUM `' . $this->prefix . 'feed`';
  56. $stm = $this->bd->prepare($sql);
  57. $ok &= $stm != false;
  58. if ($stm) {
  59. $ok &= $stm->execute();
  60. }
  61. $sql = 'VACUUM `' . $this->prefix . 'category`';
  62. $stm = $this->bd->prepare($sql);
  63. $ok &= $stm != false;
  64. if ($stm) {
  65. $ok &= $stm->execute();
  66. }
  67. return $ok;
  68. }
  69. }