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