Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support for all databases supported by laravel to the functionality for sorting alphanumeric values #149

Merged
merged 8 commits into from
Nov 25, 2021
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
"php": "^7.4.1 | ^8.0 | ^8.1",
"livewire/livewire": "^2.4",
"box/spout": "^3",
"doctrine/dbal": "^3.1",
"friendsofphp/php-cs-fixer": "^3.2"
"doctrine/dbal": "^3.1"
},
"scripts": {
"test": "@test:sqlite",
Expand Down
54 changes: 54 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
version: '3'
services:
mysql:
container_name: 'mysql_test'
image: 'mysql/mysql-server:8.0'
ports:
- '3307:3306'
command: '--default-authentication-plugin=mysql_native_password'
environment:
MYSQL_ROOT_PASSWORD: 'password'
MYSQL_ROOT_HOST: "%"
MYSQL_DATABASE: 'powergridtest'
MYSQL_USER: 'powergrid'
MYSQL_PASSWORD: 'password'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
networks:
- sail
healthcheck:
test: [ "CMD", "mysqladmin", "ping", "-ppassword" ]
retries: 3
timeout: 5s
pgsql:
container_name: 'pgsql_test'
image: 'postgres:13'
ports:
- '5433:5432'
environment:
PGPASSWORD: 'password'
POSTGRES_DB: 'powergridtest'
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'password'
networks:
- sail
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-d", "powergridtest", "-U", "postgres" ]
retries: 3
timeout: 5s
sqlsrv:
container_name: 'sqlsrv_test'
image: 'mcr.microsoft.com/mssql/server:2017-latest'
ports:
- '1434:1433'
environment:
ACCEPT_EULA: 'Y'
SA_PASSWORD: 'yourStrong(!)Password'
networks:
- sail
healthcheck:
test: [ "CMD", "/opt/mssql-tools/bin/sqlcmd", "-S", "localhost", "-U", "sa", "-P", "yourStrong(!)Password", "-Q", "select 1" ]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
70 changes: 69 additions & 1 deletion src/Helpers/SqlSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

namespace PowerComponents\LivewirePowerGrid\Helpers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\{DB, Schema};

class SqlSupport
{
/**
* @var array|string[]
*/
private static array $sortStringNumberTypes = ['string', 'varchar', 'char'];

/**
* @return string
*/
public static function like(): string
{
$driverName = DB::getDriverName();
Expand All @@ -16,4 +24,64 @@ public static function like(): string

return $likeSyntax[$driverName] ?? 'LIKE';
}

/**
* @param string $sortField
* @return string
*/
public static function sortStringAsNumber(string $sortField): string
{
$driverName = DB::getDriverName();

return self::getSortSqlByDriver($sortField, $driverName);
}

/**
* @param string $sortField
* @param string $driverName
* @param string $driverVersion
* @return string
*/
private static function getSortSqlByDriver(string $sortField, string $driverName, string $driverVersion = '*'): string
{
$sqlByDriver = [
'sqlite' => [
'*' => "CAST($sortField AS INTEGER)",
],
'mysql' => [
'*' => "CAST(NULLIF(REGEXP_REPLACE($sortField, '[[:alpha:]]+', ''), '') AS SIGNED INTEGER)",
],
'pgsql' => [
'*' => "CAST(NULLIF(REGEXP_REPLACE($sortField, '\D', '', 'g'), '') AS INTEGER)",
],
'sqlsrv' => [
'*' => "CAST(SUBSTRING($sortField, PATINDEX('%[a-z]%', $sortField), LEN($sortField)-PATINDEX('%[a-z]%', $sortField)) AS INT)",
],
];

return $sqlByDriver[$driverName][$driverVersion] ?? $sortField;
}

/**
* @param string $sortFieldType
* @return bool
*/
public static function isValidSortFieldType(string $sortFieldType): bool
{
return in_array($sortFieldType, self::$sortStringNumberTypes);
}

/**
* @param string $sortField
* @return string
*/
public static function getSortFieldType(string $sortField): string
{
$data = explode('.', $sortField);

return Schema::getConnection()
->getDoctrineColumn($data[0], $data[1])
->getType()
->getName();
}
}
14 changes: 10 additions & 4 deletions src/PowerGridComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Pagination\{AbstractPaginator, LengthAwarePaginator};
use Illuminate\Support\{Collection as BaseCollection, Str};
use Livewire\{Component, WithPagination};
use PowerComponents\LivewirePowerGrid\Helpers\{Collection, Model};
use PowerComponents\LivewirePowerGrid\Helpers\{Collection, Model, SqlSupport};
use PowerComponents\LivewirePowerGrid\Themes\ThemeBase;
use PowerComponents\LivewirePowerGrid\Traits\{BatchableExport, Checkbox, Exportable, Filter, WithSorting};

Expand Down Expand Up @@ -174,7 +174,7 @@ public function mount(): void
*/
public function showPerPage(int $perPage = 10): PowerGridComponent
{
if (\Str::contains((string) $perPage, $this->perPageValues)) {
if (Str::contains((string) $perPage, $this->perPageValues)) {
$this->perPageInput = true;
$this->perPage = $perPage;
}
Expand Down Expand Up @@ -367,6 +367,8 @@ public function fillData()
$sortField = $this->currentTable . '.' . $this->sortField;
}

$sortFieldType = SqlSupport::getSortFieldType($sortField);

/** @var Builder $results */
$results = $this->resolveModel($datasource)
->where(function (Builder $query) {
Expand All @@ -379,8 +381,8 @@ public function fillData()
->filter();
});

if ($this->withSortStringNumber) {
$results->orderByRaw("$sortField+0 $this->sortDirection");
if ($this->withSortStringNumber && SqlSupport::isValidSortFieldType($sortFieldType)) {
$results->orderByRaw(SqlSupport::sortStringAsNumber($sortField) . ' ' . $this->sortDirection);
}

$results = $results->orderBy($sortField, $this->sortDirection);
Expand Down Expand Up @@ -419,6 +421,10 @@ private function transform($results)
});
}

/**
* @param string $field
* @throws Exception
*/
public function toggleColumn(string $field): void
{
$this->columns = collect($this->columns)->map(function ($column) use ($field) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/**
* @property int $id
* @property int $name
* @property string $name
* @property Carbon $created_at
* @property Carbon $updated_at
*/
Expand Down