mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-25 03:08:51 +02:00
Use Natural_SORT_KEY for natural sorting on MariaDB database which support that
This resolves issue #243 and #402
This commit is contained in:
parent
0a482da93e
commit
289c9126d0
1 changed files with 27 additions and 3 deletions
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Doctrine\Functions;
|
namespace App\Doctrine\Functions;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Connection;
|
||||||
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
|
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
|
||||||
use Doctrine\DBAL\Platforms\MariaDBPlatform;
|
use Doctrine\DBAL\Platforms\MariaDBPlatform;
|
||||||
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
|
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
|
||||||
|
@ -36,6 +37,29 @@ class Natsort extends FunctionNode
|
||||||
{
|
{
|
||||||
private ?Node $field = null;
|
private ?Node $field = null;
|
||||||
|
|
||||||
|
private static ?bool $supportsNaturalSort = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
* @param Connection $connection
|
||||||
|
* @return bool
|
||||||
|
* @throws \Doctrine\DBAL\Exception
|
||||||
|
*/
|
||||||
|
private static function mariaDBSupportsNaturalSort(Connection $connection): bool
|
||||||
|
{
|
||||||
|
if (self::$supportsNaturalSort !== null) {
|
||||||
|
return self::$supportsNaturalSort;
|
||||||
|
}
|
||||||
|
|
||||||
|
$version = $connection->getServerVersion();
|
||||||
|
//Remove the -MariaDB suffix
|
||||||
|
$version = str_replace('-MariaDB', '', $version);
|
||||||
|
//We need at least MariaDB 10.7.0 to support the natural sort
|
||||||
|
self::$supportsNaturalSort = version_compare($version, '10.7.0', '>=');
|
||||||
|
return self::$supportsNaturalSort;
|
||||||
|
}
|
||||||
|
|
||||||
public function parse(Parser $parser): void
|
public function parse(Parser $parser): void
|
||||||
{
|
{
|
||||||
$parser->match(TokenType::T_IDENTIFIER);
|
$parser->match(TokenType::T_IDENTIFIER);
|
||||||
|
@ -56,9 +80,9 @@ class Natsort extends FunctionNode
|
||||||
return $this->field->dispatch($sqlWalker) . ' COLLATE numeric';
|
return $this->field->dispatch($sqlWalker) . ' COLLATE numeric';
|
||||||
}
|
}
|
||||||
|
|
||||||
/*if ($platform instanceof MariaDBPlatform && $sqlWalker->getConnection()->getServerVersion()) {
|
if ($platform instanceof MariaDBPlatform && self::mariaDBSupportsNaturalSort($sqlWalker->getConnection())) {
|
||||||
|
return 'NATURAL_SORT_KEY(' . $this->field->dispatch($sqlWalker) . ')';
|
||||||
}*/
|
}
|
||||||
|
|
||||||
//For every other platform, return the field as is
|
//For every other platform, return the field as is
|
||||||
return $this->field->dispatch($sqlWalker);
|
return $this->field->dispatch($sqlWalker);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue