1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00

Start on SQLite database driver

This commit is contained in:
J. King 2016-10-02 17:07:17 -04:00
parent 2e5e229474
commit 03b86c222f
10 changed files with 100 additions and 18 deletions

1
.gitignore vendored
View file

@ -4,6 +4,7 @@ vendor/simplepie/*
#temp files
cache/*
test.php
newssync.db*
# Windows image file caches
Thumbs.db

View file

@ -5,6 +5,8 @@ namespace JKingWeb\NewsSync;
const BASE = __DIR__.DIRECTORY_SEPARATOR;
const NS_BASE = __NAMESPACE__."\\";
if(!defined(NS_BASE."INSTALL")) define(NS_BASE."INSTALL", false);
spl_autoload_register(function ($class) {
if($class=="SimplePie") return;
$file = str_replace("\\", DIRECTORY_SEPARATOR, $class);
@ -14,4 +16,4 @@ spl_autoload_register(function ($class) {
}
});
$conf = new Conf();
$data = new RuntimeData(new Conf());

View file

@ -5,4 +5,18 @@ return [
"Exception.JKingWeb/NewsSync/Lang/Exception.fileUnreadable" => "Insufficient permissions to read language file \"{0}\"",
"Exception.JKingWeb/NewsSync/Lang/Exception.fileCorrupt" => "Language file \"{0}\" is corrupt or does not conform to expected format",
"Exception.JKingWeb/NewsSync/Lang/Exception.stringMissing" => "Message string \"{msgID}\" missing from all loaded language files ({fileList})",
"Exception.JKingWeb/NewsSync/Conf/Exception.fileMissing" => "Configuration file \"{0}\" does not exist",
"Exception.JKingWeb/NewsSync/Conf/Exception.fileUnreadable" => "Insufficient permissions to read configuration file \"{0}\"",
"Exception.JKingWeb/NewsSync/Conf/Exception.fileUncreatable" => "Insufficient permissions to write new configuration file \"{0}\"",
"Exception.JKingWeb/NewsSync/Conf/Exception.fileUnwritable" => "Insufficient permissions to overwrite configuration file \"{0}\"",
"Exception.JKingWeb/NewsSync/Conf/Exception.fileCorrupt" => "Configuration file \"{0}\" is corrupt or does not conform to expected format",
"Exception.JKingWeb/NewsSync/Db/Exception.extMissing" => "Required PHP extension for driver \"{0}\" not installed",
"Exception.JKingWeb/NewsSync/Db/Exception.fileMissing" => "Database file \"{0}\" does not exist",
"Exception.JKingWeb/NewsSync/Db/Exception.fileUnreadable" => "Insufficient permissions to open database file \"{0}\" for reading",
"Exception.JKingWeb/NewsSync/Db/Exception.fileUnwritable" => "Insufficient permissions to open database file \"{0}\" for writing",
"Exception.JKingWeb/NewsSync/Db/Exception.fileUnusable" => "Insufficient permissions to open database file \"{0}\" for reading or writing",
"Exception.JKingWeb/NewsSync/Db/Exception.fileUncreatable" => "Insufficient permissions to create new database file \"{0}\"",
"Exception.JKingWeb/NewsSync/Db/Exception.fileCorrupt" => "Database file \"{0}\" is corrupt or not a valid database",
];

View file

@ -1,7 +1,6 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync\Auth;
use JKingWeb\NewsSync;
class Internal implements AuthInterface {
protected $conf;

View file

@ -6,16 +6,14 @@ class Conf {
public $lang = "en";
public $dbClass = NS_BASE."Db\\DriverSQLite3";
public $dbSQLite3PDO = false;
public $dbSQLite3File = BASE."newssync.db";
public $dbPostgreSQLPDO = false;
public $dbSQLite3Key = "";
public $dbPostgreSQLHost = "localhost";
public $dbPostgreSQLUser = "newssync";
public $dbPostgreSQLPass = "";
public $dbPostgreSQLPort = 5432;
public $dbPostgreSQLDb = "newssync";
public $dbPostgreSQLSchema = "";
public $dbMySQLPDO = false;
public $dbMySQLHost = "localhost";
public $dbMySQLUser = "newssync";
public $dbMySQLPass = "";
@ -30,30 +28,26 @@ class Conf {
public function __construct(string $import_file = "") {
if($import_file != "") $this->import_file($import_file);
if($import_file != "") $this->importFile($import_file);
}
public function importFile(string $file): self {
if(!file_exists($file)) throw new Conf\Exception("fileMissing");
if(!is_readable($file)) throw new Conf\Exception("fileUnreadable");
$json = @file_get_contents($file);
if($json===false) throw new Conf\Exception("fileUnreadable");
return $this->import($json);
$arr = (@include $file);
if(!is_array($arr)) throw new Conf\Exception("fileCorrupt");
return $this->import($arr);
}
public function import(string $json): self {
if($json=="") throw new Conf\Exception("blank");
$json = json_decode($json, true);
if(!is_array($json)) throw new Conf\Exception("corrupt");
foreach($json as $key => $value) {
public function import(array $arr): self {
foreach($arr as $key => $value) {
$this->$$key = $value;
}
return $this;
}
public function export(string $file = ""): string {
return json_encode($this, JSON_PRETTY_PRINT);
// TODO
}
public function __toString(): string {

15
vendor/JKingWeb/NewsSync/Database.php vendored Normal file
View file

@ -0,0 +1,15 @@
<?php
namespace JKingWeb\NewsSync;
class Database {
protected $drv;
public function __construct(Conf $conf) {
$driver = $conf->dbClass;
$this->drv = new $driver($conf);
}
static public function listDrivers() {
}
}

View file

@ -0,0 +1,7 @@
<?php
namespace JKingWeb\NewsSync\Db;
interface DriverInterface {
function __construct(\JKingWeb\NewsSync\Conf $conf);
static function driverName(): string;
}

View file

@ -0,0 +1,44 @@
<?php
namespace JKingWeb\NewsSync\Db;
class DriverSQLite3 implements DriverInterface {
protected $db;
protected $pdo = false;
public function __construct(\JKingWeb\NewsSync\Conf $conf, bool $install = false) {
// check to make sure required extensions are loaded
if(class_exists("SQLite3")) {
$this->pdo = false;
} else if(class_exists("PDO") && in_array("sqlite",\PDO::getAvailableDrivers())) {
$this->pdo = true;
} else {
throw new Exception("extMissing", self::driverName());
}
// if the file exists (or we're initializing the database), try to open it and set initial options
if((!$install && file_exists($conf->dbSQLite3File)) || $install) {
try {
$this->db = ($this->PDO) ? (new \SQLite3($conf->dbSQLite3File, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE, $conf->dbSQLite3Key)) : (new PDO("sqlite:".$conf->dbSQLite3File));
//FIXME: add foreign key enforcement, WAL mode
} catch(\Throwable $e) {
// if opening the database doesn't work, check various pre-conditions to find out what the problem might be
foreach([$conf->dbSQLite3File, $conf->dbSQLite3File."-wal", $conf->dbSQLite3File."-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", $conf->dbSQLite3File);
}
} else {
throw new Exception("fileMissing", $conf->dbSQLite3File);
}
}
static public function driverName(): string {
return "SQLite3";
}
}

View file

@ -0,0 +1,6 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync\Db;
class Exception extends \JKingWeb\NewsSync\Exception {
}

View file

@ -8,9 +8,9 @@ class RuntimeData {
protected $auth;
public function __construct(Conf $conf) {
Lang::set();
$this->conf = $conf;
//$this->db = new Database($this);
Lang::set($conf->lang);
$this->db = new Database($this->conf);
//$this->auth = new Authenticator($this);
}
}