DatabaseDAOSQLite.php 2.9 KB

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