1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 06:14:55 +00:00

Added Custom Functions into Db/SQLite3/Driver

• Created Db/SQLite3/CustomFunctions to house the dateFormat custom
function and potentially others in the future.
• Fixed a typo in Db/SQLite3/Driver
This commit is contained in:
Dustin Wilson 2017-03-13 13:33:31 -05:00
parent 8c268581fb
commit bb8af5a62c
2 changed files with 28 additions and 4 deletions

View file

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace JKingWeb\NewsSync\Db\SQLite3;
class CustomFunctions {
// Converts from SQLite3's date format to a specified standard date format.
public static function dateFormat(string $format, string $date): string {
$date = \DateTime::createFromFormat('Y-m-d H:i:s', $date, 'UTC');
$format = strtolower($format);
switch ($format) {
case 'unix': return (string)$date->getTimestamp();
break;
case 'rfc822':
case 'http': return $date->format(\DateTime::RFC822);
break;
case 'iso8601':
default: return $date->format(\DateTime::ISO8601);
}
}
}

View file

@ -28,6 +28,9 @@ class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver {
$this->db->enableExceptions(true); $this->db->enableExceptions(true);
$this->exec("PRAGMA journal_mode = wal"); $this->exec("PRAGMA journal_mode = wal");
$this->exec("PRAGMA foreign_keys = yes"); $this->exec("PRAGMA foreign_keys = yes");
// Create custom functions
$this->db->createFunction('DATEFORMAT', CustomFunctions::dateFormat, 2);
} catch(\Throwable $e) { } catch(\Throwable $e) {
// if opening the database doesn't work, check various pre-conditions to find out what the problem might be // if opening the database doesn't work, check various pre-conditions to find out what the problem might be
if(!file_exists($file)) { if(!file_exists($file)) {
@ -108,7 +111,7 @@ class Driver extends \JKingWeb\NewsSync\Db\AbstractDriver {
} }
public function query(string $query): \JKingWeb\NewsSync\Db\Result { public function query(string $query): \JKingWeb\NewsSync\Db\Result {
Try { try {
$r = $this->db->query($query); $r = $this->db->query($query);
} catch(\Exception $e) { } catch(\Exception $e) {
list($excClass, $excMsg, $excData) = $this->exceptionBuild(); list($excClass, $excMsg, $excData) = $this->exceptionBuild();