@@ -1028,38 +1028,37 @@ protected function applyScoutSearch(string $search_keyword): bool
10281028 protected function applyFixedOrderingToQuery (string $ keyName , array $ orderedKeys )
10291029 {
10301030 $ connection = $ this ->getConnection ();
1031- $ driver_name = $ connection ->getDriverName ();
1031+ $ driverName = $ connection ->getDriverName ();
10321032
10331033 // Escape keyName and orderedKeys
1034- $ rawKeyName = $ keyName ;
1035- $ keyName = $ connection ->escape ($ keyName );
1034+ $ keyName = $ connection ->getQueryGrammar ()->wrap ($ keyName );
10361035 $ orderedKeys = collect ($ orderedKeys )
10371036 ->map (function ($ value ) use ($ connection ) {
10381037 return $ connection ->escape ($ value );
10391038 });
10401039
1041- switch ($ driver_name ) {
1040+ switch ($ driverName ) {
10421041 case 'mysql ' :
1043- // MySQL / MariaDB
10441042 $ this ->query ->orderByRaw ("FIELD( $ keyName, " .$ orderedKeys ->implode (', ' ).') ' );
10451043 return true ;
10461044
1047- /*
1048- TODO: test implementations, fix if necessary and uncomment
1049- case 'pgsql':
1050- // PostgreSQL
1051- $this->query->orderByRaw("array_position(ARRAY[" . $orderedKeys->implode(',') . "], $keyName)");
1052- return true;
1053-
1054- */
1045+ case 'pgsql ' :
1046+ case 'oracle ' :
1047+ $ this ->query ->orderByRaw (
1048+ "CASE "
1049+ .
1050+ $ orderedKeys
1051+ ->map (fn ($ value , $ index ) => "WHEN $ keyName= $ value THEN $ index " )
1052+ ->implode (' ' )
1053+ .
1054+ " END "
1055+ );
1056+ return true ;
10551057
10561058 case 'sqlite ' :
10571059 case 'sqlsrv ' :
1058- // SQLite & Microsoft SQL Server
1059- // Compatible with all SQL drivers (but ugly solution)
1060-
10611060 $ this ->query ->orderByRaw (
1062- "CASE ` $ rawKeyName ` "
1061+ "CASE $ keyName "
10631062 .
10641063 $ orderedKeys
10651064 ->map (fn ($ value , $ index ) => "WHEN $ value THEN $ index " )
0 commit comments