2016-10-02 21:07:17 +00:00
|
|
|
<?php
|
2016-10-06 02:08:43 +00:00
|
|
|
declare(strict_types=1);
|
2017-03-07 23:01:13 +00:00
|
|
|
namespace JKingWeb\NewsSync\Db\SQLite3;
|
|
|
|
use JKingWeb\NewsSync\Lang;
|
|
|
|
use JKingWeb\NewsSync\Db\Exception;
|
|
|
|
use JKingWeb\NewsSync\Db\ExceptionInput;
|
|
|
|
use JKingWeb\NewsSync\Db\ExceptionTimeout;
|
2016-10-02 21:07:17 +00:00
|
|
|
|
2017-03-07 23:01:13 +00:00
|
|
|
|
2017-03-08 14:55:16 +00:00
|
|
|
class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver {
|
|
|
|
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;
|
|
|
|
protected $data;
|
2017-03-13 18:33:31 +00:00
|
|
|
|
2017-03-02 19:19:16 +00:00
|
|
|
public function __construct(\JKingWeb\NewsSync\RuntimeData $data, bool $install = false) {
|
2017-03-02 14:04:04 +00:00
|
|
|
// check to make sure required extension is loaded
|
2017-03-08 13:19:42 +00:00
|
|
|
if(!class_exists("SQLite3")) throw new Exception("extMissing", self::driverName());
|
2017-02-16 20:29:42 +00:00
|
|
|
$this->data = $data;
|
|
|
|
$file = $data->conf->dbSQLite3File;
|
2017-03-13 23:13:06 +00:00
|
|
|
// if the file exists (or we're initializing the database), try to open it
|
2017-02-16 20:29:42 +00:00
|
|
|
try {
|
|
|
|
$this->db = new \SQLite3($file, ($install) ? \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE : \SQLITE3_OPEN_READWRITE, $data->conf->dbSQLite3Key);
|
|
|
|
} catch(\Throwable $e) {
|
|
|
|
// if opening the database doesn't work, check various pre-conditions to find out what the problem might be
|
|
|
|
if(!file_exists($file)) {
|
2017-03-08 13:19:42 +00:00
|
|
|
if($install && !is_writable(dirname($file))) throw new Exception("fileUncreatable", dirname($file));
|
|
|
|
throw new Exception("fileMissing", $file);
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
2017-03-08 13:19:42 +00:00
|
|
|
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);
|
2017-02-16 20:29:42 +00:00
|
|
|
// otherwise the database is probably corrupt
|
2017-03-08 13:19:42 +00:00
|
|
|
throw new Exception("fileCorrupt", $mainfile);
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
2017-03-13 23:13:06 +00:00
|
|
|
try {
|
|
|
|
// set initial options
|
|
|
|
$this->db->enableExceptions(true);
|
|
|
|
$this->exec("PRAGMA journal_mode = wal");
|
|
|
|
$this->exec("PRAGMA foreign_keys = yes");
|
|
|
|
// Create custom functions
|
2017-03-14 03:22:20 +00:00
|
|
|
$this->db->createFunction('DATEFORMAT', [CustomFunctions::class, 'dateFormat'], 2);
|
2017-03-13 23:13:06 +00:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
list($excClass, $excMsg, $excData) = $this->exceptionBuild();
|
|
|
|
throw new $excClass($excMsg, $excData);
|
|
|
|
}
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
2016-10-06 02:08:43 +00:00
|
|
|
|
2017-02-16 20:29:42 +00:00
|
|
|
public function __destruct() {
|
2017-03-02 14:04:04 +00:00
|
|
|
try{$this->db->close();} catch(\Exception $e) {}
|
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-03-02 03:47:51 +00:00
|
|
|
static public function driverName(): string {
|
2017-03-07 23:01:13 +00:00
|
|
|
return 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-03-03 01:47:00 +00:00
|
|
|
public function schemaUpdate(int $to): bool {
|
2017-03-02 03:47:51 +00:00
|
|
|
$ver = $this->schemaVersion();
|
2017-03-08 13:19:42 +00:00
|
|
|
if(!$this->data->conf->dbSQLite3AutoUpd) throw new Exception("updateManual", ['version' => $ver, 'driver_name' => $this->driverName()]);
|
|
|
|
if($ver >= $to) 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-03-10 02:39:42 +00:00
|
|
|
$path = $this->data->conf->dbSchemaBase.$sep."SQLite3".$sep;
|
2017-03-02 03:47:51 +00:00
|
|
|
$this->lock();
|
|
|
|
$this->begin();
|
|
|
|
for($a = $ver; $a < $to; $a++) {
|
|
|
|
$this->begin();
|
|
|
|
try {
|
|
|
|
$file = $path.$a.".sql";
|
2017-03-10 02:39:42 +00:00
|
|
|
if(!file_exists($file)) throw new Exception("updateFileMissing", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]);
|
|
|
|
if(!is_readable($file)) 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-03-10 02:39:42 +00:00
|
|
|
if($sql===false) throw new Exception("updateFileUnusable", ['file' => $file, 'driver_name' => $this->driverName(), 'current' => $a]);
|
|
|
|
try {
|
|
|
|
$this->exec($sql);
|
|
|
|
} catch(\Exception $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]);
|
2017-03-02 03:47:51 +00:00
|
|
|
} catch(\Throwable $e) {
|
|
|
|
// undo any partial changes from the failed update
|
|
|
|
$this->rollback();
|
2017-03-10 02:39:42 +00:00
|
|
|
$this->unlock();
|
2017-03-02 03:47:51 +00:00
|
|
|
// commit any successful updates if updating by more than one version
|
|
|
|
$this->commit(true);
|
|
|
|
// throw the error received
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
$this->commit();
|
|
|
|
}
|
|
|
|
$this->unlock();
|
|
|
|
$this->commit();
|
|
|
|
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);
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
list($excClass, $excMsg, $excData) = $this->exceptionBuild();
|
|
|
|
throw new $excClass($excMsg, $excData);
|
|
|
|
}
|
2017-03-02 03:47:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 23:01:13 +00:00
|
|
|
public function query(string $query): \JKingWeb\NewsSync\Db\Result {
|
2017-03-13 18:33:31 +00:00
|
|
|
try {
|
2017-03-08 14:55:16 +00:00
|
|
|
$r = $this->db->query($query);
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
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-03-07 23:01:13 +00:00
|
|
|
public function prepareArray(string $query, array $paramTypes): \JKingWeb\NewsSync\Db\Statement {
|
2017-03-08 14:55:16 +00:00
|
|
|
try {
|
|
|
|
$s = $this->db->prepare($query);
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
list($excClass, $excMsg, $excData) = $this->exceptionBuild();
|
|
|
|
throw new $excClass($excMsg, $excData);
|
|
|
|
}
|
|
|
|
return new Statement($this->db, $s, $paramTypes);
|
2017-02-16 20:29:42 +00:00
|
|
|
}
|
2016-10-02 21:07:17 +00:00
|
|
|
}
|