updated-packages
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.4.0",
|
||||
"illuminate/database": "4.2.x|~5.0",
|
||||
"illuminate/database": ">=4.2",
|
||||
"ext-mbstring": "*"
|
||||
},
|
||||
"autoload": {
|
||||
|
@@ -1,6 +1,7 @@
|
||||
<?php namespace Nicolaslopezj\Searchable;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -62,15 +63,15 @@ trait SearchableTrait
|
||||
$relevance_count += $relevance;
|
||||
|
||||
if (!$entireTextOnly) {
|
||||
$queries = $this->getSearchQueriesForColumn($query, $column, $relevance, $words);
|
||||
$queries = $this->getSearchQueriesForColumn($column, $relevance, $words);
|
||||
} else {
|
||||
$queries = [];
|
||||
}
|
||||
|
||||
if ( ($entireText === true && count($words) > 1) || $entireTextOnly === true )
|
||||
{
|
||||
$queries[] = $this->getSearchQuery($query, $column, $relevance, [$search], 50, '', '');
|
||||
$queries[] = $this->getSearchQuery($query, $column, $relevance, [$search], 30, '%', '%');
|
||||
$queries[] = $this->getSearchQuery($column, $relevance, [$search], 50, '', '');
|
||||
$queries[] = $this->getSearchQuery($column, $relevance, [$search], 30, '%', '%');
|
||||
}
|
||||
|
||||
foreach ($queries as $select)
|
||||
@@ -164,7 +165,7 @@ trait SearchableTrait
|
||||
*/
|
||||
protected function getJoins()
|
||||
{
|
||||
return array_get($this->searchable, 'joins', []);
|
||||
return Arr::get($this->searchable, 'joins', []);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,9 +195,7 @@ trait SearchableTrait
|
||||
if ($groupBy = $this->getGroupBy()) {
|
||||
$query->groupBy($groupBy);
|
||||
} else {
|
||||
$driver = $this->getDatabaseDriver();
|
||||
|
||||
if ($driver == 'sqlsrv') {
|
||||
if ($this->isSqlsrvDatabase()) {
|
||||
$columns = $this->getTableColumns();
|
||||
} else {
|
||||
$columns = $this->getTable() . '.' .$this->primaryKey;
|
||||
@@ -216,6 +215,16 @@ trait SearchableTrait
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if used database is SQLSRV.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function isSqlsrvDatabase()
|
||||
{
|
||||
return $this->getDatabaseDriver() == 'sqlsrv';
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts all the select clauses to the main query.
|
||||
*
|
||||
@@ -225,7 +234,7 @@ trait SearchableTrait
|
||||
protected function addSelectsToQuery(Builder $query, array $selects)
|
||||
{
|
||||
if (!empty($selects)) {
|
||||
$query->selectRaw('max(' . implode(' + ', $selects) . ') as relevance', $this->search_bindings);
|
||||
$query->selectRaw('max(' . implode(' + ', $selects) . ') as ' . $this->getRelevanceField(), $this->search_bindings);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,45 +247,52 @@ trait SearchableTrait
|
||||
*/
|
||||
protected function filterQueryWithRelevance(Builder $query, array $selects, $relevance_count)
|
||||
{
|
||||
$comparator = $this->getDatabaseDriver() != 'mysql' ? implode(' + ', $selects) : 'relevance';
|
||||
$comparator = $this->isMysqlDatabase() ? $this->getRelevanceField() : implode(' + ', $selects);
|
||||
|
||||
$relevance_count=number_format($relevance_count,2,'.','');
|
||||
|
||||
if ($this->getDatabaseDriver() == 'mysql') {
|
||||
if ($this->isMysqlDatabase()) {
|
||||
$bindings = [];
|
||||
} else {
|
||||
$bindings = $this->search_bindings;
|
||||
}
|
||||
$query->havingRaw("$comparator >= $relevance_count", $bindings);
|
||||
$query->orderBy('relevance', 'desc');
|
||||
$query->orderBy($this->getRelevanceField(), 'desc');
|
||||
|
||||
// add bindings to postgres
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if used database is MySQL.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isMysqlDatabase()
|
||||
{
|
||||
return $this->getDatabaseDriver() == 'mysql';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the search queries for the specified column.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $column
|
||||
* @param float $relevance
|
||||
* @param array $words
|
||||
* @return array
|
||||
*/
|
||||
protected function getSearchQueriesForColumn(Builder $query, $column, $relevance, array $words)
|
||||
protected function getSearchQueriesForColumn($column, $relevance, array $words)
|
||||
{
|
||||
$queries = [];
|
||||
|
||||
$queries[] = $this->getSearchQuery($query, $column, $relevance, $words, 15);
|
||||
$queries[] = $this->getSearchQuery($query, $column, $relevance, $words, 5, '', '%');
|
||||
$queries[] = $this->getSearchQuery($query, $column, $relevance, $words, 1, '%', '%');
|
||||
|
||||
return $queries;
|
||||
return [
|
||||
$this->getSearchQuery($column, $relevance, $words, 15),
|
||||
$this->getSearchQuery($column, $relevance, $words, 5, '', '%'),
|
||||
$this->getSearchQuery($column, $relevance, $words, 1, '%', '%')
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sql string for the given parameters.
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder $query
|
||||
* @param string $column
|
||||
* @param string $relevance
|
||||
* @param array $words
|
||||
@@ -286,9 +302,9 @@ trait SearchableTrait
|
||||
* @param string $post_word
|
||||
* @return string
|
||||
*/
|
||||
protected function getSearchQuery(Builder $query, $column, $relevance, array $words, $relevance_multiplier, $pre_word = '', $post_word = '')
|
||||
protected function getSearchQuery($column, $relevance, array $words, $relevance_multiplier, $pre_word = '', $post_word = '')
|
||||
{
|
||||
$like_comparator = $this->getDatabaseDriver() == 'pgsql' ? 'ILIKE' : 'LIKE';
|
||||
$like_comparator = $this->isPostgresqlDatabase() ? 'ILIKE' : 'LIKE';
|
||||
$cases = [];
|
||||
|
||||
foreach ($words as $word)
|
||||
@@ -300,6 +316,16 @@ trait SearchableTrait
|
||||
return implode(' + ', $cases);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if used database is PostgreSQL.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isPostgresqlDatabase()
|
||||
{
|
||||
return $this->getDatabaseDriver() == 'pgsql';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the comparison string.
|
||||
*
|
||||
@@ -309,7 +335,7 @@ trait SearchableTrait
|
||||
* @return string
|
||||
*/
|
||||
protected function getCaseCompare($column, $compare, $relevance) {
|
||||
if($this->getDatabaseDriver() == 'pgsql') {
|
||||
if ($this->isPostgresqlDatabase()) {
|
||||
$field = "LOWER(" . $column . ") " . $compare . " ?";
|
||||
return '(case when ' . $field . ' then ' . $relevance . ' else 0 end)';
|
||||
}
|
||||
@@ -327,7 +353,7 @@ trait SearchableTrait
|
||||
*/
|
||||
protected function mergeQueries(Builder $clone, Builder $original) {
|
||||
$tableName = DB::connection($this->connection)->getTablePrefix() . $this->getTable();
|
||||
if ($this->getDatabaseDriver() == 'pgsql') {
|
||||
if ($this->isPostgresqlDatabase()) {
|
||||
$original->from(DB::connection($this->connection)->raw("({$clone->toSql()}) as {$tableName}"));
|
||||
} else {
|
||||
$original->from(DB::connection($this->connection)->raw("({$clone->toSql()}) as `{$tableName}`"));
|
||||
@@ -343,4 +369,19 @@ trait SearchableTrait
|
||||
// with some scope's bindings remaning
|
||||
$original->withoutGlobalScopes()->setBindings($mergedBindings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the relevance field name, alias of ratio column in the query.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getRelevanceField()
|
||||
{
|
||||
if ($this->relevanceField ?? false) {
|
||||
return $this->relevanceField;
|
||||
}
|
||||
|
||||
// If property $this->relevanceField is not setted, return the default
|
||||
return 'relevance';
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user