Skip to content

Commit

Permalink
feat: add scout fixed ordering for pgsql and oracle
Browse files Browse the repository at this point in the history
fix: wrapping of key name
  • Loading branch information
yajra committed Nov 4, 2023
1 parent 82127aa commit 04beea7
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/QueryDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1028,38 +1028,37 @@ 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")
Expand Down

0 comments on commit 04beea7

Please sign in to comment.