1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 21:22:40 +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

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