poggit / libasynql

Asynchronous MySQL access library for PocketMine plugins.
https://poggit.github.io/libasynql
Apache License 2.0
134 stars 44 forks source link

Prevent MysqliThread fainted on select query return false #113

Closed PresentKim closed 7 months ago

PresentKim commented 7 months ago

In MariaDB, a MySQL alternative, there is a RETURNING statement in INSERT statements. (I find it safer and simpler than INSERT+SELECT)

I am using DataConnector::asyncSelect() to receive this response. However, if the query fails due to an error, the MysqliThread fainted without proccessing the generator... This makes it impossible for the code to determine the success or progress of the operation.

Before PR: image (MysqliThread goes down and after that, the correct query request doesn't work either.)

After PR: image (The error is throw correctly, and now can catch it to process it.)


Tested test.sql code

-- #! mysql
-- # { test1
CREATE TABLE IF NOT EXISTS test
(
    id    INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    value INT UNSIGNED NOT NULL
);
-- # }
-- # { test2
INSERT INTO test (value)
VALUES (-9999)
RETURNING id, value;
-- # }

Tested php code

$conn = libasynql::create($this, $this->getConfig(), ["mysql" => "test.sql"]);
Await::f2c(static function() use ($conn){
    yield from $conn->asyncGeneric("test1");
    var_dump(yield from $conn->asyncSelect("test2"));
});