Browse Source

Start on SQLite database driver

microsub
J. King 8 years ago
parent
commit
03b86c222f
  1. 1
      .gitignore
  2. 4
      bootstrap.php
  3. 14
      locale/en.php
  4. 1
      vendor/JKingWeb/NewsSync/Auth/DriverInternal.php
  5. 22
      vendor/JKingWeb/NewsSync/Conf.php
  6. 15
      vendor/JKingWeb/NewsSync/Database.php
  7. 7
      vendor/JKingWeb/NewsSync/Db/DriverInterface.php
  8. 44
      vendor/JKingWeb/NewsSync/Db/DriverSQLite3.php
  9. 6
      vendor/JKingWeb/NewsSync/Db/Exception.php
  10. 4
      vendor/JKingWeb/NewsSync/RuntimeData.php

1
.gitignore

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

4
bootstrap.php

@ -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());

14
locale/en.php

@ -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",
];

1
vendor/JKingWeb/NewsSync/Auth/DriverInternal.php

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

22
vendor/JKingWeb/NewsSync/Conf.php

@ -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

@ -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() {
}
}

7
vendor/JKingWeb/NewsSync/Db/DriverInterface.php

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

44
vendor/JKingWeb/NewsSync/Db/DriverSQLite3.php

@ -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";
}
}

6
vendor/JKingWeb/NewsSync/Db/Exception.php

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

4
vendor/JKingWeb/NewsSync/RuntimeData.php

@ -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);
}
}
Loading…
Cancel
Save