Skip to content

Commit c6bace0

Browse files
committed
[sync] Update embedded LibDB from standalone
1 parent 798bc72 commit c6bace0

7 files changed

Lines changed: 38 additions & 26 deletions

File tree

src/imperazim/db/DBManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace imperazim\db;
66

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

910
/**
1011
* Factory for creating database connections.
@@ -45,7 +46,7 @@ public static function connect(string $type, array $config): Database {
4546
};
4647
} catch (DatabaseException $e) {
4748
throw $e;
48-
} catch (\Exception $e) {
49+
} catch (Exception $e) {
4950
throw new DatabaseException("Failed to connect to the database: " . $e->getMessage(), 0, $e);
5051
}
5152
}

src/imperazim/db/Mysql.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use mysqli;
88
use mysqli_result;
99
use mysqli_sql_exception;
10+
use Closure;
11+
use Throwable;
1012
use imperazim\db\exception\DatabaseException;
1113

1214
/**
@@ -206,14 +208,14 @@ public function upsert(string $table, array $data): void {
206208
/**
207209
* Executes a callback within a transaction.
208210
*
209-
* @param \Closure $callback fn(Mysql): void
211+
* @param Closure $callback fn(Mysql): void
210212
*/
211-
public function transaction(\Closure $callback): void {
213+
public function transaction(Closure $callback): void {
212214
try {
213215
$this->connection->begin_transaction();
214216
$callback($this);
215217
$this->connection->commit();
216-
} catch (\Throwable $e) {
218+
} catch (Throwable $e) {
217219
$this->connection->rollback();
218220
throw new DatabaseException("MySQL transaction error: " . $e->getMessage(), 0, $e);
219221
}

src/imperazim/db/Sqlite3.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use PDO;
88
use PDOException;
99
use PDOStatement;
10+
use Closure;
11+
use Throwable;
1012
use imperazim\db\exception\DatabaseException;
1113

1214
/**
@@ -164,14 +166,14 @@ public function upsert(string $table, array $data): void {
164166
/**
165167
* Executes a callback within a transaction.
166168
*
167-
* @param \Closure $callback fn(Sqlite3): void
169+
* @param Closure $callback fn(Sqlite3): void
168170
*/
169-
public function transaction(\Closure $callback): void {
171+
public function transaction(Closure $callback): void {
170172
try {
171173
$this->sqlite->beginTransaction();
172174
$callback($this);
173175
$this->sqlite->commit();
174-
} catch (\Throwable $e) {
176+
} catch (Throwable $e) {
175177
$this->sqlite->rollBack();
176178
throw new DatabaseException("SQLite transaction error: " . $e->getMessage(), 0, $e);
177179
}

src/imperazim/db/async/AsyncDatabaseTask.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace imperazim\db\async;
66

7+
use Closure;
8+
use RuntimeException;
9+
use Throwable;
710
use pocketmine\scheduler\AsyncTask;
811

912
/**
@@ -25,16 +28,16 @@ final class AsyncDatabaseTask extends AsyncTask {
2528
* @param array $config Connection config
2629
* @param string $sql SQL query with ? placeholders
2730
* @param array $params Bound parameters
28-
* @param \Closure|null $onComplete fn(array $rows): void
29-
* @param \Closure|null $onError fn(\Throwable $error): void
31+
* @param Closure|null $onComplete fn(array $rows): void
32+
* @param Closure|null $onError fn(Throwable $error): void
3033
*/
3134
public function __construct(
3235
string $driverType,
3336
array $config,
3437
string $sql,
3538
array $params = [],
36-
?\Closure $onComplete = null,
37-
?\Closure $onError = null
39+
?Closure $onComplete = null,
40+
?Closure $onError = null
3841
) {
3942
$this->driverType = $driverType;
4043
$this->serializedConfig = serialize($config);
@@ -55,24 +58,24 @@ public function onRun(): void {
5558
$rows = match ($this->driverType) {
5659
'sqlite' => $this->runSqlite($config, $this->sql, $params),
5760
'mysql' => $this->runMysql($config, $this->sql, $params),
58-
default => throw new \RuntimeException("Unsupported async driver: {$this->driverType}"),
61+
default => throw new RuntimeException("Unsupported async driver: {$this->driverType}"),
5962
};
6063

6164
$this->setResult(['success' => true, 'data' => serialize($rows)]);
62-
} catch (\Throwable $e) {
65+
} catch (Throwable $e) {
6366
$this->setResult(['success' => false, 'error' => $e->getMessage()]);
6467
}
6568
}
6669

6770
public function onCompletion(): void {
68-
/** @var array{onComplete: ?\Closure, onError: ?\Closure} $callbacks */
71+
/** @var array{onComplete: ?Closure, onError: ?Closure} $callbacks */
6972
$callbacks = $this->fetchLocal(self::TLS_CALLBACKS);
7073
$result = $this->getResult();
7174

7275
if ($result['success']) {
7376
$callbacks['onComplete']?->__invoke(unserialize($result['data']));
7477
} else {
75-
$callbacks['onError']?->__invoke(new \RuntimeException($result['error']));
78+
$callbacks['onError']?->__invoke(new RuntimeException($result['error']));
7679
}
7780
}
7881

@@ -88,12 +91,12 @@ private function runSqlite(array $config, string $sql, array $params): array {
8891
private function runMysql(array $config, string $sql, array $params): array {
8992
$mysqli = new \mysqli($config['host'], $config['username'], $config['password'], $config['database']);
9093
if ($mysqli->connect_error) {
91-
throw new \RuntimeException("MySQL connection failed: " . $mysqli->connect_error);
94+
throw new RuntimeException("MySQL connection failed: " . $mysqli->connect_error);
9295
}
9396

9497
$stmt = $mysqli->prepare($sql);
9598
if ($stmt === false) {
96-
throw new \RuntimeException("MySQL prepare failed: " . $mysqli->error);
99+
throw new RuntimeException("MySQL prepare failed: " . $mysqli->error);
97100
}
98101

99102
if (!empty($params)) {

src/imperazim/db/async/AsyncQuery.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace imperazim\db\async;
66

7+
use Closure;
78
use pocketmine\Server;
89

910
/**
@@ -29,10 +30,10 @@ final class AsyncQuery {
2930
* @param string $dbPath Full path to SQLite database file
3031
* @param string $sql SQL query with ? placeholders
3132
* @param array $params Bound parameters
32-
* @param \Closure|null $onComplete fn(array $rows): void — called on main thread
33-
* @param \Closure|null $onError fn(\Throwable $error): void — called on main thread
33+
* @param Closure|null $onComplete fn(array $rows): void — called on main thread
34+
* @param Closure|null $onError fn(\Throwable $error): void — called on main thread
3435
*/
35-
public static function sqlite(string $dbPath, string $sql, array $params = [], ?\Closure $onComplete = null, ?\Closure $onError = null): void {
36+
public static function sqlite(string $dbPath, string $sql, array $params = [], ?Closure $onComplete = null, ?Closure $onError = null): void {
3637
$task = new AsyncDatabaseTask('sqlite', ['database' => $dbPath], $sql, $params, $onComplete, $onError);
3738
Server::getInstance()->getAsyncPool()->submitTask($task);
3839
}
@@ -43,10 +44,10 @@ public static function sqlite(string $dbPath, string $sql, array $params = [], ?
4344
* @param array $config Connection config: host, username, password, database
4445
* @param string $sql SQL query with ? placeholders
4546
* @param array $params Bound parameters
46-
* @param \Closure|null $onComplete fn(array $rows): void — called on main thread
47-
* @param \Closure|null $onError fn(\Throwable $error): void — called on main thread
47+
* @param Closure|null $onComplete fn(array $rows): void — called on main thread
48+
* @param Closure|null $onError fn(\Throwable $error): void — called on main thread
4849
*/
49-
public static function mysql(array $config, string $sql, array $params = [], ?\Closure $onComplete = null, ?\Closure $onError = null): void {
50+
public static function mysql(array $config, string $sql, array $params = [], ?Closure $onComplete = null, ?Closure $onError = null): void {
5051
$task = new AsyncDatabaseTask('mysql', $config, $sql, $params, $onComplete, $onError);
5152
Server::getInstance()->getAsyncPool()->submitTask($task);
5253
}

src/imperazim/db/cache/CacheLayer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace imperazim\db\cache;
66

7+
use Closure;
78
use imperazim\db\Database;
89

910
/**
@@ -37,10 +38,10 @@ public function __construct(
3738
*
3839
* @param string $key Cache key
3940
* @param int|null $ttl TTL in seconds (null = default)
40-
* @param \Closure $loader Callback that returns the data to cache
41+
* @param Closure $loader Callback that returns the data to cache
4142
* @return mixed Cached or freshly loaded data
4243
*/
43-
public function remember(string $key, ?int $ttl, \Closure $loader): mixed {
44+
public function remember(string $key, ?int $ttl, Closure $loader): mixed {
4445
if ($this->has($key)) {
4546
return $this->cache[$key]['data'];
4647
}

src/imperazim/db/exception/DatabaseException.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace imperazim\db\exception;
66

7+
use RuntimeException;
8+
79
/**
810
* Class DatabaseException
911
* @package imperazim\db\exception
1012
*/
11-
class DatabaseException extends \RuntimeException {}
13+
class DatabaseException extends RuntimeException {}

0 commit comments

Comments
 (0)