You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.6KB

  1. <?php
  2. namespace Framework\Database\Connection;
  3. use Framework\Database\Migration\SqliteMigration;
  4. use Framework\Database\QueryBuilder\SqliteQueryBuilder;
  5. use InvalidArgumentException;
  6. use Pdo;
  7. class SqliteConnection extends Connection
  8. {
  9. private Pdo $pdo;
  10. private array $config;
  11. public function __construct(array $config)
  12. {
  13. ['path' => $path] = $config;
  14. if (empty($path)) {
  15. throw new InvalidArgumentException('Connection incorrectly configured');
  16. }
  17. $this->pdo = new Pdo("sqlite:{$path}");
  18. $this->config = $config;
  19. }
  20. public function pdo(): Pdo
  21. {
  22. return $this->pdo;
  23. }
  24. public function query(): SqliteQueryBuilder
  25. {
  26. return new SqliteQueryBuilder($this);
  27. }
  28. public function createTable(string $table): SqliteMigration
  29. {
  30. return new SqliteMigration($this, $table, 'create');
  31. }
  32. public function alterTable(string $table): SqliteMigration
  33. {
  34. return new SqliteMigration($this, $table, 'alter');
  35. }
  36. public function getTables(): array
  37. {
  38. $statement = $this->pdo->prepare("SELECT name FROM sqlite_master WHERE type = 'table'");
  39. $statement->execute();
  40. $results = $statement->fetchAll(PDO::FETCH_NUM);
  41. $results = array_map(fn($result) => $result[0], $results);
  42. return $results;
  43. }
  44. public function hasTable(string $name): bool
  45. {
  46. $tables = $this->getTables();
  47. return in_array($name, $tables);
  48. }
  49. public function dropTables(): int
  50. {
  51. file_put_contents($this->config['path'], '');
  52. return 1;
  53. }
  54. }

Powered by TurnKey Linux.