From d1da6fbe5ef6c6e1da44b21e2162399563a0cbb1 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Mon, 30 May 2022 17:29:34 -0400 Subject: [PATCH] Use cases rather than casting bools to int in SQL --- lib/Database.php | 14 +++++--------- lib/Db/MySQL/Driver.php | 2 -- tests/cases/Db/BaseDriver.php | 2 -- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/lib/Database.php b/lib/Database.php index 3bfed97..107c6df 100644 --- a/lib/Database.php +++ b/lib/Database.php @@ -814,7 +814,6 @@ class Database { // validate inputs $folder = $this->folderValidateId($user, $folder)['id']; // create a complex query - $integer = $this->db->sqlToken("integer"); $q = new Query( "WITH RECURSIVE topmost(f_id, top) as ( @@ -853,7 +852,7 @@ class Database { select subscription, sum(hidden) as hidden, - sum(cast((\"read\" = 1 and hidden = 0) as $integer)) as marked + sum(case when \"read\" = 1 and hidden = 0 then 1 else 0 end) as marked from arsse_marks group by subscription ) as mark_stats on mark_stats.subscription = s.id", ["str", "int"], @@ -2053,7 +2052,6 @@ class Database { /** Deletes from the database articles which are beyond the configured clean-up threshold */ public function articleCleanup(): bool { - $integer = $this->db->sqlToken("integer"); $query = $this->db->prepareArray( "WITH RECURSIVE exempt_articles as ( @@ -2079,8 +2077,8 @@ class Database { left join ( select article, - sum(cast((starred = 1 and hidden = 0) as $integer)) as starred, - sum(cast((\"read\" = 1 or hidden = 1) as $integer)) as \"read\", + sum(case when starred = 1 and hidden = 0 then 1 else 0 end) as starred, + sum(case when \"read\" = 1 or hidden = 1 then 1 else 0 end) as \"read\", max(arsse_marks.modified) as marked_date from arsse_marks group by article @@ -2211,7 +2209,6 @@ class Database { * @param boolean $includeEmpty Whether to include (true) or supress (false) labels which have no articles assigned to them */ public function labelList(string $user, bool $includeEmpty = true): Db\Result { - $integer = $this->db->sqlToken("integer"); return $this->db->prepareArray( "SELECT * FROM ( SELECT @@ -2227,7 +2224,7 @@ class Database { SELECT label, sum(hidden) as hidden, - sum(cast((\"read\" = 1 and hidden = 0) as $integer)) as marked + sum(case when \"read\" = 1 and hidden = 0 then 1 else 0 end) as marked from arsse_marks join arsse_subscriptions on arsse_subscriptions.id = arsse_marks.subscription join arsse_label_members on arsse_label_members.article = arsse_marks.article @@ -2277,7 +2274,6 @@ class Database { $this->labelValidateId($user, $id, $byName, false); $field = $byName ? "name" : "id"; $type = $byName ? "str" : "int"; - $integer = $this->db->sqlToken("integer"); $out = $this->db->prepareArray( "SELECT id, @@ -2292,7 +2288,7 @@ class Database { SELECT label, sum(hidden) as hidden, - sum(cast((\"read\" = 1 and hidden = 0) as $integer)) as marked + sum(case when \"read\" = 1 and hidden = 0 then 1 else 0 end) as marked from arsse_marks join arsse_subscriptions on arsse_subscriptions.id = arsse_marks.subscription join arsse_label_members on arsse_label_members.article = arsse_marks.article diff --git a/lib/Db/MySQL/Driver.php b/lib/Db/MySQL/Driver.php index c61762a..de3bc3d 100644 --- a/lib/Db/MySQL/Driver.php +++ b/lib/Db/MySQL/Driver.php @@ -81,8 +81,6 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver { switch (strtolower($token)) { case "nocase": return '"utf8mb4_unicode_ci"'; - case "integer": - return "signed integer"; case "asc": return ""; default: diff --git a/tests/cases/Db/BaseDriver.php b/tests/cases/Db/BaseDriver.php index fe7f344..0999d3a 100644 --- a/tests/cases/Db/BaseDriver.php +++ b/tests/cases/Db/BaseDriver.php @@ -384,7 +384,6 @@ abstract class BaseDriver extends \JKingWeb\Arsse\Test\AbstractTest { $greatest = $this->drv->sqlToken("GrEatESt"); $nocase = $this->drv->sqlToken("noCASE"); $like = $this->drv->sqlToken("liKe"); - $integer = $this->drv->sqlToken("InTEGer"); $asc = $this->drv->sqlToken("asc"); $desc = $this->drv->sqlToken("desc"); $least = $this->drv->sqlToken("leASt"); @@ -395,7 +394,6 @@ abstract class BaseDriver extends \JKingWeb\Arsse\Test\AbstractTest { $this->assertSame("Z", $this->drv->query("SELECT $greatest('Z', 'A')")->getValue()); $this->assertSame("Z", $this->drv->query("SELECT 'Z' collate $nocase")->getValue()); $this->assertSame("Z", $this->drv->query("SELECT 'Z' where 'Z' $like 'z'")->getValue()); - $this->assertEquals(1, $this->drv->query("SELECT CAST((1=1) as $integer)")->getValue()); $this->assertEquals([null, 1, 2], array_column($this->drv->query("SELECT 1 as t union select null as t union select 2 as t order by t $asc")->getAll(), "t")); $this->assertEquals([2, 1, null], array_column($this->drv->query("SELECT 1 as t union select null as t union select 2 as t order by t $desc")->getAll(), "t")); }