|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace imperazim\db; |
| 6 | + |
| 7 | +use GlobalLogger; |
| 8 | +use PDO; |
| 9 | +use PDOException; |
| 10 | +use imperazim\db\exception\DatabaseException; |
| 11 | + |
| 12 | +/** |
| 13 | +* Class Sqlite3 |
| 14 | +* @package imperazim\db |
| 15 | +*/ |
| 16 | +final class Sqlite3 { |
| 17 | + |
| 18 | + /** @var PDO|null */ |
| 19 | + private ?PDO $sqlite = null; |
| 20 | + |
| 21 | + /** |
| 22 | + * Sqlite3 constructor. |
| 23 | + * @param string $directory The directory of the file. |
| 24 | + * @param string $fileName The name of the file. |
| 25 | + */ |
| 26 | + public function __construct( |
| 27 | + private ?string $directory, |
| 28 | + private ?string $fileName |
| 29 | + ) { |
| 30 | + $directory = rtrim(str_replace('//', '/', $directory . '/'), '/') . '/'; |
| 31 | + try { |
| 32 | + $this->sqlite = new PDO('sqlite:' . $directory . $fileName . '.db', null, null, [ |
| 33 | + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
| 34 | + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC |
| 35 | + ]); |
| 36 | + } catch (PDOException $e) { |
| 37 | + throw new DatabaseException("SQLite connection error: " . $e->getMessage(), 0, $e); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Creates tables if they do not exist. |
| 43 | + * @param array $tables An associative array where the key is the table name and the value is the SQL query to create the table. |
| 44 | + * @return void |
| 45 | + */ |
| 46 | + public function createTableIfNotExists(array $tables): void { |
| 47 | + try { |
| 48 | + foreach ($tables as $table => $rows) { |
| 49 | + $rows = implode(", ", array_keys($rows)); |
| 50 | + $this->sqlite->exec("CREATE TABLE IF NOT EXISTS $table ($rows)"); |
| 51 | + } |
| 52 | + } catch (PDOException $e) { |
| 53 | + throw new DatabaseException("SQLite create table error: " . $e->getMessage(), 0, $e); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Inserts data into a specified table. |
| 59 | + * @param string $table The name of the table to insert data into. |
| 60 | + * @param array $data An associative array where the key is the column name and the value is the value to insert. |
| 61 | + * @return void |
| 62 | + */ |
| 63 | + public function insert(string $table, array $data): void { |
| 64 | + try { |
| 65 | + $columns = implode(", ", array_keys($data)); |
| 66 | + $placeholders = implode(", ", array_fill(0, count($data), "?")); |
| 67 | + $values = array_values($data); |
| 68 | + |
| 69 | + $sql = "INSERT INTO $table ($columns) VALUES ($placeholders)"; |
| 70 | + $stmt = $this->sqlite->prepare($sql); |
| 71 | + $stmt->execute($values); |
| 72 | + } catch (PDOException $e) { |
| 73 | + throw new DatabaseException("SQLite insert error: " . $e->getMessage(), 0, $e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Selects data from a specified table with optional filters. |
| 79 | + * @param string $table The name of the table to select data from. |
| 80 | + * @param string $column The column(s) to select. |
| 81 | + * @param array $filters An array of associative arrays for filtering the results. |
| 82 | + * @return array The selected data. |
| 83 | + */ |
| 84 | + public function select(string $table, string $column, array $filters = []): array { |
| 85 | + try { |
| 86 | + $sql = "SELECT $column FROM $table"; |
| 87 | + $values = []; |
| 88 | + |
| 89 | + if (!empty($filters)) { |
| 90 | + $sql .= " WHERE " . $this->buildWhereClause($filters, $values); |
| 91 | + } |
| 92 | + |
| 93 | + $stmt = $this->sqlite->prepare($sql); |
| 94 | + $stmt->execute($values); |
| 95 | + return $stmt->fetchAll(); |
| 96 | + } catch (PDOException $e) { |
| 97 | + throw new DatabaseException("SQLite select error: " . $e->getMessage(), 0, $e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Updates data in a specified table. |
| 103 | + * @param string $table The name of the table to update data in. |
| 104 | + * @param string $column The column to update. |
| 105 | + * @param mixed $value The new value to set. |
| 106 | + * @param array $filters An array of associative arrays for filtering the rows to update. |
| 107 | + * @return bool Whether any rows were updated. |
| 108 | + */ |
| 109 | + public function update(string $table, string $column, $value, array $filters = []): bool { |
| 110 | + try { |
| 111 | + $sql = "UPDATE $table SET $column = ?"; |
| 112 | + $values = [$value]; |
| 113 | + if (!empty($filters)) { |
| 114 | + $sql .= " WHERE " . $this->buildWhereClause($filters, $values); |
| 115 | + } |
| 116 | + $stmt = $this->sqlite->prepare($sql); |
| 117 | + $stmt->execute($values); |
| 118 | + return $stmt->rowCount() > 0; |
| 119 | + } catch (PDOException $e) { |
| 120 | + throw new DatabaseException("SQLite update error: " . $e->getMessage(), 0, $e); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Checks if a record exists in a specified table. |
| 126 | + * @param string $table The name of the table to check. |
| 127 | + * @param array $conditions An array of associative arrays for the conditions to check. |
| 128 | + * @return bool Whether the record exists. |
| 129 | + */ |
| 130 | + public function exists(string $table, array $conditions): bool { |
| 131 | + try { |
| 132 | + $sql = "SELECT COUNT(*) AS count FROM $table WHERE "; |
| 133 | + $values = []; |
| 134 | + |
| 135 | + $sql .= $this->buildWhereClause($conditions, $values); |
| 136 | + |
| 137 | + $stmt = $this->sqlite->prepare($sql); |
| 138 | + $stmt->execute($values); |
| 139 | + $row = $stmt->fetch(); |
| 140 | + return $row['count'] > 0; |
| 141 | + } catch (PDOException $e) { |
| 142 | + throw new DatabaseException("SQLite exists error: " . $e->getMessage(), 0, $e); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Builds a WHERE clause from an array of conditions. |
| 148 | + * @param array $filters An array of associative arrays for filtering the results. |
| 149 | + * @param array $values Reference to the values array to be used in the prepared statement. |
| 150 | + * @return string The WHERE clause. |
| 151 | + */ |
| 152 | + private function buildWhereClause(array $filters, array &$values): string { |
| 153 | + $whereClauses = []; |
| 154 | + foreach ($filters as $filter) { |
| 155 | + $clauses = []; |
| 156 | + foreach ($filter as $key => $value) { |
| 157 | + $clauses[] = "$key = ?"; |
| 158 | + $values[] = $value; |
| 159 | + } |
| 160 | + $whereClauses[] = implode(" AND ", $clauses); |
| 161 | + } |
| 162 | + return implode(" AND ", $whereClauses); |
| 163 | + } |
| 164 | +} |
0 commit comments