nstwf/mysql-connection

Reactphp MySQL transactional connection wrapper

1.2.1 2022-12-22 08:00 UTC

This package is auto-updated.

Last update: 2024-11-04 08:59:04 UTC


README

CI codecov Packagist Version

Simple wrapper of ConnectionInterface that allows you to make transactions easier

Read main documentation before

Table of contents

Quickstart example

$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();

Usage

Factory

The main role of factory - creating ConnectionInterface, by wrapping Factory

createConnection

Create connection using lazy connection for future operations.

$factory = new \Nstwf\MysqlConnection\Factory\ConnectionFactory(new \React\MySQL\Factory());
$connection = $factory->createConnection('localhost:3306');

ConnectionInterface

That's a wrapper of original ConnectionInterface.

Currently main difference - wrapper does not support event emitter methods

See original documentation

query

See original documentation

transaction

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);
        }
    );

begin

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");

commit

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");

rollback

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");

ping

See original documentation

close

See original documentation

quit

See original documentation

Install

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.

Tests

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

License

MIT, see LICENSE file.