@@ -1001,11 +1001,9 @@ protected function applyScoutSearch(string $search_keyword): bool
1001
1001
$ this ->query ->whereIn ($ this ->scoutKey , $ search_results );
1002
1002
});
1003
1003
1004
- // Order by scout search results & disable user ordering
1005
- if (count ($ search_results ) > 0 ) {
1006
- $ this ->applyFixedOrderingToQuery ($ this ->scoutKey , $ search_results );
1007
-
1008
- // Disable user ordering because we already order by search relevancy
1004
+ // Order by scout search results & disable user ordering (if db driver is supported)
1005
+ if (count ($ search_results ) > 0 && $ this ->applyFixedOrderingToQuery ($ this ->scoutKey , $ search_results )) {
1006
+ // Disable user ordering because we already ordered by search relevancy
1009
1007
$ this ->disableUserOrdering = true ;
1010
1008
}
1011
1009
@@ -1025,16 +1023,15 @@ protected function applyScoutSearch(string $search_keyword): bool
1025
1023
*
1026
1024
* @param string $keyName
1027
1025
* @param array $orderedKeys
1028
- * @return void
1029
- *
1030
- * @throws \Exception If the database driver is unsupported.
1026
+ * @return bool
1031
1027
*/
1032
1028
protected function applyFixedOrderingToQuery (string $ keyName , array $ orderedKeys )
1033
1029
{
1034
1030
$ connection = $ this ->getConnection ();
1035
1031
$ driver_name = $ connection ->getDriverName ();
1036
1032
1037
1033
// Escape keyName and orderedKeys
1034
+ $ rawKeyName = $ keyName ;
1038
1035
$ keyName = $ connection ->escape ($ keyName );
1039
1036
$ orderedKeys = collect ($ orderedKeys )
1040
1037
->map (function ($ value ) use ($ connection ) {
@@ -1043,36 +1040,37 @@ protected function applyFixedOrderingToQuery(string $keyName, array $orderedKeys
1043
1040
1044
1041
switch ($ driver_name ) {
1045
1042
case 'mysql ' :
1046
- // MySQL
1043
+ // MySQL / MariaDB
1047
1044
$ this ->query ->orderByRaw ("FIELD( $ keyName, " .$ orderedKeys ->implode (', ' ).') ' );
1048
- break ;
1045
+ return true ;
1049
1046
1050
1047
/*
1051
1048
TODO: test implementations, fix if necessary and uncomment
1052
1049
case 'pgsql':
1053
1050
// PostgreSQL
1054
1051
$this->query->orderByRaw("array_position(ARRAY[" . $orderedKeys->implode(',') . "], $keyName)");
1055
- break;
1056
-
1057
- case 'sqlite':
1058
- case 'sqlsrv':
1059
- // SQLite & Microsoft SQL Server
1060
-
1061
- // should be generally compatible with all drivers using SQL syntax (but ugly solution)
1062
- $this->query->orderByRaw(
1063
- "CASE $keyName "
1064
- .
1065
- $orderedKeys
1066
- ->map(fn($value, $index) => "WHEN $keyName = $value THEN $index")
1067
- ->implode(' ')
1068
- .
1069
- " END"
1070
- );
1071
- break;
1052
+ return true;
1053
+
1072
1054
*/
1073
1055
1056
+ case 'sqlite ' :
1057
+ case 'sqlsrv ' :
1058
+ // SQLite & Microsoft SQL Server
1059
+ // Compatible with all SQL drivers (but ugly solution)
1060
+
1061
+ $ this ->query ->orderByRaw (
1062
+ "CASE ` $ rawKeyName` "
1063
+ .
1064
+ $ orderedKeys
1065
+ ->map (fn ($ value , $ index ) => "WHEN $ value THEN $ index " )
1066
+ ->implode (' ' )
1067
+ .
1068
+ " END "
1069
+ );
1070
+ return true ;
1071
+
1074
1072
default :
1075
- throw new \ Exception ( " Unsupported database driver: $ driver_name " ) ;
1073
+ return false ;
1076
1074
}
1077
1075
}
1078
1076
0 commit comments