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-12-18 23:29:32 +00:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
|
|
|
|
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-12-19 17:11:49 +00:00
|
|
|
protected abstract function getError(): string;
|
|
|
|
|
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-12-18 23:29:32 +00:00
|
|
|
public function schemaUpdate(int $to, string $basePath = null): bool {
|
|
|
|
$ver = $this->schemaVersion();
|
|
|
|
if (!Arsse::$conf->dbAutoUpdate) {
|
|
|
|
throw new Exception("updateManual", ['version' => $ver, 'driver_name' => $this->driverName()]);
|
|
|
|
} elseif ($ver >= $to) {
|
|
|
|
throw new Exception("updateTooNew", ['difference' => ($ver - $to), 'current' => $ver, 'target' => $to, 'driver_name' => $this->driverName()]);
|
|
|
|
}
|
|
|
|
$sep = \DIRECTORY_SEPARATOR;
|
|
|
|
$path = ($basePath ?? \JKingWeb\Arsse\BASE."sql").$sep.static::schemaID().$sep;
|
|
|
|
// lock the database
|
|
|
|
$this->savepointCreate(true);
|
|
|
|
for ($a = $this->schemaVersion(); $a < $to; $a++) {
|
|
|
|
$this->savepointCreate();
|
|
|
|
try {
|
|
|
|
$file = $path.$a.".sql";
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
throw new Exception("updateFileMissing", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]);
|
|
|
|
} elseif (!is_readable($file)) {
|
|
|
|
throw new Exception("updateFileUnreadable", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]);
|
|
|
|
}
|
|
|
|
$sql = @file_get_contents($file);
|
|
|
|
if ($sql===false) {
|
|
|
|
throw new Exception("updateFileUnusable", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]); // @codeCoverageIgnore
|
2017-12-19 17:11:49 +00:00
|
|
|
} elseif ($sql==="") {
|
|
|
|
throw new Exception("updateFileIncomplete", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]);
|
2017-12-18 23:29:32 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
$this->exec($sql);
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
throw new Exception("updateFileError", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a, 'message' => $this->getError()]);
|
|
|
|
}
|
|
|
|
if ($this->schemaVersion() != $a+1) {
|
|
|
|
throw new Exception("updateFileIncomplete", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]);
|
|
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// undo any partial changes from the failed update
|
|
|
|
$this->savepointUndo();
|
|
|
|
// commit any successful updates if updating by more than one version
|
|
|
|
$this->savepointRelease();
|
|
|
|
// throw the error received
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
$this->savepointRelease();
|
|
|
|
}
|
|
|
|
$this->savepointRelease();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|