2017-05-06 16:02:27 +00:00
|
|
|
<?php
|
2017-11-17 01:23:18 +00:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-05-06 16:02:27 +00:00
|
|
|
declare(strict_types=1);
|
2021-04-14 15:17:01 +00:00
|
|
|
|
2017-05-06 16:02:27 +00:00
|
|
|
namespace JKingWeb\Arsse\Db;
|
|
|
|
|
|
|
|
class Transaction {
|
2017-05-07 22:27:16 +00:00
|
|
|
protected $index;
|
2017-05-06 16:02:27 +00:00
|
|
|
protected $pending = false;
|
|
|
|
protected $drv;
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function __construct(Driver $drv, bool $lock = false) {
|
2017-07-08 01:06:38 +00:00
|
|
|
$this->index = $drv->savepointCreate($lock);
|
2017-05-06 16:02:27 +00:00
|
|
|
$this->drv = $drv;
|
|
|
|
$this->pending = true;
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function __destruct() {
|
|
|
|
if ($this->pending) {
|
2017-05-06 16:02:27 +00:00
|
|
|
try {
|
2017-05-07 22:27:16 +00:00
|
|
|
$this->drv->savepointUndo($this->index);
|
2017-08-29 14:50:31 +00:00
|
|
|
} catch (\Throwable $e) {
|
2017-05-06 16:02:27 +00:00
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function commit(): bool {
|
2017-05-07 22:27:16 +00:00
|
|
|
$out = $this->drv->savepointRelease($this->index);
|
|
|
|
$this->pending = false;
|
|
|
|
return $out;
|
2017-05-06 16:02:27 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function rollback(): bool {
|
2017-05-07 22:27:16 +00:00
|
|
|
$out = $this->drv->savepointUndo($this->index);
|
|
|
|
$this->pending = false;
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function getIndex(): int {
|
2017-05-07 22:27:16 +00:00
|
|
|
return $this->index;
|
|
|
|
}
|
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public function isPending(): bool {
|
2017-05-07 22:27:16 +00:00
|
|
|
return $this->pending;
|
2017-05-06 16:02:27 +00:00
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|