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/SQLite3/Driver.php

207 lines
7 KiB
PHP
Raw Normal View History

2016-10-02 21:07:17 +00:00
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
2021-04-14 15:17:01 +00:00
2017-03-28 04:12:12 +00:00
namespace JKingWeb\Arsse\Db\SQLite3;
2017-08-29 14:50:31 +00:00
use JKingWeb\Arsse\Arsse;
2017-03-28 04:12:12 +00:00
use JKingWeb\Arsse\Db\Exception;
2016-10-02 21:07:17 +00:00
2017-03-28 04:12:12 +00:00
class Driver extends \JKingWeb\Arsse\Db\AbstractDriver {
use ExceptionBuilder;
2020-03-01 23:32:01 +00:00
protected const TRANSACTIONAL_LOCKS = true;
2020-03-01 23:32:01 +00:00
public const SQLITE_BUSY = 5;
public const SQLITE_SCHEMA = 17;
public const SQLITE_CONSTRAINT = 19;
public const SQLITE_MISMATCH = 20;
2017-02-16 20:29:42 +00:00
protected $db;
protected $collator;
public function __construct() {
// check to make sure required extension is loaded
2018-12-08 00:21:44 +00:00
if (!static::requirementsMet()) {
throw new Exception("extMissing", static::driverName()); // @codeCoverageIgnore
2017-09-28 13:01:43 +00:00
}
// if no database file is specified in the configuration, use a suitable default
$dbFile = Arsse::$conf->dbSQLite3File ?? \JKingWeb\Arsse\BASE."arsse.db";
$dbKey = Arsse::$conf->dbSQLite3Key;
2018-12-08 00:21:44 +00:00
$timeout = Arsse::$conf->dbSQLite3Timeout * 1000;
2017-02-16 20:29:42 +00:00
try {
2021-05-18 22:42:42 +00:00
// check whether the file exists; if it doesn't create the file and set its mode to rw-r-----
if ($dbFile !== ":memory:" && !file_exists($dbFile)) {
if (@touch($dbFile)) {
chmod($dbFile, 0640);
}
}
2018-12-08 00:21:44 +00:00
$this->makeConnection($dbFile, $dbKey);
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
$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))) {
throw new Exception("fileUncreatable", $file);
2017-08-29 14:50:31 +00:00
} elseif (!is_readable($file) && !is_writable($file)) {
throw new Exception("fileUnusable", $file);
2017-08-29 14:50:31 +00:00
} elseif (!is_readable($file)) {
throw new Exception("fileUnreadable", $file);
2017-08-29 14:50:31 +00:00
} elseif (!is_writable($file)) {
throw new Exception("fileUnwritable", $file);
}
}
2017-02-16 20:29:42 +00:00
// otherwise the database is probably corrupt
throw new Exception("fileCorrupt", $dbFile);
2017-02-16 20:29:42 +00:00
}
// set the timeout
$timeout = Arsse::$conf->dbSQLite3Timeout ?? Arsse::$conf->dbTimeoutLock; // old SQLite-specific timeout takes precedence
$timeout = is_null($timeout) ? PHP_INT_MAX : (int) ceil($timeout * 1000);
$this->setTimeout($timeout);
// set other initial options
$this->exec("PRAGMA foreign_keys = yes");
2018-12-11 19:14:32 +00:00
// use a case-insensitive Unicode collation sequence
$this->collator = new \Collator("@kf=false");
$m = ($this->db instanceof \PDO) ? "sqliteCreateCollation" : "createCollation";
$this->db->$m("nocase", [$this->collator, "compare"]);
2017-02-16 20:29:42 +00:00
}
public static function requirementsMet(): bool {
return class_exists("SQLite3");
}
protected function makeConnection(string $file, string $key): void {
2017-12-19 17:11:49 +00:00
$this->db = new \SQLite3($file, \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE, $key);
// enable exceptions
$this->db->enableExceptions(true);
}
protected function setTimeout(int $msec): void {
$this->exec("PRAGMA busy_timeout = $msec");
}
2017-02-16 20:29:42 +00:00
public function __destruct() {
2017-08-29 14:50:31 +00:00
try {
$this->db->close();
} catch (\Exception $e) { // @codeCoverageIgnore
2017-08-29 14:50:31 +00:00
}
2017-02-16 20:29:42 +00:00
unset($this->db);
}
2017-12-19 17:11:49 +00:00
/** @codeCoverageIgnore */
public static function create(): \JKingWeb\Arsse\Db\Driver {
if (self::requirementsMet()) {
return new self;
} elseif (PDODriver::requirementsMet()) {
return new PDODriver;
} else {
2017-12-22 16:51:58 +00:00
throw new Exception("extMissing", self::driverName());
}
}
2017-08-29 14:50:31 +00:00
public static function driverName(): string {
return Arsse::$lang->msg("Driver.Db.SQLite3.Name");
}
public static function schemaID(): string {
return "SQLite3";
}
public function schemaVersion(): int {
2017-12-19 17:11:49 +00:00
return (int) $this->query("PRAGMA user_version")->getValue();
}
public function sqlToken(string $token): string {
2018-12-05 22:28:11 +00:00
switch (strtolower($token)) {
case "greatest":
return "max";
2022-04-23 17:21:52 +00:00
case "least":
return "min";
case "asc":
return "";
default:
return $token;
}
}
public function schemaUpdate(int $to, string $basePath = null): bool {
if ($to == 1) {
// if we're initializing the database for the first time, switch to WAL mode
$this->exec("PRAGMA journal_mode = wal");
}
// turn off foreign keys
$this->exec("PRAGMA foreign_keys = no");
// run the generic updater
try {
parent::schemaUpdate($to, $basePath);
2018-12-12 17:21:28 +00:00
} finally {
// turn foreign keys back on
$this->exec("PRAGMA foreign_keys = yes");
}
return true;
}
public function charsetAcceptable(): bool {
// SQLite 3 databases are UTF-8 internally, thus always acceptable
return true;
}
public function exec(string $query): bool {
try {
return (bool) $this->db->exec($query);
2017-08-29 14:50:31 +00:00
} catch (\Exception $e) {
2020-03-01 20:16:50 +00:00
[$excClass, $excMsg, $excData] = $this->buildException();
throw new $excClass($excMsg, $excData);
}
}
2017-03-28 04:12:12 +00:00
public function query(string $query): \JKingWeb\Arsse\Db\Result {
try {
$r = $this->db->query($query);
2017-08-29 14:50:31 +00:00
} catch (\Exception $e) {
2020-03-01 20:16:50 +00:00
[$excClass, $excMsg, $excData] = $this->buildException();
throw new $excClass($excMsg, $excData);
}
$changes = $this->db->changes();
$lastId = $this->db->lastInsertRowID();
return new Result($r, [$changes, $lastId]);
2017-02-16 20:29:42 +00:00
}
public function prepareArray(string $query, array $paramTypes): \JKingWeb\Arsse\Db\Statement {
return new Statement($this->db, $query, $paramTypes);
2017-02-16 20:29:42 +00:00
}
protected function lock(): bool {
$timeout = (int) $this->query("PRAGMA busy_timeout")->getValue();
$this->setTimeout(0);
try {
$this->exec("BEGIN EXCLUSIVE TRANSACTION");
} finally {
$this->setTimeout($timeout);
}
return true;
}
protected function unlock(bool $rollback = false): bool {
$this->exec((!$rollback) ? "COMMIT" : "ROLLBACK");
return true;
}
public function literalString(string $str): string {
return "'".\SQLite3::escapeString($str)."'";
}
2019-07-26 13:37:51 +00:00
public function maintenance(): bool {
// analyze the database then checkpoint and truncate the write-ahead log
$this->exec("ANALYZE; PRAGMA wal_checkpoint(truncate)");
return true;
}
2017-08-29 14:50:31 +00:00
}