1
0
mirror of https://github.com/danog/postgres.git synced 2024-11-27 04:24:45 +01:00
postgres/lib/Transaction.php

193 lines
5.7 KiB
PHP
Raw Normal View History

2016-12-30 06:21:17 +01:00
<?php
2016-09-14 16:27:39 +02:00
namespace Amp\Postgres;
use Amp\{ CallableMaker, Coroutine, Promise };
2016-09-14 16:27:39 +02:00
class Transaction implements Executor, Operation {
use Internal\Operation, CallableMaker;
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
const UNCOMMITTED = 0;
const COMMITTED = 1;
const REPEATABLE = 2;
const SERIALIZABLE = 4;
/** @var \Amp\Postgres\Executor */
private $executor;
/** @var int */
private $isolation;
/** @var callable */
private $onResolve;
2016-09-14 16:27:39 +02:00
/**
* @param \Amp\Postgres\Executor $executor
* @param int $isolation
*
* @throws \Error If the isolation level is invalid.
*/
public function __construct(Executor $executor, int $isolation = self::COMMITTED) {
switch ($isolation) {
case self::UNCOMMITTED:
case self::COMMITTED:
case self::REPEATABLE:
case self::SERIALIZABLE:
$this->isolation = $isolation;
break;
default:
throw new \Error("Isolation must be a valid transaction isolation level");
}
$this->executor = $executor;
$this->onResolve = $this->callableFromInstanceMethod("complete");
2016-09-14 16:27:39 +02:00
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
/**
* @return bool True if the transaction is active, false if it has been committed or rolled back.
2016-09-14 16:27:39 +02:00
*/
public function isActive(): bool {
return $this->executor !== null;
}
/**
* @return int
*/
public function getIsolationLevel(): int {
return $this->isolation;
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
/**
* {@inheritdoc}
*
* @throws \Amp\Postgres\TransactionError If the transaction has been committed or rolled back.
2016-09-14 16:27:39 +02:00
*/
2016-11-15 18:06:21 +01:00
public function query(string $sql): Promise {
2016-09-14 16:27:39 +02:00
if ($this->executor === null) {
throw new TransactionError("The transaction has been committed or rolled back");
}
return $this->executor->query($sql);
}
/**
* {@inheritdoc}
*
* @throws \Amp\Postgres\TransactionError If the transaction has been committed or rolled back.
2016-09-14 16:27:39 +02:00
*/
2016-11-15 18:06:21 +01:00
public function prepare(string $sql): Promise {
2016-09-14 16:27:39 +02:00
if ($this->executor === null) {
throw new TransactionError("The transaction has been committed or rolled back");
}
return $this->executor->prepare($sql);
}
/**
* {@inheritdoc}
*
* @throws \Amp\Postgres\TransactionError If the transaction has been committed or rolled back.
2016-09-14 16:27:39 +02:00
*/
2016-11-15 18:06:21 +01:00
public function execute(string $sql, ...$params): Promise {
2016-09-14 16:27:39 +02:00
if ($this->executor === null) {
throw new TransactionError("The transaction has been committed or rolled back");
}
return $this->executor->execute($sql, ...$params);
}
2017-05-16 06:28:37 +02:00
/**
* {@inheritdoc}
*
* @throws \Amp\Postgres\TransactionError If the transaction has been committed or rolled back.
*/
2016-11-15 18:06:21 +01:00
public function notify(string $channel, string $payload = ""): Promise {
if ($this->executor === null) {
throw new TransactionError("The transaction has been committed or rolled back");
}
2017-05-16 06:28:37 +02:00
return $this->executor->notify($channel, $payload);
}
2016-09-14 16:27:39 +02:00
/**
* Commits the transaction and makes it inactive.
*
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
2016-09-14 16:27:39 +02:00
*
* @throws \Amp\Postgres\TransactionError If the transaction has been committed or rolled back.
2016-09-14 16:27:39 +02:00
*/
2016-11-15 18:06:21 +01:00
public function commit(): Promise {
2016-09-14 16:27:39 +02:00
if ($this->executor === null) {
throw new TransactionError("The transaction has been committed or rolled back");
}
$promise = $this->executor->query("COMMIT");
2016-09-14 16:27:39 +02:00
$this->executor = null;
$promise->onResolve($this->onResolve);
2016-09-14 16:27:39 +02:00
return $promise;
2016-09-14 16:27:39 +02:00
}
/**
* Rolls back the transaction and makes it inactive.
*
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
2016-09-14 16:27:39 +02:00
*
* @throws \Amp\Postgres\TransactionError If the transaction has been committed or rolled back.
2016-09-14 16:27:39 +02:00
*/
2016-11-15 18:06:21 +01:00
public function rollback(): Promise {
2016-09-14 16:27:39 +02:00
if ($this->executor === null) {
throw new TransactionError("The transaction has been committed or rolled back");
}
$promise = $this->executor->query("ROLLBACK");
2016-09-14 16:27:39 +02:00
$this->executor = null;
$promise->onResolve($this->onResolve);
2016-09-14 16:27:39 +02:00
return $promise;
2016-09-14 16:27:39 +02:00
}
/**
* Creates a savepoint with the given identifier. WARNING: Identifier is not sanitized, do not pass untrusted data.
*
* @param string $identifier Savepoint identifier.
*
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
2016-09-14 16:27:39 +02:00
*
* @throws \Amp\Postgres\TransactionError If the transaction has been committed or rolled back.
2016-09-14 16:27:39 +02:00
*/
2016-11-15 18:06:21 +01:00
public function savepoint(string $identifier): Promise {
2016-09-14 16:27:39 +02:00
return $this->query("SAVEPOINT " . $identifier);
}
/**
* Rolls back to the savepoint with the given identifier. WARNING: Identifier is not sanitized, do not pass
* untrusted data.
*
* @param string $identifier Savepoint identifier.
*
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
2016-09-14 16:27:39 +02:00
*
* @throws \Amp\Postgres\TransactionError If the transaction has been committed or rolled back.
2016-09-14 16:27:39 +02:00
*/
2016-11-15 18:06:21 +01:00
public function rollbackTo(string $identifier): Promise {
2016-09-14 16:27:39 +02:00
return $this->query("ROLLBACK TO " . $identifier);
}
/**
* Releases the savepoint with the given identifier. WARNING: Identifier is not sanitized, do not pass untrusted
* data.
*
* @param string $identifier Savepoint identifier.
*
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
2016-09-14 16:27:39 +02:00
*
* @throws \Amp\Postgres\TransactionError If the transaction has been committed or rolled back.
2016-09-14 16:27:39 +02:00
*/
2016-11-15 18:06:21 +01:00
public function release(string $identifier): Promise {
2016-09-14 16:27:39 +02:00
return $this->query("RELEASE SAVEPOINT " . $identifier);
}
}