2016-10-06 02:08:43 +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 */
|
|
|
|
|
2016-10-06 02:08:43 +00:00
|
|
|
declare(strict_types=1);
|
2017-03-28 04:12:12 +00:00
|
|
|
namespace JKingWeb\Arsse\Db;
|
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-08-29 14:50:31 +00:00
|
|
|
abstract public function prepareArray(string $query, array $paramTypes): Statement;
|
|
|
|
abstract protected function lock(): bool;
|
|
|
|
abstract protected 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-08-29 14:50:31 +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 {
|
2017-08-29 14:50:31 +00:00
|
|
|
if ($lock && !$this->transDepth) {
|
2017-07-08 01:06:38 +00:00
|
|
|
$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-09-05 23:35:14 +00:00
|
|
|
$index = $index ?? $this->transDepth;
|
2017-08-29 14:50:31 +00:00
|
|
|
if (array_key_exists($index, $this->transStatus)) {
|
|
|
|
switch ($this->transStatus[$index]) {
|
2017-05-07 22:27:16 +00:00
|
|
|
case self::TR_PEND:
|
|
|
|
$this->exec("RELEASE SAVEPOINT arsse_".$index);
|
|
|
|
$this->transStatus[$index] = self::TR_COMMIT;
|
|
|
|
$a = $index;
|
2017-08-29 14:50:31 +00:00
|
|
|
while (++$a && $a <= $this->transDepth) {
|
|
|
|
if ($this->transStatus[$a] <= self::TR_PEND) {
|
2017-07-21 02:40:09 +00:00
|
|
|
$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:
|
2017-07-22 19:29:12 +00:00
|
|
|
case self::TR_ROLLBACK: //@codeCoverageIgnore
|
2017-11-06 03:13:44 +00:00
|
|
|
throw new Exception("savepointStale", ['action' => "commit", 'index' => $index]);
|
2017-05-07 22:27:16 +00:00
|
|
|
default:
|
2017-11-06 03:13:44 +00:00
|
|
|
throw new Exception("savepointStatusUnknown", $this->transStatus[$index]); // @codeCoverageIgnore
|
2017-05-07 22:27:16 +00:00
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
if ($index==$this->transDepth) {
|
|
|
|
while ($this->transDepth > 0 && $this->transStatus[$this->transDepth] > self::TR_PEND) {
|
2017-05-07 22:27:16 +00:00
|
|
|
array_pop($this->transStatus);
|
|
|
|
$this->transDepth--;
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
if (!$this->transDepth && $this->locked) {
|
2017-07-08 01:06:38 +00:00
|
|
|
$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-11-06 03:13:44 +00:00
|
|
|
throw new Exception("savepointInvalid", ['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-09-05 23:35:14 +00:00
|
|
|
$index = $index ?? $this->transDepth;
|
2017-08-29 14:50:31 +00:00
|
|
|
if (array_key_exists($index, $this->transStatus)) {
|
|
|
|
switch ($this->transStatus[$index]) {
|
2017-05-07 22:27:16 +00:00
|
|
|
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;
|
2017-08-29 14:50:31 +00:00
|
|
|
while (++$a && $a <= $this->transDepth) {
|
|
|
|
if ($this->transStatus[$a] <= self::TR_PEND) {
|
2017-07-21 02:40:09 +00:00
|
|
|
$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:
|
2017-07-22 19:29:12 +00:00
|
|
|
case self::TR_ROLLBACK: //@codeCoverageIgnore
|
2017-11-06 03:13:44 +00:00
|
|
|
throw new Exception("savepointStale", ['action' => "rollback", 'index' => $index]);
|
2017-05-07 22:27:16 +00:00
|
|
|
default:
|
2017-11-06 03:13:44 +00:00
|
|
|
throw new Exception("savepointStatusUnknown", $this->transStatus[$index]); // @codeCoverageIgnore
|
2017-05-07 22:27:16 +00:00
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
if ($index==$this->transDepth) {
|
|
|
|
while ($this->transDepth > 0 && $this->transStatus[$this->transDepth] > self::TR_PEND) {
|
2017-05-07 22:27:16 +00:00
|
|
|
array_pop($this->transStatus);
|
|
|
|
$this->transDepth--;
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
if (!$this->transDepth && $this->locked) {
|
2017-07-08 01:06:38 +00:00
|
|
|
$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-11-06 03:13:44 +00:00
|
|
|
throw new Exception("savepointInvalid", ['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);
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|