Skip to content

Commit

Permalink
Nested transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed May 1, 2023
1 parent 542e09a commit aab9597
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 64 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"php": ">=8.1",
"amphp/amp": "^3",
"amphp/pipeline": "^1",
"amphp/sql": "2.x-dev",
"amphp/sql-common": "2.x-dev"
"amphp/sql": "^2",
"amphp/sql-common": "^2"
},
"require-dev": {
"ext-pgsql": "*",
Expand Down
22 changes: 22 additions & 0 deletions src/Internal/PostgresNestedTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types=1);

namespace Amp\Postgres\Internal;

use Amp\Postgres\PostgresResult;
use Amp\Postgres\PostgresStatement;
use Amp\Postgres\PostgresTransaction;
use Amp\Sql\Common\NestedTransaction;

/**
* @internal
* @extends NestedTransaction<PostgresResult, PostgresStatement, PostgresTransaction>
*/
final class PostgresNestedTransaction extends NestedTransaction implements PostgresTransaction
{
use PostgresTransactionDelegate;

protected function getTransaction(): PostgresTransaction
{
return $this->transaction;
}
}
65 changes: 3 additions & 62 deletions src/Internal/PostgresPooledTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,76 +6,17 @@
use Amp\Postgres\PostgresStatement;
use Amp\Postgres\PostgresTransaction;
use Amp\Sql\Common\PooledTransaction;
use Amp\Sql\Result;
use Amp\Sql\Statement;

/**
* @internal
* @extends PooledTransaction<PostgresResult, PostgresStatement, PostgresTransaction>
*/
final class PostgresPooledTransaction extends PooledTransaction implements PostgresTransaction
{
protected function createStatement(Statement $statement, \Closure $release): PostgresStatement
{
\assert($statement instanceof PostgresStatement);
return new PostgresPooledStatement($statement, $release);
}

protected function createResult(Result $result, \Closure $release): PostgresResult
{
\assert($result instanceof PostgresResult);
return new PostgresPooledResult($result, $release);
}

/**
* @param \Closure():void $release
*/
public function __construct(private readonly PostgresTransaction $transaction, \Closure $release)
{
parent::__construct($transaction, $release);
}

/**
* Changes return type to this library's Result type.
*/
public function query(string $sql): PostgresResult
{
return parent::query($sql);
}

/**
* Changes return type to this library's Statement type.
*/
public function prepare(string $sql): PostgresStatement
{
return parent::prepare($sql);
}

/**
* Changes return type to this library's Result type.
*/
public function execute(string $sql, array $params = []): PostgresResult
{
return parent::execute($sql, $params);
}

public function notify(string $channel, string $payload = ""): PostgresResult
{
return $this->transaction->notify($channel, $payload);
}

public function quoteString(string $data): string
{
return $this->transaction->quoteString($data);
}

public function quoteName(string $name): string
{
return $this->transaction->quoteName($name);
}
use PostgresTransactionDelegate;

public function escapeByteA(string $data): string
protected function getTransaction(): PostgresTransaction
{
return $this->transaction->escapeByteA($data);
return $this->transaction;
}
}
80 changes: 80 additions & 0 deletions src/Internal/PostgresTransactionDelegate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php declare(strict_types=1);

namespace Amp\Postgres\Internal;

use Amp\Postgres\PostgresResult;
use Amp\Postgres\PostgresStatement;
use Amp\Postgres\PostgresTransaction;
use Amp\Sql\Result;
use Amp\Sql\Statement;

/** @internal */
trait PostgresTransactionDelegate
{
abstract protected function getTransaction(): PostgresTransaction;

/**
* @param \Closure():void $release
*/
protected function createStatement(Statement $statement, \Closure $release): PostgresStatement
{
\assert($statement instanceof PostgresStatement);
return new PostgresPooledStatement($statement, $release);
}

/**
* @param \Closure():void $release
*/
protected function createResult(Result $result, \Closure $release): PostgresResult
{
\assert($result instanceof PostgresResult);
return new PostgresPooledResult($result, $release);
}

/**
* Changes return type to this library's Result type.
*/
public function query(string $sql): PostgresResult
{
return parent::query($sql);
}

/**
* Changes return type to this library's Statement type.
*/
public function prepare(string $sql): PostgresStatement
{
return parent::prepare($sql);
}

/**
* Changes return type to this library's Result type.
*/
public function execute(string $sql, array $params = []): PostgresResult
{
return parent::execute($sql, $params);
}

/**
* @param non-empty-string $channel
*/
public function notify(string $channel, string $payload = ""): PostgresResult
{
return $this->getTransaction()->notify($channel, $payload);
}

public function quoteString(string $data): string
{
return $this->getTransaction()->quoteString($data);
}

public function quoteName(string $name): string
{
return $this->getTransaction()->quoteName($name);
}

public function escapeByteA(string $data): string
{
return $this->transaction->escapeByteA($data);
}
}
46 changes: 46 additions & 0 deletions src/PostgresNestableTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);

namespace Amp\Postgres;

use Amp\Sql\Common\NestableTransaction;
use Amp\Sql\Transaction;
use Amp\Sql\TransactionIsolation;
use Amp\Sql\TransactionIsolationLevel;

/**
* @extends NestableTransaction<PostgresResult, PostgresStatement, PostgresTransaction>
*/
final class PostgresNestableTransaction extends NestableTransaction implements PostgresLink
{
protected function createNestedTransaction(
Transaction $transaction,
\Closure $release,
string $identifier,
): Transaction {
return new Internal\PostgresNestedTransaction($transaction, $release, $identifier);
}

/**
* Changes return type to this library's Transaction type.
*/
public function beginTransaction(
TransactionIsolation $isolation = TransactionIsolationLevel::Committed
): PostgresTransaction {
return parent::beginTransaction($isolation);
}

public function quoteString(string $data): string
{
return $this->transaction->quoteString($data);
}

public function quoteName(string $name): string
{
return $this->transaction->quoteName($name);
}

public function escapeByteA(string $data): string
{
return $this->transaction->escapeByteA($data);
}
}

0 comments on commit aab9597

Please sign in to comment.