2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres;
|
|
|
|
|
2017-06-21 05:17:53 +02:00
|
|
|
use Amp\Promise;
|
2016-09-14 16:27:39 +02:00
|
|
|
|
2017-08-01 07:38:12 +02:00
|
|
|
class Transaction implements Handle, Operation {
|
2016-09-14 16:27:39 +02:00
|
|
|
const UNCOMMITTED = 0;
|
|
|
|
const COMMITTED = 1;
|
|
|
|
const REPEATABLE = 2;
|
|
|
|
const SERIALIZABLE = 4;
|
|
|
|
|
2017-08-01 07:38:12 +02:00
|
|
|
/** @var \Amp\Postgres\Handle */
|
|
|
|
private $handle;
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $isolation;
|
|
|
|
|
2017-11-18 05:00:52 +01:00
|
|
|
/** @var \Amp\Postgres\Internal\ReferenceQueue */
|
2017-08-03 07:42:53 +02:00
|
|
|
private $queue;
|
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
/**
|
2017-08-01 07:38:12 +02:00
|
|
|
* @param \Amp\Postgres\Handle $handle
|
2016-09-14 16:27:39 +02:00
|
|
|
* @param int $isolation
|
|
|
|
*
|
|
|
|
* @throws \Error If the isolation level is invalid.
|
|
|
|
*/
|
2017-08-01 07:38:12 +02:00
|
|
|
public function __construct(Handle $handle, int $isolation = self::COMMITTED) {
|
2016-09-14 16:27:39 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2017-08-01 07:38:12 +02:00
|
|
|
$this->handle = $handle;
|
2017-11-18 05:00:52 +01:00
|
|
|
$this->queue = new Internal\ReferenceQueue;
|
2017-07-29 17:25:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct() {
|
2017-08-01 07:38:12 +02:00
|
|
|
if ($this->handle) {
|
2017-08-03 07:42:53 +02:00
|
|
|
$this->rollback(); // Invokes $this->queue->complete().
|
2017-07-29 17:25:06 +02:00
|
|
|
}
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2017-08-03 07:42:53 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2017-11-18 05:00:52 +01:00
|
|
|
public function onDestruct(callable $onComplete) {
|
|
|
|
$this->queue->onDestruct($onComplete);
|
2017-08-03 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 22:38:17 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function isAlive(): bool {
|
|
|
|
return $this->handle !== null && $this->handle->isAlive();
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
/**
|
2017-05-26 20:14:04 +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 {
|
2017-08-01 07:38:12 +02:00
|
|
|
return $this->handle !== null;
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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}
|
2017-05-26 20:14:04 +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 query(string $sql): Promise {
|
2017-08-01 07:38:12 +02:00
|
|
|
if ($this->handle === null) {
|
2016-09-14 16:27:39 +02:00
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
|
|
|
|
2017-08-01 07:38:12 +02:00
|
|
|
return $this->handle->query($sql);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2017-05-26 20:14:04 +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 prepare(string $sql): Promise {
|
2017-08-01 07:38:12 +02:00
|
|
|
if ($this->handle === null) {
|
2016-09-14 16:27:39 +02:00
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
|
|
|
|
2017-08-01 07:38:12 +02:00
|
|
|
return $this->handle->prepare($sql);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2017-05-26 20:14:04 +02:00
|
|
|
*
|
|
|
|
* @throws \Amp\Postgres\TransactionError If the transaction has been committed or rolled back.
|
2016-09-14 16:27:39 +02:00
|
|
|
*/
|
2017-11-18 04:33:49 +01:00
|
|
|
public function execute(string $sql, array $params = []): Promise {
|
2017-08-01 07:38:12 +02:00
|
|
|
if ($this->handle === null) {
|
2016-09-14 16:27:39 +02:00
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
|
|
|
|
2017-11-18 04:33:49 +01:00
|
|
|
return $this->handle->execute($sql, $params);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
|
|
|
|
2016-09-21 07:18:24 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2017-05-26 20:14:04 +02:00
|
|
|
*
|
|
|
|
* @throws \Amp\Postgres\TransactionError If the transaction has been committed or rolled back.
|
2016-09-21 07:18:24 +02:00
|
|
|
*/
|
2016-11-15 18:06:21 +01:00
|
|
|
public function notify(string $channel, string $payload = ""): Promise {
|
2017-08-01 07:38:12 +02:00
|
|
|
if ($this->handle === null) {
|
2016-09-21 07:18:24 +02:00
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2017-08-01 07:38:12 +02:00
|
|
|
return $this->handle->notify($channel, $payload);
|
2016-09-21 07:18:24 +02:00
|
|
|
}
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Commits the transaction and makes it inactive.
|
|
|
|
*
|
2017-03-17 16:17:24 +01:00
|
|
|
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
2017-05-26 20:14:04 +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 {
|
2017-08-01 07:38:12 +02:00
|
|
|
if ($this->handle === null) {
|
2016-09-14 16:27:39 +02:00
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
|
|
|
|
2017-08-01 07:38:12 +02:00
|
|
|
$promise = $this->handle->query("COMMIT");
|
|
|
|
$this->handle = null;
|
2017-11-18 05:00:52 +01:00
|
|
|
$promise->onResolve([$this->queue, "unreference"]);
|
2016-09-14 16:27:39 +02:00
|
|
|
|
2017-05-27 05:52:55 +02:00
|
|
|
return $promise;
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rolls back the transaction and makes it inactive.
|
|
|
|
*
|
2017-03-17 16:17:24 +01:00
|
|
|
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
2017-05-26 20:14:04 +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 {
|
2017-08-01 07:38:12 +02:00
|
|
|
if ($this->handle === null) {
|
2016-09-14 16:27:39 +02:00
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
|
|
|
|
2017-08-01 07:38:12 +02:00
|
|
|
$promise = $this->handle->query("ROLLBACK");
|
|
|
|
$this->handle = null;
|
2017-11-18 05:00:52 +01:00
|
|
|
$promise->onResolve([$this->queue, "unreference"]);
|
2016-09-14 16:27:39 +02:00
|
|
|
|
2017-05-27 05:52:55 +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.
|
|
|
|
*
|
2016-09-21 07:18:24 +02:00
|
|
|
* @param string $identifier Savepoint identifier.
|
|
|
|
*
|
2017-03-17 16:17:24 +01:00
|
|
|
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
2017-05-26 20:14:04 +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 {
|
2017-08-01 07:38:12 +02:00
|
|
|
return $this->query("SAVEPOINT " . $this->quoteName($identifier));
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-01 07:38:12 +02:00
|
|
|
* Rolls back to the savepoint with the given identifier.
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
2016-09-21 07:18:24 +02:00
|
|
|
* @param string $identifier Savepoint identifier.
|
|
|
|
*
|
2017-03-17 16:17:24 +01:00
|
|
|
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
2017-05-26 20:14:04 +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 {
|
2017-08-01 07:38:12 +02:00
|
|
|
return $this->query("ROLLBACK TO " . $this->quoteName($identifier));
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Releases the savepoint with the given identifier. WARNING: Identifier is not sanitized, do not pass untrusted
|
|
|
|
* data.
|
|
|
|
*
|
2016-09-21 07:18:24 +02:00
|
|
|
* @param string $identifier Savepoint identifier.
|
|
|
|
*
|
2017-03-17 16:17:24 +01:00
|
|
|
* @return \Amp\Promise<\Amp\Postgres\CommandResult>
|
2016-09-14 16:27:39 +02:00
|
|
|
*
|
2017-05-26 20:14:04 +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 {
|
2017-08-01 07:38:12 +02:00
|
|
|
return $this->query("RELEASE SAVEPOINT " . $this->quoteName($identifier));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @throws \Amp\Postgres\TransactionError If the transaction has been committed or rolled back.
|
|
|
|
*/
|
|
|
|
public function quoteString(string $data): string {
|
|
|
|
if ($this->handle === null) {
|
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->handle->quoteString($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
|
|
|
* @throws \Amp\Postgres\TransactionError If the transaction has been committed or rolled back.
|
|
|
|
*/
|
|
|
|
public function quoteName(string $name): string {
|
|
|
|
if ($this->handle === null) {
|
|
|
|
throw new TransactionError("The transaction has been committed or rolled back");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->handle->quoteName($name);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
}
|