Skip to content

Commit

Permalink
Merge pull request #3090 from yajra/pgsql-oracle-scout
Browse files Browse the repository at this point in the history
feat: add scout fixed ordering for pgsql and oracle
  • Loading branch information
yajra authored Nov 4, 2023
2 parents 82127aa + 868b5ed commit 52e49ac
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1028,45 +1028,47 @@ protected function applyScoutSearch(string $search_keyword): bool
protected function applyFixedOrderingToQuery(string $keyName, array $orderedKeys)
{
$connection = $this->getConnection();
$driver_name = $connection->getDriverName();
$driverName = $connection->getDriverName();

// Escape keyName and orderedKeys
$rawKeyName = $keyName;
$keyName = $connection->escape($keyName);
$keyName = $connection->getQueryGrammar()->wrap($keyName);
$orderedKeys = collect($orderedKeys)
->map(function ($value) use ($connection) {
return $connection->escape($value);
});

switch ($driver_name) {
switch ($driverName) {
case 'mysql':
// MySQL / MariaDB
$this->query->orderByRaw("FIELD($keyName, ".$orderedKeys->implode(',').')');

return true;

/*
TODO: test implementations, fix if necessary and uncomment
case 'pgsql':
// PostgreSQL
$this->query->orderByRaw("array_position(ARRAY[" . $orderedKeys->implode(',') . "], $keyName)");
return true;
case 'pgsql':
case 'oracle':
$this->query->orderByRaw(
'CASE '
.
$orderedKeys
->map(fn ($value, $index) => "WHEN $keyName=$value THEN $index")
->implode(' ')
.
' END'
);

*/
return true;

case 'sqlite':
case 'sqlsrv':
// SQLite & Microsoft SQL Server
// Compatible with all SQL drivers (but ugly solution)

$this->query->orderByRaw(
"CASE `$rawKeyName` "
"CASE $keyName "
.
$orderedKeys
->map(fn($value, $index) => "WHEN $value THEN $index")
->map(fn ($value, $index) => "WHEN $value THEN $index")
->implode(' ')
.
" END"
' END'
);

return true;

default:
Expand Down

0 comments on commit 52e49ac

Please sign in to comment.