DatabaseDAOSQLite.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 !== false ? $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. if (is_array($value) && is_string($value['name'] ?? null)) {
  25. $tables[$value['name']] = true;
  26. }
  27. }
  28. return count(array_keys($tables, true, true)) == count($tables);
  29. }
  30. /** @return list<array{name:string,type:string,notnull:bool,default:mixed}> */
  31. #[\Override]
  32. public function getSchema(string $table): array {
  33. $sql = 'PRAGMA table_info(' . $table . ')';
  34. $stm = $this->pdo->query($sql);
  35. if ($stm !== false && ($res = $stm->fetchAll(PDO::FETCH_ASSOC)) !== false) {
  36. /** @var list<array{name:string,type:string,notnull:bool,dflt_value:string|int|bool|null}> $res */
  37. return $this->listDaoToSchema($res);
  38. }
  39. return [];
  40. }
  41. #[\Override]
  42. public function entryIsCorrect(): bool {
  43. return $this->checkTable('entry', [
  44. 'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read', 'is_favorite', 'id_feed', 'tags',
  45. ]);
  46. }
  47. #[\Override]
  48. public function entrytmpIsCorrect(): bool {
  49. return $this->checkTable('entrytmp', [
  50. 'id', 'guid', 'title', 'author', 'content', 'link', 'date', 'lastSeen', 'hash', 'is_read', 'is_favorite', 'id_feed', 'tags'
  51. ]);
  52. }
  53. /**
  54. * @param array<string,string|int|bool|null> $dao
  55. * @return array{'name':string,'type':string,'notnull':bool,'default':mixed}
  56. */
  57. #[\Override]
  58. public function daoToSchema(array $dao): array {
  59. return [
  60. 'name' => is_string($dao['name'] ?? null) ? $dao['name'] : '',
  61. 'type' => is_string($dao['type'] ?? null) ? strtolower($dao['type']) : '',
  62. 'notnull' => empty($dao['notnull']),
  63. 'default' => is_scalar($dao['dflt_value'] ?? null) ? $dao['dflt_value'] : null,
  64. ];
  65. }
  66. #[\Override]
  67. public function size(bool $all = false): int {
  68. $sum = 0;
  69. if ($all) {
  70. foreach (glob(DATA_PATH . '/users/*/db.sqlite') ?: [] as $filename) {
  71. $sum += (@filesize($filename) ?: 0);
  72. }
  73. } else {
  74. $sum = (@filesize(DATA_PATH . '/users/' . $this->current_user . '/db.sqlite') ?: 0);
  75. }
  76. return $sum;
  77. }
  78. #[\Override]
  79. public function optimize(): bool {
  80. $ok = $this->pdo->exec('VACUUM') !== false;
  81. if (!$ok) {
  82. $info = $this->pdo->errorInfo();
  83. Minz_Log::warning(__METHOD__ . ' error : ' . json_encode($info));
  84. }
  85. return $ok;
  86. }
  87. }