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

73 lines
2.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2017-03-28 04:12:12 +00:00
namespace JKingWeb\Arsse\Db;
2016-10-17 20:49:39 +00:00
use JKingWeb\DrUUID\UUID as UUID;
abstract class AbstractDriver implements Driver {
2017-02-16 20:29:42 +00:00
protected $transDepth = 0;
2017-02-16 20:29:42 +00:00
public function schemaVersion(): int {
try {
2017-03-28 04:12:12 +00:00
return (int) $this->query("SELECT value from arsse_settings where key is schema_version")->getValue();
2017-03-09 22:14:26 +00:00
} catch(Exception $e) {
2017-02-16 20:29:42 +00:00
return 0;
}
}
2017-02-16 20:29:42 +00:00
public function begin(): bool {
2017-03-28 04:12:12 +00:00
$this->exec("SAVEPOINT arsse_".(++$this->transDepth));
2017-02-16 20:29:42 +00:00
return true;
}
2017-02-16 20:29:42 +00:00
public function commit(bool $all = false): bool {
if($this->transDepth==0) return false;
if(!$all) {
2017-03-28 04:12:12 +00:00
$this->exec("RELEASE SAVEPOINT arsse_".($this->transDepth--));
2017-02-16 20:29:42 +00:00
} else {
$this->exec("COMMIT TRANSACTION");
$this->transDepth = 0;
2017-02-16 20:29:42 +00:00
}
return true;
}
2017-02-16 20:29:42 +00:00
public function rollback(bool $all = false): bool {
if($this->transDepth==0) return false;
if(!$all) {
2017-03-28 04:12:12 +00:00
$this->exec("ROLLBACK TRANSACTION TO SAVEPOINT arsse_".($this->transDepth));
2017-02-16 20:29:42 +00:00
// rollback to savepoint does not collpase the savepoint
2017-03-28 04:12:12 +00:00
$this->exec("RELEASE SAVEPOINT arsse_".($this->transDepth--));
2017-02-16 20:29:42 +00:00
} else {
$this->exec("ROLLBACK TRANSACTION");
$this->transDepth = 0;
}
return true;
}
2017-02-16 20:29:42 +00:00
public function lock(): bool {
if($this->schemaVersion() < 1) return true;
if($this->isLocked()) return false;
$uuid = UUID::mintStr();
2017-03-09 22:14:26 +00:00
try {
2017-03-28 04:12:12 +00:00
$this->prepare("INSERT INTO arsse_settings(key,value) values(?,?)", "str", "str")->run("lock", $uuid);
2017-03-09 22:14:26 +00:00
} catch(ExceptionInput $e) {
return false;
}
2017-02-16 20:29:42 +00:00
sleep(1);
2017-03-28 04:12:12 +00:00
return ($this->query("SELECT value from arsse_settings where key is 'lock'")->getValue() == $uuid);
2017-02-16 20:29:42 +00:00
}
2016-10-17 20:49:39 +00:00
2017-02-16 20:29:42 +00:00
public function unlock(): bool {
2017-03-10 02:39:42 +00:00
if($this->schemaVersion() < 1) return true;
2017-03-28 04:12:12 +00:00
$this->exec("DELETE from arsse_settings where key is 'lock'");
2017-03-09 22:14:26 +00:00
return true;
2017-02-16 20:29:42 +00:00
}
2016-10-17 20:49:39 +00:00
2017-02-16 20:29:42 +00:00
public function isLocked(): bool {
if($this->schemaVersion() < 1) return false;
2017-03-28 04:12:12 +00:00
return ($this->query("SELECT count(*) from arsse_settings where key is 'lock'")->getValue() > 0);
2017-02-16 20:29:42 +00:00
}
2016-10-17 20:49:39 +00:00
public function prepare(string $query, ...$paramType): Statement {
2017-02-16 20:29:42 +00:00
return $this->prepareArray($query, $paramType);
}
}