Skip to content

Commit

Permalink
Merge pull request #204 from rjorel/master
Browse files Browse the repository at this point in the history
Minor changes
  • Loading branch information
nicolaslopezj authored Mar 30, 2020
2 parents d8e6ce4 + 8578348 commit b1d24ea
Showing 1 changed file with 47 additions and 22 deletions.
69 changes: 47 additions & 22 deletions src/SearchableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public function scopeSearchRestricted(Builder $q, $search, $restriction, $thresh
$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)
Expand Down Expand Up @@ -195,9 +195,7 @@ protected function makeGroupBy(Builder $query)
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;
Expand All @@ -217,6 +215,16 @@ protected function makeGroupBy(Builder $query)
}
}

/**
* Check if used database is SQLSRV.
*
* @return bool
*/
protected function isSqlsrvDatabase()
{
return $this->getDatabaseDriver() == 'sqlsrv';
}

/**
* Puts all the select clauses to the main query.
*
Expand All @@ -239,11 +247,11 @@ protected function addSelectsToQuery(Builder $query, array $selects)
*/
protected function filterQueryWithRelevance(Builder $query, array $selects, $relevance_count)
{
$comparator = $this->getDatabaseDriver() != 'mysql' ? implode(' + ', $selects) : $this->getRelevanceField();
$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;
Expand All @@ -254,30 +262,37 @@ protected function filterQueryWithRelevance(Builder $query, array $selects, $rel
// 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
Expand All @@ -287,9 +302,9 @@ protected function getSearchQueriesForColumn(Builder $query, $column, $relevance
* @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)
Expand All @@ -301,6 +316,16 @@ protected function getSearchQuery(Builder $query, $column, $relevance, array $wo
return implode(' + ', $cases);
}

/**
* Check if used database is PostgreSQL.
*
* @return bool
*/
private function isPostgresqlDatabase()
{
return $this->getDatabaseDriver() == 'pgsql';
}

/**
* Returns the comparison string.
*
Expand All @@ -310,7 +335,7 @@ protected function getSearchQuery(Builder $query, $column, $relevance, array $wo
* @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)';
}
Expand All @@ -328,7 +353,7 @@ protected function getCaseCompare($column, $compare, $relevance) {
*/
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}`"));
Expand Down

0 comments on commit b1d24ea

Please sign in to comment.