From 6413d3a489e03213510b39c4a2099f1ed602f865 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Mon, 13 Mar 2017 19:13:06 -0400 Subject: [PATCH] Re-order SQLite3 driver initialization Should handle error reporting better --- lib/Db/SQLite3/Driver.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/Db/SQLite3/Driver.php b/lib/Db/SQLite3/Driver.php index 2686443f..7d223df6 100644 --- a/lib/Db/SQLite3/Driver.php +++ b/lib/Db/SQLite3/Driver.php @@ -22,15 +22,9 @@ class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver { if(!class_exists("SQLite3")) throw new Exception("extMissing", self::driverName()); $this->data = $data; $file = $data->conf->dbSQLite3File; - // if the file exists (or we're initializing the database), try to open it and set initial options + // if the file exists (or we're initializing the database), try to open it try { $this->db = new \SQLite3($file, ($install) ? \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE : \SQLITE3_OPEN_READWRITE, $data->conf->dbSQLite3Key); - $this->db->enableExceptions(true); - $this->exec("PRAGMA journal_mode = wal"); - $this->exec("PRAGMA foreign_keys = yes"); - - // Create custom functions - $this->db->createFunction('DATEFORMAT', CustomFunctions::dateFormat, 2); } 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)) { @@ -43,6 +37,17 @@ class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver { // otherwise the database is probably corrupt throw new Exception("fileCorrupt", $mainfile); } + try { + // set initial options + $this->db->enableExceptions(true); + $this->exec("PRAGMA journal_mode = wal"); + $this->exec("PRAGMA foreign_keys = yes"); + // Create custom functions + $this->db->createFunction('DATEFORMAT', CustomFunctions::dateFormat, 2); + } catch(\Exception $e) { + list($excClass, $excMsg, $excData) = $this->exceptionBuild(); + throw new $excClass($excMsg, $excData); + } } public function __destruct() {