Skip to content

Commit d7f6e19

Browse files
committed
Fix MySQLPool example
1 parent 25e605f commit d7f6e19

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

mysql-example.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
2-
require_once __DIR__ . "/classes/Async.php";
3-
require_once __DIR__ . "/classes/AsyncMySQL/MySQLPool.php";
2+
3+
use AsyncMySQL\MySQLPool;
4+
5+
require_once __DIR__ . "/src/Async.php";
6+
require_once __DIR__ . "/src/AsyncMySQL/MySQLPool.php";
47

58
header("content-type: text/plain");
69

src/AsyncMySQL/AsyncMySQL.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22
namespace AsyncMySQL;
33

4+
use const MYSQLI_ASYNC;
5+
use const MYSQLI_REPORT_ERROR;
6+
use const MYSQLI_REPORT_STRICT;
7+
use const MYSQLI_STORE_RESULT;
8+
49
require_once __DIR__ . "/exceptions/OperationsOutOfSync.php";
510
require_once __DIR__ . "/exceptions/QueryTimedOut.php";
611

@@ -26,7 +31,7 @@ public function __construct(
2631
public string $database,
2732
public int $port = 3306
2833
){
29-
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
34+
\mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
3035

3136
$connection = new \mysqli($host, $user, $password, $database, $port);
3237
$this->connection = $connection;
@@ -40,15 +45,15 @@ public function __construct(
4045
* Will return the result of the query.
4146
* @throws OperationsOutOfSync
4247
*/
43-
public function execute(string $query, array $namedArgs = []): Fiber{
48+
public function execute(string $query, array $namedArgs = []): \Fiber{
4449

4550
if (!$this->isAvailable){
4651
throw new OperationsOutOfSync("Cannot run execute on this AsyncMySQL object at this time. It is currently handling another query. Consider using the MySQLPool class.");
4752
}
4853

4954
$this->isAvailable = false;
5055

51-
return new Fiber(function() use ($query, $namedArgs){
56+
return new \Fiber(function() use ($query, $namedArgs){
5257
if (count($namedArgs) > 0){
5358
$query = $this->buildQueryFromNamedArgs($query, $namedArgs);
5459
}
@@ -58,7 +63,7 @@ public function execute(string $query, array $namedArgs = []): Fiber{
5863
$errors = [];
5964
$rejections = [];
6065
$beginTime = time();
61-
Fiber::suspend();
66+
\Fiber::suspend();
6267

6368
$numReadyQueries;
6469
do{
@@ -67,7 +72,7 @@ public function execute(string $query, array $namedArgs = []): Fiber{
6772
break;
6873
}
6974

70-
Fiber::suspend();
75+
\Fiber::suspend();
7176
} while ((time() - $beginTime <= self::$defaultQueryTimeout));
7277

7378
$this->isAvailable = true;

src/AsyncMySQL/MySQLPool.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public function __construct(
2424
* Runs an asynchronous ::execute() on an available AsyncMySQL object.
2525
* If none are available, will wait until one is.
2626
*/
27-
public function execute(string $query, array $namedArgs = []): Fiber{
27+
public function execute(string $query, array $namedArgs = []): \Fiber{
2828
$asyncMySQL = null;
2929

30-
return new Fiber(function() use ($query, $namedArgs){
30+
return new \Fiber(function() use ($query, $namedArgs){
3131
do{
3232
foreach ($this->asyncMySQLs as $obj){
3333
if ($obj->isAvailable){
@@ -37,11 +37,11 @@ public function execute(string $query, array $namedArgs = []): Fiber{
3737
}
3838

3939
if ($asyncMySQL === null){
40-
Fiber::suspend();
40+
\Fiber::suspend();
4141
}
4242
} while ($asyncMySQL === null);
4343

44-
return Async::await($asyncMySQL->execute($query, $namedArgs));
44+
return \Async::await($asyncMySQL->execute($query, $namedArgs));
4545
});
4646

4747
}

0 commit comments

Comments
 (0)