1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 17:12:41 +00:00
Arsse/vendor/JKingWeb/NewsSync/Db/DriverSQLite3.php

65 lines
2.5 KiB
PHP
Raw Normal View History

2016-10-02 21:07:17 +00:00
<?php
declare(strict_types=1);
2016-10-02 21:07:17 +00:00
namespace JKingWeb\NewsSync\Db;
class DriverSQLite3 implements Driver {
use Common, CommonSQLite3;
2016-10-02 21:07:17 +00:00
protected $db;
private function __construct(\JKingWeb\NewsSync\Conf $conf, bool $install = false) {
// normalize the path
$path = $conf->dbSQLite3Path;
$sep = \DIRECTORY_SEPARATOR;
if(substr($path,-(strlen($sep))) != $sep) $path .= $sep;
$mainfile = $path."newssync-main.db";
$feedfile = $path."newssync-feeds.db";
// if the files exists (or we're initializing the database), try to open it and set initial options
try {
$this->db = new \SQLite3($mainfile, ($install) ? \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE : \SQLITE3_OPEN_READWRITE, $conf->dbSQLite3Key);
$this->db->enableExceptions(true);
$attach = "'".$this->db->escapeString($feedfile)."'";
$this->exec("ATTACH DATABASE $attach AS feeds");
$this->exec("PRAGMA main.jounral_mode = wal");
$this->exec("PRAGMA feeds.jounral_mode = wal");
$this->exec("PRAGMA foreign_keys = yes");
} catch(\Throwable $e) {
// if opening the database doesn't work, check various pre-conditions to find out what the problem might be
foreach([$mainfile, $mainfile."-wal", $mainfile."-shm", $feedfile, $feedfile."-wal", $feedfile."-shm"] as $file) {
if(!file_exists($file)) {
if($install && !is_writable(dirname($file))) throw new Exception("fileUncreatable", dirname($file));
throw new Exception("fileMissing", $file);
}
if(!is_readable($file) && !is_writable($file)) throw new Exception("fileUnusable", $file);
if(!is_readable($file)) throw new Exception("fileUnreadable", $file);
if(!is_writable($file)) throw new Exception("fileUnwritable", $file);
}
// otherwise the database is probably corrupt
throw new Exception("fileCorrupt", $mainfile);
}
}
public function __destruct() {
$this->db->close();
unset($this->db);
}
static public function create(\JKingWeb\NewsSync\Conf $conf, bool $install = false): Driver {
2016-10-02 21:07:17 +00:00
// check to make sure required extensions are loaded
if(class_exists("SQLite3")) {
return new self($conf, $install);
2016-10-02 21:07:17 +00:00
} else if(class_exists("PDO") && in_array("sqlite",\PDO::getAvailableDrivers())) {
return new DriverSQLite3PDO($conf, $install);
2016-10-02 21:07:17 +00:00
} else {
throw new Exception("extMissing", self::driverName());
}
}
public function unsafeQuery(string $query): Result {
return new ResultSQLite3($this->db->query($query));
}
public function prepareArray(string $query, array $paramTypes): Statement {
return new StatementSQLite3($query, $paramTypes);
2016-10-02 21:07:17 +00:00
}
}