Skip to content

Commit 5b0563f

Browse files
committed
Made scout search fixed ordering optional & enabled sqlite/mssql integration
1 parent 7eb12ab commit 5b0563f

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

src/QueryDataTable.php

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,11 +1001,9 @@ protected function applyScoutSearch(string $search_keyword): bool
10011001
$this->query->whereIn($this->scoutKey, $search_results);
10021002
});
10031003

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
10091007
$this->disableUserOrdering = true;
10101008
}
10111009

@@ -1025,16 +1023,15 @@ protected function applyScoutSearch(string $search_keyword): bool
10251023
*
10261024
* @param string $keyName
10271025
* @param array $orderedKeys
1028-
* @return void
1029-
*
1030-
* @throws \Exception If the database driver is unsupported.
1026+
* @return bool
10311027
*/
10321028
protected function applyFixedOrderingToQuery(string $keyName, array $orderedKeys)
10331029
{
10341030
$connection = $this->getConnection();
10351031
$driver_name = $connection->getDriverName();
10361032

10371033
// Escape keyName and orderedKeys
1034+
$rawKeyName = $keyName;
10381035
$keyName = $connection->escape($keyName);
10391036
$orderedKeys = collect($orderedKeys)
10401037
->map(function ($value) use ($connection) {
@@ -1043,36 +1040,37 @@ protected function applyFixedOrderingToQuery(string $keyName, array $orderedKeys
10431040

10441041
switch ($driver_name) {
10451042
case 'mysql':
1046-
// MySQL
1043+
// MySQL / MariaDB
10471044
$this->query->orderByRaw("FIELD($keyName, ".$orderedKeys->implode(',').')');
1048-
break;
1045+
return true;
10491046

10501047
/*
10511048
TODO: test implementations, fix if necessary and uncomment
10521049
case 'pgsql':
10531050
// PostgreSQL
10541051
$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+
10721054
*/
10731055

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+
10741072
default:
1075-
throw new \Exception("Unsupported database driver: $driver_name");
1073+
return false;
10761074
}
10771075
}
10781076

0 commit comments

Comments
 (0)