Skip to content

Commit bd759b2

Browse files
committed
[sync] Update embedded LibDB from standalone
1 parent e66f935 commit bd759b2

14 files changed

Lines changed: 2012 additions & 218 deletions

File tree

src/imperazim/db/DBManager.php

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,49 @@
55
namespace imperazim\db;
66

77
use imperazim\db\exception\DatabaseException;
8+
use Exception;
89

910
/**
10-
* Class DBManager
11-
* @package imperazim\db
11+
* Factory for creating database connections.
12+
*
13+
* Usage:
14+
* $db = DBManager::connect('sqlite', ['database' => '/path/to/mydb']);
15+
* $db = DBManager::connect('mysql', [
16+
* 'host' => 'localhost',
17+
* 'username' => 'root',
18+
* 'password' => '',
19+
* 'database' => 'mydb'
20+
* ]);
1221
*/
1322
final class DBManager {
1423

15-
/**
16-
* Connects to the specified database using the provided configuration.
17-
* @param string $type The type of database (e.g., 'mysql', 'sqlite').
18-
* @param array $config The configuration array for the database connection.
19-
* @return mixed The database instance.
20-
* @throws DatabaseException If the connection fails or the configuration is invalid.
21-
*/
22-
public static function connect(string $type, array $config): mixed {
23-
try {
24-
switch (strtolower($type)) {
25-
case 'mysql':
26-
return Mysql::connect($config['host'], $config['username'], $config['password'], $config['database']);
27-
case 'sqlite':
28-
$directory = dirname($config['database']);
29-
$fileName = pathinfo($config['database'], PATHINFO_FILENAME);
30-
return new Sqlite3($directory, $fileName);
31-
default:
32-
throw new DatabaseException("Unsupported database type: $type");
33-
}
34-
} catch (\Exception $e) {
35-
throw new DatabaseException("Failed to connect to the database: " . $e->getMessage(), 0, $e);
24+
/**
25+
* Connects to the specified database.
26+
*
27+
* @param string $type Database type: 'mysql' or 'sqlite'
28+
* @param array $config Connection configuration
29+
* @return Database Database instance
30+
* @throws DatabaseException If connection fails or type unsupported
31+
*/
32+
public static function connect(string $type, array $config): Database {
33+
try {
34+
return match (strtolower($type)) {
35+
'mysql' => Mysql::connect(
36+
$config['host'],
37+
$config['username'],
38+
$config['password'],
39+
$config['database']
40+
),
41+
'sqlite' => new Sqlite3(
42+
dirname($config['database']),
43+
pathinfo($config['database'], PATHINFO_FILENAME)
44+
),
45+
default => throw new DatabaseException("Unsupported database type: $type"),
46+
};
47+
} catch (DatabaseException $e) {
48+
throw $e;
49+
} catch (Exception $e) {
50+
throw new DatabaseException("Failed to connect to the database: " . $e->getMessage(), 0, $e);
51+
}
3652
}
37-
}
3853
}

src/imperazim/db/Database.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)