1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 09:02:41 +00:00
Arsse/lib/Db/Transaction.php

50 lines
1.1 KiB
PHP
Raw Normal View History

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