DatabaseDAOSQLite.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This class is used to test database is well-constructed (SQLite).
  5. */
  6. class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO {
  7. #[\Override]
  8. public function tablesAreCorrect(): bool {
  9. $sql = 'SELECT name FROM sqlite_master WHERE type="table"';
  10. $stm = $this->pdo->query($sql);
  11. $res = $stm ? $stm->fetchAll(PDO::FETCH_ASSOC) : false;
  12. if ($res === false) {
  13. return false;
  14. }
  15. $tables = [
  16. $this->pdo->prefix() . 'category' => false,
  17. $this->pdo->prefix() . 'feed' => false,
  18. $this->pdo->prefix() . 'entry' => false,
  19. $this->pdo->prefix() . 'entrytmp' => false,
  20. $this->pdo->prefix() . 'tag' => false,
  21. $this->pdo->prefix() . 'entrytag' => false,
  22. ];
  23. foreach ($res as $value) {
  24. $tables[$value['name']] = true;
  25. }
  26. return count(array_keys($tables, true, true)) == count($tables);
  27. }
  28. /** @return array<array<string,string|int|bool|null>> */
  29. #[\Override]
  30. public function getSchema(string $table): array {
  31. $sql = 'PRAGMA table_info(' . $table . ')';
  32. $stm = $this->pdo->query($sql);
  33. return $stm ? $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC) ?: []) : [];
  34. }
  35. #[\Override]
  36. public function entryIsCorrect(): bool {
  37. return $this->checkTable('entry', [
  38. 'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read', 'is_favorite', 'id_feed', 'tags',
  39. ]);
  40. }
  41. #[\Override]
  42. public function entrytmpIsCorrect(): bool {
  43. return $this->checkTable('entrytmp', [
  44. 'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read', 'is_favorite', 'id_feed', 'tags'
  45. ]);
  46. }
  47. /**
  48. * @param array<string,string|int|bool|null> $dao
  49. * @return array{'name':string,'type':string,'notnull':bool,'default':mixed}
  50. */
  51. #[\Override]
  52. public function daoToSchema(array $dao): array {
  53. return [
  54. 'name' => (string)$dao['name'],
  55. 'type' => strtolower((string)$dao['type']),
  56. 'notnull' => $dao['notnull'] == '1' ? true : false,
  57. 'default' => $dao['dflt_value'],
  58. ];
  59. }
  60. #[\Override]
  61. public function size(bool $all = false): int {
  62. $sum = 0;
  63. if ($all) {
  64. foreach (glob(DATA_PATH . '/users/*/db.sqlite') ?: [] as $filename) {
  65. $sum += (@filesize($filename) ?: 0);
  66. }
  67. } else {
  68. $sum = (@filesize(DATA_PATH . '/users/' . $this->current_user . '/db.sqlite') ?: 0);
  69. }
  70. return $sum;
  71. }
  72. #[\Override]
  73. public function optimize(): bool {
  74. $ok = $this->pdo->exec('VACUUM') !== false;
  75. if (!$ok) {
  76. $info = $this->pdo->errorInfo();
  77. Minz_Log::warning(__METHOD__ . ' error : ' . json_encode($info));
  78. }
  79. return $ok;
  80. }
  81. }