4
0

DatabaseDAOSQLite.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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(): bool {
  7. $sql = 'SELECT name FROM sqlite_master WHERE type="table"';
  8. $stm = $this->pdo->query($sql);
  9. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  10. $tables = array(
  11. $this->pdo->prefix() . 'category' => false,
  12. $this->pdo->prefix() . 'feed' => false,
  13. $this->pdo->prefix() . 'entry' => false,
  14. $this->pdo->prefix() . 'entrytmp' => false,
  15. $this->pdo->prefix() . 'tag' => false,
  16. $this->pdo->prefix() . 'entrytag' => false,
  17. );
  18. foreach ($res as $value) {
  19. $tables[$value['name']] = true;
  20. }
  21. return count(array_keys($tables, true, true)) == count($tables);
  22. }
  23. public function getSchema(string $table): array {
  24. $sql = 'PRAGMA table_info(' . $table . ')';
  25. $stm = $this->pdo->query($sql);
  26. return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
  27. }
  28. public function entryIsCorrect(): bool {
  29. return $this->checkTable('entry', array(
  30. 'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read',
  31. 'is_favorite', 'id_feed', 'tags',
  32. ));
  33. }
  34. public function entrytmpIsCorrect(): bool {
  35. return $this->checkTable('entrytmp', array(
  36. 'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read',
  37. 'is_favorite', 'id_feed', 'tags',
  38. ));
  39. }
  40. public function daoToSchema(array $dao): array {
  41. return [
  42. 'name' => $dao['name'],
  43. 'type' => strtolower($dao['type']),
  44. 'notnull' => $dao['notnull'] === '1' ? true : false,
  45. 'default' => $dao['dflt_value'],
  46. ];
  47. }
  48. public function size(bool $all = false): int {
  49. $sum = 0;
  50. if ($all) {
  51. foreach (glob(DATA_PATH . '/users/*/db.sqlite') as $filename) {
  52. $sum += @filesize($filename);
  53. }
  54. } else {
  55. $sum = @filesize(DATA_PATH . '/users/' . $this->current_user . '/db.sqlite');
  56. }
  57. return intval($sum);
  58. }
  59. public function optimize(): bool {
  60. $ok = $this->pdo->exec('VACUUM') !== false;
  61. if (!$ok) {
  62. $info = $this->pdo->errorInfo();
  63. Minz_Log::warning(__METHOD__ . ' error : ' . json_encode($info));
  64. }
  65. return $ok;
  66. }
  67. }