|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace imperazim\db; |
| 6 | + |
| 7 | +/** |
| 8 | +* Common base class for database drivers. |
| 9 | +* |
| 10 | +* Both Mysql and Sqlite3 extend this class, providing |
| 11 | +* a consistent API for CRUD operations. |
| 12 | +*/ |
| 13 | +abstract class Database { |
| 14 | + |
| 15 | + /** |
| 16 | + * Creates tables if they do not exist. |
| 17 | + * |
| 18 | + * @param array $tables Associative array: table name => column definitions array |
| 19 | + */ |
| 20 | + abstract public function createTableIfNotExists(array $tables): void; |
| 21 | + |
| 22 | + /** |
| 23 | + * Inserts a row into a table. |
| 24 | + * |
| 25 | + * @param string $table Table name |
| 26 | + * @param array $data Column => value pairs |
| 27 | + */ |
| 28 | + abstract public function insert(string $table, array $data): void; |
| 29 | + |
| 30 | + /** |
| 31 | + * Selects rows from a table. |
| 32 | + * |
| 33 | + * @param string $table Table name |
| 34 | + * @param string $columns Column(s) to select (e.g. "*", "name, age") |
| 35 | + * @param array $filters Where conditions: [["col" => "val"], ...] |
| 36 | + * @return array Result rows |
| 37 | + */ |
| 38 | + abstract public function select(string $table, string $columns, array $filters = []): array; |
| 39 | + |
| 40 | + /** |
| 41 | + * Updates rows in a table. |
| 42 | + * |
| 43 | + * @param string $table Table name |
| 44 | + * @param string $column Column to update |
| 45 | + * @param mixed $value New value |
| 46 | + * @param array $filters Where conditions |
| 47 | + * @return bool Whether any rows were affected |
| 48 | + */ |
| 49 | + abstract public function update(string $table, string $column, mixed $value, array $filters = []): bool; |
| 50 | + |
| 51 | + /** |
| 52 | + * Deletes rows from a table. |
| 53 | + * |
| 54 | + * @param string $table Table name |
| 55 | + * @param array $filters Where conditions (required — no empty deletes) |
| 56 | + * @return int Number of deleted rows |
| 57 | + */ |
| 58 | + abstract public function delete(string $table, array $filters): int; |
| 59 | + |
| 60 | + /** |
| 61 | + * Checks if a record exists in a table. |
| 62 | + * |
| 63 | + * @param string $table Table name |
| 64 | + * @param array $conditions Where conditions |
| 65 | + * @return bool Whether matching record exists |
| 66 | + */ |
| 67 | + abstract public function exists(string $table, array $conditions): bool; |
| 68 | + |
| 69 | + /** |
| 70 | + * Executes a raw query with parameter binding. |
| 71 | + * |
| 72 | + * @param string $sql SQL with placeholders (?) |
| 73 | + * @param array $params Bound parameter values |
| 74 | + * @return array Result rows (empty for non-SELECT) |
| 75 | + */ |
| 76 | + abstract public function query(string $sql, array $params = []): array; |
| 77 | + |
| 78 | + /** |
| 79 | + * Closes the database connection. |
| 80 | + */ |
| 81 | + abstract public function close(): void; |
| 82 | +} |
0 commit comments