Simple wrapper of ConnectionInterface
that allows you to make transactions easier
Read main documentation before
Table of contents
$factory = new \Nstwf\MysqlConnection\Factory\ConnectionFactory(new \React\MySQL\Factory());
$connection = $factory->createConnection('localhost:3306');
$connection
->transaction(function (\Nstwf\MysqlConnection\ConnectionInterface $connection) {
return $connection->query('update users set name = "Tim" where id = 3');
})
->then(function () {
echo 'OK';
}, function (\Throwable $throwable) {
echo $throwable->getMessage();
});
$connection->quit();
The main role of factory - creating ConnectionInterface
, by wrapping Factory
Create connection using lazy connection for future operations.
$factory = new \Nstwf\MysqlConnection\Factory\ConnectionFactory(new \React\MySQL\Factory());
$connection = $factory->createConnection('localhost:3306');
That's a wrapper of original ConnectionInterface
.
Currently main difference - wrapper does not support event emitter methods
The transaction(callable $callable): PromiseInterface
method can be used to perform a transaction.
$connection
->transaction(function (\Nstwf\MysqlConnection\ConnectionInterface $connection) {
return $connection->query('update users set name = "Tim" where id = 3');
})
->then(function () {
echo 'OK';
}, function (\Throwable $throwable) {
echo $throwable->getMessage();
});
Equals to:
$connection
->query("BEGIN")
->then(
fn() => $connection->query("COMMIT"),
function (\Throwable $throwable) use ($connection) {
return $connection->query("ROLLBACK")
->then(fn() => $throwable);
}
);
The begin(): PromiseInterface
method can be used to begin the transaction.
$connection
->begin()
->then(function () {
echo 'Begin';
}, function (\Throwable $throwable) {
echo $throwable->getMessage();
});
Equals to:
Sql case-insensitive
$connection->query("BEGIN");
// or
$connection->query("START TRANSACTION");
The commit(): PromiseInterface
method can be used to commit the transaction.
$connection
->commit()
->then(function () use ($connection) {
echo 'Commit';
}, function (\Throwable $throwable) {
echo $throwable->getMessage();
});
Equals to:
sql case-insensitive
$connection->query("COMMIT");
The rollback(): PromiseInterface
method can be used to rollback the transaction.
$connection
->rollback()
->then(function () use ($connection) {
echo 'Rollback';
}, function (\Throwable $throwable) {
echo $throwable->getMessage();
});
Equals to:
Sql case-insensitive
$connection->query("ROLLBACK");
The recommended way to install this library is through Composer. New to Composer?
This project follows SemVer. This will install the latest supported version:
composer require nstwf/mysql-connection
See also the CHANGELOG for details about version upgrades.
It's highly recommended to use PHP 8+ * for this project.
To run the test suite, you first need to clone this repo and then install all dependencies through Composer:
composer install
To run the test suite, go to the project root and run:
vendor/bin/phpunit
MIT, see LICENSE file.
- friends-of-reactphp/mysql - main project