From ba6b5dee857a5ea88aad40204e75f8283bff7103 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Thu, 1 Jun 2017 16:24:11 -0400 Subject: [PATCH] Simplify in-database settings The previous complexity is really not required. Also modified how settingSet() works to avoid a REPLACE query, which is not compatible with PostgreSQL. --- lib/Database.php | 96 +++-------------------------------------------- sql/SQLite3/0.sql | 7 +--- 2 files changed, 7 insertions(+), 96 deletions(-) diff --git a/lib/Database.php b/lib/Database.php index 0ffadec..6a5f21c 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -105,98 +105,12 @@ class Database { return $this->db->begin(); } - public function settingSet(string $key, $in, string $type = null): bool { - if(!$type) { - switch(gettype($in)) { - case "boolean": $type = "bool"; break; - case "integer": $type = "int"; break; - case "double": $type = "numeric"; break; - case "string": - case "array": $type = "json"; break; - case "resource": - case "unknown type": - case "NULL": $type = "null"; break; - case "object": - if($in instanceof DateTimeInterface) { - $type = "timestamp"; - } else { - $type = "text"; - } - break; - default: $type = 'null'; break; - } + public function settingSet(string $key, string $value): bool { + $out = !$this->db->prepare("UPDATE arsse_settings set value = ? where key is ?", "str", "str")->run($value, $key)->changes(); + if(!$out) { + $out = $this->db->prepare("INSERT INTO arsse_settings(key,value)", "str", "str")->run($key, $value)->changes(); } - $type = strtolower($type); - switch($type) { - case "integer": - $type = "int"; - case "int": - $value = $in; - break; - case "float": - case "double": - case "real": - $type = "numeric"; - case "numeric": - $value = $in; - break; - case "str": - case "string": - $type = "text"; - case "text": - $value = $in; - break; - case "json": - if(is_array($in) || is_object($in)) { - $value = json_encode($in); - } else { - $value = $in; - } - break; - case "datetime": - $type = "timestamp"; - case "timestamp": - if($in instanceof DateTimeInterface) { - $value = gmdate(self::FORMAT_TS, $in->format("U")); - } else if(is_numeric($in)) { - $value = gmdate(self::FORMAT_TS, $in); - } else { - $value = gmdate(self::FORMAT_TS, gmstrftime($in)); - } - break; - case "date": - if($in instanceof DateTimeInterface) { - $value = gmdate(self::FORMAT_DATE, $in->format("U")); - } else if(is_numeric($in)) { - $value = gmdate(self::FORMAT_DATE, $in); - } else { - $value = gmdate(self::FORMAT_DATE, gmstrftime($in)); - } - break; - case "time": - if($in instanceof DateTimeInterface) { - $value = gmdate(self::FORMAT_TIME, $in->format("U")); - } else if(is_numeric($in)) { - $value = gmdate(self::FORMAT_TIME, $in); - } else { - $value = gmdate(self::FORMAT_TIME, gmstrftime($in)); - } - break; - case "boolean": - case "bit": - $type = "bool"; - case "bool": - $value = (int) $in; - break; - case "null": - $value = null; - break; - default: - $type = "text"; - $value = $in; - break; - } - return (bool) $this->db->prepare("REPLACE INTO arsse_settings(key,value,type) values(?,?,?)", "str", "str", "str")->run($key, $value, $type)->changes(); // FIXME: this will not work on PostgreSQL + return (bool) $out; } public function settingRemove(string $key): bool { diff --git a/sql/SQLite3/0.sql b/sql/SQLite3/0.sql index 4b2ea65..2f8bf77 100644 --- a/sql/SQLite3/0.sql +++ b/sql/SQLite3/0.sql @@ -1,10 +1,7 @@ -- settings create table arsse_settings( key varchar(255) primary key not null, -- setting key - value varchar(255), -- setting value, serialized as a string - type varchar(255) not null check( - type in('int','numeric','text','timestamp','date','time','bool','null','json') - ) default 'text' -- the deserialized type of the value + value varchar(255) -- setting value, serialized as a string ) without rowid; -- users @@ -117,4 +114,4 @@ create table arsse_categories( -- set version marker pragma user_version = 1; -insert into arsse_settings values('schema_version',1,'int'); \ No newline at end of file +insert into arsse_settings(key,value) values('schema_version','1'); \ No newline at end of file