Allow to emulate natural sorting on SQLite databases

This commit is contained in:
Jan Böhmer 2024-06-18 00:09:44 +02:00
parent 289c9126d0
commit 272fe0516b
5 changed files with 84 additions and 0 deletions

View file

@ -27,6 +27,7 @@ use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
use Doctrine\DBAL\Platforms\MariaDBPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Query\Parser;
@ -39,6 +40,19 @@ class Natsort extends FunctionNode
private static ?bool $supportsNaturalSort = null;
private static bool $allowSlowNaturalSort = false;
/**
* As we can not inject parameters into the function, we use an event listener, to call the value on the static function.
* This is the only way to inject the value into the function.
* @param bool $allow
* @return void
*/
public static function allowSlowNaturalSort(bool $allow = true): void
{
self::$allowSlowNaturalSort = $allow;
}
/**
* Check if the MariaDB version which is connected to supports the natural sort (meaning it has a version of 10.7.0 or higher)
* The result is cached in memory.
@ -84,6 +98,14 @@ class Natsort extends FunctionNode
return 'NATURAL_SORT_KEY(' . $this->field->dispatch($sqlWalker) . ')';
}
//Do the following operations only if we allow slow natural sort
if (self::$allowSlowNaturalSort) {
if ($platform instanceof SQLitePlatform) {
return $this->field->dispatch($sqlWalker).' COLLATE NATURAL_CMP';
}
}
//For every other platform, return the field as is
return $this->field->dispatch($sqlWalker);
}