1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-22 13:12:41 +00:00

Sort nulls consistently

PostgreSQL normally sorts nulls after everything else in ascending order
and vice versa; we reverse this, to match SQLIte and MySQL
This commit is contained in:
J. King 2021-02-02 10:00:08 -05:00
parent 9d7ada7f59
commit ed27e0aaaa
5 changed files with 14 additions and 0 deletions

View file

@ -75,6 +75,8 @@ interface Driver {
* - "nocase": the name of a general-purpose case-insensitive collation sequence
* - "like": the case-insensitive LIKE operator
* - "integer": the integer type to use for explicit casts
* - "asc": ascending sort order
* - "desc": descending sort order
*/
public function sqlToken(string $token): string;

View file

@ -83,6 +83,8 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver {
return '"utf8mb4_unicode_ci"';
case "integer":
return "signed integer";
case "asc":
return "";
default:
return $token;
}

View file

@ -119,6 +119,10 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver {
return '"und-x-icu"';
case "like":
return "ilike";
case "asc":
return "nulls first";
case "desc":
return "desc nulls last";
default:
return $token;
}

View file

@ -114,6 +114,8 @@ class Driver extends \JKingWeb\Arsse\Db\AbstractDriver {
switch (strtolower($token)) {
case "greatest":
return "max";
case "asc":
return "";
default:
return $token;
}

View file

@ -385,6 +385,8 @@ abstract class BaseDriver extends \JKingWeb\Arsse\Test\AbstractTest {
$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");
$this->assertSame("NOT_A_TOKEN", $this->drv->sqlToken("NOT_A_TOKEN"));
@ -392,5 +394,7 @@ abstract class BaseDriver extends \JKingWeb\Arsse\Test\AbstractTest {
$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], array_column($this->drv->query("SELECT 1 as t union select null as t order by t $asc")->getAll(), "t"));
$this->assertEquals([1, null], array_column($this->drv->query("SELECT 1 as t union select null as t order by t $desc")->getAll(), "t"));
}
}