From bb8af5a62cd4ec427c84dcd4ab58cfcb51bfc4b6 Mon Sep 17 00:00:00 2001 From: Dustin Wilson Date: Mon, 13 Mar 2017 13:33:31 -0500 Subject: [PATCH] Added Custom Functions into Db/SQLite3/Driver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Created Db/SQLite3/CustomFunctions to house the dateFormat custom function and potentially others in the future. • Fixed a typo in Db/SQLite3/Driver --- lib/Db/SQLite3/CustomFunctions.php | 21 +++++++++++++++++++++ lib/Db/SQLite3/Driver.php | 11 +++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 lib/Db/SQLite3/CustomFunctions.php diff --git a/lib/Db/SQLite3/CustomFunctions.php b/lib/Db/SQLite3/CustomFunctions.php new file mode 100644 index 00000000..3559186a --- /dev/null +++ b/lib/Db/SQLite3/CustomFunctions.php @@ -0,0 +1,21 @@ +getTimestamp(); + break; + case 'rfc822': + case 'http': return $date->format(\DateTime::RFC822); + break; + case 'iso8601': + default: return $date->format(\DateTime::ISO8601); + } + } +} \ No newline at end of file diff --git a/lib/Db/SQLite3/Driver.php b/lib/Db/SQLite3/Driver.php index b9a9911b..2686443f 100644 --- a/lib/Db/SQLite3/Driver.php +++ b/lib/Db/SQLite3/Driver.php @@ -13,10 +13,10 @@ class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver { const SQLITE_BUSY = 5; const SQLITE_CONSTRAINT = 19; const SQLITE_MISMATCH = 20; - + protected $db; protected $data; - + public function __construct(\JKingWeb\NewsSync\RuntimeData $data, bool $install = false) { // check to make sure required extension is loaded if(!class_exists("SQLite3")) throw new Exception("extMissing", self::driverName()); @@ -28,6 +28,9 @@ class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver { $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)) { @@ -47,7 +50,7 @@ class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver { unset($this->db); } - + static public function driverName(): string { return Lang::msg("Driver.Db.SQLite3.Name"); } @@ -108,7 +111,7 @@ class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver { } public function query(string $query): \JKingWeb\NewsSync\Db\Result { - Try { + try { $r = $this->db->query($query); } catch(\Exception $e) { list($excClass, $excMsg, $excData) = $this->exceptionBuild();