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/CommonSQLite3.php

50 lines
1.7 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync\Db;
Trait CommonSQLite3 {
static public function driverName(): string {
return "SQLite 3";
}
public function schemaVersion(): int {
return $this->query("PRAGMA user_version")->getSingle();
}
2016-10-17 20:49:39 +00:00
public function update(int $to): bool {
$ver = $this->schemaVersion();
if(!$this->data->conf->dbSQLite3AutoUpd) throw new Update\Exception("manual", ['version' => $ver, 'driver_name' => $this->driverName()]);
if($ver >= $to) throw new Update\Exception("tooNew", ['difference' => ($ver - $to), 'current' => $ver, 'target' => $to, 'driver_name' => $this->driverName()]);
$sep = \DIRECTORY_SEPARATOR;
$path = \JKingWeb\NewsSync\BASE."sql".$sep."SQLite3".$sep;
2016-10-17 20:49:39 +00:00
$this->lock();
$this->begin();
2016-10-17 20:49:39 +00:00
for($a = $ver; $a < $to; $a++) {
$this->begin();
try {
$file = $path.$a.".sql";
if(!file_exists($file)) throw new Update\Exception("fileMissing", ['file' => $file, 'driver_name' => $this->driverName()]);
if(!is_readable($file)) throw new Update\Exception("fileUnreadable", ['file' => $file, 'driver_name' => $this->driverName()]);
$sql = @file_get_contents($file);
if($sql===false) throw new Update\Exception("fileUnusable", ['file' => $file, 'driver_name' => $this->driverName()]);
$this->exec($sql);
} catch(\Throwable $e) {
// undo any partial changes from the failed update
$this->rollback();
// commit any successful updates if updating by more than one version
$this->commit(true);
// throw the error received
throw $e;
2016-10-17 20:49:39 +00:00
}
$this->commit();
}
2016-10-17 20:49:39 +00:00
$this->unlock();
$this->commit();
2016-10-17 20:49:39 +00:00
return true;
}
public function exec(string $query): bool {
return (bool) $this->db->exec($query);
}
}