DatabaseDAOSQLite.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. protected function selectVersion(): string {
  68. return $this->fetchValue('SELECT sqlite_version()') ?? '';
  69. }
  70. #[\Override]
  71. public function size(bool $all = false): int {
  72. $sum = 0;
  73. if ($all) {
  74. foreach (glob(DATA_PATH . '/users/*/db.sqlite') ?: [] as $filename) {
  75. $sum += (@filesize($filename) ?: 0);
  76. }
  77. } else {
  78. $sum = (@filesize(DATA_PATH . '/users/' . $this->current_user . '/db.sqlite') ?: 0);
  79. }
  80. return $sum;
  81. }
  82. #[\Override]
  83. public function optimize(): bool {
  84. $ok = $this->pdo->exec('VACUUM') !== false;
  85. if (!$ok) {
  86. $info = $this->pdo->errorInfo();
  87. Minz_Log::warning(__METHOD__ . ' error : ' . json_encode($info));
  88. }
  89. return $ok;
  90. }
  91. #[\Override]
  92. public static function strilike(string $haystack, string $needle): bool {
  93. return stripos($haystack, $needle) !== false;
  94. }
  95. }