2016-10-02 21:07:17 +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\SQLite3;
|
2017-08-29 14:50:31 +00:00
|
|
|
|
2017-07-17 11:47:57 +00:00
|
|
|
use JKingWeb\Arsse\Arsse;
|
2017-03-28 04:12:12 +00:00
|
|
|
use JKingWeb\Arsse\Db\Exception;
|
|
|
|
use JKingWeb\Arsse\Db\ExceptionInput;
|
|
|
|
use JKingWeb\Arsse\Db\ExceptionTimeout;
|
2016-10-02 21:07:17 +00:00
|
|
|
|
2017-03-28 04:12:12 +00:00
|
|
|
class Driver extends \JKingWeb\Arsse\Db\AbstractDriver {
|
2017-03-08 14:55:16 +00:00
|
|
|
use ExceptionBuilder;
|
|
|
|
|
2017-03-07 23:01:13 +00:00
|
|
|
const SQLITE_BUSY = 5;
|
|
|
|
const SQLITE_CONSTRAINT = 19;
|
|
|
|
const SQLITE_MISMATCH = 20;
|
2017-03-13 18:33:31 +00:00
|
|
|
|
2017-02-16 20:29:42 +00:00
|
|
|
protected $db;
|
2017-03-13 18:33:31 +00:00
|
|
|
|
2017-10-19 19:32:18 +00:00
|
|
|
public function __construct(string $dbFile = null) {
|
2017-03-02 14:04:04 +00:00
|
|
|
// check to make sure required extension is loaded
|
2017-08-29 14:50:31 +00:00
|
|
|
if (!class_exists("SQLite3")) {
|
2017-07-22 19:29:12 +00:00
|
|
|
throw new Exception("extMissing", self::driverName()); // @codeCoverageIgnore
|
2017-09-28 13:01:43 +00:00
|
|
|
}
|
2017-09-05 23:35:14 +00:00
|
|
|
// if no database file is specified in the configuration, use a suitable default
|
2017-10-19 19:32:18 +00:00
|
|
|
$dbFile = $dbFile ?? Arsse::$conf->dbSQLite3File ?? \JKingWeb\Arsse\BASE."arsse.db";
|
2017-07-22 19:29:12 +00:00
|
|
|
$mode = \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE;
|
2017-08-18 03:05:08 +00:00
|
|
|
$timeout = Arsse::$conf->dbSQLite3Timeout * 1000;
|
2017-02-16 20:29:42 +00:00
|
|
|
try {
|
2017-07-22 19:29:12 +00:00
|
|
|
$this->db = $this->makeConnection($dbFile, $mode, Arsse::$conf->dbSQLite3Key);
|
2017-12-01 21:37:58 +00:00
|
|
|
// enable exceptions
|
2017-07-21 21:15:43 +00:00
|
|
|
$this->db->enableExceptions(true);
|
2017-12-01 21:37:58 +00:00
|
|
|
// set the timeout; parameters are not allowed for pragmas, but this usage should be safe
|
|
|
|
$this->exec("PRAGMA busy_timeout = $timeout");
|
|
|
|
// set other initial options
|
2017-07-21 21:15:43 +00:00
|
|
|
$this->exec("PRAGMA foreign_keys = yes");
|
2017-08-29 14:50:31 +00:00
|
|
|
} catch (\Throwable $e) {
|
2017-02-16 20:29:42 +00:00
|
|
|
// if opening the database doesn't work, check various pre-conditions to find out what the problem might be
|
2017-07-22 19:29:12 +00:00
|
|
|
$files = [
|
|
|
|
$dbFile, // main database file
|
|
|
|
$dbFile."-wal", // write-ahead log journal
|
|
|
|
$dbFile."-shm", // shared memory index
|
|
|
|
];
|
2017-08-29 14:50:31 +00:00
|
|
|
foreach ($files as $file) {
|
|
|
|
if (!file_exists($file) && !is_writable(dirname($file))) {
|
2017-07-22 19:29:12 +00:00
|
|
|
throw new Exception("fileUncreatable", $file);
|
2017-08-29 14:50:31 +00:00
|
|
|
} elseif (!is_readable($file) && !is_writable($file)) {
|
2017-07-22 19:29:12 +00:00
|
|
|
throw new Exception("fileUnusable", $file);
|
2017-08-29 14:50:31 +00:00
|
|
|
} elseif (!is_readable($file)) {
|
2017-07-22 19:29:12 +00:00
|
|
|
throw new Exception("fileUnreadable", $file);
|
2017-08-29 14:50:31 +00:00
|
|
|
} elseif (!is_writable($file)) {
|
2017-07-22 19:29:12 +00:00
|
|
|
throw new Exception("fileUnwritable", $file);
|
2017-07-21 21:15:43 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-16 20:29:42 +00:00
|
|
|
// otherwise the database is probably corrupt
|
2017-07-22 19:29:12 +00:00
|
|
|
throw new Exception("fileCorrupt", $dbFile);
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-06 02:08:43 +00:00
|
|
|
|
2017-07-12 00:27:37 +00:00
|
|
|
protected function makeConnection(string $file, int $opts, string $key) {
|
2017-07-08 01:06:38 +00:00
|
|
|
return new \SQLite3($file, $opts, $key);
|
|
|
|
}
|
|
|
|
|
2017-02-16 20:29:42 +00:00
|
|
|
public function __destruct() {
|
2017-08-29 14:50:31 +00:00
|
|
|
try {
|
|
|
|
$this->db->close();
|
2017-09-05 23:35:14 +00:00
|
|
|
} catch (\Exception $e) { // @codeCoverageIgnore
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|
2017-02-16 20:29:42 +00:00
|
|
|
unset($this->db);
|
|
|
|
}
|
2016-10-06 02:08:43 +00:00
|
|
|
|
2017-03-13 18:33:31 +00:00
|
|
|
|
2017-08-29 14:50:31 +00:00
|
|
|
public static function driverName(): string {
|
2017-07-17 11:47:57 +00:00
|
|
|
return Arsse::$lang->msg("Driver.Db.SQLite3.Name");
|
2017-03-02 03:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function schemaVersion(): int {
|
2017-03-07 23:01:13 +00:00
|
|
|
return $this->query("PRAGMA user_version")->getValue();
|
2017-03-02 03:47:51 +00:00
|
|
|
}
|
|
|
|
|
2017-08-20 03:56:32 +00:00
|
|
|
public function schemaUpdate(int $to, string $basePath = null): bool {
|
2017-03-02 03:47:51 +00:00
|
|
|
$ver = $this->schemaVersion();
|
2017-08-29 14:50:31 +00:00
|
|
|
if (!Arsse::$conf->dbAutoUpdate) {
|
2017-07-21 21:15:43 +00:00
|
|
|
throw new Exception("updateManual", ['version' => $ver, 'driver_name' => $this->driverName()]);
|
2017-08-29 14:50:31 +00:00
|
|
|
} elseif ($ver >= $to) {
|
2017-07-21 21:15:43 +00:00
|
|
|
throw new Exception("updateTooNew", ['difference' => ($ver - $to), 'current' => $ver, 'target' => $to, 'driver_name' => $this->driverName()]);
|
|
|
|
}
|
2017-03-02 03:47:51 +00:00
|
|
|
$sep = \DIRECTORY_SEPARATOR;
|
2017-08-20 03:56:32 +00:00
|
|
|
$path = ($basePath ?? \JKingWeb\Arsse\BASE."sql").$sep."SQLite3".$sep;
|
2017-12-07 23:05:34 +00:00
|
|
|
// turn off foreign keys
|
|
|
|
$this->exec("PRAGMA foreign_keys = no");
|
2017-07-08 01:06:38 +00:00
|
|
|
// lock the database
|
|
|
|
$this->savepointCreate(true);
|
2017-08-29 14:50:31 +00:00
|
|
|
for ($a = $this->schemaVersion(); $a < $to; $a++) {
|
2017-05-06 16:02:27 +00:00
|
|
|
$this->savepointCreate();
|
2017-03-02 03:47:51 +00:00
|
|
|
try {
|
|
|
|
$file = $path.$a.".sql";
|
2017-08-29 14:50:31 +00:00
|
|
|
if (!file_exists($file)) {
|
2017-07-21 21:15:43 +00:00
|
|
|
throw new Exception("updateFileMissing", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]);
|
2017-08-29 14:50:31 +00:00
|
|
|
} elseif (!is_readable($file)) {
|
2017-07-21 21:15:43 +00:00
|
|
|
throw new Exception("updateFileUnreadable", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]);
|
|
|
|
}
|
2017-03-02 03:47:51 +00:00
|
|
|
$sql = @file_get_contents($file);
|
2017-08-29 14:50:31 +00:00
|
|
|
if ($sql===false) {
|
2017-07-22 19:29:12 +00:00
|
|
|
throw new Exception("updateFileUnusable", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]); // @codeCoverageIgnore
|
2017-07-21 21:15:43 +00:00
|
|
|
}
|
2017-03-10 02:39:42 +00:00
|
|
|
try {
|
|
|
|
$this->exec($sql);
|
2017-08-29 14:50:31 +00:00
|
|
|
} catch (\Throwable $e) {
|
2017-03-10 02:39:42 +00:00
|
|
|
throw new Exception("updateFileError", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a, 'message' => $this->getError()]);
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
if ($this->schemaVersion() != $a+1) {
|
2017-07-21 21:15:43 +00:00
|
|
|
throw new Exception("updateFileIncomplete", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]);
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
} catch (\Throwable $e) {
|
2017-03-02 03:47:51 +00:00
|
|
|
// undo any partial changes from the failed update
|
2017-05-06 16:02:27 +00:00
|
|
|
$this->savepointUndo();
|
2017-03-02 03:47:51 +00:00
|
|
|
// commit any successful updates if updating by more than one version
|
2017-05-07 22:27:16 +00:00
|
|
|
$this->savepointRelease();
|
2017-03-02 03:47:51 +00:00
|
|
|
// throw the error received
|
|
|
|
throw $e;
|
|
|
|
}
|
2017-05-06 16:02:27 +00:00
|
|
|
$this->savepointRelease();
|
2017-03-02 03:47:51 +00:00
|
|
|
}
|
2017-05-06 16:02:27 +00:00
|
|
|
$this->savepointRelease();
|
2017-12-07 23:05:34 +00:00
|
|
|
// turn foreign keys back on
|
|
|
|
$this->exec("PRAGMA foreign_keys = yes");
|
2017-03-02 03:47:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-29 23:14:59 +00:00
|
|
|
public function charsetAcceptable(): bool {
|
|
|
|
// SQLite 3 databases are UTF-8 internally, thus always acceptable
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-10 02:39:42 +00:00
|
|
|
protected function getError(): string {
|
|
|
|
return $this->db->lastErrorMsg();
|
|
|
|
}
|
|
|
|
|
2017-03-02 03:47:51 +00:00
|
|
|
public function exec(string $query): bool {
|
2017-03-08 14:55:16 +00:00
|
|
|
try {
|
|
|
|
return (bool) $this->db->exec($query);
|
2017-08-29 14:50:31 +00:00
|
|
|
} catch (\Exception $e) {
|
2017-03-08 14:55:16 +00:00
|
|
|
list($excClass, $excMsg, $excData) = $this->exceptionBuild();
|
|
|
|
throw new $excClass($excMsg, $excData);
|
|
|
|
}
|
2017-03-02 03:47:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 04:12:12 +00:00
|
|
|
public function query(string $query): \JKingWeb\Arsse\Db\Result {
|
2017-03-13 18:33:31 +00:00
|
|
|
try {
|
2017-03-08 14:55:16 +00:00
|
|
|
$r = $this->db->query($query);
|
2017-08-29 14:50:31 +00:00
|
|
|
} catch (\Exception $e) {
|
2017-03-08 14:55:16 +00:00
|
|
|
list($excClass, $excMsg, $excData) = $this->exceptionBuild();
|
|
|
|
throw new $excClass($excMsg, $excData);
|
|
|
|
}
|
2017-03-09 19:48:42 +00:00
|
|
|
$changes = $this->db->changes();
|
|
|
|
$lastId = $this->db->lastInsertRowID();
|
|
|
|
return new Result($r, [$changes, $lastId]);
|
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 prepareArray(string $query, array $paramTypes): \JKingWeb\Arsse\Db\Statement {
|
2017-03-08 14:55:16 +00:00
|
|
|
try {
|
|
|
|
$s = $this->db->prepare($query);
|
2017-08-29 14:50:31 +00:00
|
|
|
} catch (\Exception $e) {
|
2017-03-08 14:55:16 +00:00
|
|
|
list($excClass, $excMsg, $excData) = $this->exceptionBuild();
|
|
|
|
throw new $excClass($excMsg, $excData);
|
|
|
|
}
|
2017-07-07 15:49:54 +00:00
|
|
|
return new Statement($this->db, $s, $paramTypes);
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
2017-07-08 01:06:38 +00:00
|
|
|
|
|
|
|
protected function lock(): bool {
|
|
|
|
$this->exec("BEGIN EXCLUSIVE TRANSACTION");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function unlock(bool $rollback = false): bool {
|
|
|
|
$this->exec((!$rollback) ? "COMMIT" : "ROLLBACK");
|
|
|
|
return true;
|
|
|
|
}
|
2017-08-29 14:50:31 +00:00
|
|
|
}
|