From 2fe1042e7e4b9b52851e23af98e5eccbd330fef3 Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Fri, 27 Sep 2024 15:42:04 +0200 Subject: [PATCH] feat: dispatch transactions events cc #96 --- src/Driver/Driver.php | 22 +++++++++++++++++++++- src/Driver/Postgres/PostgresDriver.php | 14 ++------------ src/Event/AbstractDriverEvent.php | 13 +++++++++++++ src/Event/QueryExecuted.php | 5 +++-- src/Event/TransactionBeginning.php | 10 ++++++++++ src/Event/TransactionCommited.php | 8 ++++++++ src/Event/TransactionRolledBack.php | 8 ++++++++ src/EventDispatcherAwareTrait.php | 2 +- 8 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 src/Event/AbstractDriverEvent.php create mode 100644 src/Event/TransactionBeginning.php create mode 100644 src/Event/TransactionCommited.php create mode 100644 src/Event/TransactionRolledBack.php diff --git a/src/Driver/Driver.php b/src/Driver/Driver.php index 533fb6f7..1fe11895 100644 --- a/src/Driver/Driver.php +++ b/src/Driver/Driver.php @@ -16,6 +16,9 @@ use Cycle\Database\Config\PDOConnectionConfig; use Cycle\Database\Config\ProvidesSourceString; use Cycle\Database\Event\QueryExecuted; +use Cycle\Database\Event\TransactionBeginning; +use Cycle\Database\Event\TransactionCommited; +use Cycle\Database\Event\TransactionRolledBack; use Cycle\Database\EventDispatcherAwareInterface; use Cycle\Database\EventDispatcherAwareTrait; use Cycle\Database\Exception\DriverException; @@ -321,6 +324,15 @@ public function getTransactionLevel(): int * @param string|null $isolationLevel */ public function beginTransaction(string $isolationLevel = null): bool + { + $transactionCreated = $this->createTransaction($isolationLevel); + + $this->eventDispatcher?->dispatch(new TransactionBeginning($this)); + + return $transactionCreated; + } + + protected function createTransaction(string $isolationLevel = null): bool { ++$this->transactionLevel; @@ -391,7 +403,11 @@ public function commitTransaction(): bool $this->logger?->info('Commit transaction'); try { - return $this->getPDO()->commit(); + $transactionCommited = $this->getPDO()->commit(); + + $this->eventDispatcher?->dispatch(new TransactionCommited($this)); + + return $transactionCommited; } catch (Throwable $e) { throw $this->mapException($e, 'COMMIT TRANSACTION'); } @@ -399,6 +415,8 @@ public function commitTransaction(): bool $this->releaseSavepoint($this->transactionLevel + 1); + $this->eventDispatcher?->dispatch(new TransactionCommited($this)); + return true; } @@ -436,6 +454,8 @@ public function rollbackTransaction(): bool $this->rollbackSavepoint($this->transactionLevel + 1); + $this->eventDispatcher?->dispatch(new TransactionRolledBack($this)); + return true; } diff --git a/src/Driver/Postgres/PostgresDriver.php b/src/Driver/Postgres/PostgresDriver.php index be60e157..e29cd2d4 100644 --- a/src/Driver/Postgres/PostgresDriver.php +++ b/src/Driver/Postgres/PostgresDriver.php @@ -19,6 +19,7 @@ use Cycle\Database\Driver\Postgres\Query\PostgresInsertQuery; use Cycle\Database\Driver\Postgres\Query\PostgresSelectQuery; use Cycle\Database\Driver\Postgres\Query\PostgresUpdateQuery; +use Cycle\Database\Event\TransactionBeginning; use Cycle\Database\Exception\DriverException; use Cycle\Database\Exception\StatementException; use Cycle\Database\Query\QueryBuilder; @@ -130,18 +131,7 @@ public function resetPrimaryKeys(): void $this->primaryKeys = []; } - /** - * Start SQL transaction with specified isolation level (not all DBMS support it). Nested - * transactions are processed using savepoints. - * - * @link http://en.wikipedia.org/wiki/Database_transaction - * @link http://en.wikipedia.org/wiki/Isolation_(database_systems) - * - * @param string|null $isolationLevel - * - * @return bool - */ - public function beginTransaction(string $isolationLevel = null): bool + protected function createTransaction(string $isolationLevel = null): bool { ++$this->transactionLevel; diff --git a/src/Event/AbstractDriverEvent.php b/src/Event/AbstractDriverEvent.php new file mode 100644 index 00000000..10fc6bd1 --- /dev/null +++ b/src/Event/AbstractDriverEvent.php @@ -0,0 +1,13 @@ +