mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 17:39:06 +02:00
Use natural sorting for string datatables columns when using postgres
The natural sorting solution is quite portable, so this should be possible for other database types too later
This commit is contained in:
parent
8a42dfa154
commit
9db822eabd
7 changed files with 86 additions and 11 deletions
|
@ -46,6 +46,7 @@ doctrine:
|
||||||
regexp: App\Doctrine\Functions\Regexp
|
regexp: App\Doctrine\Functions\Regexp
|
||||||
field: DoctrineExtensions\Query\Mysql\Field
|
field: DoctrineExtensions\Query\Mysql\Field
|
||||||
field2: App\Doctrine\Functions\Field2
|
field2: App\Doctrine\Functions\Field2
|
||||||
|
natsort: App\Doctrine\Functions\Natsort
|
||||||
|
|
||||||
when@test:
|
when@test:
|
||||||
doctrine:
|
doctrine:
|
||||||
|
|
|
@ -24,6 +24,9 @@ final class Version20240606203053 extends AbstractMultiPlatformMigration impleme
|
||||||
|
|
||||||
public function postgreSQLUp(Schema $schema): void
|
public function postgreSQLUp(Schema $schema): void
|
||||||
{
|
{
|
||||||
|
//Create a collation for natural sorting
|
||||||
|
$this->addSql("CREATE COLLATION numeric (provider = icu, locale = 'en@colNumeric=yes');");
|
||||||
|
|
||||||
$this->addSql('CREATE TABLE api_tokens (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, name VARCHAR(255) NOT NULL, valid_until TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, token VARCHAR(68) NOT NULL, level SMALLINT NOT NULL, last_time_used TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, last_modified TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, datetime_added TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, user_id INT DEFAULT NULL, PRIMARY KEY(id))');
|
$this->addSql('CREATE TABLE api_tokens (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, name VARCHAR(255) NOT NULL, valid_until TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, token VARCHAR(68) NOT NULL, level SMALLINT NOT NULL, last_time_used TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, last_modified TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, datetime_added TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, user_id INT DEFAULT NULL, PRIMARY KEY(id))');
|
||||||
$this->addSql('CREATE UNIQUE INDEX UNIQ_2CAD560E5F37A13B ON api_tokens (token)');
|
$this->addSql('CREATE UNIQUE INDEX UNIQ_2CAD560E5F37A13B ON api_tokens (token)');
|
||||||
$this->addSql('CREATE INDEX IDX_2CAD560EA76ED395 ON api_tokens (user_id)');
|
$this->addSql('CREATE INDEX IDX_2CAD560EA76ED395 ON api_tokens (user_id)');
|
||||||
|
|
|
@ -86,6 +86,7 @@ final class AttachmentDataTable implements DataTableTypeInterface
|
||||||
|
|
||||||
$dataTable->add('name', TextColumn::class, [
|
$dataTable->add('name', TextColumn::class, [
|
||||||
'label' => 'attachment.edit.name',
|
'label' => 'attachment.edit.name',
|
||||||
|
'orderField' => 'NATSORT(attachment.name)',
|
||||||
'render' => function ($value, Attachment $context) {
|
'render' => function ($value, Attachment $context) {
|
||||||
//Link to external source
|
//Link to external source
|
||||||
if ($context->isExternal()) {
|
if ($context->isExternal()) {
|
||||||
|
@ -111,6 +112,7 @@ final class AttachmentDataTable implements DataTableTypeInterface
|
||||||
$dataTable->add('attachment_type', TextColumn::class, [
|
$dataTable->add('attachment_type', TextColumn::class, [
|
||||||
'label' => 'attachment.table.type',
|
'label' => 'attachment.table.type',
|
||||||
'field' => 'attachment_type.name',
|
'field' => 'attachment_type.name',
|
||||||
|
'orderField' => 'NATSORT(attachment_type.name)',
|
||||||
'render' => fn($value, Attachment $context): string => sprintf(
|
'render' => fn($value, Attachment $context): string => sprintf(
|
||||||
'<a href="%s">%s</a>',
|
'<a href="%s">%s</a>',
|
||||||
$this->entityURLGenerator->editURL($context->getAttachmentType()),
|
$this->entityURLGenerator->editURL($context->getAttachmentType()),
|
||||||
|
|
|
@ -154,7 +154,7 @@ class LogDataTable implements DataTableTypeInterface
|
||||||
|
|
||||||
$dataTable->add('user', TextColumn::class, [
|
$dataTable->add('user', TextColumn::class, [
|
||||||
'label' => 'log.user',
|
'label' => 'log.user',
|
||||||
'orderField' => 'user.name',
|
'orderField' => 'NATSORT(user.name)',
|
||||||
'render' => function ($value, AbstractLogEntry $context): string {
|
'render' => function ($value, AbstractLogEntry $context): string {
|
||||||
$user = $context->getUser();
|
$user = $context->getUser();
|
||||||
|
|
||||||
|
|
|
@ -108,36 +108,40 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
->add('name', TextColumn::class, [
|
->add('name', TextColumn::class, [
|
||||||
'label' => $this->translator->trans('part.table.name'),
|
'label' => $this->translator->trans('part.table.name'),
|
||||||
'render' => fn($value, Part $context) => $this->partDataTableHelper->renderName($context),
|
'render' => fn($value, Part $context) => $this->partDataTableHelper->renderName($context),
|
||||||
|
'orderField' => 'NATSORT(part.name)'
|
||||||
])
|
])
|
||||||
->add('id', TextColumn::class, [
|
->add('id', TextColumn::class, [
|
||||||
'label' => $this->translator->trans('part.table.id'),
|
'label' => $this->translator->trans('part.table.id'),
|
||||||
])
|
])
|
||||||
->add('ipn', TextColumn::class, [
|
->add('ipn', TextColumn::class, [
|
||||||
'label' => $this->translator->trans('part.table.ipn'),
|
'label' => $this->translator->trans('part.table.ipn'),
|
||||||
|
'orderField' => 'NATSORT(part.ipn)'
|
||||||
])
|
])
|
||||||
->add('description', MarkdownColumn::class, [
|
->add('description', MarkdownColumn::class, [
|
||||||
'label' => $this->translator->trans('part.table.description'),
|
'label' => $this->translator->trans('part.table.description'),
|
||||||
|
'orderField' => 'NATSORT(part.description)'
|
||||||
])
|
])
|
||||||
->add('category', EntityColumn::class, [
|
->add('category', EntityColumn::class, [
|
||||||
'label' => $this->translator->trans('part.table.category'),
|
'label' => $this->translator->trans('part.table.category'),
|
||||||
'property' => 'category',
|
'property' => 'category',
|
||||||
'orderField' => '_category.name'
|
'orderField' => 'NATSORT(_category.name)'
|
||||||
])
|
])
|
||||||
->add('footprint', EntityColumn::class, [
|
->add('footprint', EntityColumn::class, [
|
||||||
'property' => 'footprint',
|
'property' => 'footprint',
|
||||||
'label' => $this->translator->trans('part.table.footprint'),
|
'label' => $this->translator->trans('part.table.footprint'),
|
||||||
'orderField' => '_footprint.name'
|
'orderField' => 'NATSORT(_footprint.name)'
|
||||||
])
|
])
|
||||||
->add('manufacturer', EntityColumn::class, [
|
->add('manufacturer', EntityColumn::class, [
|
||||||
'property' => 'manufacturer',
|
'property' => 'manufacturer',
|
||||||
'label' => $this->translator->trans('part.table.manufacturer'),
|
'label' => $this->translator->trans('part.table.manufacturer'),
|
||||||
'orderField' => '_manufacturer.name'
|
'orderField' => 'NATSORT(_manufacturer.name)'
|
||||||
])
|
])
|
||||||
->add('storelocation', TextColumn::class, [
|
->add('storelocation', TextColumn::class, [
|
||||||
'label' => $this->translator->trans('part.table.storeLocations'),
|
'label' => $this->translator->trans('part.table.storeLocations'),
|
||||||
'orderField' => '_storelocations.name',
|
'orderField' => 'NATSORT(_storelocations.name)',
|
||||||
'render' => fn ($value, Part $context) => $this->partDataTableHelper->renderStorageLocations($context),
|
'render' => fn ($value, Part $context) => $this->partDataTableHelper->renderStorageLocations($context),
|
||||||
], alias: 'storage_location')
|
], alias: 'storage_location')
|
||||||
|
|
||||||
->add('amount', TextColumn::class, [
|
->add('amount', TextColumn::class, [
|
||||||
'label' => $this->translator->trans('part.table.amount'),
|
'label' => $this->translator->trans('part.table.amount'),
|
||||||
'render' => fn ($value, Part $context) => $this->partDataTableHelper->renderAmount($context),
|
'render' => fn ($value, Part $context) => $this->partDataTableHelper->renderAmount($context),
|
||||||
|
@ -151,7 +155,7 @@ final class PartsDataTable implements DataTableTypeInterface
|
||||||
->add('partUnit', TextColumn::class, [
|
->add('partUnit', TextColumn::class, [
|
||||||
'field' => 'partUnit.name',
|
'field' => 'partUnit.name',
|
||||||
'label' => $this->translator->trans('part.table.partUnit'),
|
'label' => $this->translator->trans('part.table.partUnit'),
|
||||||
'orderField' => '_partUnit.name'
|
'orderField' => 'NATSORT(_partUnit.name)'
|
||||||
])
|
])
|
||||||
->add('addedDate', LocaleDateTimeColumn::class, [
|
->add('addedDate', LocaleDateTimeColumn::class, [
|
||||||
'label' => $this->translator->trans('part.table.addedDate'),
|
'label' => $this->translator->trans('part.table.addedDate'),
|
||||||
|
|
|
@ -82,7 +82,7 @@ class ProjectBomEntriesDataTable implements DataTableTypeInterface
|
||||||
|
|
||||||
->add('name', TextColumn::class, [
|
->add('name', TextColumn::class, [
|
||||||
'label' => $this->translator->trans('part.table.name'),
|
'label' => $this->translator->trans('part.table.name'),
|
||||||
'orderField' => 'part.name',
|
'orderField' => 'NATSORT(part.name)',
|
||||||
'render' => function ($value, ProjectBOMEntry $context) {
|
'render' => function ($value, ProjectBOMEntry $context) {
|
||||||
if(!$context->getPart() instanceof Part) {
|
if(!$context->getPart() instanceof Part) {
|
||||||
return htmlspecialchars($context->getName());
|
return htmlspecialchars($context->getName());
|
||||||
|
@ -101,7 +101,7 @@ class ProjectBomEntriesDataTable implements DataTableTypeInterface
|
||||||
])
|
])
|
||||||
->add('ipn', TextColumn::class, [
|
->add('ipn', TextColumn::class, [
|
||||||
'label' => $this->translator->trans('part.table.ipn'),
|
'label' => $this->translator->trans('part.table.ipn'),
|
||||||
'orderField' => 'part.ipn',
|
'orderField' => 'NATSORT(part.ipn)',
|
||||||
'visible' => false,
|
'visible' => false,
|
||||||
'render' => function ($value, ProjectBOMEntry $context) {
|
'render' => function ($value, ProjectBOMEntry $context) {
|
||||||
if($context->getPart() instanceof Part) {
|
if($context->getPart() instanceof Part) {
|
||||||
|
@ -124,18 +124,18 @@ class ProjectBomEntriesDataTable implements DataTableTypeInterface
|
||||||
->add('category', EntityColumn::class, [
|
->add('category', EntityColumn::class, [
|
||||||
'label' => $this->translator->trans('part.table.category'),
|
'label' => $this->translator->trans('part.table.category'),
|
||||||
'property' => 'part.category',
|
'property' => 'part.category',
|
||||||
'orderField' => 'category.name',
|
'orderField' => 'NATSORT(category.name)',
|
||||||
])
|
])
|
||||||
->add('footprint', EntityColumn::class, [
|
->add('footprint', EntityColumn::class, [
|
||||||
'property' => 'part.footprint',
|
'property' => 'part.footprint',
|
||||||
'label' => $this->translator->trans('part.table.footprint'),
|
'label' => $this->translator->trans('part.table.footprint'),
|
||||||
'orderField' => 'footprint.name',
|
'orderField' => 'NATSORT(footprint.name)',
|
||||||
])
|
])
|
||||||
|
|
||||||
->add('manufacturer', EntityColumn::class, [
|
->add('manufacturer', EntityColumn::class, [
|
||||||
'property' => 'part.manufacturer',
|
'property' => 'part.manufacturer',
|
||||||
'label' => $this->translator->trans('part.table.manufacturer'),
|
'label' => $this->translator->trans('part.table.manufacturer'),
|
||||||
'orderField' => 'manufacturer.name',
|
'orderField' => 'NATSORT(manufacturer.name)',
|
||||||
])
|
])
|
||||||
|
|
||||||
->add('mountnames', TextColumn::class, [
|
->add('mountnames', TextColumn::class, [
|
||||||
|
|
65
src/Doctrine/Functions/Natsort.php
Normal file
65
src/Doctrine/Functions/Natsort.php
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published
|
||||||
|
* by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
|
||||||
|
namespace App\Doctrine\Functions;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
|
||||||
|
use Doctrine\DBAL\Platforms\MariaDBPlatform;
|
||||||
|
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
|
||||||
|
use Doctrine\ORM\Query\AST\Node;
|
||||||
|
use Doctrine\ORM\Query\Parser;
|
||||||
|
use Doctrine\ORM\Query\SqlWalker;
|
||||||
|
use Doctrine\ORM\Query\TokenType;
|
||||||
|
|
||||||
|
class Natsort extends FunctionNode
|
||||||
|
{
|
||||||
|
private Node $field;
|
||||||
|
|
||||||
|
public function parse(Parser $parser): void
|
||||||
|
{
|
||||||
|
$parser->match(TokenType::T_IDENTIFIER);
|
||||||
|
$parser->match(TokenType::T_OPEN_PARENTHESIS);
|
||||||
|
|
||||||
|
$this->field = $parser->ArithmeticExpression();
|
||||||
|
|
||||||
|
$parser->match(TokenType::T_CLOSE_PARENTHESIS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSql(SqlWalker $sqlWalker): string
|
||||||
|
{
|
||||||
|
$platform = $sqlWalker->getConnection()->getDatabasePlatform();
|
||||||
|
|
||||||
|
if ($platform instanceof AbstractPostgreSQLDriver) {
|
||||||
|
return $this->field->dispatch($sqlWalker) . ' COLLATE numeric';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($platform instanceof MariaDBPlatform && $sqlWalker->getConnection()->getServerVersion()) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//For every other platform, return the field as is
|
||||||
|
return $this->field->dispatch($sqlWalker);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue