2016-10-06 02:08:43 +00:00
|
|
|
<?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;
|
2016-10-06 02:08:43 +00:00
|
|
|
|
2017-03-03 01:47:00 +00:00
|
|
|
abstract class AbstractDriver implements Driver {
|
2017-07-08 01:06:38 +00:00
|
|
|
protected $locked = false;
|
2017-02-16 20:29:42 +00:00
|
|
|
protected $transDepth = 0;
|
2017-05-07 22:27:16 +00:00
|
|
|
protected $transStatus = [];
|
2017-02-19 22:02:03 +00:00
|
|
|
|
2017-07-07 15:49:54 +00:00
|
|
|
public abstract function prepareArray(string $query, array $paramTypes): Statement;
|
2017-07-08 01:06:38 +00:00
|
|
|
protected abstract function lock(): bool;
|
|
|
|
protected abstract function unlock(bool $rollback = false) : bool;
|
2017-06-04 22:00:18 +00:00
|
|
|
|
2017-07-21 15:13:04 +00:00
|
|
|
/** @codeCoverageIgnore */
|
2017-02-16 20:29:42 +00:00
|
|
|
public function schemaVersion(): int {
|
2017-07-21 15:13:04 +00:00
|
|
|
// FIXME: generic schemaVersion() will need to be covered for database engines other than SQLite
|
2017-02-16 20:29:42 +00:00
|
|
|
try {
|
2017-07-07 15:49:54 +00:00
|
|
|
return (int) $this->query("SELECT value from arsse_meta 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-19 22:02:03 +00:00
|
|
|
|
2017-07-08 01:06:38 +00:00
|
|
|
public function begin(bool $lock = false): Transaction {
|
|
|
|
return new Transaction($this, $lock);
|
2017-05-06 16:02:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-08 01:06:38 +00:00
|
|
|
public function savepointCreate(bool $lock = false): int {
|
|
|
|
if($lock && !$this->transDepth) {
|
|
|
|
$this->lock();
|
|
|
|
$this->locked = true;
|
|
|
|
}
|
2017-03-28 04:12:12 +00:00
|
|
|
$this->exec("SAVEPOINT arsse_".(++$this->transDepth));
|
2017-05-07 22:27:16 +00:00
|
|
|
$this->transStatus[$this->transDepth] = self::TR_PEND;
|
|
|
|
return $this->transDepth;
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
2016-10-06 02:08:43 +00:00
|
|
|
|
2017-05-07 22:27:16 +00:00
|
|
|
public function savepointRelease(int $index = null): bool {
|
2017-07-21 02:40:09 +00:00
|
|
|
if(is_null($index)) {
|
|
|
|
$index = $this->transDepth;
|
|
|
|
}
|
2017-05-07 22:27:16 +00:00
|
|
|
if(array_key_exists($index, $this->transStatus)) {
|
|
|
|
switch($this->transStatus[$index]) {
|
|
|
|
case self::TR_PEND:
|
|
|
|
$this->exec("RELEASE SAVEPOINT arsse_".$index);
|
|
|
|
$this->transStatus[$index] = self::TR_COMMIT;
|
|
|
|
$a = $index;
|
|
|
|
while(++$a && $a <= $this->transDepth) {
|
2017-07-21 02:40:09 +00:00
|
|
|
if($this->transStatus[$a] <= self::TR_PEND) {
|
|
|
|
$this->transStatus[$a] = self::TR_PEND_COMMIT;
|
|
|
|
}
|
2017-05-07 22:27:16 +00:00
|
|
|
}
|
|
|
|
$out = true;
|
|
|
|
break;
|
|
|
|
case self::TR_PEND_COMMIT:
|
|
|
|
$this->transStatus[$index] = self::TR_COMMIT;
|
|
|
|
$out = true;
|
|
|
|
break;
|
|
|
|
case self::TR_PEND_ROLLBACK:
|
|
|
|
$this->transStatus[$index] = self::TR_COMMIT;
|
|
|
|
$out = false;
|
|
|
|
break;
|
|
|
|
case self::TR_COMMIT:
|
|
|
|
case self::TR_ROLLBACK:
|
|
|
|
throw new ExceptionSavepoint("stale", ['action' => "commit", 'index' => $index]);
|
|
|
|
default:
|
|
|
|
throw new Exception("unknownSavepointStatus", $this->transStatus[$index]);
|
|
|
|
}
|
|
|
|
if($index==$this->transDepth) {
|
|
|
|
while($this->transDepth > 0 && $this->transStatus[$this->transDepth] > self::TR_PEND) {
|
|
|
|
array_pop($this->transStatus);
|
|
|
|
$this->transDepth--;
|
|
|
|
}
|
|
|
|
}
|
2017-07-08 01:06:38 +00:00
|
|
|
if(!$this->transDepth && $this->locked) {
|
|
|
|
$this->unlock();
|
|
|
|
$this->locked = false;
|
|
|
|
}
|
2017-05-07 22:27:16 +00:00
|
|
|
return $out;
|
2017-02-16 20:29:42 +00:00
|
|
|
} else {
|
2017-05-07 22:27:16 +00:00
|
|
|
throw new ExceptionSavepoint("invalid", ['action' => "commit", 'index' => $index]);
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-06 02:08:43 +00:00
|
|
|
|
2017-05-07 22:27:16 +00:00
|
|
|
public function savepointUndo(int $index = null): bool {
|
2017-07-21 02:40:09 +00:00
|
|
|
if(is_null($index)) {
|
|
|
|
$index = $this->transDepth;
|
|
|
|
}
|
2017-05-07 22:27:16 +00:00
|
|
|
if(array_key_exists($index, $this->transStatus)) {
|
|
|
|
switch($this->transStatus[$index]) {
|
|
|
|
case self::TR_PEND:
|
|
|
|
$this->exec("ROLLBACK TRANSACTION TO SAVEPOINT arsse_".$index);
|
|
|
|
$this->exec("RELEASE SAVEPOINT arsse_".$index);
|
|
|
|
$this->transStatus[$index] = self::TR_ROLLBACK;
|
|
|
|
$a = $index;
|
|
|
|
while(++$a && $a <= $this->transDepth) {
|
2017-07-21 02:40:09 +00:00
|
|
|
if($this->transStatus[$a] <= self::TR_PEND) {
|
|
|
|
$this->transStatus[$a] = self::TR_PEND_ROLLBACK;
|
|
|
|
}
|
2017-05-07 22:27:16 +00:00
|
|
|
}
|
|
|
|
$out = true;
|
|
|
|
break;
|
|
|
|
case self::TR_PEND_COMMIT:
|
|
|
|
$this->transStatus[$index] = self::TR_ROLLBACK;
|
|
|
|
$out = false;
|
|
|
|
break;
|
|
|
|
case self::TR_PEND_ROLLBACK:
|
|
|
|
$this->transStatus[$index] = self::TR_ROLLBACK;
|
|
|
|
$out = true;
|
|
|
|
break;
|
|
|
|
case self::TR_COMMIT:
|
|
|
|
case self::TR_ROLLBACK:
|
|
|
|
throw new ExceptionSavepoint("stale", ['action' => "rollback", 'index' => $index]);
|
|
|
|
default:
|
|
|
|
throw new Exception("unknownSavepointStatus", $this->transStatus[$index]);
|
|
|
|
}
|
|
|
|
if($index==$this->transDepth) {
|
|
|
|
while($this->transDepth > 0 && $this->transStatus[$this->transDepth] > self::TR_PEND) {
|
|
|
|
array_pop($this->transStatus);
|
|
|
|
$this->transDepth--;
|
|
|
|
}
|
|
|
|
}
|
2017-07-08 01:06:38 +00:00
|
|
|
if(!$this->transDepth && $this->locked) {
|
|
|
|
$this->unlock(true);
|
|
|
|
$this->locked = false;
|
|
|
|
}
|
2017-05-07 22:27:16 +00:00
|
|
|
return $out;
|
2017-02-16 20:29:42 +00:00
|
|
|
} else {
|
2017-05-07 22:27:16 +00:00
|
|
|
throw new ExceptionSavepoint("invalid", ['action' => "rollback", 'index' => $index]);
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-06 02:08:43 +00:00
|
|
|
|
2017-07-07 15:49:54 +00:00
|
|
|
public function prepare(string $query, ...$paramType): Statement {
|
2017-02-16 20:29:42 +00:00
|
|
|
return $this->prepareArray($query, $paramType);
|
|
|
|
}
|
2016-10-06 02:08:43 +00:00
|
|
|
}
|