Skip to content

Commit

Permalink
feat: dispatch transactions events
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Sep 27, 2024
1 parent 5c85a7b commit 2fe1042
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 16 deletions.
22 changes: 21 additions & 1 deletion src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -391,14 +403,20 @@ 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');
}
}

$this->releaseSavepoint($this->transactionLevel + 1);

$this->eventDispatcher?->dispatch(new TransactionCommited($this));

return true;
}

Expand Down Expand Up @@ -436,6 +454,8 @@ public function rollbackTransaction(): bool

$this->rollbackSavepoint($this->transactionLevel + 1);

$this->eventDispatcher?->dispatch(new TransactionRolledBack($this));

return true;
}

Expand Down
14 changes: 2 additions & 12 deletions src/Driver/Postgres/PostgresDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
13 changes: 13 additions & 0 deletions src/Event/AbstractDriverEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Cycle\Database\Event;

use Cycle\Database\Driver\DriverInterface;

abstract class AbstractDriverEvent
{
public function __construct(
public readonly DriverInterface $driver,
) {
}
}
5 changes: 3 additions & 2 deletions src/Event/QueryExecuted.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use Cycle\Database\Driver\DriverInterface;

class QueryExecuted
final class QueryExecuted extends AbstractDriverEvent
{
public function __construct(
public readonly string $query,
public readonly array $params,
public readonly float $queryStart,
public readonly float $queryEnd,
public readonly DriverInterface $driver,
DriverInterface $driver,
) {
parent::__construct($driver);
}
}
10 changes: 10 additions & 0 deletions src/Event/TransactionBeginning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Cycle\Database\Event;

use Cycle\Database\Event\AbstractDriverEvent;

class TransactionBeginning extends AbstractDriverEvent
{

}
8 changes: 8 additions & 0 deletions src/Event/TransactionCommited.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Cycle\Database\Event;

class TransactionCommited extends AbstractDriverEvent
{

}
8 changes: 8 additions & 0 deletions src/Event/TransactionRolledBack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Cycle\Database\Event;

class TransactionRolledBack extends AbstractDriverEvent
{

}
2 changes: 1 addition & 1 deletion src/EventDispatcherAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait EventDispatcherAwareTrait
{
private ?EventDispatcherInterface $eventDispatcher = null;
protected ?EventDispatcherInterface $eventDispatcher = null;

public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
{
Expand Down

0 comments on commit 2fe1042

Please sign in to comment.