44
55namespace imperazim \db \async ;
66
7+ use Closure ;
8+ use RuntimeException ;
9+ use Throwable ;
710use 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 )) {
0 commit comments