4
0

DatabaseDAOSQLite.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * This class is used to test database is well-constructed (SQLite).
  4. */
  5. class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO {
  6. public function tablesAreCorrect() {
  7. $sql = 'SELECT name FROM sqlite_master WHERE type="table"';
  8. $stm = $this->bd->prepare($sql);
  9. $stm->execute();
  10. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  11. $tables = array(
  12. 'category' => false,
  13. 'feed' => false,
  14. 'entry' => false,
  15. 'entrytmp' => false,
  16. 'tag' => false,
  17. 'entrytag' => false,
  18. );
  19. foreach ($res as $value) {
  20. $tables[$value['name']] = true;
  21. }
  22. return count(array_keys($tables, true, true)) == count($tables);
  23. }
  24. public function getSchema($table) {
  25. $sql = 'PRAGMA table_info(' . $table . ')';
  26. $stm = $this->bd->prepare($sql);
  27. $stm->execute();
  28. return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
  29. }
  30. public function entryIsCorrect() {
  31. return $this->checkTable('entry', array(
  32. 'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read',
  33. 'is_favorite', 'id_feed', 'tags',
  34. ));
  35. }
  36. public function entrytmpIsCorrect() {
  37. return $this->checkTable('entrytmp', array(
  38. 'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read',
  39. 'is_favorite', 'id_feed', 'tags',
  40. ));
  41. }
  42. public function daoToSchema($dao) {
  43. return array(
  44. 'name' => $dao['name'],
  45. 'type' => strtolower($dao['type']),
  46. 'notnull' => $dao['notnull'] === '1' ? true : false,
  47. 'default' => $dao['dflt_value'],
  48. );
  49. }
  50. public function size($all = false) {
  51. return @filesize(join_path(DATA_PATH, 'users', $this->current_user, 'db.sqlite'));
  52. }
  53. public function optimize() {
  54. $sql = 'VACUUM';
  55. $stm = $this->bd->prepare($sql);
  56. if ($stm) {
  57. return $stm->execute();
  58. }
  59. return false;
  60. }
  61. }